From 129be7142aba0758d16cc1bba7cbb11d9b0a3ba1 Mon Sep 17 00:00:00 2001 From: BilalKhanWDI Date: Tue, 24 Sep 2024 20:10:45 +0530 Subject: [PATCH] - Fixed a bug for logout, clearing cache for my list - Made last data sync, will check in 1 hour if the last sync done time is past 1 hour, will give a sync in background - 3-3:30 meeting with WOKA for ads - Finalised the data sync when app goes in background. - Muted google ads from splash. - Completed Data sync from logout , it will now show first its syncing the data and then it will logout. --- WOKA.xcodeproj/project.pbxproj | 12 +- WOKA/Audio Books/AudioBookHomeVC.swift | 2 +- WOKA/Constants K/GVar.swift | 30 +++ .../AdsDM.swift | 0 .../PersistentStorage.swift | 184 +++++++++++------- WOKA/Games/Controller/GamesListVC.swift | 4 +- WOKA/Home/Controller/HomeVC.swift | 8 +- WOKA/Home/MyListDataTemp.swift | 4 + WOKA/Home/ViewModel/MyListVM.swift | 2 + WOKA/Main/AuthFunc/AuthFunc.swift | 19 +- WOKA/Main/Delegate/SceneDelegate.swift | 7 + WOKA/Main/JWPlayerManager.swift | 3 +- .../Controller/SplashVC.swift | 5 +- WOKA/Shop/Controller/ShopListingVC.swift | 4 +- WOKA/Shop/ViewModel/ShopListingVM.swift | 2 +- .../Controller/SideMenuVC.swift | 15 +- .../ViewModel/SideMenuVM.swift | 34 +++- WOKA/Theme/Controller/ThemeOneVC.swift | 5 +- WOKA/Theme/ViewModel/ThemeOneVM.swift | 2 +- 19 files changed, 242 insertions(+), 100 deletions(-) rename WOKA/{Main/AuthFunc => DataSync&AdsManage}/AdsDM.swift (100%) rename WOKA/{ => DataSync&AdsManage}/PersistentStorage.swift (85%) diff --git a/WOKA.xcodeproj/project.pbxproj b/WOKA.xcodeproj/project.pbxproj index 8b316d4..2f215ab 100644 --- a/WOKA.xcodeproj/project.pbxproj +++ b/WOKA.xcodeproj/project.pbxproj @@ -853,6 +853,15 @@ path = Validations; sourceTree = ""; }; + 521E5A9E2CA2AD3500AEF131 /* DataSync&AdsManage */ = { + isa = PBXGroup; + children = ( + 52ACC1292C610EC900791528 /* PersistentStorage.swift */, + 52CC4A732C883B3F001BE47C /* AdsDM.swift */, + ); + path = "DataSync&AdsManage"; + sourceTree = ""; + }; 522242582BFC73E40085C632 /* SideMenu */ = { isa = PBXGroup; children = ( @@ -947,6 +956,7 @@ 523ED25C2BDA2BC700CFED02 /* WOKA */ = { isa = PBXGroup; children = ( + 521E5A9E2CA2AD3500AEF131 /* DataSync&AdsManage */, 5276865D2C8879FD001A5496 /* AdClicksImpressions+CoreDataClass.swift */, 5276865E2C8879FD001A5496 /* AdClicksImpressions+CoreDataProperties.swift */, 527A2BD32C57D0B20080DF9B /* Address */, @@ -972,7 +982,6 @@ 9C27E1612BDB6F0F00EC1DA9 /* Main */, 525954152BE8CAC900191286 /* Network Adapter */, 523ED28E2BDA372C00CFED02 /* OnBoarding Module */, - 52ACC1292C610EC900791528 /* PersistentStorage.swift */, 52A6DC942C4E385500F63C51 /* Shop */, 522A93142C0DE8770098FE49 /* SideBarNav */, 525327D72BFCC30400F64283 /* TabBar & SideMenu */, @@ -1721,7 +1730,6 @@ 525327D82BFCDDF700F64283 /* AuthFuncStartupSoundHandling.swift */, 52FDBA7A2BFF2712009D7AC7 /* AuthFuncTimeHandling.swift */, 9CC0D2F92C6F33BE0019DF73 /* AuthFuncUserVideoView.swift */, - 52CC4A732C883B3F001BE47C /* AdsDM.swift */, ); path = AuthFunc; sourceTree = ""; diff --git a/WOKA/Audio Books/AudioBookHomeVC.swift b/WOKA/Audio Books/AudioBookHomeVC.swift index 2851adc..ec4ae5a 100644 --- a/WOKA/Audio Books/AudioBookHomeVC.swift +++ b/WOKA/Audio Books/AudioBookHomeVC.swift @@ -99,7 +99,7 @@ class AudioBookHomeVC: UIViewController { */ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let audioBooksAd = adsData.result?.filter({$0.slug == AdsEnum.audioBooks.rawValue}).first, let adLink = audioBooksAd.advertisement?.adLink, let adID = audioBooksAd.id{ + if let audioBooksAd = adsData.result?.filter({$0.slug == AdsEnum.audioBooks.rawValue}).first, let adLink = audioBooksAd.advertisement?.adLink, let adID = audioBooksAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1) if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) diff --git a/WOKA/Constants K/GVar.swift b/WOKA/Constants K/GVar.swift index f8390df..50f4f21 100644 --- a/WOKA/Constants K/GVar.swift +++ b/WOKA/Constants K/GVar.swift @@ -41,10 +41,40 @@ extension K{ */ static var adTimeStamp : Date? = nil + /* + This will handle sync timing for sending data from coredata. + Basically keeping this to do the sync every hour if data is founnd + */ + static var lastDataSync : Date? = nil + /* User Skipped update */ static var skippedUpdate : Bool? = nil + + /* + Reset to defaults at the time of logout + */ + static func resetToDefault() { + lastOpenTimestamp = nil + reloadMyList = false + topView = TopViewEnum.theme1 + myListSoftReload = false + reloadMyListAudioBooks = false + reloadKaraoke = false + reloadGames = false + reloadHindiWebseries = false + reloadEnglishWebseries = false + reloadMyListAll = 0 + reloadMyListAllID = 0 + reloadContinueKaraoke = false + reloadContinueAudioBooks = false + reloadContinueWebSeries = false + selectedUsername = nil + adTimeStamp = nil + lastDataSync = nil + skippedUpdate = nil + } } } diff --git a/WOKA/Main/AuthFunc/AdsDM.swift b/WOKA/DataSync&AdsManage/AdsDM.swift similarity index 100% rename from WOKA/Main/AuthFunc/AdsDM.swift rename to WOKA/DataSync&AdsManage/AdsDM.swift diff --git a/WOKA/PersistentStorage.swift b/WOKA/DataSync&AdsManage/PersistentStorage.swift similarity index 85% rename from WOKA/PersistentStorage.swift rename to WOKA/DataSync&AdsManage/PersistentStorage.swift index a1bfa6d..6810a30 100644 --- a/WOKA/PersistentStorage.swift +++ b/WOKA/DataSync&AdsManage/PersistentStorage.swift @@ -8,11 +8,6 @@ import Foundation import CoreData -/** - Hey there, I hope you enjoyed the video, if you have any questions then please feel free to ask I will be happy to answer them. - Do share this with your iOS group on whatsapp or facebook or anyone who wants to learn iOS - */ - enum PersistentStorageEnum : String{ case UserClicks case click_counts @@ -72,13 +67,6 @@ struct AdsClickImpressionsData : Codable { } } -//struct UserVideoViewData { -// let postId: Int -// let postType: Int -// let watchedDuration: Double -// let categoryId: Int -//} - final class PersistentStorage { @@ -225,13 +213,10 @@ final class PersistentStorage } //Sending Clicks data to our server - func sendDataToServer() { - + func sendDataToServer(isLogout : Bool = false) { //We need to create a context from this container let managedContext = PersistentStorage.shared.context - -// let path = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) - // debugPrint(path[0]) + let fetchRequest:NSFetchRequest = NSFetchRequest.init(entityName: "UserClicks") fetchRequest.fetchLimit = 15 do { @@ -241,8 +226,8 @@ final class PersistentStorage print("UserClicks Count from SendData to server :- ", result.count) //if data is less , dont keep sending the data to server - if result.count < 5 { - print("Not Enough Data") + if isLogout == false,result.count < 3 { + print("Not Enough Clicks Data") return } @@ -256,7 +241,7 @@ final class PersistentStorage // send data to server NetworkManager.shareInstance.nwCallRawJSON(clicksData: userClicks) { isDone in if isDone{ // if data is send to server and we get success callback then delete that data from coredata entity. - self.deleteData(result) + self.deleteData(result,isLogout: isLogout) } } } @@ -267,7 +252,7 @@ final class PersistentStorage } //Delete data from CoreData - func deleteData(_ data: [UserClicks]) { + func deleteData(_ data: [UserClicks],isLogout : Bool = false) { let managedContext = PersistentStorage.shared.context data.forEach { clicks in @@ -278,7 +263,7 @@ final class PersistentStorage try managedContext.save() //after deleting check if more data exist. - getAllData() + getAllData(isLogout: isLogout) print("Deleted data") } catch let error { debugPrint("Failed to delete data:", error) @@ -286,7 +271,7 @@ final class PersistentStorage } //Get all data from DB - func getAllData() { + func getAllData(isLogout : Bool = false) { //We need to create a context from this container let managedContext = PersistentStorage.shared.context let fetchRequest:NSFetchRequest = NSFetchRequest.init(entityName: "UserClicks") @@ -296,7 +281,7 @@ final class PersistentStorage print("UserClicks Count from getAllData :- ", result.count) //if data is less , dont keep sending the data to server - if result.count < 5 { + if isLogout == false,result.count < 5 { print("Not Enough Data") return } else{ @@ -364,9 +349,13 @@ final class PersistentStorage let userClicks = UserClickData(clickCounts: 1, categoryId: catID, postId: postID, postType: postType.rawValue) PersistentStorage.shared.checkWebSeries(clicksData: userClicks) } - // MARK: - Handling ADS +} + +// MARK: - Handling ADS + +extension PersistentStorage{ - func sendAdsData() { + func sendAdsData(isLogout : Bool = false) { //create a context from this container let managedContext = PersistentStorage.shared.context @@ -378,20 +367,21 @@ final class PersistentStorage print("userImpressions Count from SendAdsData to server :- ", result.count) //if data is less , dont keep sending the data to server - if result.count < 2 { - print("Not Enough Data") + if isLogout == false,result.count < 2 { + print("Not Enough Ads Data") return } var userImpressions = [AdsClickImpressionsData]() - + //map the impressions result.forEach { ads in // device type 1- android , 2 - iOS userImpressions.append(AdsClickImpressionsData(adID: Int(ads.ad_id), noOfClick: Int(ads.no_of_click), noOfOmpression: Int(ads.no_of_impression))) - print("ID:-" , ads.ad_id, "No Of Clicks :- ", ads.no_of_click, "Impressions :- ", ads.no_of_impression) + print("ADs Data :- ","ID:-" , ads.ad_id, "No Of Clicks :- ", ads.no_of_click, "Impressions :- ", ads.no_of_impression) } + //Send it to server and delete from our DB NetworkManager.shareInstance.nwCallRawJSONAds(adsData: userImpressions) { isDone in if isDone{ self.deleteAdsData(result) @@ -404,8 +394,50 @@ final class PersistentStorage } } + func deleteAdsData(_ data: [AdClicksImpressions],isLogout : Bool = false) { + let managedContext = PersistentStorage.shared.context + + data.forEach { clicks in + managedContext.delete(clicks) + } + + do { + try managedContext.save() + getAllAdsData(isLogout: isLogout) + print("Deleted data") + } catch let error { + debugPrint("Failed to delete data:", error) + } + } + + func getAllAdsData(isLogout : Bool = false) { + //We need to create a context from this container + let managedContext = PersistentStorage.shared.context + let fetchRequest:NSFetchRequest = NSFetchRequest.init(entityName: "AdClicksImpressions") + do { + guard let result = try managedContext.fetch(fetchRequest) as? [AdClicksImpressions] else {return} + + print("UserClicks Count from getAllData :- ", result.count) + //if data is less , dont keep sending the data to server + if isLogout == false,result.count < 2 { + print("Not Enough Ads Data") + return + }else{ + // send ads data again to server + sendAdsData() + print("Ads Data sent Again.") + } + +// result.forEach { clicks in +// // device type 1- android , 2 - iOS +// print("ID:-" , PostType(rawValue: Int(clicks.post_type))!, "CatID:- ", clicks.category_id, "PostID:- ", clicks.post_id , "Count:-", clicks.click_counts) +// } + } catch let error{ + debugPrint(error) + } + } + func checkIfAdExist(adsData : AdsClickImpressionsData) { - let managedContext = PersistentStorage.shared.context let fetchRequest = NSFetchRequest(entityName: "AdClicksImpressions") fetchRequest.predicate = NSPredicate(format: "ad_id == %@" ,adsData.adID.toString()) @@ -436,8 +468,9 @@ final class PersistentStorage } } + // MARK: - Add Ads to CoreData + func createAdsData(data : AdsClickImpressionsData){ - //We need to create a context from this container let managedContext = PersistentStorage.shared.context @@ -448,6 +481,7 @@ final class PersistentStorage do { try managedContext.save() + //show the ad saved in cored data retrieveAdsData(adID: data.adID) } catch let error as NSError { print("Could not save. \(error), \(error.userInfo)") @@ -475,50 +509,60 @@ final class PersistentStorage let adsData = AdsClickImpressionsData(adID: adID, noOfClick: clicks, noOfOmpression: impressions) PersistentStorage.shared.checkIfAdExist( adsData: adsData) } - - func deleteAdsData(_ data: [AdClicksImpressions]) { + +} + +// MARK: - Sync CoreData to server + +extension PersistentStorage{ + + func getEntityDataCount() -> (Int,Int){ let managedContext = PersistentStorage.shared.context - - data.forEach { clicks in - managedContext.delete(clicks) - } - + let adsFetchReq:NSFetchRequest = NSFetchRequest.init(entityName: "AdClicksImpressions") + let clicksFetchReq:NSFetchRequest = NSFetchRequest.init(entityName: "UserClicks") + do { - try managedContext.save() - getAllAdsData() - print("Deleted data") - } catch let error { - debugPrint("Failed to delete data:", error) + // Perform the count request + let adsCount = try managedContext.count(for: adsFetchReq) + let clicksCount = try managedContext.count(for: clicksFetchReq) + return (adsCount,clicksCount) + } catch let error as NSError { + print("Error fetching count: \(error), \(error.userInfo)") + return (0,0) } } - func getAllAdsData() { - //We need to create a context from this container - let managedContext = PersistentStorage.shared.context - let fetchRequest:NSFetchRequest = NSFetchRequest.init(entityName: "AdClicksImpressions") -// fetchRequest.fetchLimit = 2 - do { - guard let result = try managedContext.fetch(fetchRequest) as? [AdClicksImpressions] else {return} - - print("UserClicks Count from getAllData :- ", result.count) - //if data is less , dont keep sending the data to server - if result.count < 2 { - print("Not Enough Ads Data") - return - } else{ - // send ads data again to server - sendAdsData() - print("Ads Data sent Again.") + func checkLastSync(){ + /* + check ad timestamp from gvar + show ads if the saved timestamp time and current time has diffrenece of 30 minutes + */ + if let timeStamp = K.GVar.lastDataSync{ + /* + check if timestamp difference is equal and greater than 60 minutes i.e 3600 seconds + */ + let duration = DateFormatterLib.dateDifferenceINT(date1: timeStamp, date2: Date()) + print("Last Sync Duration :- ", duration, " Seconds.") + + if duration <= 3600{ //if last data sync has not + // dont sync data. + print("Last sync has not passed 1 hour") + }else{ + //Sync data to server if the last sync time has been changed. + syncData() } - -// result.forEach { clicks in -// // device type 1- android , 2 - iOS -// print("ID:-" , PostType(rawValue: Int(clicks.post_type))!, "CatID:- ", clicks.category_id, "PostID:- ", clicks.post_id , "Count:-", clicks.click_counts) -// } - } - catch let error - { - debugPrint(error) + }else{ + /* + Assign the date and sync the data to server + */ + syncData() } } + + func syncData(){ + K.GVar.lastDataSync = Date() + self.sendAdsData() + self.sendDataToServer() + print("Data Synced to Server.") + } } diff --git a/WOKA/Games/Controller/GamesListVC.swift b/WOKA/Games/Controller/GamesListVC.swift index 762cf4b..1a1a2b5 100644 --- a/WOKA/Games/Controller/GamesListVC.swift +++ b/WOKA/Games/Controller/GamesListVC.swift @@ -67,7 +67,7 @@ class GamesListVC: UIViewController { */ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first, let adID = gamesAd.id{ + if let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first, let adID = gamesAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1) } } @@ -86,7 +86,7 @@ class GamesListVC: UIViewController { */ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first, let adLink = gamesAd.advertisement?.adLink, let adID = gamesAd.id{ + if let gamesAd = adsData.result?.filter({$0.slug == AdsEnum.games.rawValue}).first, let adLink = gamesAd.advertisement?.adLink, let adID = gamesAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1) if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) diff --git a/WOKA/Home/Controller/HomeVC.swift b/WOKA/Home/Controller/HomeVC.swift index 98a2176..cd188f3 100644 --- a/WOKA/Home/Controller/HomeVC.swift +++ b/WOKA/Home/Controller/HomeVC.swift @@ -18,13 +18,7 @@ class HomeVC: UIViewController ,ChildViewControllerDelegate{ override func viewDidLoad() { super.viewDidLoad() - //setup google ads for fullscreen if its turned on. - if let adsData = AuthFunc.shareInstance.adsData, let gamesIntersitialAd = adsData.result?.filter({$0.slug == AdsEnum.gameInterestial.rawValue}).first, gamesIntersitialAd.googleAd != nil{ - /* - setup google ads. - */ - GoogleInterstistialADSetup.shareInstance.setupGoogleIntersitialAD() - } + switch AuthFunc.shareInstance.selectedTheme { case .theme1: diff --git a/WOKA/Home/MyListDataTemp.swift b/WOKA/Home/MyListDataTemp.swift index e466f88..14fb1a3 100644 --- a/WOKA/Home/MyListDataTemp.swift +++ b/WOKA/Home/MyListDataTemp.swift @@ -18,6 +18,10 @@ class MyListDataTemp{ var isDatafetched = false + func removeAll(){ + isDatafetched = false + MyListDataTemp.shareInstance.favListingData = nil + } // MARK: - Get Favourite Listing diff --git a/WOKA/Home/ViewModel/MyListVM.swift b/WOKA/Home/ViewModel/MyListVM.swift index b5d5962..9197664 100644 --- a/WOKA/Home/ViewModel/MyListVM.swift +++ b/WOKA/Home/ViewModel/MyListVM.swift @@ -52,6 +52,8 @@ class MyListVM{ DispatchQueue.main.async { self.vc.shimmerView.forEach { shimmer in if load{ + shimmer.gradientColorOne = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) + shimmer.gradientColorTwo = #colorLiteral(red: 0.6, green: 0.8862745098, blue: 1, alpha: 1) shimmer.startShimmer() }else{ shimmer.stopShimmer() diff --git a/WOKA/Main/AuthFunc/AuthFunc.swift b/WOKA/Main/AuthFunc/AuthFunc.swift index 36ff7fc..ebedfe5 100644 --- a/WOKA/Main/AuthFunc/AuthFunc.swift +++ b/WOKA/Main/AuthFunc/AuthFunc.swift @@ -195,9 +195,19 @@ class AuthFunc{ UserDefaults.standard.setValue(nil, forKey: K.UserDefaultsStruct.isUserLogined) UserDefaults.standard.setValue(nil, forKey: K.UserDefaultsStruct.userAccessToken) UserDefaults.standard.setValue(nil, forKey: K.UserDefaultsStruct.userType) + + //clear userdata userData = nil + + //clear cart & Mylist cache CartDataCache.shareInstance.removeAll() - selectedTheme = .theme1 + MyListDataTemp.shareInstance.removeAll() + + //Reset the Global Var + K.GVar.resetToDefault() + + //Reset default selected theme + selectedTheme = .theme1 } func loginDefaults(data : UserDataDM.ResultData){ @@ -245,6 +255,13 @@ class AuthFunc{ case 1: guard let data = data.data else{return} AuthFunc.shareInstance.adsData = data + //setup google ads for fullscreen if its turned on. +// if let gamesIntersitialAd = data.result?.filter({$0.slug == AdsEnum.gameInterestial.rawValue}).first, gamesIntersitialAd.googleAd != nil{ +// /* +// setup google ads. +// */ +// GoogleInterstistialADSetup.shareInstance.setupGoogleIntersitialAD() +// } onCompletion?(true) default: onCompletion?(false) diff --git a/WOKA/Main/Delegate/SceneDelegate.swift b/WOKA/Main/Delegate/SceneDelegate.swift index d0a633e..96ca735 100644 --- a/WOKA/Main/Delegate/SceneDelegate.swift +++ b/WOKA/Main/Delegate/SceneDelegate.swift @@ -44,6 +44,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // Called as the scene transitions from the foreground to the background. // Use this method to save data, release shared resources, and store enough scene-specific state information // to restore the scene back to its current state. + + /* + If user is loggined check for sync + */ + if AuthFunc.shareInstance.checkLogin(){ + PersistentStorage.shared.checkLastSync() + } } diff --git a/WOKA/Main/JWPlayerManager.swift b/WOKA/Main/JWPlayerManager.swift index 8cb3c0f..7f83489 100644 --- a/WOKA/Main/JWPlayerManager.swift +++ b/WOKA/Main/JWPlayerManager.swift @@ -126,7 +126,8 @@ class JWPlayerManager { let adBreakBuilder1 = JWAdBreakBuilder() .offset(.midroll(percent: 20)) // .offset(.from(string: "20%")!) - .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250,728x90&gdfp_req=1&output=vast4&unviewed_position_start=1&env=vp&impl=s&correlator=7898879")!]) +// .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_preroll_skippable&sz=640x480&ciu_szs=300x250,728x90&gdfp_req=1&output=vast4&unviewed_position_start=1&env=vp&impl=s&correlator=7898879")!]) + .tags([URL(string: "https://pubads.g.doubleclick.net/gampad/ads?iu=/23204950200/VideoAd&description_url=https%3A%2F%2Fwokaland.com%2F&tfcd=0&npa=0&sz=480x320%7C640x480&ciu_szs=480x320&min_ad_duration=5000&max_ad_duration=20000&gdfp_req=1&unviewed_position_start=1&output=vast&env=vp&impl=s&correlator=")!]) guard let adBreak1 = try? adBreakBuilder1.build() else { print("Ad error 2") diff --git a/WOKA/OnBoarding Module/Controller/SplashVC.swift b/WOKA/OnBoarding Module/Controller/SplashVC.swift index 9630a0a..a19ce98 100644 --- a/WOKA/OnBoarding Module/Controller/SplashVC.swift +++ b/WOKA/OnBoarding Module/Controller/SplashVC.swift @@ -35,8 +35,8 @@ class SplashVC: UIViewController { */ DispatchQueue.main.asyncAfter(deadline: .now(), execute: { [weak self] in guard let self else{return} - AdReusable.sharedInstance.setupBannerAd(bannerView: vm.banner1, in: adBanner1, adUnitID: K.GoogleAdIDs.splashBanner1, viewController: self) - AdReusable.sharedInstance.setupBannerAd(bannerView: vm.banner2, in: adBanner2, adUnitID: K.GoogleAdIDs.splashBanner2, viewController: self) +// AdReusable.sharedInstance.setupBannerAd(bannerView: vm.banner1, in: adBanner1, adUnitID: K.GoogleAdIDs.splashBanner1, viewController: self) +// AdReusable.sharedInstance.setupBannerAd(bannerView: vm.banner2, in: adBanner2, adUnitID: K.GoogleAdIDs.splashBanner2, viewController: self) vm.initView() }) @@ -54,7 +54,6 @@ class SplashVC: UIViewController { vm.playSplashSound() animateImageScale() - } @IBAction func languageBtnTapped(_ sender: UIButton) { diff --git a/WOKA/Shop/Controller/ShopListingVC.swift b/WOKA/Shop/Controller/ShopListingVC.swift index ce854c3..f42ef41 100644 --- a/WOKA/Shop/Controller/ShopListingVC.swift +++ b/WOKA/Shop/Controller/ShopListingVC.swift @@ -55,7 +55,7 @@ class ShopListingVC: UIViewController { */ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let shopSuperCatAd = adsData.result?.filter({$0.slug == AdsEnum.shop_super_category.rawValue}).first, let adID = shopSuperCatAd.id{ + if let shopSuperCatAd = adsData.result?.filter({$0.slug == AdsEnum.shop_super_category.rawValue}).first, let adID = shopSuperCatAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1) } } @@ -95,7 +95,7 @@ extension ShopListingVC : TableViewSRC{ if vm.superCatData[indexPath.row].isAD == true{ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let shopSuperCatAd = adsData.result?.filter({$0.slug == AdsEnum.shop_super_category.rawValue}).first,let adLink = shopSuperCatAd.advertisement?.adLink, let adID = shopSuperCatAd.id{ + if let shopSuperCatAd = adsData.result?.filter({$0.slug == AdsEnum.shop_super_category.rawValue}).first,let adLink = shopSuperCatAd.advertisement?.adLink, let adID = shopSuperCatAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1) if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url) diff --git a/WOKA/Shop/ViewModel/ShopListingVM.swift b/WOKA/Shop/ViewModel/ShopListingVM.swift index aae2b60..a71f92a 100644 --- a/WOKA/Shop/ViewModel/ShopListingVM.swift +++ b/WOKA/Shop/ViewModel/ShopListingVM.swift @@ -90,7 +90,7 @@ class ShopListingVM{ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries if let shopSuperCatAd = adsData.result?.filter({$0.slug == AdsEnum.shop_super_category.rawValue}).first, let bannerImage = shopSuperCatAd.advertisement?.bannerImage{ - self.superCatData.append(ShopSuperCategoryDM.ResultData(id: shopSuperCatAd.id, superCategoryName: shopSuperCatAd.advertisement?.title, superCategoryThumbnail: bannerImage,isAD: true)) + self.superCatData.append(ShopSuperCategoryDM.ResultData(id: shopSuperCatAd.advertisement?.id, superCategoryName: shopSuperCatAd.advertisement?.title, superCategoryThumbnail: bannerImage,isAD: true)) }else{ // check if google ads } diff --git a/WOKA/TabBar & SideMenu/Controller/SideMenuVC.swift b/WOKA/TabBar & SideMenu/Controller/SideMenuVC.swift index 0d948ce..28f9bc5 100644 --- a/WOKA/TabBar & SideMenu/Controller/SideMenuVC.swift +++ b/WOKA/TabBar & SideMenu/Controller/SideMenuVC.swift @@ -114,12 +114,15 @@ class SideMenuVC: UIViewController { switch mode{ case .yes: PersistentStorage.shared.addOthersCount() - //If user clicked to proceed on login. Call the api. - vm.logoutUser { isDone in - self.sideMenuController?.hideMenu() - AuthFunc.shareInstance.logout() - UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB)) - } + /* + Make sure all data is synced to the server for click data & ad impressions + */ + PersistentStorage.shared.sendAdsData(isLogout: true) + PersistentStorage.shared.sendDataToServer(isLogout: true) + Utilities.startProgressHUD(msg: "Syncing...") + vm.checkData() +// self.timer. + case .no: PersistentStorage.shared.addOthersCount() print("no") diff --git a/WOKA/TabBar & SideMenu/ViewModel/SideMenuVM.swift b/WOKA/TabBar & SideMenu/ViewModel/SideMenuVM.swift index 0468975..6bd0d04 100644 --- a/WOKA/TabBar & SideMenu/ViewModel/SideMenuVM.swift +++ b/WOKA/TabBar & SideMenu/ViewModel/SideMenuVM.swift @@ -11,7 +11,8 @@ import Alamofire class SideMenuVM{ weak var vc : SideMenuVC! - + var syncTimer : Timer? + func initView(){ /* @@ -178,4 +179,35 @@ class SideMenuVM{ } } } + + // MARK: - Sync Data + + func startSyncTimer(){ + if syncTimer == nil { + syncTimer = Timer.scheduledTimer(timeInterval: 2.5, target: self, selector: #selector(checkData), userInfo: nil, repeats: true) + } + } + + @objc func checkData(){ + let count = PersistentStorage.shared.getEntityDataCount() + if count.0 == 0 && count.1 == 0{ // ads , clicks + // if ads and clicks are synced to server, make sure to stop timer and logout user + stopSyncTimer() + Utilities.dismissProgressHUD() + //If user clicked to proceed on login. Call the api. + logoutUser { isDone in + self.vc.sideMenuController?.hideMenu() + AuthFunc.shareInstance.logout() + UIApplication.setRootView(LoginNavVC.instantiate(from: .AuthenticationSB)) + } + }else{ + startSyncTimer() + } + } + + func stopSyncTimer(){ + syncTimer?.invalidate() + syncTimer = nil + } + } diff --git a/WOKA/Theme/Controller/ThemeOneVC.swift b/WOKA/Theme/Controller/ThemeOneVC.swift index 6011b6f..dd9c15f 100644 --- a/WOKA/Theme/Controller/ThemeOneVC.swift +++ b/WOKA/Theme/Controller/ThemeOneVC.swift @@ -98,7 +98,7 @@ class ThemeOneVC: UIViewController { } override func viewDidAppear(_ animated: Bool) { - AuthFunc.shareInstance.getAds() +// AuthFunc.shareInstance.getAds() K.GVar.topView = .theme1 vm.shouldAnimate = true @@ -116,10 +116,11 @@ class ThemeOneVC: UIViewController { */ if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let themeOneAd = adsData.result?.filter({$0.slug == AdsEnum.themeOne.rawValue}).first, let adID = themeOneAd.id{ + if let themeOneAd = adsData.result?.filter({$0.slug == AdsEnum.themeOne.rawValue}).first, let adID = themeOneAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID ,impressions: 1) } } + } override func viewDidLayoutSubviews() { diff --git a/WOKA/Theme/ViewModel/ThemeOneVM.swift b/WOKA/Theme/ViewModel/ThemeOneVM.swift index e289493..bb60d9a 100644 --- a/WOKA/Theme/ViewModel/ThemeOneVM.swift +++ b/WOKA/Theme/ViewModel/ThemeOneVM.swift @@ -315,7 +315,7 @@ class ThemeOneVM{ //check url from api if let adsData = AuthFunc.shareInstance.adsData{ // check if ads data contains ad for webseries - if let themeOneAd = adsData.result?.filter({$0.slug == AdsEnum.themeOne.rawValue}).first, let adLink = themeOneAd.advertisement?.adLink, let adID = themeOneAd.id{ + if let themeOneAd = adsData.result?.filter({$0.slug == AdsEnum.themeOne.rawValue}).first, let adLink = themeOneAd.advertisement?.adLink, let adID = themeOneAd.advertisement?.id{ PersistentStorage.shared.addAdsCount(adID: adID,clicks: 1) if let url = URL(string: adLink), UIApplication.shared.canOpenURL(url) {