From 85307a21d8c5e51ef48a05a7d623243903a9b170 Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Fri, 26 Apr 2024 16:34:30 +0200 Subject: [PATCH] refactor: Create ToolbarModel --- .../New Message/ComposeMessageBodyView.swift | 45 +++++++++---------- .../New Message/ComposeMessageView.swift | 15 ++----- .../MailRichEditor/EditorCoordinator.swift | 18 ++++---- .../MailRichEditor/EditorToolbar.swift | 8 ++++ .../MailRichEditor/EditorView.swift | 15 +------ 5 files changed, 45 insertions(+), 56 deletions(-) diff --git a/Mail/Views/New Message/ComposeMessageBodyView.swift b/Mail/Views/New Message/ComposeMessageBodyView.swift index ddae8fb622..0ebe19bc1f 100644 --- a/Mail/Views/New Message/ComposeMessageBodyView.swift +++ b/Mail/Views/New Message/ComposeMessageBodyView.swift @@ -22,22 +22,21 @@ import MailCoreUI import RealmSwift import SwiftModalPresentation import SwiftUI +@_spi(Advanced) import SwiftUIIntrospect struct ComposeMessageBodyView: View { - @State private var isShowingCamera = false - @ModalState(context: ContextKeys.compose) private var isShowingFileSelection = false - @ModalState(context: ContextKeys.compose) private var isShowingPhotoLibrary = false + @State private var editorModel = EditorModel() + @StateObject private var toolbarModel = EditorToolbarModel() @ObservedRealmObject var draft: Draft - @Binding var editorModel: EditorModel @Binding var editorFocus: Bool - @Binding var isShowingAIPrompt: Bool - @Binding var isShowingAlert: NewMessageAlert? + @Binding var currentSignature: Signature? @ObservedObject var attachmentsManager: AttachmentsManager let messageReply: MessageReply? + let scrollView: UIScrollView? private var isRemoteContentBlocked: Bool { return UserDefaults.shared.displayExternalContent == .askMe && messageReply?.frozenMessage.localSafeDisplay == false @@ -47,18 +46,13 @@ struct ComposeMessageBodyView: View { VStack { AttachmentsHeaderView(attachmentsManager: attachmentsManager) - EditorView( - body: $draft.body, - model: $editorModel, - isShowingFileSelection: $isShowingFileSelection, - isShowingCamera: $isShowingCamera, - isShowingPhotoLibrary: $isShowingPhotoLibrary, - isShowingAIPrompt: $isShowingAIPrompt, - isShowingAlert: $isShowingAlert - ) - .frame(height: editorModel.height) + EditorView(body: $draft.body, model: $editorModel, toolbarModel: toolbarModel) + .frame(height: editorModel.height) } - .fullScreenCover(isPresented: $isShowingCamera) { + .customAlert(isPresented: .constant(false)) { + AddLinkView(actionHandler: { _ in }) + } + .fullScreenCover(isPresented: $toolbarModel.isShowingCamera) { CameraPicker { data in attachmentsManager.importAttachments( attachments: [data], @@ -68,7 +62,7 @@ struct ComposeMessageBodyView: View { } .ignoresSafeArea() } - .sheet(isPresented: $isShowingFileSelection) { + .sheet(isPresented: $toolbarModel.isShowingFileSelection) { DocumentPicker(pickerType: .selectContent([.item]) { urls in attachmentsManager.importAttachments( attachments: urls, @@ -78,7 +72,7 @@ struct ComposeMessageBodyView: View { }) .ignoresSafeArea() } - .sheet(isPresented: $isShowingPhotoLibrary) { + .sheet(isPresented: $toolbarModel.isShowingPhotoLibrary) { ImagePicker { results in attachmentsManager.importAttachments( attachments: results, @@ -89,18 +83,23 @@ struct ComposeMessageBodyView: View { .ignoresSafeArea() } } + + private func keepCursorVisible(_ cursorPosition: CGPoint?) { + guard let scrollView, let cursorPosition else { + return + } + } } #Preview { let draft = Draft() return ComposeMessageBodyView(draft: draft, - editorModel: .constant(EditorModel()), editorFocus: .constant(false), - isShowingAIPrompt: .constant(false), - isShowingAlert: .constant(nil), + currentSignature: .constant(nil), attachmentsManager: AttachmentsManager( draftLocalUUID: draft.localUUID, mailboxManager: PreviewHelper.sampleMailboxManager ), - messageReply: nil) + messageReply: nil, + scrollView: nil) } diff --git a/Mail/Views/New Message/ComposeMessageView.swift b/Mail/Views/New Message/ComposeMessageView.swift index f7b4c4ac23..ccff91a570 100644 --- a/Mail/Views/New Message/ComposeMessageView.swift +++ b/Mail/Views/New Message/ComposeMessageView.swift @@ -84,7 +84,6 @@ struct ComposeMessageView: View { @State private var currentSignature: Signature? @State private var initialAttachments = [Attachable]() - @State private var editorModel = EditorModel() @Weak private var scrollView: UIScrollView? @StateObject private var attachmentsManager: AttachmentsManager @@ -159,12 +158,11 @@ struct ComposeMessageView: View { if autocompletionType == nil && !isLoadingContent { ComposeMessageBodyView( draft: draft, - editorModel: $editorModel, editorFocus: $editorFocus, - isShowingAIPrompt: $aiModel.isShowingPrompt, - isShowingAlert: $isShowingAlert, + currentSignature: $currentSignature, attachmentsManager: attachmentsManager, - messageReply: messageReply + messageReply: messageReply, + scrollView: scrollView ) } } @@ -199,7 +197,6 @@ struct ComposeMessageView: View { // let rect = CGRect(x: 0, y: realPosition, width: 1, height: 1) // scrollView.scrollRectToVisible(rect, animated: true) // } - .onChange(of: editorModel.cursorPosition, perform: keepCursorVisible) .onChange(of: autocompletionType) { newValue in guard newValue != nil else { return } @@ -349,12 +346,6 @@ struct ComposeMessageView: View { } dismissMessageView() } - - private func keepCursorVisible(_ cursorPosition: CGPoint?) { - guard let scrollView, let cursorPosition else { - return - } - } } #Preview { diff --git a/Mail/Views/New Message/MailRichEditor/EditorCoordinator.swift b/Mail/Views/New Message/MailRichEditor/EditorCoordinator.swift index d750ef94f5..7c4c8a7259 100644 --- a/Mail/Views/New Message/MailRichEditor/EditorCoordinator.swift +++ b/Mail/Views/New Message/MailRichEditor/EditorCoordinator.swift @@ -110,13 +110,14 @@ extension EditorCoordinator { private func performAppAction(_ action: EditorToolbarAction) { switch action { case .ai: - parent.isShowingAIPrompt = true + // parent.isShowingAIPrompt = true + break case .addFile: - parent.isShowingFileSelection = true + parent.toolbarModel.isShowingFileSelection = true case .addPhoto: - parent.isShowingPhotoLibrary = true + parent.toolbarModel.isShowingPhotoLibrary = true case .takePhoto: - parent.isShowingCamera = true + parent.toolbarModel.isShowingCamera = true case .programMessage: showWorkInProgressSnackBar() default: @@ -140,10 +141,11 @@ extension EditorCoordinator { case .unorderedList: richEditorView.unorderedList() case .link: - parent.isShowingAlert = NewMessageAlert(type: .link { path in - guard let url = URL(string: path) else { return } - richEditorView.addLink(url) - }) +// parent.isShowingAlert = NewMessageAlert(type: .link { path in +// guard let url = URL(string: path) else { return } +// richEditorView.addLink(url) +// }) + break default: fatalError("Action not handled") } diff --git a/Mail/Views/New Message/MailRichEditor/EditorToolbar.swift b/Mail/Views/New Message/MailRichEditor/EditorToolbar.swift index b0181cbe96..14dfbd0272 100644 --- a/Mail/Views/New Message/MailRichEditor/EditorToolbar.swift +++ b/Mail/Views/New Message/MailRichEditor/EditorToolbar.swift @@ -20,8 +20,16 @@ import InfomaniakDI import InfomaniakRichEditor import MailCore import MailResources +import SwiftModalPresentation import UIKit +final class EditorToolbarModel: ObservableObject { + @ModalPublished(context: ContextKeys.compose) var isShowingCamera = false + @ModalPublished(context: ContextKeys.compose) var isShowingFileSelection = false + @ModalPublished(context: ContextKeys.compose) var isShowingPhotoLibrary = false + @ModalPublished(context: ContextKeys.compose) var isShowingLinkAlert = false +} + enum EditorToolbarStyle { case main case textEdition diff --git a/Mail/Views/New Message/MailRichEditor/EditorView.swift b/Mail/Views/New Message/MailRichEditor/EditorView.swift index 1348192721..25a79c63e7 100644 --- a/Mail/Views/New Message/MailRichEditor/EditorView.swift +++ b/Mail/Views/New Message/MailRichEditor/EditorView.swift @@ -29,12 +29,7 @@ struct EditorView: UIViewRepresentable { @Binding var body: String @Binding var model: EditorModel - - @Binding var isShowingFileSelection: Bool - @Binding var isShowingCamera: Bool - @Binding var isShowingPhotoLibrary: Bool - @Binding var isShowingAIPrompt: Bool - @Binding var isShowingAlert: NewMessageAlert? + @ObservedObject var toolbarModel: EditorToolbarModel func makeUIView(context: Context) -> RichEditorView { let editor = RichEditorView() @@ -56,11 +51,5 @@ struct EditorView: UIViewRepresentable { } #Preview { - EditorView(body: .constant(""), - model: .constant(EditorModel()), - isShowingFileSelection: .constant(false), - isShowingCamera: .constant(false), - isShowingPhotoLibrary: .constant(false), - isShowingAIPrompt: .constant(false), - isShowingAlert: .constant(nil)) + EditorView(body: .constant(""), model: .constant(EditorModel()), toolbarModel: EditorToolbarModel()) }