-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the ViewModel classes into their own separate files
PiperOrigin-RevId: 618233437
- Loading branch information
1 parent
2f2c894
commit a37e1d8
Showing
10 changed files
with
177 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import GoogleMobileAds | ||
|
||
class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { | ||
private var interstitialAd: GADInterstitialAd? | ||
|
||
func loadAd() async { | ||
do { | ||
interstitialAd = try await GADInterstitialAd.load( | ||
withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest()) | ||
interstitialAd?.fullScreenContentDelegate = self | ||
} catch { | ||
print("Failed to load interstitial ad with error: \(error.localizedDescription)") | ||
} | ||
} | ||
|
||
func showAd() { | ||
guard let interstitialAd = interstitialAd else { | ||
return print("Ad wasn't ready.") | ||
} | ||
|
||
interstitialAd.present(fromRootViewController: nil) | ||
} | ||
|
||
// MARK: - GADFullScreenContentDelegate methods | ||
|
||
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { | ||
interstitialAd = nil | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeAdViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import GoogleMobileAds | ||
|
||
class NativeAdViewModel: NSObject, ObservableObject, GADNativeAdLoaderDelegate { | ||
@Published var nativeAd: GADNativeAd? | ||
private var adLoader: GADAdLoader! | ||
|
||
func refreshAd() { | ||
adLoader = GADAdLoader( | ||
adUnitID: | ||
"ca-app-pub-3940256099942544/3986624511", | ||
rootViewController: nil, | ||
adTypes: [.native], options: nil) | ||
adLoader.delegate = self | ||
adLoader.load(GADRequest()) | ||
} | ||
|
||
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) { | ||
self.nativeAd = nativeAd | ||
nativeAd.delegate = self | ||
} | ||
|
||
func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) { | ||
print("\(adLoader) failed with error: \(error.localizedDescription)") | ||
} | ||
} | ||
|
||
// MARK: - GADNativeAdDelegate implementation | ||
extension NativeAdViewModel: GADNativeAdDelegate { | ||
func nativeAdDidRecordClick(_ nativeAd: GADNativeAd) { | ||
print("\(#function) called") | ||
} | ||
|
||
func nativeAdDidRecordImpression(_ nativeAd: GADNativeAd) { | ||
print("\(#function) called") | ||
} | ||
|
||
func nativeAdWillPresentScreen(_ nativeAd: GADNativeAd) { | ||
print("\(#function) called") | ||
} | ||
|
||
func nativeAdWillDismissScreen(_ nativeAd: GADNativeAd) { | ||
print("\(#function) called") | ||
} | ||
|
||
func nativeAdDidDismissScreen(_ nativeAd: GADNativeAd) { | ||
print("\(#function) called") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...dvanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import GoogleMobileAds | ||
|
||
class RewardedInterstitialViewModel: NSObject, ObservableObject, | ||
GADFullScreenContentDelegate | ||
{ | ||
@Published var coins = 0 | ||
private var rewardedInterstitialAd: GADRewardedInterstitialAd? | ||
|
||
func loadAd() async { | ||
do { | ||
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load( | ||
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest()) | ||
rewardedInterstitialAd?.fullScreenContentDelegate = self | ||
} catch { | ||
print( | ||
"Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") | ||
} | ||
} | ||
|
||
func showAd() { | ||
guard let rewardedInterstitialAd = rewardedInterstitialAd else { | ||
return print("Ad wasn't ready.") | ||
} | ||
|
||
rewardedInterstitialAd.present(fromRootViewController: nil) { | ||
let reward = rewardedInterstitialAd.adReward | ||
print("Reward amount: \(reward.amount)") | ||
self.addCoins(reward.amount.intValue) | ||
} | ||
} | ||
|
||
func addCoins(_ amount: Int) { | ||
coins += amount | ||
} | ||
|
||
// MARK: - GADFullScreenContentDelegate methods | ||
|
||
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { | ||
rewardedInterstitialAd = nil | ||
} | ||
} |
Oops, something went wrong.