2 // SettingsViewController.swift
5 // Created by Pawel Dabrowski on 17/09/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
10 import UserNotifications
11 class SettingsViewController: MainViewController {
13 @IBOutlet weak var notificationsLabel: UILabel!
14 @IBOutlet weak var notificationsSwitch: UISwitch!
15 @IBOutlet weak var subscriptionLabel: UILabel!
16 @IBOutlet weak var subscriptionStatusLabel: UILabel!
17 @IBOutlet weak var deleteFilesLabel: UILabel!
18 @IBOutlet weak var deleteFilesButton: UIButton!
19 @IBOutlet weak var becomeFriendButton: UIButton!
21 override func viewDidLoad() {
23 title = "nav_settings".localized
24 notificationsLabel.text = "settings_notifications".localized.uppercased()
25 subscriptionLabel.text = "subscribtion_state".localized.uppercased()
26 deleteFilesLabel.text = "delete_files".localized.uppercased()
27 deleteFilesButton.text = "delete".localized.uppercased()
28 deleteFilesButton.layer.cornerRadius = 15
29 deleteFilesButton.layer.borderColor = UIColor.white.cgColor
30 deleteFilesButton.layer.borderWidth = 1.0
31 deleteFilesButton.backgroundColor = Constants.Colors.navbarBgColor()
33 let userIsPremium = DatabaseManager.shared.isUserPremium()
34 subscriptionStatusLabel.text = userIsPremium ? "active".localized.uppercased() : "inactive".localized.uppercased()
36 var becomeFriendButtonHidden = DatabaseManager.shared.isUserPremium()
37 if Constants.donateEnabled == false {
38 becomeFriendButtonHidden = true
41 becomeFriendButton.isHidden = becomeFriendButtonHidden
42 becomeFriendButton.layer.cornerRadius = 18
45 override func viewWillAppear(_ animated: Bool) {
46 super.viewWillAppear(animated)
47 updateNotificationsSwitchValue()
50 func updateNotificationsSwitchValue() {
51 if #available(iOS 10.0, *) {
52 let current = UNUserNotificationCenter.current()
53 current.getNotificationSettings(completionHandler: { [weak self] settings in
54 DispatchQueue.main.async {
55 switch settings.authorizationStatus {
57 self?.notificationsSwitch.setOn(false, animated: true)
58 // Authorization request has not been made yet
60 self?.notificationsSwitch.setOn(false, animated: true)
61 // User has denied authorization.
62 // You could tell them to change this in Settings
64 self?.notificationsSwitch.setOn(true, animated: true)
70 // Fallback on earlier versions
71 if UIApplication.shared.isRegisteredForRemoteNotifications {
72 notificationsSwitch.setOn(true, animated: true)
74 notificationsSwitch.setOn(false, animated: true)
79 @IBAction func deleteFilesButtonAction() {
81 let alertController = UIAlertController(
83 message: "delete_files_question".localized,
84 preferredStyle: UIAlertControllerStyle.alert
87 let yesAction = UIAlertAction(title: "yes".localized, style: UIAlertActionStyle.default) { [weak self]
88 (result : UIAlertAction) -> Void in
89 self?.deleteAllFiles()
92 let noAction = UIAlertAction(title: "no".localized, style: UIAlertActionStyle.cancel) { [weak self]
93 (result : UIAlertAction) -> Void in
95 alertController.addAction(yesAction)
96 alertController.addAction(noAction)
98 self.present(alertController, animated: true, completion: nil)
101 private func deleteAllFiles() {
103 PlayerController.shared.stopAndClear()
104 DatabaseManager.shared.removeAllBooksFromDownloaded()
105 presentToast(message: "all_files_removed".localized)
108 @IBAction func switchAction() {
109 UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
112 @IBAction func becomeFriendButtonAction() {
113 appDelegate.mainNavigator.presentPayPalForm()