From 127fef70784fbc5b53a13910db6574324f198dd2 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Tue, 26 Jan 2021 17:12:03 -0800 Subject: [PATCH] Identity change guards --- Localizations/Localizable.strings | 1 + .../NewStatusViewController.swift | 30 ++++++++++++++++++- .../View Models/CompositionViewModel.swift | 4 +++ .../View Models/NewStatusViewModel.swift | 13 +++++++- .../View Models/StatusViewModel.swift | 2 ++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Localizations/Localizable.strings b/Localizations/Localizable.strings index 943e414..9b8492a 100644 --- a/Localizations/Localizable.strings +++ b/Localizations/Localizable.strings @@ -46,6 +46,7 @@ "camera-access.open-system-settings" = "Open system settings"; "cancel" = "Cancel"; "compose.attachment.uploading" = "Uploading"; +"compose.attachments-will-be-discarded" = "Attachments will be discarded when changing accounts"; "compose.browse" = "Browse"; "compose.mark-media-sensitive" = "Mark media as sensitive"; "compose.photo-library" = "Photo Library"; diff --git a/View Controllers/NewStatusViewController.swift b/View Controllers/NewStatusViewController.swift index 40b4d0c..7506bdd 100644 --- a/View Controllers/NewStatusViewController.swift +++ b/View Controllers/NewStatusViewController.swift @@ -433,7 +433,7 @@ private extension NewStatusViewController { .map { identity in UIDeferredMenuElement { completion in let action = UIAction(title: identity.handle) { [weak self] _ in - self?.viewModel.setIdentity(identity) + self?.changeIdentity(identity) } if let image = identity.image { @@ -460,6 +460,34 @@ private extension NewStatusViewController { return changeIdentityButton } + func changeIdentity(_ identity: Identity) { + if viewModel.compositionViewModels.contains(where: { !$0.attachmentViewModels.isEmpty }) { + let alertController = UIAlertController( + title: nil, + message: NSLocalizedString("compose.attachments-will-be-discarded", comment: ""), + preferredStyle: .alert) + + let okAction = UIAlertAction( + title: NSLocalizedString("ok", comment: ""), + style: .destructive) { [weak self] _ in + guard let self = self else { return } + + for compositionViewModel in self.viewModel.compositionViewModels { + compositionViewModel.discardAttachments() + } + + self.viewModel.setIdentity(identity) + } + let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel) { _ in } + + alertController.addAction(okAction) + alertController.addAction(cancelAction) + present(alertController, animated: true) + } else { + viewModel.setIdentity(identity) + } + } + func adjustContentInset(notification: Notification) { guard let keyboardFrameEnd = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return } diff --git a/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift b/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift index a563de8..634a664 100644 --- a/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/CompositionViewModel.swift @@ -191,6 +191,10 @@ public extension CompositionViewModel { eventsSubject.send(.updateAttachment(publisher)) } + + func discardAttachments() { + attachmentViewModels = [] + } } public extension CompositionViewModel.PollOption { diff --git a/ViewModels/Sources/ViewModels/View Models/NewStatusViewModel.swift b/ViewModels/Sources/ViewModels/View Models/NewStatusViewModel.swift index 7e448ae..ab6aa80 100644 --- a/ViewModels/Sources/ViewModels/View Models/NewStatusViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/NewStatusViewModel.swift @@ -11,9 +11,9 @@ public final class NewStatusViewModel: ObservableObject { @Published public private(set) var identityContext: IdentityContext @Published public private(set) var authenticatedIdentities = [Identity]() @Published public var canPost = false - @Published public var canChangeIdentity = true @Published public var alertItem: AlertItem? @Published public private(set) var postingState = PostingState.composing + public let canChangeIdentity: Bool public let inReplyToViewModel: StatusViewModel? public let events: AnyPublisher @@ -36,6 +36,17 @@ public final class NewStatusViewModel: ObservableObject { events = eventsSubject.eraseToAnyPublisher() visibility = identityContext.identity.preferences.postingDefaultVisibility + if let inReplyTo = inReplyTo { + switch inReplyTo.visibility { + case .public, .unlisted: + canChangeIdentity = true + default: + canChangeIdentity = false + } + } else { + canChangeIdentity = true + } + let compositionViewModel: CompositionViewModel if let redraft = redraft { diff --git a/ViewModels/Sources/ViewModels/View Models/StatusViewModel.swift b/ViewModels/Sources/ViewModels/View Models/StatusViewModel.swift index 7eb4833..7add244 100644 --- a/ViewModels/Sources/ViewModels/View Models/StatusViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/StatusViewModel.swift @@ -101,6 +101,8 @@ public extension StatusViewModel { return URL(string: website) } + var visibility: Status.Visibility { statusService.status.displayStatus.visibility } + var repliesCount: Int { statusService.status.displayStatus.repliesCount } var reblogsCount: Int { statusService.status.displayStatus.reblogsCount }