Skip to content

Commit

Permalink
Moved the ViewModel classes into their own separate files
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 618233437
  • Loading branch information
Justin Malandruccolo authored and maddevrelgithubbot committed Mar 22, 2024
1 parent 2f2c894 commit a37e1d8
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 157 deletions.
16 changes: 16 additions & 0 deletions Swift/advanced/SwiftUIDemo/SwiftUIDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
/* Begin PBXBuildFile section */
A81C3A4B28786EFF002BFF3B /* InterstitialContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A81C3A4928786EFF002BFF3B /* InterstitialContentView.swift */; };
A83160CD286DF6A8007B34CD /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A8C7446A2864F75B00327C12 /* Assets.xcassets */; };
A83664782BADFFA400F4183A /* RewardedInterstitialViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A83664772BADFFA400F4183A /* RewardedInterstitialViewModel.swift */; };
A836647A2BADFFDC00F4183A /* RewardedViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A83664792BADFFDC00F4183A /* RewardedViewModel.swift */; };
A836647C2BAE000F00F4183A /* NativeAdViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A836647B2BAE000F00F4183A /* NativeAdViewModel.swift */; };
A836647E2BAE002B00F4183A /* InterstitialViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A836647D2BAE002B00F4183A /* InterstitialViewModel.swift */; };
A841C7BD2885D0DF00C37F77 /* NativeAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = A841C7BB2885D0DF00C37F77 /* NativeAdView.xib */; };
A841C7BE2885D0DF00C37F77 /* NativeContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A841C7BC2885D0DF00C37F77 /* NativeContentView.swift */; };
A86D7F88289CD83A00496237 /* CountdownTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A86D7F87289CD83A00496237 /* CountdownTimer.swift */; };
Expand All @@ -24,6 +28,10 @@

/* Begin PBXFileReference section */
A81C3A4928786EFF002BFF3B /* InterstitialContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InterstitialContentView.swift; sourceTree = "<group>"; };
A83664772BADFFA400F4183A /* RewardedInterstitialViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewardedInterstitialViewModel.swift; sourceTree = "<group>"; };
A83664792BADFFDC00F4183A /* RewardedViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewardedViewModel.swift; sourceTree = "<group>"; };
A836647B2BAE000F00F4183A /* NativeAdViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeAdViewModel.swift; sourceTree = "<group>"; };
A836647D2BAE002B00F4183A /* InterstitialViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InterstitialViewModel.swift; sourceTree = "<group>"; };
A841C7BB2885D0DF00C37F77 /* NativeAdView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NativeAdView.xib; sourceTree = "<group>"; };
A841C7BC2885D0DF00C37F77 /* NativeContentView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NativeContentView.swift; sourceTree = "<group>"; };
A86D7F87289CD83A00496237 /* CountdownTimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountdownTimer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -62,6 +70,7 @@
isa = PBXGroup;
children = (
A81C3A4928786EFF002BFF3B /* InterstitialContentView.swift */,
A836647D2BAE002B00F4183A /* InterstitialViewModel.swift */,
);
name = Interstitial;
path = SwiftUIDemo/Interstitial;
Expand All @@ -71,6 +80,7 @@
isa = PBXGroup;
children = (
A841C7BB2885D0DF00C37F77 /* NativeAdView.xib */,
A836647B2BAE000F00F4183A /* NativeAdViewModel.swift */,
A841C7BC2885D0DF00C37F77 /* NativeContentView.swift */,
);
path = Native;
Expand All @@ -80,6 +90,7 @@
isa = PBXGroup;
children = (
A88A283A28AEEA27003946EE /* RewardedContentView.swift */,
A83664792BADFFDC00F4183A /* RewardedViewModel.swift */,
);
name = Rewarded;
path = SwiftUIDemo/Rewarded;
Expand All @@ -90,6 +101,7 @@
children = (
A88A283D28AEEA3A003946EE /* AdDialogContentView.swift */,
A88A283E28AEEA3A003946EE /* RewardedInterstitialContentView.swift */,
A83664772BADFFA400F4183A /* RewardedInterstitialViewModel.swift */,
);
name = "Rewarded-Interstitial";
path = "SwiftUIDemo/Rewarded-Interstitial";
Expand Down Expand Up @@ -230,14 +242,18 @@
files = (
A841C7BE2885D0DF00C37F77 /* NativeContentView.swift in Sources */,
A8C744972864F9E400327C12 /* MenuItem.swift in Sources */,
A836647E2BAE002B00F4183A /* InterstitialViewModel.swift in Sources */,
A8C744952864F9CD00327C12 /* BannerContentView.swift in Sources */,
A86D7F88289CD83A00496237 /* CountdownTimer.swift in Sources */,
A83664782BADFFA400F4183A /* RewardedInterstitialViewModel.swift in Sources */,
A88A283B28AEEA27003946EE /* RewardedContentView.swift in Sources */,
A895BBAF2AA0B20100E1ECBD /* GoogleMobileAdsConsentManager.swift in Sources */,
A81C3A4B28786EFF002BFF3B /* InterstitialContentView.swift in Sources */,
A8C744962864F9E100327C12 /* MenuView.swift in Sources */,
A88A283F28AEEA3A003946EE /* AdDialogContentView.swift in Sources */,
A836647A2BADFFDC00F4183A /* RewardedViewModel.swift in Sources */,
A88A284028AEEA3A003946EE /* RewardedInterstitialContentView.swift in Sources */,
A836647C2BAE000F00F4183A /* NativeAdViewModel.swift in Sources */,
A8C744922864F9B800327C12 /* SwiftUIDemoApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,3 @@ struct InterstitialContentView_Previews: PreviewProvider {
InterstitialContentView(navigationTitle: "Interstitial")
}
}

private 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
}
}
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
}
}
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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,50 +105,3 @@ private struct NativeAdView: UIViewRepresentable {
}
}
}

private 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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SwiftUI
struct AdDialogContentView: View {
@StateObject private var countdownTimer = CountdownTimer()
@Binding var isPresenting: Bool
@Binding var showAd: Bool
@Binding var countdownComplete: Bool

var body: some View {
ZStack {
Expand Down Expand Up @@ -36,7 +36,7 @@ struct AdDialogContentView: View {
}
}
.onAppear {
showAd = false
countdownComplete = false
}
.onChange(
of: isPresenting,
Expand All @@ -48,8 +48,8 @@ struct AdDialogContentView: View {
of: countdownTimer.isComplete,
perform: { newValue in
if newValue {
countdownComplete = true
isPresenting = false
showAd = true
}
}
)
Expand All @@ -59,6 +59,6 @@ struct AdDialogContentView: View {

struct AdDialogContentView_Previews: PreviewProvider {
static var previews: some View {
AdDialogContentView(isPresenting: .constant(true), showAd: .constant(false))
AdDialogContentView(isPresenting: .constant(true), countdownComplete: .constant(false))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct RewardedInterstitialContentView: View {
ZStack {
rewardedInterstitialBody

AdDialogContentView(isPresenting: $showAdDialog, showAd: $showAd)
AdDialogContentView(isPresenting: $showAdDialog, countdownComplete: $showAd)
.opacity(showAdDialog ? 1 : 0)
}
}
Expand Down Expand Up @@ -79,43 +79,3 @@ struct RewardedIntersititalContentView_Previews: PreviewProvider {
RewardedInterstitialContentView(navigationTitle: "Rewarded Interstitial")
}
}

private 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
}
}
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
}
}
Loading

0 comments on commit a37e1d8

Please sign in to comment.