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",