Skip to content

Commit

Permalink
#110 Added restarting of timer when app goes to background
Browse files Browse the repository at this point in the history
kperusko committed Feb 8, 2016
1 parent 9f82c24 commit 368e4db
Showing 3 changed files with 44 additions and 14 deletions.
10 changes: 6 additions & 4 deletions DesignStudioExpress/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -36,14 +36,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


// 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.
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.
AppDelegate.designStudio.disableScheduledTimer(true)
}

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
AppDelegate.designStudio.rescheduleTimer()
}

func applicationDidBecomeActive(application: UIApplication) {
46 changes: 37 additions & 9 deletions DesignStudioExpress/ViewModels/RunningDesignStudio.swift
Original file line number Diff line number Diff line change
@@ -33,10 +33,11 @@ class RunningDesignStudio: NSObject {
private var data: DesignStudio?
private var isRunning = false
private var timer: NSTimer?
private var shouldRescheduleTimer = false

// flag controls if we need to move to next object
// and start the global timer (that shows the End activity screen)
// we have to move to next activity after:
// Flag that controls if we need to move to next object
// and start the global timer (that shows the End activity screen).
// We have to move to next activity after:
// - challenges screen has disappeared
// - next button is clicked on the Timer screen
// - next button is clicked on the End activity screen
@@ -87,7 +88,8 @@ class RunningDesignStudio: NSObject {
// called when user clicks the main action button on challenges screen
// we need to open an appropriate screen based on the state of the design studio
// that can be not started|running|finished
func challengesScreenActionButton (designStudio: DesignStudio) { // start the design studio
func challengesScreenActionButton (designStudio: DesignStudio) {
// start the design studio
if !self.isRunning && !designStudio.started {
self.startDesignStudio(designStudio)
NSNotificationCenter.defaultCenter().postNotificationName(NotificationIdentifier.DesignStudioStarted.rawValue, object: self, userInfo: nil)
@@ -236,7 +238,7 @@ class RunningDesignStudio: NSObject {
func finishDesignStudio() {
// don't forget to stop the timer at the end
// we don't want any more popups
self.timer?.invalidate()
self.disableScheduledTimer(false)
// remove also all local notifications
UIApplication.sharedApplication().scheduledLocalNotifications?.removeAll()

@@ -388,15 +390,41 @@ class RunningDesignStudio: NSObject {
// that will show notify when activity is ended,
// so we can show End activity screen from any of the current views we're on
private func startGlobalTimer() {
self.timer?.invalidate()
if let _ = self.currentActivity {
self.timer = NSTimer.scheduledTimerWithTimeInterval(Double(self.currentActivityRemainingDuration), target: self, selector: "notifyEndActivity", userInfo: nil, repeats: false)

if self.currentActivity != nil {
self.scheduleTimer()
self.createLocalNotification(self.currentActivity!.title + " is finished",
sinceNow: Double(self.currentActivityRemainingDuration))
}
}

func rescheduleTimer() {
if self.shouldRescheduleTimer {
self.shouldRescheduleTimer = false
self.scheduleTimer()
}
}

// disable the timer (e.g. when the app is moving to the background)
// and saves the state for the timer if we need to reschedule it
func disableScheduledTimer(rememberTimerState: Bool) {
if rememberTimerState && self.timer != nil && self.timer!.valid {
self.shouldRescheduleTimer = true
}

self.timer?.invalidate()
self.timer = nil
}

// schedules a NSTimer that will fire when activity ends
// fires a notification that's showing the End Activity screen
private func scheduleTimer() {
if self.isRunning && self.currentActivity != nil {
self.timer?.invalidate()
self.timer = nil
self.timer = NSTimer.scheduledTimerWithTimeInterval(Double(self.currentActivityRemainingDuration), target: self, selector: "notifyEndActivity", userInfo: nil, repeats: false)
}
}

private func createLocalNotification(body: String, sinceNow: Double) {
let timeIsUpAlarm = UILocalNotification()
timeIsUpAlarm.fireDate = NSDate(timeIntervalSinceNow: sinceNow)
Original file line number Diff line number Diff line change
@@ -228,7 +228,7 @@ class UITabBarControllerBase: UITabBarController {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showPostDesignStudioScreen:", name: NotificationIdentifier.ShowPostDesignStudioScreen.rawValue, object: nil)
}

// remove
// remove all observers on deinit
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

0 comments on commit 368e4db

Please sign in to comment.