-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathReleaseNotesHostViewController.swift
219 lines (176 loc) · 8.59 KB
/
ReleaseNotesHostViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// ReleaseNotesHostViewController.swift
// ownCloud
//
// Created by Matthias Hühne on 04.12.19.
// Copyright © 2019 ownCloud GmbH. All rights reserved.
//
/*
* Copyright (C) 2019, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/
import UIKit
import ownCloudSDK
import StoreKit
class ReleaseNotesHostViewController: UIViewController {
// MARK: - Constants
private let cornerRadius : CGFloat = 8.0
private let padding : CGFloat = 20.0
private let smallPadding : CGFloat = 10.0
private let buttonHeight : CGFloat = 44.0
private let headerHeight : CGFloat = 60.0
// MARK: - Instance Variables
var titleLabel = UILabel()
var proceedButton = ThemeButton()
var footerButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
Theme.shared.register(client: self)
ReleaseNotesDatasource.setUserPreferenceValue(NSString(utf8String: VendorServices.shared.appVersion), forClassSettingsKey: .lastSeenReleaseNotesVersion)
let headerView = UIView()
headerView.backgroundColor = .clear
headerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(headerView)
NSLayoutConstraint.activate([
headerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
headerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
headerView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
headerView.heightAnchor.constraint(equalToConstant: headerHeight)
])
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.setContentHuggingPriority(UILayoutPriority.defaultLow, for: NSLayoutConstraint.Axis.horizontal)
titleLabel.text = "New in ownCloud".localized
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 0
titleLabel.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.headline)
titleLabel.adjustsFontForContentSizeCategory = true
headerView.addSubview(titleLabel)
NSLayoutConstraint.activate([
titleLabel.leftAnchor.constraint(greaterThanOrEqualTo: headerView.safeAreaLayoutGuide.leftAnchor, constant: padding),
titleLabel.rightAnchor.constraint(lessThanOrEqualTo: headerView.safeAreaLayoutGuide.rightAnchor, constant: padding * -1),
titleLabel.centerXAnchor.constraint(equalTo: headerView.safeAreaLayoutGuide.centerXAnchor),
titleLabel.topAnchor.constraint(equalTo: headerView.safeAreaLayoutGuide.topAnchor, constant: padding)
])
let releaseNotesController = ReleaseNotesTableViewController(style: .plain)
if let containerView = releaseNotesController.view {
containerView.backgroundColor = .clear
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
let bottomView = UIView()
bottomView.backgroundColor = .clear
bottomView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bottomView)
NSLayoutConstraint.activate([
bottomView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
bottomView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
bottomView.topAnchor.constraint(equalTo: containerView.bottomAnchor),
bottomView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
proceedButton.setTitle("Proceed".localized, for: .normal)
proceedButton.translatesAutoresizingMaskIntoConstraints = false
proceedButton.addTarget(self, action: #selector(dismissView), for: .touchUpInside)
bottomView.addSubview(proceedButton)
footerButton.setTitle("Thank you for using ownCloud.\nIf you like our App, please leave an AppStore review.\n❤️".localized, for: .normal)
footerButton.titleLabel?.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.footnote)
footerButton.titleLabel?.adjustsFontForContentSizeCategory = true
footerButton.titleLabel?.numberOfLines = 0
footerButton.titleLabel?.textAlignment = .center
footerButton.translatesAutoresizingMaskIntoConstraints = false
footerButton.addTarget(self, action: #selector(rateApp), for: .touchUpInside)
bottomView.addSubview(footerButton)
NSLayoutConstraint.activate([
footerButton.leadingAnchor.constraint(equalTo: bottomView.leadingAnchor, constant: padding),
footerButton.trailingAnchor.constraint(equalTo: bottomView.trailingAnchor, constant: padding * -1),
footerButton.topAnchor.constraint(equalTo: bottomView.topAnchor, constant: smallPadding),
footerButton.bottomAnchor.constraint(equalTo: proceedButton.topAnchor, constant: padding * -1)
])
NSLayoutConstraint.activate([
proceedButton.leadingAnchor.constraint(equalTo: bottomView.leadingAnchor, constant: padding),
proceedButton.trailingAnchor.constraint(equalTo: bottomView.trailingAnchor, constant: padding * -1),
proceedButton.heightAnchor.constraint(equalToConstant: buttonHeight),
proceedButton.bottomAnchor.constraint(equalTo: bottomView.bottomAnchor, constant: smallPadding * -1)
])
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
containerView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
containerView.bottomAnchor.constraint(equalTo: bottomView.topAnchor)
])
}
}
deinit {
Theme.shared.unregister(client: self)
}
@objc func dismissView() {
self.dismiss(animated: true, completion: nil)
}
@objc func rateApp() {
SKStoreReviewController.requestReview()
}
}
// MARK: - Themeable implementation
extension ReleaseNotesHostViewController : Themeable {
func applyThemeCollection(theme: Theme, collection: ThemeCollection, event: ThemeEvent) {
self.view.backgroundColor = collection.tableBackgroundColor
titleLabel.applyThemeCollection(collection, itemStyle: .logo)
proceedButton.backgroundColor = collection.neutralColors.normal.background
proceedButton.setTitleColor(collection.neutralColors.normal.foreground, for: .normal)
footerButton.titleLabel?.textColor = collection.tableRowColors.labelColor
}
}
class ReleaseNotesDatasource : NSObject, OCClassSettingsUserPreferencesSupport {
var shouldShowReleaseNotes: Bool {
if let lastSeenReleaseNotesVersion = self.classSetting(forOCClassSettingsKey: .lastSeenReleaseNotesVersion) as? String {
if lastSeenReleaseNotesVersion.compare(VendorServices.shared.appVersion, options: .numeric) == .orderedDescending || lastSeenReleaseNotesVersion.compare(VendorServices.shared.appVersion, options: .numeric) == .orderedSame {
return false
}
if let path = Bundle.main.path(forResource: "ReleaseNotes", ofType: "plist"), let releaseNotesValues = NSDictionary(contentsOfFile: path), let versionsValues = releaseNotesValues["Versions"] as? NSArray {
let relevantReleaseNotes = versionsValues.filter {
if let version = ($0 as AnyObject)["Version"] as? String, version.compare(VendorServices.shared.appVersion, options: .numeric) == .orderedDescending {
return false
}
return true
}
if relevantReleaseNotes.count > 0 {
return true
}
}
return false
} else if self.classSetting(forOCClassSettingsKey: .lastSeenAppVersion) != nil, self.classSetting(forOCClassSettingsKey: .lastSeenAppVersion) as? String != VendorServices.shared.appVersion {
return true
}
return false
}
func releaseNotes(for version: String) -> [[String:Any]]? {
if let path = Bundle.main.path(forResource: "ReleaseNotes", ofType: "plist") {
if let releaseNotesValues = NSDictionary(contentsOfFile: path), let versionsValues = releaseNotesValues["Versions"] as? NSArray {
let relevantReleaseNotes = versionsValues.filter {
if let version = ($0 as AnyObject)["Version"] as? String, version.compare(VendorServices.shared.appVersion, options: .numeric) == .orderedAscending {
return false
}
return true
}
return relevantReleaseNotes as? [[String:Any]]
}
}
return nil
}
}
extension OCClassSettingsKey {
static let lastSeenReleaseNotesVersion = OCClassSettingsKey("lastSeenReleaseNotesVersion")
static let lastSeenAppVersion = OCClassSettingsKey("lastSeenAppVersion")
}
extension ReleaseNotesDatasource : OCClassSettingsSupport {
static let classSettingsIdentifier : OCClassSettingsIdentifier = .app
static func defaultSettings(forIdentifier identifier: OCClassSettingsIdentifier) -> [OCClassSettingsKey : Any]? {
if identifier == .app {
return nil
}
return nil
}
}