-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathBlogDetailsTableViewModel.swift
More file actions
1517 lines (1311 loc) · 52.2 KB
/
BlogDetailsTableViewModel.swift
File metadata and controls
1517 lines (1311 loc) · 52.2 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Foundation
import UIKit
import WordPressLegacy
import WordPressShared
import WordPressSharedObjC
import WordPressUI
import Support
private struct Section {
let title: String?
let rows: [Row]
let footerTitle: String?
let category: SectionCategory
init(
title: String? = nil,
rows: [Row],
footerTitle: String? = nil,
category: SectionCategory
) {
self.title = title
self.rows = rows
self.footerTitle = footerTitle
self.category = category
}
}
@objc public final class BlogDetailsTableViewModel: NSObject {
private var blog: Blog
private weak var tableView: UITableView?
private weak var viewController: BlogDetailsViewController?
private var sections: [Section] = []
var restorableSelectedRow: BlogDetailsRowKind? {
didSet {
if let row = restorableSelectedRow,
let section = sections.first(where: { $0.rows.contains { $0.kind == row } }),
[.jetpackBrandingCard, .domainCredit].contains(section.category) {
restorableSelectedRow = nil
}
}
}
var restorableSelectedIndexPath: IndexPath? {
restorableSelectedRow.flatMap(indexPath(for:))
}
var gravatarIcon: UIImage? {
didSet {
if let indexPath = self.indexPath(for: .me) {
tableView?.reloadRows(at: [indexPath], with: .automatic)
}
}
}
var useSiteMenuStyle = false
@objc public init(blog: Blog, viewController: BlogDetailsViewController) {
self.blog = blog
self.viewController = viewController
super.init()
}
@objc public func configure(tableView: UITableView) {
self.tableView = tableView
// Register standard cells
tableView.register(WPTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.standard)
tableView.register(WPTableViewCellValue1.self, forCellReuseIdentifier: CellIdentifiers.plan)
tableView.register(WPTableViewCellValue1.self, forCellReuseIdentifier: CellIdentifiers.settings)
tableView.register(WPTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.removeSite)
// Register header/footer views
tableView.register(BlogDetailsSectionFooterView.self, forHeaderFooterViewReuseIdentifier: CellIdentifiers.sectionFooter)
// Register special card cells
tableView.register(MigrationSuccessCell.self, forCellReuseIdentifier: CellIdentifiers.migrationSuccess)
tableView.register(JetpackBrandingMenuCardCell.self, forCellReuseIdentifier: CellIdentifiers.jetpackBrandingCard)
tableView.register(JetpackRemoteInstallTableViewCell.self, forCellReuseIdentifier: CellIdentifiers.jetpackInstall)
tableView.register(ExtensiveLoggingCell.self, forCellReuseIdentifier: CellIdentifiers.extensiveLogging)
tableView.register(XMLRPCDisabledCell.self, forCellReuseIdentifier: CellIdentifiers.xmlrpcDisabled)
tableView.delegate = self
tableView.dataSource = self
}
@objc public func viewWillAppear() {
if !isSplitViewDisplayed {
restorableSelectedRow = nil
}
}
@objc public func configureTableViewData() {
guard let viewController else { return }
var newSections: [Section] = []
if viewController.shouldShowJetpackInstallCard() {
newSections.append(Section(rows: [], category: .jetpackInstallCard))
}
if viewController.shouldShowTopJetpackBrandingMenuCard {
newSections.append(Section(rows: [], category: .jetpackBrandingCard))
}
if blog.isSelfHosted, ExtensiveLogging.enabled {
newSections.append(Section(rows: [], category: .extensiveLogging))
}
if blog.isSelfHosted, viewController.showXMLRPCDisabled {
newSections.append(Section(rows: [], category: .xmlrpcDisabled))
}
if viewController.isDashboardEnabled() && isSplitViewDisplayed {
newSections.append(buildHomeSection())
}
if AppConfiguration.isWordPress {
if viewController.shouldAddJetpackSection() {
newSections.append(buildJetpackSection())
}
if viewController.shouldAddGeneralSection() {
newSections.append(buildGeneralSection())
}
newSections.append(buildPublishTypeSection())
if viewController.shouldAddPersonalizeSection() {
newSections.append(buildPersonalizeSection())
}
newSections.append(buildConfigurationSection())
newSections.append(buildExternalSection())
} else {
newSections.append(buildContentSection())
if let trafficSection = buildTrafficSection() {
newSections.append(trafficSection)
}
newSections.append(contentsOf: buildMaintenanceSections())
}
if blog.supports(.removable) {
newSections.append(buildRemoveSiteSection())
}
if viewController.shouldShowBottomJetpackBrandingMenuCard {
newSections.append(Section(rows: [], category: .jetpackBrandingCard))
}
sections = newSections
}
private var isSplitViewDisplayed: Bool {
viewController?.isSidebarModeEnabled ?? false
}
func defaultSubsection() -> BlogDetailsRowKind {
if !JetpackFeaturesRemovalCoordinator.jetpackFeaturesEnabled() {
return .posts
}
if let viewController, viewController.isDashboardEnabled() {
return .home
}
return .stats
}
func optimumScrollPosition(for indexPath: IndexPath) -> UITableView.ScrollPosition {
guard let tableView, !isSplitViewDisplayed else { return .none }
let cellRect = tableView.rectForRow(at: indexPath)
return CGRectContainsRect(tableView.bounds, cellRect) ? .none : .middle
}
@objc public func reloadTableViewPreservingSelection() {
guard let tableView else { return }
let previousSelection = tableView.indexPathForSelectedRow
tableView.reloadData()
if isSplitViewDisplayed, let indexPath = restorableSelectedIndexPath {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: optimumScrollPosition(for: indexPath))
if previousSelection != indexPath {
sections[indexPath.section].rows[indexPath.row].action?([:])
}
}
}
@objc public func showInitialDetailsForBlog() {
guard isSplitViewDisplayed else { return }
let row = defaultSubsection()
self.restorableSelectedRow = row
self.showDetailView(for: row)
}
@objc func numberOfSections() -> Int {
sections.count
}
func showDetailViewForMe(userInfo: [String: Any]) -> MeViewController {
guard let viewController else {
wpAssertionFailure("The view controller should not be nil")
return MeViewController()
}
restorableSelectedRow = .me
return viewController.showMe()
}
func showDetailView(for row: BlogDetailsRowKind, userInfo: [String: Any] = [:]) {
for (sectionIndex, section) in sections.enumerated() {
for (rowIndex, rowItem) in section.rows.enumerated() where rowItem.kind == row {
let indexPath = IndexPath(row: rowIndex, section: sectionIndex)
if rowItem.showsSelectionState {
restorableSelectedRow = row
tableView?.selectRow(at: indexPath, animated: false, scrollPosition: optimumScrollPosition(for: indexPath))
}
// Call the row's action
rowItem.action?(userInfo)
return
}
}
}
func indexPath(for row: BlogDetailsRowKind) -> IndexPath? {
for (sectionIndex, section) in sections.enumerated() {
for (rowIndex, rowItem) in section.rows.enumerated() where rowItem.kind == row {
return IndexPath(row: rowIndex, section: sectionIndex)
}
}
return nil
}
}
extension BlogDetailsTableViewModel: UITableViewDataSource {
public func numberOfSections(in tableView: UITableView) -> Int {
sections.count
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard section < sections.count else { return 0 }
switch sections[section].category {
case .jetpackInstallCard, .migrationSuccess, .jetpackBrandingCard, .extensiveLogging, .xmlrpcDisabled:
// The "card" sections do not set the `rows` property. It's hard-coded to show specific types of cards.
wpAssert(sections[section].rows.count == 0)
return 1
default:
return sections[section].rows.count
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.section < sections.count else {
return UITableViewCell()
}
let section = sections[indexPath.section]
let cell: UITableViewCell
switch section.category {
case .jetpackInstallCard:
cell = configureJetpackInstallCell(tableView: tableView)
case .migrationSuccess:
cell = configureMigrationSuccessCell(tableView: tableView)
case .jetpackBrandingCard:
cell = configureJetpackBrandingCell(tableView: tableView)
case .extensiveLogging:
cell = configureExtensiveLoggingCell(tableView: tableView)
case .xmlrpcDisabled:
cell = configureXMLRPCDisabledCell(tableView: tableView)
default:
if indexPath.row < section.rows.count {
let row = section.rows[indexPath.row]
cell = configureStandardCell(tableView: tableView, indexPath: indexPath, row: row)
} else {
cell = UITableViewCell()
}
}
if useSiteMenuStyle {
configureForDisplayingOnSiteMenu(cell)
}
return cell
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard section < sections.count else { return nil }
return sections[section].title
}
private func configureForDisplayingOnSiteMenu(_ cell: UITableViewCell) {
cell.textLabel?.font = .preferredFont(forTextStyle: .body)
cell.backgroundColor = .clear
cell.selectedBackgroundView = {
let backgroundView = UIView()
backgroundView.backgroundColor = .secondarySystemFill
backgroundView.layer.cornerRadius = DesignConstants.radius(.large)
backgroundView.layer.cornerCurve = .continuous
let container = UIView()
container.addSubview(backgroundView)
backgroundView.pinEdges(insets: UIEdgeInsets(.horizontal, 16))
return container
}()
cell.focusStyle = .custom
cell.focusEffect = nil
}
}
extension BlogDetailsTableViewModel: UITableViewDelegate {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard indexPath.section < sections.count else { return }
let section = sections[indexPath.section]
guard indexPath.row < section.rows.count else { return }
let row = section.rows[indexPath.row]
row.action?([:])
if row.showsSelectionState {
restorableSelectedRow = row.kind
} else {
if !isSplitViewDisplayed {
tableView.deselectRow(at: indexPath, animated: true)
} else if let indexPath = restorableSelectedIndexPath {
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}
}
}
public func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
let isNewSelection = (indexPath != tableView.indexPathForSelectedRow)
return isNewSelection ? indexPath : nil
}
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
guard section < sections.count else { return 0 }
let detailSection = sections[section]
let isLastSection = section == sections.count - 1
let hasTitle = !(detailSection.footerTitle?.isEmpty ?? true)
if hasTitle {
return UITableView.automaticDimension
}
if isLastSection {
return 40.0
}
return 0
}
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard section < sections.count else { return 0 }
let detailSection = sections[section]
let hasTitle = !(detailSection.title?.isEmpty ?? true)
if useSiteMenuStyle {
return hasTitle ? 48 : 0
}
return hasTitle ? 40.0 : 20.0
}
public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard useSiteMenuStyle else { return nil }
guard let title = self.tableView(tableView, titleForHeaderInSection: section) else { return nil }
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .headline)
label.text = title
let headerView = UIView()
headerView.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: headerView.leadingAnchor, constant: 20),
label.bottomAnchor.constraint(equalTo: headerView.bottomAnchor, constant: -8),
label.trailingAnchor.constraint(equalTo: headerView.trailingAnchor, constant: 20)
])
return headerView
}
public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
guard section < sections.count,
let footerTitle = sections[section].footerTitle,
!footerTitle.isEmpty else {
return nil
}
guard let footerView = tableView.dequeueReusableHeaderFooterView(
withIdentifier: CellIdentifiers.sectionFooterIdentifier
) as? BlogDetailsSectionFooterView else {
return nil
}
let shouldShowExtraSpacing = (section + 1 < sections.count) && (sections[section + 1].title != nil)
footerView.updateUI(title: footerTitle, shouldShowExtraSpacing: shouldShowExtraSpacing)
return footerView
}
}
private extension BlogDetailsTableViewModel {
func configureStandardCell(
tableView: UITableView,
indexPath: IndexPath,
row: Row
) -> UITableViewCell {
let identifier = switch row.kind {
case .removeSite:
CellIdentifiers.removeSite
case .jetpackSettings, .siteSettings, .domain:
CellIdentifiers.settings
default:
CellIdentifiers.standard
}
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
cell.accessibilityHint = row.accessibilityHint
cell.accessoryView = nil
cell.textLabel?.textAlignment = .natural
if row.kind == .removeSite {
cell.accessoryType = .none
WPStyleGuide.configureTableViewDestructiveActionCell(cell)
} else {
if row.showsDisclosureIndicator {
cell.accessoryType = isSplitViewDisplayed ? .none : .disclosureIndicator
} else {
cell.accessoryType = .none
}
WPStyleGuide.configureTableViewCell(cell)
}
cell.textLabel?.text = row.title
cell.accessibilityIdentifier = row.accessibilityIdentifier ?? identifier
cell.detailTextLabel?.text = row.detail
cell.imageView?.image = row.image
cell.imageView?.tintColor = row.imageColor
if let accessoryView = row.accessoryView {
cell.accessoryView = accessoryView
}
return cell
}
func configureJetpackInstallCell(tableView: UITableView) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.jetpackInstall
) as? JetpackRemoteInstallTableViewCell,
let viewController else {
return UITableViewCell()
}
cell.configure(blog: blog, viewController: viewController)
return cell
}
func configureMigrationSuccessCell(tableView: UITableView) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.migrationSuccess
) as? MigrationSuccessCell,
let viewController else {
return UITableViewCell()
}
if viewController.isSidebarModeEnabled {
cell.configureForSidebarMode()
}
cell.configure(with: viewController)
return cell
}
func configureJetpackBrandingCell(tableView: UITableView) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.jetpackBrandingCard
) as? JetpackBrandingMenuCardCell,
let viewController else {
return UITableViewCell()
}
cell.configure(with: viewController)
return cell
}
func configureExtensiveLoggingCell(tableView: UITableView) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.extensiveLogging
) as? ExtensiveLoggingCell,
let viewController else {
return UITableViewCell()
}
cell.configure(with: viewController)
return cell
}
func configureXMLRPCDisabledCell(tableView: UITableView) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: CellIdentifiers.xmlrpcDisabled
) as? XMLRPCDisabledCell,
let viewController else {
return UITableViewCell()
}
cell.configure(with: viewController)
return cell
}
}
private extension BlogDetailsTableViewModel {
func buildHomeSection() -> Section {
return Section(rows: [Row.home(viewController: viewController)], category: .home)
}
func buildContentSection() -> Section {
var rows: [Row] = []
rows.append(Row.posts(viewController: viewController))
if blog.supports(.pages) {
rows.append(Row.pages(viewController: viewController))
}
rows.append(Row.media(viewController: viewController))
rows.append(Row.comments(viewController: viewController))
let title = isSplitViewDisplayed ? nil : Strings.contentSectionTitle
return Section(title: title, rows: rows, category: .content)
}
func buildRemoveSiteSection() -> Section {
return Section(rows: [Row.removeSite(viewController: viewController)], category: .removeSite)
}
func buildJetpackSection() -> Section {
var rows: [Row] = []
if blog.isViewingStatsAllowed() {
rows.append(Row.stats(viewController: viewController))
}
if blog.supports(.activity) && !blog.isWPForTeams() {
rows.append(Row.activityLog(viewController: viewController))
}
if blog.isBackupsAllowed() {
rows.append(Row.backup(viewController: viewController))
}
if blog.isScanAllowed() {
rows.append(Row.scan(viewController: viewController))
}
if blog.supports(.jetpackSettings) {
rows.append(Row.jetpackSettings(viewController: viewController))
}
if viewController?.shouldShowBlaze() == true {
rows.append(Row.blaze(viewController: viewController))
}
let title = if blog.supports(.jetpackSettings) {
Strings.jetpackSection
} else {
""
}
return Section(title: title, rows: rows, category: .jetpack)
}
func buildGeneralSection() -> Section {
var rows: [Row] = []
if blog.isViewingStatsAllowed() {
rows.append(Row.stats(viewController: viewController))
}
if blog.supports(.activity) && !blog.isWPForTeams() {
rows.append(Row.activity(viewController: viewController))
}
if viewController?.shouldShowBlaze() == true {
rows.append(Row.blaze(viewController: viewController))
}
return Section(rows: rows, category: .general)
}
func buildPublishTypeSection() -> Section {
var rows: [Row] = []
rows.append(Row.posts(viewController: viewController))
rows.append(Row.media(viewController: viewController))
if blog.supports(.pages) {
rows.append(Row.pages(viewController: viewController))
}
rows.append(Row.comments(viewController: viewController))
let title = Strings.publishSection
return Section(title: title, rows: rows, category: .content)
}
func buildPersonalizeSection() -> Section {
var rows: [Row] = []
if blog.supports(.themeBrowsing) && !blog.isWPForTeams() {
rows.append(Row.themes(viewController: viewController))
}
if blog.supports(.menus) {
rows.append(Row.menus(viewController: viewController))
}
let title = Strings.personalizeSection
return Section(title: title, rows: rows, category: .personalize)
}
func buildConfigurationSection() -> Section {
guard let viewController else {
return Section(title: "Configure", rows: [], category: .configure)
}
var rows: [Row] = []
// Me row
if viewController.shouldAddMeRow() {
rows.append(Row.me(icon: gravatarIcon, viewController: viewController))
// Note: Gravatar image download would be handled by viewController
}
// Sharing row
if viewController.shouldAddSharingRow() {
rows.append(Row.sharing(viewController: viewController))
}
// People row
if viewController.shouldAddPeopleRow() {
rows.append(Row.people(viewController: viewController))
}
// Users row
if viewController.shouldAddUsersRow() {
rows.append(Row.users(viewController: viewController))
}
// Plugins row
if viewController.shouldAddPluginsRow() {
rows.append(Row.plugins(viewController: viewController))
}
// Site Settings row (always included)
rows.append(Row.siteSettings(viewController: viewController))
// Domains row
if viewController.shouldAddDomainRegistrationRow() {
rows.append(Row.domains(viewController: viewController))
}
let title = Strings.configureSection
return Section(title: title, rows: rows, category: .configure)
}
func buildExternalSection() -> Section {
guard let viewController else {
return Section(title: Strings.externalSection, rows: [], category: .external)
}
var rows: [Row] = []
rows.append(Row.viewSite(viewController: viewController))
if shouldDisplayLinkToWPAdmin(for: blog) {
rows.append(Row.admin(viewController: viewController, blog: blog))
}
let title = Strings.externalSection
return Section(title: title, rows: rows, category: .external)
}
func buildTrafficSection() -> Section? {
guard let viewController else { return nil }
var rows: [Row] = []
if blog.isViewingStatsAllowed() {
rows.append(Row.stats(viewController: viewController))
}
if viewController.shouldShowSubscribersRow {
rows.append(Row.subscribers(viewController: viewController))
}
if viewController.shouldAddSharingRow() {
rows.append(Row.social(viewController: viewController))
}
if viewController.shouldShowBlaze() {
rows.append(Row.blaze(viewController: viewController))
}
if rows.isEmpty {
return nil
}
let title = Strings.trafficSectionTitle
return Section(title: title, rows: rows, category: .traffic)
}
func buildMaintenanceSections() -> [Section] {
guard let viewController else { return [] }
var sections: [Section] = []
var firstSectionRows: [Row] = []
var secondSectionRows: [Row] = []
var thirdSectionRows: [Row] = []
// First section: Activity, Backup, Scan, Site Monitoring
if blog.supports(.activity) && !blog.isWPForTeams() {
firstSectionRows.append(Row.activityLog(viewController: viewController))
}
if blog.isBackupsAllowed() {
firstSectionRows.append(Row.backup(viewController: viewController))
}
if blog.isScanAllowed() {
firstSectionRows.append(Row.scan(viewController: viewController))
}
if RemoteFeatureFlag.siteMonitoring.enabled() && blog.supports(.siteMonitoring) {
firstSectionRows.append(Row.siteMonitoring(viewController: viewController))
}
// Second section: People, Users, Plugins, Themes, Menus, Domains, Application Passwords, Site Settings
if viewController.shouldAddPeopleRow() {
secondSectionRows.append(Row.people(viewController: viewController))
}
if viewController.shouldAddUsersRow() {
secondSectionRows.append(Row.users(viewController: viewController))
}
if viewController.shouldAddPluginsRow() {
secondSectionRows.append(Row.plugins(viewController: viewController))
}
if blog.supports(.themeBrowsing) && !blog.isWPForTeams() {
secondSectionRows.append(Row.themes(viewController: viewController))
}
if blog.supports(.menus) {
secondSectionRows.append(Row.menus(viewController: viewController))
}
if viewController.shouldAddDomainRegistrationRow() {
secondSectionRows.append(Row.domains(viewController: viewController))
}
if FeatureFlag.allowApplicationPasswords.enabled {
secondSectionRows.append(Row.applicationPasswords(viewController: viewController))
}
// Site Settings (always included)
secondSectionRows.append(Row.siteSettings(viewController: viewController))
// Third section: WP Admin
if shouldDisplayLinkToWPAdmin(for: blog) {
thirdSectionRows.append(Row.admin(viewController: viewController, blog: blog))
}
// Build sections with proper titles
let sectionTitle = Strings.maintenanceSectionTitle
var shouldAddSectionTitle = true
if !firstSectionRows.isEmpty {
sections.append(Section(
title: sectionTitle,
rows: firstSectionRows,
category: .maintenance
))
shouldAddSectionTitle = false
}
if !secondSectionRows.isEmpty {
sections.append(Section(
title: shouldAddSectionTitle ? sectionTitle : nil,
rows: secondSectionRows,
category: .maintenance
))
shouldAddSectionTitle = false
}
if !thirdSectionRows.isEmpty {
sections.append(Section(
title: shouldAddSectionTitle ? sectionTitle : nil,
rows: thirdSectionRows,
category: .maintenance
))
}
return sections
}
// MARK: - Helper Methods
private func shouldDisplayLinkToWPAdmin(for blog: Blog) -> Bool {
if !blog.isHostedAtWPcom {
return true
}
// For .com users, check if account was created before HideWPAdminDate
let hideWPAdminDateString = "2015-09-07T00:00:00Z"
guard let hideWPAdminDate = ISO8601DateFormatter().date(from: hideWPAdminDateString) else {
return false
}
let context = ContextManager.shared.mainContext
guard let defaultAccount = try? WPAccount.lookupDefaultWordPressComAccount(in: context),
let dateCreated = defaultAccount.dateCreated else {
return false
}
return dateCreated < hideWPAdminDate
}
}
enum BlogDetailsUserInfoKeys {
static let source = "source"
static let showPicker = "show-picker"
static let showManagePlugins = "show-manage-plugins"
static let siteMonitoringTab = "site-monitoring-tab"
}
// MARK: - Table view content
private enum SectionCategory {
case reminders
case domainCredit
case extensiveLogging
case xmlrpcDisabled
case home
case general
case jetpack
case personalize
case configure
case external
case removeSite
case migrationSuccess
case jetpackBrandingCard
case jetpackInstallCard
case content
case traffic
case maintenance
}
enum BlogDetailsRowKind {
case reminders
case domain
case stats
case posts
case customize
case themes
case media
case pages
case activity
case backup
case scan
case jetpackSettings
case me
case comments
case sharing
case people
case subscribers
case plugins
case home
case migrationSuccess
case jetpackBrandingCard
case blaze
case menu
case applicationPasswords
case siteMonitoring
case viewSite
case admin
case siteSettings
case removeSite
}
private struct Row {
let kind: BlogDetailsRowKind
let title: String
let accessibilityIdentifier: String?
let accessibilityHint: String?
let image: UIImage?
let imageColor: UIColor?
let accessoryView: UIView?
let detail: String?
let showsSelectionState: Bool
let showsDisclosureIndicator: Bool
let action: (([String: Any]) -> Void)?
init(
kind: BlogDetailsRowKind,
title: String,
accessibilityIdentifier: String? = nil,
accessibilityHint: String? = nil,
image: UIImage?,
imageColor: UIColor? = .label,
accessoryView: UIView? = nil,
detail: String? = nil,
showsSelectionState: Bool = true,
showsDisclosureIndicator: Bool = true,
action: (([String: Any]) -> Void)? = nil,
) {
self.title = title
self.accessibilityIdentifier = accessibilityIdentifier
self.accessibilityHint = accessibilityHint
self.image = imageColor == nil ? image : image?.withRenderingMode(.alwaysTemplate)
self.imageColor = imageColor
self.accessoryView = accessoryView
self.detail = detail
self.showsSelectionState = showsSelectionState
self.showsDisclosureIndicator = showsDisclosureIndicator
self.action = action
self.kind = kind
}
}
extension Row {
static func home(viewController: BlogDetailsViewController?) -> Row {
Row(
kind: .home,
title: Strings.home,
accessibilityIdentifier: "Home Row",
image: UIImage(named: "site-menu-home"),
action: { [weak viewController] _ in
viewController?.showDashboard()
}
)
}
static func posts(viewController: BlogDetailsViewController?) -> Row {
Row(
kind: .posts,
title: Strings.posts,
accessibilityIdentifier: "Blog Post Row",
image: (UIImage(named: "site-menu-posts"))?.imageFlippedForRightToLeftLayoutDirection(),
action: { [weak viewController] userInfo in
// When called from showDetailView, use .link as source (matching Objective-C behavior)
// When called from direct tap, use .row (default behavior)
let source: BlogDetailsNavigationSource = userInfo.isEmpty ? .row : .link
viewController?.showPostList(from: source)
}
)
}
static func pages(viewController: BlogDetailsViewController?) -> Row {
Row(
kind: .pages,
title: Strings.pages,
accessibilityIdentifier: "Site Pages Row",
image: UIImage(named: "site-menu-pages"),
action: { [weak viewController] userInfo in
// When called from showDetailView, use .link as source (matching Objective-C behavior)
// When called from direct tap, use .row (default behavior)
let source: BlogDetailsNavigationSource = userInfo.isEmpty ? .row : .link
viewController?.showPageList(from: source)
}
)
}
static func media(viewController: BlogDetailsViewController?) -> Row {
Row(
kind: .media,
title: Strings.media,
accessibilityIdentifier: "Media Row",
image: UIImage(named: "site-menu-media"),
action: { [weak viewController] userInfo in
let showPicker = (userInfo[BlogDetailsUserInfoKeys.showPicker] as? NSNumber)?.boolValue ?? false
viewController?.showMediaLibrary(from: .link, showPicker: showPicker)
}
)
}
static func comments(viewController: BlogDetailsViewController?) -> Row {
Row(
kind: .comments,
title: Strings.comments,
image: (UIImage(named: "site-menu-comments"))?.imageFlippedForRightToLeftLayoutDirection(),
action: { [weak viewController] userInfo in
// When called from showDetailView, use .link as source (matching Objective-C behavior)
// When called from direct tap, use .row (default behavior)