-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AllTimeViewController.swift
490 lines (375 loc) · 16.6 KB
/
AllTimeViewController.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import UIKit
import NotificationCenter
import WordPressKit
import WordPressUI
import Reachability
class AllTimeViewController: UIViewController {
// MARK: - Properties
@IBOutlet private var tableView: UITableView!
private var statsValues: AllTimeWidgetStats? {
didSet {
updateStatsLabels()
tableView.reloadData()
}
}
private var visitorCount: String = Constants.noDataLabel
private var viewCount: String = Constants.noDataLabel
private var postCount: String = Constants.noDataLabel
private var bestCount: String = Constants.noDataLabel
private var siteUrl: String = Constants.noDataLabel
private var haveSiteUrl: Bool {
siteUrl != Constants.noDataLabel
}
private var siteID: NSNumber?
private var timeZone: TimeZone?
private var oauthToken: String?
private var isConfigured = false {
didSet {
setAvailableDisplayMode()
}
}
private var isReachable = true {
didSet {
setAvailableDisplayMode()
if isReachable != oldValue,
let completionHandler = widgetCompletionBlock {
widgetPerformUpdate(completionHandler: completionHandler)
}
}
}
private var showNoConnection: Bool {
return !isReachable && statsValues == nil
}
private var loadingFailed = false {
didSet {
setAvailableDisplayMode()
if loadingFailed != oldValue {
tableView.reloadData()
}
}
}
private var failedState: Bool {
return !isConfigured || showNoConnection || loadingFailed
}
private let tracks = Tracks(appGroupName: WPAppGroupName)
private let reachability: Reachability = .forInternetConnection()
private typealias WidgetCompletionBlock = (NCUpdateResult) -> Void
private var widgetCompletionBlock: WidgetCompletionBlock?
// MARK: - View
override func viewDidLoad() {
super.viewDidLoad()
retrieveSiteConfiguration()
registerTableCells()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadSavedData()
setupReachability()
resizeView()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
reachability.stopNotifier()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
let updatedRowCount = numberOfRowsToDisplay()
// If the number of rows has not changed, do nothing.
guard updatedRowCount != tableView.numberOfRows(inSection: 0) else {
return
}
coordinator.animate(alongsideTransition: { _ in
self.tableView.performBatchUpdates({
var indexPathsToInsert = [IndexPath]()
var indexPathsToDelete = [IndexPath]()
// If no connection was displayed, then rows are just being added.
// Otherwise, a data row is being inserted/deleted.
if self.tableView.visibleCells.first is WidgetNoConnectionCell {
let indexRange = (1..<updatedRowCount)
let indexPaths = indexRange.map({ return IndexPath(row: $0, section: 0) })
indexPathsToInsert.append(contentsOf: indexPaths)
} else {
let lastDataRowIndexPath = IndexPath(row: 1, section: 0)
indexPathsToInsert.append(lastDataRowIndexPath)
indexPathsToDelete.append(lastDataRowIndexPath)
}
updatedRowCount > self.minRowsToDisplay() ?
self.tableView.insertRows(at: indexPathsToInsert, with: .fade) :
self.tableView.deleteRows(at: indexPathsToDelete, with: .fade)
})
})
}
}
// MARK: - Widget Updating
extension AllTimeViewController: NCWidgetProviding {
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
widgetCompletionBlock = completionHandler
retrieveSiteConfiguration()
isReachable = reachability.isReachable()
if !isConfigured || !isReachable {
DDLogError("All Time Widget: unable to update. Configured: \(isConfigured) Reachable: \(isReachable)")
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
}
completionHandler(.failed)
return
}
fetchData(completionHandler: completionHandler)
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
tracks.trackDisplayModeChanged(properties: ["expanded": activeDisplayMode == .expanded])
resizeView(withMaximumSize: maxSize)
}
}
// MARK: - Table View Methods
extension AllTimeViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfRowsToDisplay()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if showNoConnection {
return noConnectionCellFor(indexPath: indexPath)
}
if !isConfigured || loadingFailed {
return unconfiguredCellFor(indexPath: indexPath)
}
return statCellFor(indexPath: indexPath)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if failedState,
let maxCompactSize = extensionContext?.widgetMaximumSize(for: .compact) {
// Use the max compact height for unconfigured view.
return maxCompactSize.height
}
if showUrl() && indexPath.row == numberOfRowsToDisplay() - 1 {
return WidgetUrlCell.height
}
return UITableView.automaticDimension
}
}
// MARK: - Private Extension
private extension AllTimeViewController {
// MARK: - Tap Gesture Handling
@IBAction func handleTapGesture() {
// If showing the loading failed view, reload the widget.
if loadingFailed,
let completionHandler = widgetCompletionBlock {
widgetPerformUpdate(completionHandler: completionHandler)
return
}
// Otherwise, open the app.
guard isReachable,
let extensionContext = extensionContext,
let containingAppURL = appURL() else {
DDLogError("All Time Widget: Unable to get extensionContext or appURL.")
return
}
trackAppLaunch()
extensionContext.open(containingAppURL, completionHandler: nil)
}
func appURL() -> URL? {
let urlString = (siteID != nil) ? (Constants.statsUrl + siteID!.stringValue) : Constants.baseUrl
return URL(string: urlString)
}
func trackAppLaunch() {
guard let siteID = siteID else {
tracks.trackExtensionConfigureLaunched()
return
}
tracks.trackExtensionStatsLaunched(siteID.intValue)
}
// MARK: - Site Configuration
func retrieveSiteConfiguration() {
guard let sharedDefaults = UserDefaults(suiteName: WPAppGroupName) else {
DDLogError("All Time Widget: Unable to get sharedDefaults.")
isConfigured = false
return
}
siteID = sharedDefaults.object(forKey: WPStatsTodayWidgetUserDefaultsSiteIdKey) as? NSNumber
siteUrl = sharedDefaults.string(forKey: WPStatsTodayWidgetUserDefaultsSiteUrlKey) ?? Constants.noDataLabel
oauthToken = fetchOAuthBearerToken()
if let timeZoneName = sharedDefaults.string(forKey: WPStatsTodayWidgetUserDefaultsSiteTimeZoneKey) {
timeZone = TimeZone(identifier: timeZoneName)
}
isConfigured = siteID != nil && timeZone != nil && oauthToken != nil
}
func fetchOAuthBearerToken() -> String? {
let oauth2Token = try? SFHFKeychainUtils.getPasswordForUsername(WPStatsTodayWidgetKeychainTokenKey, andServiceName: WPStatsTodayWidgetKeychainServiceName, accessGroup: WPAppKeychainAccessGroup)
return oauth2Token as String?
}
// MARK: - Data Management
func loadSavedData() {
statsValues = AllTimeWidgetStats.loadSavedData()
}
func saveData() {
statsValues?.saveData()
}
func fetchData(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
guard let statsRemote = statsRemote() else {
return
}
statsRemote.getInsight { [weak self] (allTimesStats: StatsAllTimesInsight?, error) in
self?.loadingFailed = (error != nil)
if error != nil {
DDLogError("All Time Widget: Error fetching StatsAllTimesInsight: \(String(describing: error?.localizedDescription))")
completionHandler(.failed)
return
}
DDLogDebug("All Time Widget: Fetched StatsAllTimesInsight data.")
DispatchQueue.main.async { [weak self] in
let updatedStats = AllTimeWidgetStats(views: allTimesStats?.viewsCount,
visitors: allTimesStats?.visitorsCount,
posts: allTimesStats?.postsCount,
bestViews: allTimesStats?.bestViewsPerDayCount)
// Update the widget only if the data has changed.
guard updatedStats != self?.statsValues else {
completionHandler(.noData)
return
}
self?.statsValues = updatedStats
completionHandler(.newData)
self?.saveData()
}
}
}
func statsRemote() -> StatsServiceRemoteV2? {
guard
let siteID = siteID,
let timeZone = timeZone,
let oauthToken = oauthToken
else {
DDLogError("All Time Widget: Missing site ID, timeZone or oauth2Token")
return nil
}
let wpApi = WordPressComRestApi(oAuthToken: oauthToken)
return StatsServiceRemoteV2(wordPressComRestApi: wpApi, siteID: siteID.intValue, siteTimezone: timeZone)
}
// MARK: - Table Helpers
func registerTableCells() {
let twoColumnCellNib = UINib(nibName: String(describing: WidgetTwoColumnCell.self), bundle: Bundle(for: WidgetTwoColumnCell.self))
tableView.register(twoColumnCellNib, forCellReuseIdentifier: WidgetTwoColumnCell.reuseIdentifier)
let unconfiguredCellNib = UINib(nibName: String(describing: WidgetUnconfiguredCell.self), bundle: Bundle(for: WidgetUnconfiguredCell.self))
tableView.register(unconfiguredCellNib, forCellReuseIdentifier: WidgetUnconfiguredCell.reuseIdentifier)
let urlCellNib = UINib(nibName: String(describing: WidgetUrlCell.self), bundle: Bundle(for: WidgetUrlCell.self))
tableView.register(urlCellNib, forCellReuseIdentifier: WidgetUrlCell.reuseIdentifier)
let noConnectionCellNib = UINib(nibName: String(describing: WidgetNoConnectionCell.self), bundle: Bundle(for: WidgetNoConnectionCell.self))
tableView.register(noConnectionCellNib, forCellReuseIdentifier: WidgetNoConnectionCell.reuseIdentifier)
}
func noConnectionCellFor(indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: WidgetNoConnectionCell.reuseIdentifier, for: indexPath) as? WidgetNoConnectionCell else {
return UITableViewCell()
}
return cell
}
func unconfiguredCellFor(indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: WidgetUnconfiguredCell.reuseIdentifier, for: indexPath) as? WidgetUnconfiguredCell else {
return UITableViewCell()
}
loadingFailed ? cell.configure(for: .loadingFailed) : cell.configure(for: .allTime)
return cell
}
func statCellFor(indexPath: IndexPath) -> UITableViewCell {
// URL Cell
if showUrl() && indexPath.row == numberOfRowsToDisplay() - 1 {
guard let urlCell = tableView.dequeueReusableCell(withIdentifier: WidgetUrlCell.reuseIdentifier, for: indexPath) as? WidgetUrlCell else {
return UITableViewCell()
}
urlCell.configure(siteUrl: siteUrl)
return urlCell
}
// Data Cells
guard let cell = tableView.dequeueReusableCell(withIdentifier: WidgetTwoColumnCell.reuseIdentifier, for: indexPath) as? WidgetTwoColumnCell else {
return UITableViewCell()
}
if indexPath.row == 0 {
cell.configure(leftItemName: LocalizedText.views,
leftItemData: viewCount,
rightItemName: LocalizedText.visitors,
rightItemData: visitorCount)
} else {
cell.configure(leftItemName: LocalizedText.posts,
leftItemData: postCount,
rightItemName: LocalizedText.bestViews,
rightItemData: bestCount)
}
return cell
}
func showUrl() -> Bool {
return (isConfigured && haveSiteUrl)
}
// MARK: - Expand / Compact View Helpers
func setAvailableDisplayMode() {
// If something went wrong, don't allow the widget to be expanded.
extensionContext?.widgetLargestAvailableDisplayMode = failedState ? .compact : .expanded
}
func minRowsToDisplay() -> Int {
return showUrl() ? 2 : 1
}
func maxRowsToDisplay() -> Int {
return showUrl() ? 3 : 2
}
func numberOfRowsToDisplay() -> Int {
return extensionContext?.widgetActiveDisplayMode == .compact ? minRowsToDisplay() : maxRowsToDisplay()
}
func resizeView(withMaximumSize size: CGSize? = nil) {
guard let maxSize = size ?? extensionContext?.widgetMaximumSize(for: .compact) else {
return
}
let expanded = extensionContext?.widgetActiveDisplayMode == .expanded
preferredContentSize = expanded ? CGSize(width: maxSize.width, height: expandedHeight()) : maxSize
}
func expandedHeight() -> CGFloat {
var height: CGFloat = 0
let dataRowHeight: CGFloat
// This method is called before the rows are updated.
// So if a no connection cell was displayed, use the default height for data rows.
// Otherwise, use the actual height from the first data row.
if tableView.visibleCells.first is WidgetNoConnectionCell {
dataRowHeight = WidgetTwoColumnCell.defaultHeight
} else {
dataRowHeight = tableView.rectForRow(at: IndexPath(row: 0, section: 0)).height
}
let numRows = numberOfRowsToDisplay()
if showUrl() {
height += WidgetUrlCell.height
height += (dataRowHeight * CGFloat(numRows - 1))
} else {
height += (dataRowHeight * CGFloat(numRows))
}
return height
}
// MARK: - Reachability
func setupReachability() {
isReachable = reachability.isReachable()
NotificationCenter.default.addObserver(self,
selector: #selector(reachabilityChanged),
name: NSNotification.Name.reachabilityChanged,
object: nil)
reachability.startNotifier()
}
@objc func reachabilityChanged() {
isReachable = reachability.isReachable()
}
// MARK: - Helpers
func updateStatsLabels() {
guard let values = statsValues else {
return
}
viewCount = values.views.abbreviatedString()
visitorCount = values.visitors.abbreviatedString()
postCount = values.posts.abbreviatedString()
bestCount = values.bestViews.abbreviatedString()
}
// MARK: - Constants
enum LocalizedText {
static let visitors = NSLocalizedString("Visitors", comment: "Stats Visitors Label")
static let views = NSLocalizedString("Views", comment: "Stats Views Label")
static let posts = NSLocalizedString("Posts", comment: "Stats Posts Label")
static let bestViews = NSLocalizedString("Best views ever", comment: "Stats 'Best views ever' Label")
}
enum Constants {
static let noDataLabel = "-"
static let baseUrl: String = "\(WPComScheme)://"
static let statsUrl: String = Constants.baseUrl + "viewstats?siteId="
}
}