-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathReaderTabViewController.swift
More file actions
176 lines (157 loc) · 6.75 KB
/
Copy pathReaderTabViewController.swift
File metadata and controls
176 lines (157 loc) · 6.75 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
import Combine
import UIKit
import SwiftUI
import WordPressData
import WordPressUI
final class ReaderTabViewController: UITabBarController, UITabBarControllerDelegate {
private var menuStore = ReaderMenuStore()
private let library = ReaderPresenter(
viewModel: ReaderSidebarViewModel(isReaderAppModeEnabled: true)
)
private let notificationsButtonViewModel = NotificationsButtonViewModel()
private var cancellables: [AnyCancellable] = []
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
if ReaderSidebarViewModel().getTopic(for: .following) != nil {
setupViewControllers()
} else {
loadMenuItems()
}
}
// TODO: (reader) remove the need to fetch the menu on first launch before showing anything
private func loadMenuItems() {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.startAnimating()
view.addSubview(activityIndicator)
activityIndicator.pinCenter()
menuStore.onCompletion = { [weak self] in
activityIndicator.removeFromSuperview()
self?.setupViewControllers()
self?.menuStore.onCompletion = nil
}
menuStore.refreshMenu()
}
private func setupViewControllers() {
self.viewControllers = [
makeHomeViewController(),
makeLibraryViewController(),
makeDiscoverViewController(),
makeNotificationsViewController(),
makeMeViewController()
]
}
// MARK: - Tabs
private func makeHomeViewController() -> UIViewController {
let homeVC = ReaderHomeViewController()
// TODO: (reader) refactor to not require `topic`
homeVC.readerTopic = ReaderSidebarViewModel().getTopic(for: .following)
homeVC.tabBarItem = UITabBarItem(
title: SharedStrings.Reader.home,
image: UIImage(named: "reader-menu-home"),
selectedImage: nil
)
// TODO: (reader) remove it; had to use due to how ghosts are implemented (separate table)
homeVC.tabBarItem.scrollEdgeAppearance = {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
return appearance
}()
return UINavigationController(rootViewController: homeVC)
}
private func makeLibraryViewController() -> UIViewController {
let libraryVC = library.sidebar
libraryVC.tabBarItem = UITabBarItem(
title: SharedStrings.Reader.library,
image: UIImage(named: "reader-menu-subscriptions"),
selectedImage: nil
)
libraryVC.tabBarItem.scrollEdgeAppearance = {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
return appearance
}()
return library.prepareForLibraryPresentation()
}
private func makeDiscoverViewController() -> UIViewController {
let discoverVC: UIViewController = {
// TODO: (reader) refactor to not require `topic`
if let topic = ReaderSidebarViewModel().getTopic(for: .discover) {
ReaderDiscoverTabViewController(topic: topic)
} else {
UIViewController()
}
}()
discoverVC.tabBarItem = UITabBarItem(
title: Strings.discover,
image: UIImage(named: "reader-menu-explorer"),
selectedImage: nil
)
discoverVC.tabBarItem.scrollEdgeAppearance = {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
return appearance
}()
let navigationVC = UINavigationController(rootViewController: discoverVC)
navigationVC.navigationBar.prefersLargeTitles = true
return navigationVC
}
private func makeNotificationsViewController() -> UIViewController {
let notificationsVC = Notifications.instantiateInitialViewController()
notificationsVC.tabBarItem = UITabBarItem(
title: Strings.notifications,
image: UIImage(named: "tab-bar-notifications"),
selectedImage: UIImage(named: "tab-bar-notifications")
)
notificationsVC.isReaderAppModeEnabled = true
let navigationVC = UINavigationController(rootViewController: notificationsVC)
notificationsVC.enableLargeTitles()
notificationsButtonViewModel.$counter.sink { [weak notificationsVC] count in
let image = UIImage(named: count == 0 ? "tab-bar-notifications" : "tab-bar-notifications-unread")
notificationsVC?.tabBarItem.image = image
notificationsVC?.tabBarItem.selectedImage = image
}.store(in: &cancellables)
return navigationVC
}
private func makeMeViewController() -> UIViewController {
let meVC = ReaderProfileViewController()
// TODO: (reader) display your profile icons
meVC.tabBarItem = UITabBarItem(
title: Strings.me,
image: UIImage(named: "tab-bar-me"),
selectedImage: UIImage(named: "tab-bar-me")
)
// TODO: (reader) observe gravatar updates
if let account = try? WPAccount.lookupDefaultWordPressComAccount(in: ContextManager.shared.mainContext),
let avatarURL = account.avatarURL.flatMap(URL.init) {
Task { @MainActor [weak meVC] in
do {
let image = try await ImageDownloader.shared.image(from: avatarURL)
meVC?.tabBarItem.configureGravatarImage(image)
} catch {
// Do nothing
}
}
}
return UINavigationController(rootViewController: meVC)
}
// MAKR: - UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if selectedIndex == viewControllers?.firstIndex(of: viewController) {
(viewController as? UINavigationController)?.scrollContentToTopAnimated(true)
}
return true
}
}
private extension UIViewController {
func enableLargeTitles() {
assert(navigationController != nil)
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.prefersLargeTitles = true
}
}
private enum Strings {
static let discover = NSLocalizedString("readerApp.tabBar.discover", value: "Discover", comment: "Reader app primary navigation tab bar")
static let notifications = NSLocalizedString("readerApp.tabBar.notifications", value: "Notifications", comment: "Reader app primary navigation tab bar")
static let me = NSLocalizedString("readerApp.tabBar.me", value: "Me", comment: "Reader app primary navigation tab bar")
}