Skip to content

Commit b16cdc9

Browse files
committed
Refactor how drafts are handled on a coordinator level and add support for them on thread timelines.
1 parent 27a877d commit b16cdc9

File tree

6 files changed

+33
-16
lines changed

6 files changed

+33
-16
lines changed

ElementX/Sources/Screens/RoomScreen/ComposerToolbar/ComposerToolbarModels.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ enum ComposerToolbarViewModelAction {
3939

4040
enum ComposerToolbarViewAction {
4141
case composerAppeared
42+
case composerDisappeared
43+
4244
case sendMessage
4345
case editLastMessage
4446
case cancelReply

ElementX/Sources/Screens/RoomScreen/ComposerToolbar/ComposerToolbarViewModel.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,22 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
161161
}
162162
}
163163
.store(in: &cancellables)
164+
165+
NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification).sink { [weak self] _ in
166+
self?.saveDraft()
167+
}
168+
.store(in: &cancellables)
164169
}
165170

166171
// MARK: - Public
172+
173+
func start() {
174+
Task { await loadDraft() }
175+
}
176+
177+
func stop() {
178+
saveDraft()
179+
}
167180

168181
override func process(viewAction: ComposerToolbarViewAction) {
169182
switch viewAction {
@@ -172,6 +185,8 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
172185
hasAppeard = true
173186
wysiwygViewModel.setup()
174187
}
188+
case .composerDisappeared:
189+
saveDraft()
175190
case .sendMessage:
176191
guard !state.sendButtonDisabled else { return }
177192

ElementX/Sources/Screens/RoomScreen/ComposerToolbar/ComposerToolbarViewModelProtocol.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import WysiwygComposer
1313
protocol ComposerToolbarViewModelProtocol {
1414
var actions: AnyPublisher<ComposerToolbarViewModelAction, Never> { get }
1515
var context: ComposerToolbarViewModelType.Context { get }
16+
17+
func start()
18+
func stop()
1619

1720
func process(timelineAction: TimelineComposerAction)
18-
func loadDraft() async
19-
func saveDraft()
2021
}

ElementX/Sources/Screens/RoomScreen/ComposerToolbar/View/ComposerToolbar.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ struct ComposerToolbar: View {
181181
} onAppearAction: {
182182
context.send(viewAction: .composerAppeared)
183183
}
184+
.onDisappear {
185+
context.send(viewAction: .composerDisappeared)
186+
}
184187
.environmentObject(context)
185188
.focused($composerFocused)
186189
.padding(.leading, context.composerFormattingEnabled ? 7 : 0)

ElementX/Sources/Screens/RoomScreen/RoomScreenCoordinator.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
107107
analyticsService: ServiceLocator.shared.analytics,
108108
composerDraftService: parameters.composerDraftService)
109109
self.composerViewModel = composerViewModel
110-
111-
NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification).sink { _ in
112-
composerViewModel.saveDraft()
113-
}
114-
.store(in: &cancellables)
115110
}
116111

117112
// MARK: - Public
@@ -193,8 +188,9 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
193188
}
194189
.store(in: &cancellables)
195190

196-
// Loading the draft requires the subscriptions to be set up first otherwise the room won't be be able to propagate the information to the composer.
197-
Task { await composerViewModel.loadDraft() }
191+
// Loading the draft requires the subscriptions to be set up first otherwise
192+
// the room won't be be able to propagate the information to the composer.
193+
composerViewModel.start()
198194
}
199195

200196
func focusOnEvent(_ focussedEvent: FocusEvent) {
@@ -212,7 +208,7 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
212208
}
213209

214210
func stop() {
215-
composerViewModel.saveDraft()
211+
composerViewModel.stop()
216212
roomViewModel.stop()
217213
}
218214

@@ -221,10 +217,7 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
221217

222218
return AnyView(RoomScreen(context: roomViewModel.context,
223219
timelineContext: timelineViewModel.context,
224-
composerToolbar: composerToolbar)
225-
.onDisappear { [weak self] in
226-
self?.composerViewModel.saveDraft()
227-
})
220+
composerToolbar: composerToolbar))
228221
}
229222
}
230223

ElementX/Sources/Screens/ThreadTimelineScreen/ThreadTimelineScreenCoordinator.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ final class ThreadTimelineScreenCoordinator: CoordinatorProtocol {
7474
maxExpandedHeight: ComposerConstant.maxHeight,
7575
parserStyle: .elementX)
7676

77-
#warning("Drafts are not handled and they can't be without rust side changes")
78-
7977
composerViewModel = ComposerToolbarViewModel(initialText: nil,
8078
roomProxy: parameters.roomProxy,
8179
isInThread: true,
@@ -144,9 +142,14 @@ final class ThreadTimelineScreenCoordinator: CoordinatorProtocol {
144142
timelineViewModel.process(composerAction: action)
145143
}
146144
.store(in: &cancellables)
145+
146+
// Loading the draft requires the subscriptions to be set up first otherwise
147+
// the room won't be be able to propagate the information to the composer.
148+
composerViewModel.start()
147149
}
148150

149151
func stop() {
152+
composerViewModel.stop()
150153
viewModel.stop()
151154
}
152155

0 commit comments

Comments
 (0)