Skip to content

Commit

Permalink
refactor(ActionsView): Reply - Use binding instead of closure
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Apr 27, 2023
1 parent 1c49cd5 commit 046f9ed
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
15 changes: 7 additions & 8 deletions Mail/Views/Bottom sheets/Actions/ActionsPanelViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@ struct ActionsPanelViewModifier: ViewModifier {
@StateObject private var moveSheet = MoveSheet()

@Binding var actionsTarget: ActionsTarget?
@State var reportDisplayProblemMessage: Message?

var completionHandler: (() -> Void)?

func body(content: Content) -> some View {
content
.floatingPanel(item: $actionsTarget) { target in
ActionsView(mailboxManager: mailboxManager,
target: target,
moveSheet: moveSheet) { message, replyMode in
// FIXME: There seems to be a bug where SwiftUI looses the "context" and attempts to present
// the view controller before waiting for the dismiss of the first one if we use a closure
// (this "fix" is temporary)
DispatchQueue.main.async {
navigationStore.messageReply = MessageReply(message: message, replyMode: replyMode)
}
} completionHandler: {
moveSheet: moveSheet,
messageReply: $navigationStore.messageReply) {
completionHandler?()
}
}
Expand All @@ -56,5 +52,8 @@ struct ActionsPanelViewModifier: ViewModifier {
MoveEmailView.sheetView(from: folderId, moveHandler: handler)
}
}
.floatingPanel(item: $reportDisplayProblemMessage) { message in
ReportJunkView(mailboxManager: mailboxManager, target: .message(message))
}
}
}
7 changes: 3 additions & 4 deletions Mail/Views/Bottom sheets/Actions/ActionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ActionsView: View {
init(mailboxManager: MailboxManager,
target: ActionsTarget,
moveSheet: MoveSheet? = nil,
replyHandler: ((Message, ReplyMode) -> Void)? = nil,
messageReply: Binding<MessageReply?>? = nil,
completionHandler: (() -> Void)? = nil) {
var matomoCategory = MatomoUtils.EventCategory.bottomSheetMessageActions
if case .threads = target {
Expand All @@ -38,8 +38,8 @@ struct ActionsView: View {
viewModel = ActionsViewModel(mailboxManager: mailboxManager,
target: target,
moveSheet: moveSheet,
messageReply: messageReply,
matomoCategory: matomoCategory,
replyHandler: replyHandler,
completionHandler: completionHandler)
}

Expand Down Expand Up @@ -70,8 +70,7 @@ struct ActionsView: View {

struct ActionsView_Previews: PreviewProvider {
static var previews: some View {
ActionsView(mailboxManager: PreviewHelper.sampleMailboxManager,
target: .threads([PreviewHelper.sampleThread], false)) { _, _ in /* Preview */ }
ActionsView(mailboxManager: PreviewHelper.sampleMailboxManager, target: .threads([PreviewHelper.sampleThread], false))
.accentColor(AccentColor.pink.primary.swiftUIColor)
}
}
Expand Down
16 changes: 12 additions & 4 deletions Mail/Views/Bottom sheets/Actions/ActionsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,22 @@ enum ActionsTarget: Equatable, Identifiable {
}

private func reply(mode: ReplyMode) async throws {
var displayedMessageReply: MessageReply?
switch target {
case let .threads(threads, _):
case .threads(let threads, _):
// We don't handle this action in multiple selection
guard threads.count == 1, let thread = threads.first,
let message = thread.messages.last(where: { !$0.isDraft }) else { break }
replyHandler?(message, mode)
case let .message(message):
replyHandler?(message, mode)
displayedMessageReply = MessageReply(message: message, replyMode: mode)
case .message(let message):
displayedMessageReply = MessageReply(message: message, replyMode: mode)
}

// FIXME: There seems to be a bug where SwiftUI looses the "context" and attempts to present
// the view controller before waiting for the dismiss of the first one if we use a closure
// (this "fix" is temporary)
DispatchQueue.main.async {
self.messageReply?.wrappedValue = displayedMessageReply
}
}

Expand Down
9 changes: 5 additions & 4 deletions Mail/Views/Bottom sheets/Actions/ReplyActionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ struct ReplyActionsView: View {

init(mailboxManager: MailboxManager,
message: Message,
replyHandler: @escaping (Message, ReplyMode) -> Void) {
messageReply: Binding<MessageReply?>?) {
viewModel = ActionsViewModel(mailboxManager: mailboxManager,
target: .message(message),
matomoCategory: .replyBottomSheet,
replyHandler: replyHandler)
messageReply: messageReply,
matomoCategory: .replyBottomSheet)
}

var body: some View {
Expand All @@ -52,7 +52,8 @@ struct ReplyActionsView: View {
struct ReplyActionsView_Previews: PreviewProvider {
static var previews: some View {
ReplyActionsView(mailboxManager: PreviewHelper.sampleMailboxManager,
message: PreviewHelper.sampleMessage) { _, _ in /* Preview */ }
message: PreviewHelper.sampleMessage,
messageReply: nil)
.accentColor(AccentColor.pink.primary.swiftUIColor)
}
}
4 changes: 1 addition & 3 deletions Mail/Views/Thread/MessageHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ struct MessageHeaderView: View {
}
.actionsPanel(actionsTarget: $actionsTarget)
.floatingPanel(item: $replyOrReplyAllMessage) { message in
ReplyActionsView(mailboxManager: mailboxManager, message: message) { message, replyMode in
navigationStore.messageReply = MessageReply(message: message, replyMode: replyMode)
}
ReplyActionsView(mailboxManager: mailboxManager, message: message, messageReply: $navigationStore.messageReply)
}
}

Expand Down
9 changes: 3 additions & 6 deletions Mail/Views/Thread/ThreadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ struct ThreadView: View {

@State private var headerHeight: CGFloat = 0
@State private var displayNavigationTitle = false
@State private var messageReply: MessageReply?
@State private var replyOrReplyAllMessage: Message?
@State private var actionsTarget: ActionsTarget?

Expand Down Expand Up @@ -132,9 +131,7 @@ struct ThreadView: View {
}
.actionsPanel(actionsTarget: $actionsTarget)
.floatingPanel(item: $replyOrReplyAllMessage) { message in
ReplyActionsView(mailboxManager: mailboxManager, message: message) { message, replyMode in
navigationStore.messageReply = MessageReply(message: message, replyMode: replyMode)
}
ReplyActionsView(mailboxManager: mailboxManager, message: message, messageReply: $navigationStore.messageReply)
}
.onChange(of: thread.messages) { newMessagesList in
if newMessagesList.isEmpty || thread.messageInFolderCount == 0 {
Expand All @@ -158,11 +155,11 @@ struct ThreadView: View {
if message.canReplyAll {
replyOrReplyAllMessage = message
} else {
messageReply = MessageReply(message: message, replyMode: .reply)
navigationStore.messageReply = MessageReply(message: message, replyMode: .reply)
}
case .forward:
guard let message = thread.messages.last else { return }
messageReply = MessageReply(message: message, replyMode: .forward)
navigationStore.messageReply = MessageReply(message: message, replyMode: .forward)
case .archive:
Task {
await tryOrDisplayError {
Expand Down

0 comments on commit 046f9ed

Please sign in to comment.