added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / Settings / SettingsViewController.swift
1 //
2 //  SettingsViewController.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 17/09/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10 import UserNotifications
11 class SettingsViewController: MainViewController {
12
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!
20
21     override func viewDidLoad() {
22         super.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()
32         
33         let userIsPremium = DatabaseManager.shared.isUserPremium()
34         subscriptionStatusLabel.text = userIsPremium ? "active".localized.uppercased() : "inactive".localized.uppercased()
35         
36         var becomeFriendButtonHidden = DatabaseManager.shared.isUserPremium()
37         if Constants.donateEnabled == false {
38             becomeFriendButtonHidden = true
39         }
40         
41         becomeFriendButton.isHidden = becomeFriendButtonHidden
42         becomeFriendButton.layer.cornerRadius = 18
43     }
44     
45     override func viewWillAppear(_ animated: Bool) {
46         super.viewWillAppear(animated)
47         updateNotificationsSwitchValue()
48     }
49
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 {
56                     case .notDetermined:
57                         self?.notificationsSwitch.setOn(false, animated: true)
58                     // Authorization request has not been made yet
59                     case .denied:
60                         self?.notificationsSwitch.setOn(false, animated: true)
61                         // User has denied authorization.
62                     // You could tell them to change this in Settings
63                     case .authorized:
64                         self?.notificationsSwitch.setOn(true, animated: true)
65                     }
66                 }
67
68             })
69         } else {
70             // Fallback on earlier versions
71             if UIApplication.shared.isRegisteredForRemoteNotifications {
72                 notificationsSwitch.setOn(true, animated: true)
73             } else {
74                 notificationsSwitch.setOn(false, animated: true)
75             }
76         }
77     }
78     
79     @IBAction func deleteFilesButtonAction() {
80         
81         let alertController = UIAlertController(
82             title: nil,
83             message: "delete_files_question".localized,
84             preferredStyle: UIAlertControllerStyle.alert
85         )
86         
87         let yesAction = UIAlertAction(title: "yes".localized, style: UIAlertActionStyle.default) { [weak self]
88             (result : UIAlertAction) -> Void in
89             self?.deleteAllFiles()
90         }
91         
92         let noAction = UIAlertAction(title: "no".localized, style: UIAlertActionStyle.cancel) { [weak self]
93             (result : UIAlertAction) -> Void in
94         }
95         alertController.addAction(yesAction)
96         alertController.addAction(noAction)
97
98         self.present(alertController, animated: true, completion: nil)
99     }
100     
101     private func deleteAllFiles() {
102         
103         PlayerController.shared.stopAndClear()
104         DatabaseManager.shared.removeAllBooksFromDownloaded()
105         presentToast(message: "all_files_removed".localized)
106     }
107     
108     @IBAction func switchAction() {
109         UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
110     }
111     
112     @IBAction func becomeFriendButtonAction() {
113         appDelegate.mainNavigator.presentPayPalForm()
114     }
115 }