From d409d26478078c38f616ec4356ac131e05a6dad4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 23 Nov 2022 14:07:03 -0500 Subject: [PATCH] Fix pressing CW button in Compose not toggling field visibility Bring back the wrapper view, turn's out it was load bearing. We need to be able to observe both the ui state and the draft object, while also updating the observed draft object when the ui state's draft changes, and this seems like the most straightforward way of doing that. --- .../Compose/ComposeHostingController.swift | 23 ++++++++++++++++--- Tusker/Screens/Compose/ComposeView.swift | 21 ++++------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Tusker/Screens/Compose/ComposeHostingController.swift b/Tusker/Screens/Compose/ComposeHostingController.swift index 895b4bda..2238fc20 100644 --- a/Tusker/Screens/Compose/ComposeHostingController.swift +++ b/Tusker/Screens/Compose/ComposeHostingController.swift @@ -16,7 +16,7 @@ protocol ComposeHostingControllerDelegate: AnyObject { func dismissCompose(mode: ComposeUIState.DismissMode) -> Bool } -class ComposeHostingController: UIHostingController, DuckableViewController { +class ComposeHostingController: UIHostingController, DuckableViewController { weak var delegate: ComposeHostingControllerDelegate? weak var duckableDelegate: DuckableViewControllerDelegate? @@ -36,11 +36,11 @@ class ComposeHostingController: UIHostingController, DuckableViewCo self.uiState = ComposeUIState(draft: realDraft) - let compose = ComposeView( + let wrapper = Wrapper( mastodonController: mastodonController, uiState: uiState ) - super.init(rootView: compose) + super.init(rootView: wrapper) self.uiState.delegate = self @@ -129,6 +129,23 @@ class ComposeHostingController: UIHostingController, DuckableViewCo } +extension ComposeHostingController { + struct Wrapper: View { + let mastodonController: MastodonController + @ObservedObject var uiState: ComposeUIState + var draft: Draft { + uiState.draft + } + + var body: some View { + ComposeView() + .environmentObject(mastodonController) + .environmentObject(uiState) + .environmentObject(draft) + } + } +} + extension ComposeHostingController: ComposeUIStateDelegate { var assetPickerDelegate: AssetPickerViewControllerDelegate? { self } diff --git a/Tusker/Screens/Compose/ComposeView.swift b/Tusker/Screens/Compose/ComposeView.swift index cceec341..7023fa4d 100644 --- a/Tusker/Screens/Compose/ComposeView.swift +++ b/Tusker/Screens/Compose/ComposeView.swift @@ -42,11 +42,9 @@ import Combine } struct ComposeView: View { - @ObservedObject var mastodonController: MastodonController - @ObservedObject var uiState: ComposeUIState - var draft: Draft { - uiState.draft - } + @EnvironmentObject var mastodonController: MastodonController + @EnvironmentObject var uiState: ComposeUIState + @EnvironmentObject var draft: Draft @State private var globalFrameOutsideList: CGRect = .zero @State private var contentWarningBecomeFirstResponder = false @@ -62,11 +60,6 @@ struct ComposeView: View { private let stackPadding: CGFloat = 8 - init(mastodonController: MastodonController, uiState: ComposeUIState) { - self.mastodonController = mastodonController - self.uiState = uiState - } - private var charactersRemaining: Int { let limit = mastodonController.instanceFeatures.maxStatusChars let cwCount = draft.contentWarningEnabled ? draft.contentWarning.count : 0 @@ -84,12 +77,6 @@ struct ComposeView: View { } var body: some View { - bodyWithoutEnvironment - .environmentObject(uiState) - .environmentObject(mastodonController) - } - - private var bodyWithoutEnvironment: some View { ZStack(alignment: .top) { mainList .scrollDismissesKeyboardInteractivelyIfAvailable() @@ -173,7 +160,7 @@ struct ComposeView: View { .listRowInsets(EdgeInsets(top: draft.inReplyToID == nil ? 8 : 4, leading: 8, bottom: 4, trailing: 8)) .listRowSeparator(.hidden) - if draft.contentWarningEnabled { + if uiState.draft.contentWarningEnabled { ComposeEmojiTextField( text: $uiState.draft.contentWarning, placeholder: "Write your warning here",