From 582ff3623c291e23fe36d053b71753b3d948facd Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sat, 26 Sep 2020 00:45:39 -0700 Subject: [PATCH] Connect account header text view --- .../CollectionViewController.swift | 8 ++++---- .../ViewModels/AccountStatusesViewModel.swift | 11 +++++++++++ .../ViewModels/Entities/NavigationEvent.swift | 2 +- .../ViewModels/StatusListViewModel.swift | 4 ++-- Views/AccountHeaderView.swift | 18 ++++++++++++++++++ 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/View Controllers/CollectionViewController.swift b/View Controllers/CollectionViewController.swift index 830c56b..4ee9c3d 100644 --- a/View Controllers/CollectionViewController.swift +++ b/View Controllers/CollectionViewController.swift @@ -171,10 +171,10 @@ private extension CollectionViewController { .dropFirst() .receive(on: DispatchQueue.main) .sink { [weak self] _ in - accountHeaderView.viewModel = accountsStatusesViewModel - self?.sizeTableHeaderFooterViews() - } - .store(in: &cancellables) + accountHeaderView.viewModel = accountsStatusesViewModel + self?.sizeTableHeaderFooterViews() + } + .store(in: &cancellables) tableView.tableHeaderView = accountHeaderView } } diff --git a/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift b/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift index 876d509..8e2acc3 100644 --- a/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift +++ b/ViewModels/Sources/ViewModels/AccountStatusesViewModel.swift @@ -28,6 +28,17 @@ public class AccountStatusesViewModel: StatusListViewModel { .assign(to: &$accountViewModel) } + public override var navigationEvents: AnyPublisher { + $accountViewModel.compactMap { $0 } + .flatMap(\.events) + .flatMap { $0 } + .map(NavigationEvent.init) + .compactMap { $0 } + .assignErrorsToAlertItem(to: \.alertItem, on: self) + .merge(with: super.navigationEvents) + .eraseToAnyPublisher() + } + public override func request(maxID: String? = nil, minID: String? = nil) { if case .statuses = collection, maxID == nil { accountStatusesService.fetchPinnedStatuses() diff --git a/ViewModels/Sources/ViewModels/Entities/NavigationEvent.swift b/ViewModels/Sources/ViewModels/Entities/NavigationEvent.swift index 74a867f..c4af1b1 100644 --- a/ViewModels/Sources/ViewModels/Entities/NavigationEvent.swift +++ b/ViewModels/Sources/ViewModels/Entities/NavigationEvent.swift @@ -11,7 +11,7 @@ public enum NavigationEvent { } extension NavigationEvent { - init?(_ event: CollectionItemEvent) { + public init?(_ event: CollectionItemEvent) { switch event { case .ignorableOutput: return nil diff --git a/ViewModels/Sources/ViewModels/StatusListViewModel.swift b/ViewModels/Sources/ViewModels/StatusListViewModel.swift index 0dba126..ad7e428 100644 --- a/ViewModels/Sources/ViewModels/StatusListViewModel.swift +++ b/ViewModels/Sources/ViewModels/StatusListViewModel.swift @@ -8,7 +8,6 @@ import ServiceLayer public class StatusListViewModel: ObservableObject { @Published public private(set) var items = [[CollectionItem]]() @Published public var alertItem: AlertItem? - public let navigationEvents: AnyPublisher public private(set) var nextPageMaxID: String? public private(set) var maintainScrollPositionOfItem: CollectionItem? @@ -22,7 +21,6 @@ public class StatusListViewModel: ObservableObject { init(statusListService: StatusListService) { self.statusListService = statusListService - navigationEvents = navigationEventsSubject.eraseToAnyPublisher() statusListService.statusSections .combineLatest(statusListService.filters.map { $0.regularExpression() }) @@ -43,6 +41,8 @@ public class StatusListViewModel: ObservableObject { .store(in: &cancellables) } + public var navigationEvents: AnyPublisher { navigationEventsSubject.eraseToAnyPublisher() } + public var title: AnyPublisher { Just(statusListService.title).eraseToAnyPublisher() } public func request(maxID: String? = nil, minID: String? = nil) { diff --git a/Views/AccountHeaderView.swift b/Views/AccountHeaderView.swift index 8da5abf..6a66150 100644 --- a/Views/AccountHeaderView.swift +++ b/Views/AccountHeaderView.swift @@ -44,6 +44,23 @@ class AccountHeaderView: UIView { } } +extension AccountHeaderView: UITextViewDelegate { + func textView( + _ textView: UITextView, + shouldInteractWith URL: URL, + in characterRange: NSRange, + interaction: UITextItemInteraction) -> Bool { + switch interaction { + case .invokeDefaultAction: + viewModel?.accountViewModel?.urlSelected(URL) + return false + case .preview: return false + case .presentActions: return false + @unknown default: return false + } + } +} + private extension AccountHeaderView { func initializationActions() { let baseStackView = UIStackView() @@ -55,6 +72,7 @@ private extension AccountHeaderView { baseStackView.axis = .vertical noteTextView.isScrollEnabled = false + noteTextView.delegate = self baseStackView.addArrangedSubview(noteTextView) for (index, collection) in AccountStatusCollection.allCases.enumerated() {