natsu1211 / Alarm-ios-swift

clone of the official IOS clock alarm app written in swift
MIT License
388 stars 116 forks source link

Infinite Sound looping in other view controller #6

Closed Infinite7Soul closed 7 years ago

Infinite7Soul commented 7 years ago

If I set Alarm Clock and then move to the other VC I will get infinite sound looping and I won't get UIAlertController so Alarm Clock will crash because there's no changing of UISwitch (my Alarm Clock without repeating) so if I set Alarm Clock with everyday repeating I will crash it and I will never get other repeatings https://drive.google.com/open?id=0B9-ghTf1M_xCRFdOZk9BcG96S00

Infinite7Soul commented 7 years ago

I've made improvements and resolved this problem:

  1. I've made a new swift file with this code: `import Foundation

import UIKit

public extension UIWindow { public var visibleViewController: UIViewController? { return UIWindow.getVisibleViewControllerFrom(vc: self.rootViewController) }

public static func getVisibleViewControllerFrom(vc: UIViewController?) -> UIViewController? {
    if let nc = vc as? UINavigationController {
        return UIWindow.getVisibleViewControllerFrom(vc: nc.visibleViewController)
    } else if let tc = vc as? UITabBarController {
        return UIWindow.getVisibleViewControllerFrom(vc: tc.selectedViewController)
    } else {
        if let pvc = vc?.presentedViewController {
            return UIWindow.getVisibleViewControllerFrom(vc: pvc)
        } else {
            return vc
        }
    }
}

}`

  1. Then in main.stroyboard I've made Storyboard.ID for MainAlarmViewController

    2017-03-25 14 44 43
  2. I've changed AppDelegate.swift: ` //receive local notification when app in foreground func application(_ application: UIApplication, didReceive notification: UILocalNotification) { //show an alert window let storageController = UIAlertController(title: "Alarm", message: nil, preferredStyle: .alert) var isSnooze: Bool = false var soundName: String = "" var index: Int = -1 if let userInfo = notification.userInfo { isSnooze = userInfo["snooze"] as! Bool soundName = userInfo["soundName"] as! String index = userInfo["index"] as! Int }

    playSound(soundName)
    //schedule notification for snooze
    if isSnooze {
        alarmScheduler.setNotificationForSnooze(snoozeMinute: 9, soundName: soundName, index: index)
        let snoozeOption = UIAlertAction(title: "Snooze", style: .default) {
            (action:UIAlertAction)->Void in self.audioPlayer?.stop()
        }
        storageController.addAction(snoozeOption)
    }
    let stopOption = UIAlertAction(title: "OK", style: .default) {
        (action:UIAlertAction)->Void in self.audioPlayer?.stop()
        //change UI
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mainVC = storyboard.instantiateViewController(withIdentifier: "Alarm") as! MainAlarmViewController
        if mainVC.alarmModel.alarms[index].repeatWeekdays.isEmpty {
            mainVC.alarmModel.alarms[index].enabled = false
        }
        let cells = mainVC.tableView.visibleCells
        for cell in cells {
            if cell.tag == index {
                let sw = cell.accessoryView as! UISwitch
                if mainVC.alarmModel.alarms[index].repeatWeekdays.isEmpty {
                    sw.setOn(false, animated: false)
                    cell.backgroundColor = UIColor.groupTableViewBackground
                    cell.textLabel?.alpha = 0.5
                    cell.detailTextLabel?.alpha = 0.5
                }
            }
        }
    }
    
    storageController.addAction(stopOption)
    window?.visibleViewController?.navigationController?.present(storageController, animated: true, completion: nil)

    }`

That's it. Alarm-ios-swift-swift-3.0.1 2.zip

https://drive.google.com/open?id=0B9-ghTf1M_xCR3BOcDVMU2xFNjQ

natsu1211 commented 7 years ago

Thanks again for the reporting. you are right that if we are not at MainAlarmView when the notification fired, the code will not working.

If you wish, pull request is welcomed!

Infinite7Soul commented 7 years ago

As you see I've resolved the problem :)

Infinite7Soul commented 7 years ago

Looks like I've resolved the problem badly. If I stay in MainAlarmVC and I get a notification it will not be working properly. In video you will see that I'm making notification without repeating and I'm getting a notification but UISwitch doesn't change. Sorry

https://drive.google.com/open?id=0B9-ghTf1M_xCMjI1X0xqM3FPWTg