Last active
October 13, 2022 08:55
-
-
Save BadhanGanesh/43e942d102490f1933a1c3cb8b6cc3e0 to your computer and use it in GitHub Desktop.
PopUpTimeChecker will check if something that happened last time exceeds a specific time duration.
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
import UIKit | |
class ImageViewController: UIViewController { | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
if let hiResFlag = hiResFlag { | |
if hiResFlag == false { | |
if PopUpTimeChecker.shouldShowPopUp() { | |
self.presentAlert() | |
} | |
} | |
} | |
} | |
func presentAlert() { | |
let alert = UIAlertController.init(title: nil, message: "Show Pop up", preferredStyle: .alert) | |
let action = UIAlertAction.init(title: "Yeahh!", style: .default, handler: nil) | |
alert.addAction(action) | |
self.present(alert, animated: true, completion: nil) | |
} | |
} |
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
import UIKit | |
@objc class PopUpTimeChecker { | |
////////////////////////////////////////////////////////////////////// | |
//MARK:- | |
//MARK:- Properties | |
//MARK:- | |
////////////////////////////////////////////////////////////////////// | |
@objc static var popUpTimeInterval: UInt64 = 1296000 //15 days in seconds | |
@objc static var hiResFlag: Bool? { | |
get { | |
return UserDefaults.standard.object(forKey: "HiResFlag") as? Bool | |
} | |
set { | |
UserDefaults.standard.setValue(newValue, forKey: "HiResFlag") | |
} | |
} | |
@objc static var isFirstTimePopUp: Bool { | |
get { | |
let value = UserDefaults.standard.object(forKey: "IsFirstTimePopUp") | |
return value == nil ? true : value as! Bool | |
} | |
set { | |
UserDefaults.standard.setValue(newValue, forKey: "IsFirstTimePopUp") | |
} | |
} | |
@objc static var lastDateOfPopUp: Date? { | |
get { | |
return UserDefaults.standard.object(forKey: "LastDateOfPopUp") as? Date | |
} | |
set { | |
UserDefaults.standard.setValue(newValue, forKey: "LastDateOfPopUp") | |
} | |
} | |
////////////////////////////////////////////////////////////////////// | |
//MARK:- | |
//MARK:- Methods | |
//MARK:- | |
////////////////////////////////////////////////////////////////////// | |
@objc static fileprivate func setLastPopUpDate() { | |
//Setting current date to last shown pop up date | |
lastDateOfPopUp = Date() | |
} | |
@objc static fileprivate func timeIntervalSinceLastPopUp() -> UInt64 { | |
//Returning how much time (in seconds) has passed from last popup date until now | |
return UInt64(Date().timeIntervalSince(lastDateOfPopUp!)) | |
} | |
@objc @discardableResult static func shouldShowPopUp() -> Bool { | |
//We proceed further only if we have the last date when pop up was displayed, else we create and set it as the current date | |
guard let _ = lastDateOfPopUp else { | |
self.setLastPopUpDate() | |
return self.shouldShowPopUp() | |
} | |
let timeInterval = timeIntervalSinceLastPopUp() | |
if timeInterval > popUpTimeInterval { | |
self.setLastPopUpDate() | |
return true //Show pop up | |
} else { | |
if isFirstTimePopUp { | |
//If this is the first time, you just allow the pop up to show, don't allow otherwise | |
isFirstTimePopUp = false | |
return true | |
} else { | |
return false | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment