Identity change guards

This commit is contained in:
Justin Mazzocchi 2021-01-26 17:12:03 -08:00
parent 6b4e17e41e
commit 127fef7078
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
5 changed files with 48 additions and 2 deletions

View file

@ -46,6 +46,7 @@
"camera-access.open-system-settings" = "Open system settings"; "camera-access.open-system-settings" = "Open system settings";
"cancel" = "Cancel"; "cancel" = "Cancel";
"compose.attachment.uploading" = "Uploading"; "compose.attachment.uploading" = "Uploading";
"compose.attachments-will-be-discarded" = "Attachments will be discarded when changing accounts";
"compose.browse" = "Browse"; "compose.browse" = "Browse";
"compose.mark-media-sensitive" = "Mark media as sensitive"; "compose.mark-media-sensitive" = "Mark media as sensitive";
"compose.photo-library" = "Photo Library"; "compose.photo-library" = "Photo Library";

View file

@ -433,7 +433,7 @@ private extension NewStatusViewController {
.map { identity in .map { identity in
UIDeferredMenuElement { completion in UIDeferredMenuElement { completion in
let action = UIAction(title: identity.handle) { [weak self] _ in let action = UIAction(title: identity.handle) { [weak self] _ in
self?.viewModel.setIdentity(identity) self?.changeIdentity(identity)
} }
if let image = identity.image { if let image = identity.image {
@ -460,6 +460,34 @@ private extension NewStatusViewController {
return changeIdentityButton 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) { func adjustContentInset(notification: Notification) {
guard let keyboardFrameEnd = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect guard let keyboardFrameEnd = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
else { return } else { return }

View file

@ -191,6 +191,10 @@ public extension CompositionViewModel {
eventsSubject.send(.updateAttachment(publisher)) eventsSubject.send(.updateAttachment(publisher))
} }
func discardAttachments() {
attachmentViewModels = []
}
} }
public extension CompositionViewModel.PollOption { public extension CompositionViewModel.PollOption {

View file

@ -11,9 +11,9 @@ public final class NewStatusViewModel: ObservableObject {
@Published public private(set) var identityContext: IdentityContext @Published public private(set) var identityContext: IdentityContext
@Published public private(set) var authenticatedIdentities = [Identity]() @Published public private(set) var authenticatedIdentities = [Identity]()
@Published public var canPost = false @Published public var canPost = false
@Published public var canChangeIdentity = true
@Published public var alertItem: AlertItem? @Published public var alertItem: AlertItem?
@Published public private(set) var postingState = PostingState.composing @Published public private(set) var postingState = PostingState.composing
public let canChangeIdentity: Bool
public let inReplyToViewModel: StatusViewModel? public let inReplyToViewModel: StatusViewModel?
public let events: AnyPublisher<Event, Never> public let events: AnyPublisher<Event, Never>
@ -36,6 +36,17 @@ public final class NewStatusViewModel: ObservableObject {
events = eventsSubject.eraseToAnyPublisher() events = eventsSubject.eraseToAnyPublisher()
visibility = identityContext.identity.preferences.postingDefaultVisibility 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 let compositionViewModel: CompositionViewModel
if let redraft = redraft { if let redraft = redraft {

View file

@ -101,6 +101,8 @@ public extension StatusViewModel {
return URL(string: website) return URL(string: website)
} }
var visibility: Status.Visibility { statusService.status.displayStatus.visibility }
var repliesCount: Int { statusService.status.displayStatus.repliesCount } var repliesCount: Int { statusService.status.displayStatus.repliesCount }
var reblogsCount: Int { statusService.status.displayStatus.reblogsCount } var reblogsCount: Int { statusService.status.displayStatus.reblogsCount }