Refactoring

This commit is contained in:
Justin Mazzocchi 2021-02-04 13:33:29 -08:00
parent 1359b80a8e
commit 19176f955c
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
6 changed files with 51 additions and 25 deletions

View File

@ -30,7 +30,7 @@ class ShareExtensionNavigationViewController: UINavigationController {
}
setViewControllers(
[NewStatusViewController(viewModel: newStatusViewModel)],
[NewStatusViewController(viewModel: newStatusViewModel, rootViewModel: nil)],
animated: false)
}
}

View File

@ -24,6 +24,15 @@ final class MainNavigationViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
viewModel.$presentedNewStatusViewModel.sink { [weak self] in
if let newStatusViewModel = $0 {
self?.presentNewStatus(newStatusViewModel: newStatusViewModel)
} else {
self?.dismissNewStatus()
}
}
.store(in: &cancellables)
viewModel.$presentingSecondaryNavigation.sink { [weak self] in
if $0 {
self?.presentSecondaryNavigation()
@ -56,6 +65,7 @@ final class MainNavigationViewController: UITabBarController {
private extension MainNavigationViewController {
static let secondaryNavigationViewTag = UUID().hashValue
static let newStatusViewTag = UUID().hashValue
func setupViewControllers(pending: Bool) {
var controllers: [UIViewController] = [
@ -96,18 +106,9 @@ private extension MainNavigationViewController {
func setupNewStatusButton() {
let newStatusButtonView = NewStatusButtonView(primaryAction: UIAction { [weak self] _ in
guard let self = self else { return }
let newStatusViewModel = self.rootViewModel.newStatusViewModel(
identityContext: self.viewModel.identityContext)
let newStatusViewController = NewStatusViewController(viewModel: newStatusViewModel)
let newStatusNavigationController = UINavigationController(rootViewController: newStatusViewController)
if UIDevice.current.userInterfaceIdiom == .phone {
newStatusNavigationController.modalPresentationStyle = .fullScreen
} else {
newStatusNavigationController.isModalInPresentation = true
}
self.present(newStatusNavigationController, animated: true)
self.viewModel.presentedNewStatusViewModel =
self.rootViewModel.newStatusViewModel(identityContext: self.viewModel.identityContext)
})
view.addSubview(newStatusButtonView)
@ -158,6 +159,28 @@ private extension MainNavigationViewController {
}
}
func presentNewStatus(newStatusViewModel: NewStatusViewModel) {
let newStatusViewController = NewStatusViewController(viewModel: newStatusViewModel,
rootViewModel: rootViewModel)
let navigationController = UINavigationController(rootViewController: newStatusViewController)
if UIDevice.current.userInterfaceIdiom == .phone {
navigationController.modalPresentationStyle = .overFullScreen
} else {
navigationController.isModalInPresentation = true
}
navigationController.view.tag = Self.newStatusViewTag
present(navigationController, animated: true)
}
func dismissNewStatus() {
if presentedViewController?.view.tag == Self.newStatusViewTag {
dismiss(animated: true)
}
}
func handle(navigation: Navigation) {
let vc: UIViewController

View File

@ -11,6 +11,7 @@ import ViewModels
// swiftlint:disable file_length
final class NewStatusViewController: UIViewController {
private let viewModel: NewStatusViewModel
private let rootViewModel: RootViewModel?
private let scrollView = UIScrollView()
private let stackView = UIStackView()
private let activityIndicatorView = UIActivityIndicatorView(style: .large)
@ -24,8 +25,9 @@ final class NewStatusViewController: UIViewController {
private let documentPickerResuls = PassthroughSubject<[URL]?, Never>()
private var cancellables = Set<AnyCancellable>()
init(viewModel: NewStatusViewModel) {
init(viewModel: NewStatusViewModel, rootViewModel: RootViewModel?) {
self.viewModel = viewModel
self.rootViewModel = rootViewModel
super.init(nibName: nil, bundle: nil)
@ -241,7 +243,7 @@ private extension NewStatusViewController {
if let extensionContext = extensionContext {
extensionContext.completeRequest(returningItems: nil)
} else {
presentingViewController?.dismiss(animated: true)
rootViewModel?.navigationViewModel?.presentedNewStatusViewModel = nil
}
}

View File

@ -452,20 +452,10 @@ private extension TableViewController {
}
func compose(inReplyToViewModel: StatusViewModel?, redraft: Status?) {
let newStatusViewModel = rootViewModel.newStatusViewModel(
rootViewModel.navigationViewModel?.presentedNewStatusViewModel = rootViewModel.newStatusViewModel(
identityContext: viewModel.identityContext,
inReplyTo: inReplyToViewModel,
redraft: redraft)
let newStatusViewController = NewStatusViewController(viewModel: newStatusViewModel)
let navigationController = UINavigationController(rootViewController: newStatusViewController)
if UIDevice.current.userInterfaceIdiom == .phone {
navigationController.modalPresentationStyle = .overFullScreen
} else {
navigationController.isModalInPresentation = true
}
present(navigationController, animated: true)
}
func confirmDelete(statusViewModel: StatusViewModel, redraft: Bool) {

View File

@ -10,6 +10,7 @@ public final class NavigationViewModel: ObservableObject {
public let navigations: AnyPublisher<Navigation, Never>
@Published public private(set) var recentIdentities = [Identity]()
@Published public var presentedNewStatusViewModel: NewStatusViewModel?
@Published public var presentingSecondaryNavigation = false
@Published public var alertItem: AlertItem?
@ -110,6 +111,10 @@ public extension NavigationViewModel {
titleComponents: ["follow-requests"])))
}
func navigate(pushNotification: PushNotification) {
// TODO
}
func viewModel(timeline: Timeline) -> CollectionItemsViewModel {
CollectionItemsViewModel(
collectionService: identityContext.service.navigationService.timelineService(timeline: timeline),

View File

@ -178,6 +178,12 @@ private extension RootViewModel {
if identityId != navigationViewModel?.identityContext.identity.id {
identitySelected(id: identityId, immediate: false, notify: true)
}
$navigationViewModel.first { $0?.identityContext.identity.id == identityId }
// Ensure views are set up if switching accounts
.delay(for: .milliseconds(1), scheduler: DispatchQueue.main)
.sink { $0?.navigate(pushNotification: pushNotification) }
.store(in: &cancellables)
}
func notifyIdentityChange(identityContext: IdentityContext) {