-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BaseActivityListViewController.swift
581 lines (440 loc) · 21.1 KB
/
BaseActivityListViewController.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import Foundation
import CocoaLumberjack
import SVProgressHUD
import WordPressShared
import WordPressFlux
struct ActivityListConfiguration {
/// An identifier of the View Controller
let identifier: String
/// The title of the View Controller
let title: String
/// The title for when loading activities
let loadingTitle: String
/// Title for when there are no activities
let noActivitiesTitle: String
/// Subtitle for when there are no activities
let noActivitiesSubtitle: String
/// Title for when there are no activities for the selected filter
let noMatchingTitle: String
/// Subtitle for when there are no activities for the selected filter
let noMatchingSubtitle: String
/// Event to be fired when the date range button is tapped
let filterbarRangeButtonTapped: WPAnalyticsEvent
/// Event to be fired when a date range is selected
let filterbarSelectRange: WPAnalyticsEvent
/// Event to be fired when the range date reset button is tapped
let filterbarResetRange: WPAnalyticsEvent
/// The number of items to be requested for each page
let numberOfItemsPerPage: Int
}
/// ActivityListViewController is used as a base ViewController for
/// Jetpack's Activity Log and Backup
///
class BaseActivityListViewController: UIViewController, TableViewContainer, ImmuTablePresenter {
let site: JetpackSiteRef
let store: ActivityStore
let configuration: ActivityListConfiguration
let isFreeWPCom: Bool
var changeReceipt: Receipt?
var isUserTriggeredRefresh: Bool = false
let containerStackView = UIStackView()
let filterView = FilterBarView()
let dateFilterChip = FilterChipButton()
let activityTypeFilterChip = FilterChipButton()
var tableView: UITableView = UITableView()
let refreshControl = UIRefreshControl()
let numberOfItemsPerPage = 100
fileprivate lazy var handler: ImmuTableViewHandler = {
return ImmuTableViewHandler(takeOver: self, with: self)
}()
private lazy var spinner: UIActivityIndicatorView = {
let spinner = UIActivityIndicatorView(style: .medium)
spinner.startAnimating()
spinner.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 44)
return spinner
}()
var viewModel: ActivityListViewModel
private enum Constants {
static let estimatedRowHeight: CGFloat = 62
}
// MARK: - GUI
fileprivate var noResultsViewController: NoResultsViewController?
// MARK: - Constructors
init(site: JetpackSiteRef,
store: ActivityStore,
isFreeWPCom: Bool = false) {
fatalError("A configuration struct needs to be provided")
}
init(site: JetpackSiteRef,
store: ActivityStore,
configuration: ActivityListConfiguration,
isFreeWPCom: Bool = false) {
self.site = site
self.store = store
self.isFreeWPCom = isFreeWPCom
self.configuration = configuration
self.viewModel = ActivityListViewModel(site: site, store: store, configuration: configuration)
super.init(nibName: nil, bundle: nil)
self.changeReceipt = viewModel.onChange { [weak self] in
self?.refreshModel()
}
view.addSubview(containerStackView)
containerStackView.axis = .vertical
if site.shouldShowActivityLogFilter() {
setupFilterBar()
}
containerStackView.addArrangedSubview(tableView)
containerStackView.translatesAutoresizingMaskIntoConstraints = false
view.pinSubviewToSafeArea(containerStackView)
tableView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(userRefresh), for: .valueChanged)
title = configuration.title
}
@objc private func showCalendar() {
let calendarViewController = CalendarViewController(startDate: viewModel.after, endDate: viewModel.before)
calendarViewController.delegate = self
let navigationController = UINavigationController(rootViewController: calendarViewController)
present(navigationController, animated: true, completion: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc convenience init?(blog: Blog) {
precondition(blog.dotComID != nil)
guard let siteRef = JetpackSiteRef(blog: blog) else {
return nil
}
let isFreeWPCom = blog.isHostedAtWPcom && !blog.hasPaidPlan
self.init(site: siteRef, store: StoreContainer.shared.activity, isFreeWPCom: isFreeWPCom)
}
// MARK: - View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
refreshModel()
tableView.estimatedRowHeight = Constants.estimatedRowHeight
WPStyleGuide.configureColors(view: view, tableView: tableView)
let nib = UINib(nibName: ActivityListSectionHeaderView.identifier, bundle: nil)
tableView.register(nib, forHeaderFooterViewReuseIdentifier: ActivityListSectionHeaderView.identifier)
ImmuTable.registerRows([ActivityListRow.self, RewindStatusRow.self], tableView: tableView)
tableView.tableFooterView = spinner
tableView.tableFooterView?.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
SVProgressHUD.dismiss()
}
@objc func userRefresh() {
isUserTriggeredRefresh = true
viewModel.refresh(after: viewModel.after, before: viewModel.before, group: viewModel.selectedGroups)
}
func refreshModel() {
updateHeader()
handler.viewModel = viewModel.tableViewModel(presenter: self)
updateRefreshControl()
updateNoResults()
updateFilters()
}
private func updateHeader() {
tableView.tableHeaderView = viewModel.backupDownloadHeader()
guard let tableHeaderView = tableView.tableHeaderView else {
return
}
tableHeaderView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableHeaderView.topAnchor.constraint(equalTo: tableView.topAnchor),
tableHeaderView.safeLeadingAnchor.constraint(equalTo: tableView.safeLeadingAnchor),
tableHeaderView.safeTrailingAnchor.constraint(equalTo: tableView.safeTrailingAnchor)
])
tableView.tableHeaderView?.layoutIfNeeded()
}
private func updateRefreshControl() {
switch (viewModel.refreshing, refreshControl.isRefreshing) {
case (true, false):
if isUserTriggeredRefresh {
refreshControl.beginRefreshing()
isUserTriggeredRefresh = false
} else if tableView.numberOfSections > 0 {
tableView.tableFooterView?.isHidden = false
}
case (false, true):
refreshControl.endRefreshing()
default:
tableView.tableFooterView?.isHidden = true
break
}
}
private func updateFilters() {
viewModel.dateFilterIsActive ? dateFilterChip.enableResetButton() : dateFilterChip.disableResetButton()
dateFilterChip.title = viewModel.dateRangeDescription()
viewModel.groupFilterIsActive ? activityTypeFilterChip.enableResetButton() : activityTypeFilterChip.disableResetButton()
activityTypeFilterChip.title = viewModel.activityTypeDescription()
}
private func setupFilterBar() {
containerStackView.addArrangedSubview(filterView)
filterView.add(button: dateFilterChip)
filterView.add(button: activityTypeFilterChip)
setupDateFilter()
setupActivityTypeFilter()
}
private func setupDateFilter() {
dateFilterChip.resetButton.accessibilityLabel = NSLocalizedString("Reset Date Range filter", comment: "Accessibility label for the reset date range button")
dateFilterChip.tapped = { [unowned self] in
WPAnalytics.track(self.configuration.filterbarRangeButtonTapped)
self.showCalendar()
}
dateFilterChip.resetTapped = { [unowned self] in
WPAnalytics.track(self.configuration.filterbarResetRange)
self.viewModel.removeDateFilter()
self.dateFilterChip.disableResetButton()
}
}
private func setupActivityTypeFilter() {
activityTypeFilterChip.resetButton.accessibilityLabel = NSLocalizedString("Reset Activity Type filter", comment: "Accessibility label for the reset activity type button")
activityTypeFilterChip.tapped = { [weak self] in
guard let self = self else {
return
}
WPAnalytics.track(.activitylogFilterbarTypeButtonTapped)
let activityTypeSelectorViewController = ActivityTypeSelectorViewController(
viewModel: self.viewModel
)
activityTypeSelectorViewController.delegate = self
let navigationController = UINavigationController(rootViewController: activityTypeSelectorViewController)
self.present(navigationController, animated: true, completion: nil)
}
activityTypeFilterChip.resetTapped = { [weak self] in
WPAnalytics.track(.activitylogFilterbarResetType)
self?.viewModel.removeGroupFilter()
self?.activityTypeFilterChip.disableResetButton()
}
}
}
extension BaseActivityListViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
handler.tableView(tableView, numberOfRowsInSection: section)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
handler.tableView(tableView, cellForRowAt: indexPath)
}
}
// MARK: - UITableViewDelegate
extension BaseActivityListViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let isLastSection = handler.viewModel.sections.count == section + 1
guard isFreeWPCom, isLastSection, let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: ActivityListSectionHeaderView.identifier) as? ActivityListSectionHeaderView else {
return nil
}
cell.separator.isHidden = true
cell.titleLabel.text = NSLocalizedString("Since you're on a free plan, you'll see limited events in your Activity Log.", comment: "Text displayed as a footer of a table view with Activities when user is on a free plan")
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let isLastSection = handler.viewModel.sections.count == section + 1
guard isFreeWPCom, isLastSection else {
return 0.0
}
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: ActivityListSectionHeaderView.identifier) as? ActivityListSectionHeaderView else {
return nil
}
cell.titleLabel.text = handler.tableView(tableView, titleForHeaderInSection: section)?.localizedUppercase
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return ActivityListSectionHeaderView.height
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
guard let row = handler.viewModel.rowAtIndexPath(indexPath) as? ActivityListRow else {
return false
}
return row.activity.isRewindable
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
let shouldLoadMore = offsetY > contentHeight - (2 * scrollView.frame.size.height) && viewModel.hasMore
if shouldLoadMore {
viewModel.loadMore()
}
}
}
// MARK: - NoResultsViewControllerDelegate
extension BaseActivityListViewController: NoResultsViewControllerDelegate {
func actionButtonPressed() {
let supportVC = SupportTableViewController()
supportVC.showFromTabBar()
}
}
// MARK: - ActivityPresenter
extension BaseActivityListViewController: ActivityPresenter {
func presentDetailsFor(activity: FormattableActivity) {
let detailVC = ActivityDetailViewController.loadFromStoryboard()
detailVC.site = site
detailVC.rewindStatus = store.state.rewindStatus[site]
detailVC.formattableActivity = activity
detailVC.presenter = self
self.navigationController?.pushViewController(detailVC, animated: true)
}
func presentBackupOrRestoreFor(activity: Activity, from sender: UIButton) {
let rewindStatus = store.state.rewindStatus[site]
let title = rewindStatus?.isMultisite() == true ? RewindStatus.Strings.multisiteNotAvailable : nil
let alertController = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
if rewindStatus?.state == .active {
let restoreTitle = NSLocalizedString("Restore", comment: "Title displayed for restore action.")
let restoreOptionsVC = JetpackRestoreOptionsViewController(site: site,
activity: activity,
isAwaitingCredentials: store.isAwaitingCredentials(site: site))
restoreOptionsVC.restoreStatusDelegate = self
restoreOptionsVC.presentedFrom = configuration.identifier
alertController.addDefaultActionWithTitle(restoreTitle, handler: { _ in
self.present(UINavigationController(rootViewController: restoreOptionsVC), animated: true)
})
}
let backupTitle = NSLocalizedString("Download backup", comment: "Title displayed for download backup action.")
let backupOptionsVC = JetpackBackupOptionsViewController(site: site, activity: activity)
backupOptionsVC.backupStatusDelegate = self
backupOptionsVC.presentedFrom = configuration.identifier
alertController.addDefaultActionWithTitle(backupTitle, handler: { _ in
self.present(UINavigationController(rootViewController: backupOptionsVC), animated: true)
})
let cancelTitle = NSLocalizedString("Cancel", comment: "Title for cancel action. Dismisses the action sheet.")
alertController.addCancelActionWithTitle(cancelTitle)
if let presentationController = alertController.popoverPresentationController {
presentationController.permittedArrowDirections = .any
presentationController.sourceView = sender
presentationController.sourceRect = sender.bounds
}
self.present(alertController, animated: true, completion: nil)
}
func presentRestoreFor(activity: Activity, from: String? = nil) {
guard activity.isRewindable, activity.rewindID != nil else {
return
}
let restoreOptionsVC = JetpackRestoreOptionsViewController(site: site,
activity: activity,
isAwaitingCredentials: store.isAwaitingCredentials(site: site))
restoreOptionsVC.restoreStatusDelegate = self
restoreOptionsVC.presentedFrom = from ?? configuration.identifier
let navigationVC = UINavigationController(rootViewController: restoreOptionsVC)
self.present(navigationVC, animated: true)
}
func presentBackupFor(activity: Activity, from: String? = nil) {
let backupOptionsVC = JetpackBackupOptionsViewController(site: site, activity: activity)
backupOptionsVC.backupStatusDelegate = self
backupOptionsVC.presentedFrom = from ?? configuration.identifier
let navigationVC = UINavigationController(rootViewController: backupOptionsVC)
self.present(navigationVC, animated: true)
}
}
// MARK: - Restores handling
extension BaseActivityListViewController {
fileprivate func restoreSiteToRewindID(_ rewindID: String) {
navigationController?.popToViewController(self, animated: true)
store.actionDispatcher.dispatch(ActivityAction.rewind(site: site, rewindID: rewindID))
}
}
// MARK: - NoResults Handling
private extension BaseActivityListViewController {
func updateNoResults() {
if let noResultsViewModel = viewModel.noResultsViewModel() {
showNoResults(noResultsViewModel)
} else {
noResultsViewController?.view.isHidden = true
}
}
func showNoResults(_ viewModel: NoResultsViewController.Model) {
if noResultsViewController == nil {
noResultsViewController = NoResultsViewController.controller()
noResultsViewController?.delegate = self
guard let noResultsViewController = noResultsViewController else {
return
}
if noResultsViewController.view.superview != tableView {
tableView.addSubview(withFadeAnimation: noResultsViewController.view)
}
addChild(noResultsViewController)
noResultsViewController.view.translatesAutoresizingMaskIntoConstraints = false
}
noResultsViewController?.bindViewModel(viewModel)
noResultsViewController?.didMove(toParent: self)
tableView.pinSubviewToSafeArea(noResultsViewController!.view)
noResultsViewController?.view.isHidden = false
}
}
// MARK: - Restore Status Handling
extension BaseActivityListViewController: JetpackRestoreStatusViewControllerDelegate {
func didFinishViewing(_ controller: JetpackRestoreStatusViewController) {
controller.dismiss(animated: true, completion: { [weak self] in
guard let self = self else {
return
}
self.store.fetchRewindStatus(site: self.site)
})
}
}
// MARK: - Restore Status Handling
extension BaseActivityListViewController: JetpackBackupStatusViewControllerDelegate {
func didFinishViewing() {
viewModel.refresh()
}
}
// MARK: - Calendar Handling
extension BaseActivityListViewController: CalendarViewControllerDelegate {
func didCancel(calendar: CalendarViewController) {
calendar.dismiss(animated: true, completion: nil)
}
func didSelect(calendar: CalendarViewController, startDate: Date?, endDate: Date?) {
guard startDate != viewModel.after || endDate != viewModel.before else {
calendar.dismiss(animated: true, completion: nil)
return
}
trackSelectedRange(startDate: startDate, endDate: endDate)
viewModel.refresh(after: startDate, before: endDate, group: viewModel.selectedGroups)
calendar.dismiss(animated: true, completion: nil)
}
private func trackSelectedRange(startDate: Date?, endDate: Date?) {
guard let startDate = startDate else {
if viewModel.after != nil || viewModel.before != nil {
WPAnalytics.track(configuration.filterbarResetRange)
}
return
}
var duration: Int // Number of selected days
var distance: Int // Distance from the startDate to today (in days)
if let endDate = endDate {
duration = Int((endDate.timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate) / Double(24 * 60 * 60)) + 1
} else {
duration = 1
}
distance = Int((Date().timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate) / Double(24 * 60 * 60))
WPAnalytics.track(configuration.filterbarSelectRange, properties: ["duration": duration, "distance": distance])
}
}
// MARK: - Activity type filter handling
extension BaseActivityListViewController: ActivityTypeSelectorDelegate {
func didCancel(selectorViewController: ActivityTypeSelectorViewController) {
selectorViewController.dismiss(animated: true, completion: nil)
}
func didSelect(selectorViewController: ActivityTypeSelectorViewController, groups: [ActivityGroup]) {
guard groups != viewModel.selectedGroups else {
selectorViewController.dismiss(animated: true, completion: nil)
return
}
trackSelectedGroups(groups)
viewModel.refresh(after: viewModel.after, before: viewModel.before, group: groups)
selectorViewController.dismiss(animated: true, completion: nil)
}
private func trackSelectedGroups(_ selectedGroups: [ActivityGroup]) {
if !viewModel.selectedGroups.isEmpty && selectedGroups.isEmpty {
WPAnalytics.track(.activitylogFilterbarResetType)
} else {
let totalActivitiesSelected = selectedGroups.map { $0.count }.reduce(0, +)
var selectTypeProperties: [AnyHashable: Any] = [:]
selectedGroups.forEach { selectTypeProperties["group_\($0.key)"] = true }
selectTypeProperties["num_groups_selected"] = selectedGroups.count
selectTypeProperties["num_total_activities_selected"] = totalActivitiesSelected
WPAnalytics.track(.activitylogFilterbarSelectType, properties: selectTypeProperties)
}
}
}