mirror of
https://github.com/TermiT/Flycut.git
synced 2025-02-23 15:34:58 +08:00
Move observers for events that should involve the Model from the AppDelegate to the ViewController, eliminating a messy linkage from AppDelegate to ViewController.
This commit is contained in:
parent
2a9197dccf
commit
d65a709aae
2 changed files with 15 additions and 11 deletions
|
@ -14,15 +14,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
|||
|
||||
var window: UIWindow?
|
||||
|
||||
var myViewController: ViewController!
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
|
||||
// Override point for customization after application launch.
|
||||
let appDelegate = UIApplication.shared.delegate as! AppDelegate
|
||||
let viewController = self.window?.rootViewController as! ViewController
|
||||
|
||||
appDelegate.myViewController = viewController
|
||||
|
||||
let center = UNUserNotificationCenter.current()
|
||||
center.requestAuthorization(options:[]) { (granted, error) in
|
||||
// Enable or disable features based on authorization.
|
||||
|
@ -59,13 +52,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
|||
func applicationWillResignActive(_ application: UIApplication) {
|
||||
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
|
||||
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
|
||||
myViewController.saveEngine();
|
||||
}
|
||||
|
||||
func applicationDidEnterBackground(_ application: UIApplication) {
|
||||
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
|
||||
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
||||
myViewController.saveEngine();
|
||||
}
|
||||
|
||||
func applicationWillEnterForeground(_ application: UIApplication) {
|
||||
|
@ -74,12 +65,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
|
|||
|
||||
func applicationDidBecomeActive(_ application: UIApplication) {
|
||||
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
||||
myViewController.checkForClippingAddedToClipboard()
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ application: UIApplication) {
|
||||
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
||||
myViewController.saveEngine();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -76,6 +76,21 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
|
|||
NotificationCenter.default.addObserver(self, selector: #selector(self.checkForClippingAddedToClipboard), name: .UIPasteboardChanged, object: nil)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(self.applicationWillTerminate), name: .UIApplicationWillTerminate, object: nil)
|
||||
|
||||
// Check for clipping whenever we become active.
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(self.checkForClippingAddedToClipboard), name: .UIApplicationDidBecomeActive, object: nil)
|
||||
checkForClippingAddedToClipboard() // Since the first-launch notification will occur before we add observer.
|
||||
|
||||
// Register for notifications for the scenarios in which we should save the engine.
|
||||
[ Notification.Name.UIApplicationWillResignActive,
|
||||
Notification.Name.UIApplicationDidEnterBackground,
|
||||
Notification.Name.UIApplicationWillTerminate ]
|
||||
.forEach { (notification) in
|
||||
NotificationCenter.default.addObserver(self,
|
||||
selector: #selector(self.saveEngine),
|
||||
name: notification,
|
||||
object: nil)
|
||||
}
|
||||
}
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
|
|
Loading…
Reference in a new issue