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.
This commit is contained in:
Shadowfacts 2022-11-23 14:07:03 -05:00
parent 76fc73de95
commit d409d26478
2 changed files with 24 additions and 20 deletions

View File

@ -16,7 +16,7 @@ protocol ComposeHostingControllerDelegate: AnyObject {
func dismissCompose(mode: ComposeUIState.DismissMode) -> Bool func dismissCompose(mode: ComposeUIState.DismissMode) -> Bool
} }
class ComposeHostingController: UIHostingController<ComposeView>, DuckableViewController { class ComposeHostingController: UIHostingController<ComposeHostingController.Wrapper>, DuckableViewController {
weak var delegate: ComposeHostingControllerDelegate? weak var delegate: ComposeHostingControllerDelegate?
weak var duckableDelegate: DuckableViewControllerDelegate? weak var duckableDelegate: DuckableViewControllerDelegate?
@ -36,11 +36,11 @@ class ComposeHostingController: UIHostingController<ComposeView>, DuckableViewCo
self.uiState = ComposeUIState(draft: realDraft) self.uiState = ComposeUIState(draft: realDraft)
let compose = ComposeView( let wrapper = Wrapper(
mastodonController: mastodonController, mastodonController: mastodonController,
uiState: uiState uiState: uiState
) )
super.init(rootView: compose) super.init(rootView: wrapper)
self.uiState.delegate = self self.uiState.delegate = self
@ -129,6 +129,23 @@ class ComposeHostingController: UIHostingController<ComposeView>, 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 { extension ComposeHostingController: ComposeUIStateDelegate {
var assetPickerDelegate: AssetPickerViewControllerDelegate? { self } var assetPickerDelegate: AssetPickerViewControllerDelegate? { self }

View File

@ -42,11 +42,9 @@ import Combine
} }
struct ComposeView: View { struct ComposeView: View {
@ObservedObject var mastodonController: MastodonController @EnvironmentObject var mastodonController: MastodonController
@ObservedObject var uiState: ComposeUIState @EnvironmentObject var uiState: ComposeUIState
var draft: Draft { @EnvironmentObject var draft: Draft
uiState.draft
}
@State private var globalFrameOutsideList: CGRect = .zero @State private var globalFrameOutsideList: CGRect = .zero
@State private var contentWarningBecomeFirstResponder = false @State private var contentWarningBecomeFirstResponder = false
@ -62,11 +60,6 @@ struct ComposeView: View {
private let stackPadding: CGFloat = 8 private let stackPadding: CGFloat = 8
init(mastodonController: MastodonController, uiState: ComposeUIState) {
self.mastodonController = mastodonController
self.uiState = uiState
}
private var charactersRemaining: Int { private var charactersRemaining: Int {
let limit = mastodonController.instanceFeatures.maxStatusChars let limit = mastodonController.instanceFeatures.maxStatusChars
let cwCount = draft.contentWarningEnabled ? draft.contentWarning.count : 0 let cwCount = draft.contentWarningEnabled ? draft.contentWarning.count : 0
@ -84,12 +77,6 @@ struct ComposeView: View {
} }
var body: some View { var body: some View {
bodyWithoutEnvironment
.environmentObject(uiState)
.environmentObject(mastodonController)
}
private var bodyWithoutEnvironment: some View {
ZStack(alignment: .top) { ZStack(alignment: .top) {
mainList mainList
.scrollDismissesKeyboardInteractivelyIfAvailable() .scrollDismissesKeyboardInteractivelyIfAvailable()
@ -173,7 +160,7 @@ struct ComposeView: View {
.listRowInsets(EdgeInsets(top: draft.inReplyToID == nil ? 8 : 4, leading: 8, bottom: 4, trailing: 8)) .listRowInsets(EdgeInsets(top: draft.inReplyToID == nil ? 8 : 4, leading: 8, bottom: 4, trailing: 8))
.listRowSeparator(.hidden) .listRowSeparator(.hidden)
if draft.contentWarningEnabled { if uiState.draft.contentWarningEnabled {
ComposeEmojiTextField( ComposeEmojiTextField(
text: $uiState.draft.contentWarning, text: $uiState.draft.contentWarning,
placeholder: "Write your warning here", placeholder: "Write your warning here",