Skip to content

Commit

Permalink
refactor: Create ToolbarModel
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Apr 29, 2024
1 parent 19d9c9f commit e9364da
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 56 deletions.
43 changes: 20 additions & 23 deletions Mail/Views/New Message/ComposeMessageBodyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +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 currentSignature: Signature?
@Binding var isShowingAIPrompt: Bool
@Binding var isShowingAlert: NewMessageAlert?

@ObservedObject var attachmentsManager: AttachmentsManager

let messageReply: MessageReply?
let scrollView: UIScrollView?

private var isRemoteContentBlocked: Bool {
return UserDefaults.shared.displayExternalContent == .askMe && messageReply?.frozenMessage.localSafeDisplay == false
Expand All @@ -48,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],
Expand All @@ -69,7 +62,7 @@ struct ComposeMessageBodyView: View {
}
.ignoresSafeArea()
}
.sheet(isPresented: $isShowingFileSelection) {
.sheet(isPresented: $toolbarModel.isShowingFileSelection) {
DocumentPicker(pickerType: .selectContent([.item]) { urls in
attachmentsManager.importAttachments(
attachments: urls,
Expand All @@ -79,7 +72,7 @@ struct ComposeMessageBodyView: View {
})
.ignoresSafeArea()
}
.sheet(isPresented: $isShowingPhotoLibrary) {
.sheet(isPresented: $toolbarModel.isShowingPhotoLibrary) {
ImagePicker { results in
attachmentsManager.importAttachments(
attachments: results,
Expand All @@ -90,19 +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),
currentSignature: .constant(nil),
isShowingAIPrompt: .constant(false),
isShowingAlert: .constant(nil),
attachmentsManager: AttachmentsManager(
draftLocalUUID: draft.localUUID,
mailboxManager: PreviewHelper.sampleMailboxManager
),
messageReply: nil)
messageReply: nil,
scrollView: nil)
}
14 changes: 2 additions & 12 deletions Mail/Views/New Message/ComposeMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -161,13 +160,11 @@ struct ComposeMessageView: View {
if autocompletionType == nil && !isLoadingContent {
ComposeMessageBodyView(
draft: draft,
editorModel: $editorModel,
editorFocus: $editorFocus,
currentSignature: $currentSignature,
isShowingAIPrompt: $aiModel.isShowingPrompt,
isShowingAlert: $isShowingAlert,
attachmentsManager: attachmentsManager,
messageReply: messageReply
messageReply: messageReply,
scrollView: scrollView
)
}
}
Expand Down Expand Up @@ -202,7 +199,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 }

Expand Down Expand Up @@ -353,12 +349,6 @@ struct ComposeMessageView: View {
}
dismissMessageView()
}

private func keepCursorVisible(_ cursorPosition: CGPoint?) {
guard let scrollView, let cursorPosition else {
return
}
}
}

#Preview {
Expand Down
19 changes: 11 additions & 8 deletions Mail/Views/New Message/MailRichEditor/EditorCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import InfomaniakRichEditor
import MailCore
import MailCoreUI
import UIKit

@MainActor
Expand Down Expand Up @@ -110,13 +111,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:
Expand All @@ -140,10 +142,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")
}
Expand Down
9 changes: 9 additions & 0 deletions Mail/Views/New Message/MailRichEditor/EditorToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
import InfomaniakDI
import InfomaniakRichEditor
import MailCore
import MailCoreUI
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
Expand Down
15 changes: 2 additions & 13 deletions Mail/Views/New Message/MailRichEditor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
}

0 comments on commit e9364da

Please sign in to comment.