Skip to content

Commit

Permalink
feat: Update toolbar behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jul 12, 2024
1 parent 269fa4d commit c9ae7e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Mail/Views/Alerts/AddLinkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct AddLinkView: View {
@LazyInjectService private var snackbarPresenter: SnackBarPresentable
@LazyInjectService private var matomo: MatomoUtils

var actionHandler: ((String, URL) -> Void)?
var actionHandler: ((URL, String) -> Void)?

private var textPlaceholder: String {
if url.isEmpty {
Expand Down Expand Up @@ -87,7 +87,7 @@ struct AddLinkView: View {
return
}

actionHandler?(text, url)
actionHandler?(url, text)
}
}

Expand Down
23 changes: 14 additions & 9 deletions Mail/Views/New Message/ComposeEditor/ComposeEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct ComposeEditor: View {
@State private var toolbar = EditorToolbarView()
@StateObject private var textAttributes = TextAttributes()

@ModalState(context: ContextKeys.compose) var isShowingLinkAlert = false
@ModalState(context: ContextKeys.compose) var isShowingFileSelection = false
@ModalState(context: ContextKeys.compose) var isShowingPhotoLibrary = false
@ModalState(context: ContextKeys.compose) var isShowingCamera = false
Expand All @@ -44,12 +45,9 @@ struct ComposeEditor: View {
.editorInputAccessoryView(toolbar)
.editorCSS(Self.customCSS)
.onAppear(perform: configureToolbar)
// .customAlert(item: .constant(nil)) { alert in
// switch alert.type {
// case .link(let handler):
// AddLinkView(actionHandler: handler)
// }
// }
.customAlert(isPresented: $isShowingLinkAlert) {
AddLinkView(actionHandler: didCreateLink)
}
.sheet(isPresented: $isShowingFileSelection) {
DocumentPicker(pickerType: .selectContent([.item], didPickDocument))
.ignoresSafeArea()
Expand All @@ -71,21 +69,28 @@ struct ComposeEditor: View {

private func didTapMainToolbarButton(_ action: EditorToolbarAction) {
switch action {
case .link:
isShowingLinkAlert = true
case .ai:
// TODO: Show AI
break
case .addFile:
isShowingFileSelection.toggle()
isShowingFileSelection = true
case .addPhoto:
isShowingPhotoLibrary.toggle()
isShowingPhotoLibrary = true
case .takePhoto:
isShowingCamera.toggle()
isShowingCamera = true
case .programMessage:
showWorkInProgressSnackBar()
default:
print("Action not handled.")
}
}

private func didCreateLink(url: URL, text: String) {
textAttributes.addLink(url: url, text: text)
}

private func didPickDocument(_ urls: [URL]) {
attachmentsManager.importAttachments(
attachments: urls,
Expand Down
24 changes: 5 additions & 19 deletions Mail/Views/New Message/ComposeEditor/EditorToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,8 @@ import InfomaniakRichEditor
import MailCore
import MailCoreUI
import MailResources
import SwiftModalPresentation
import UIKit

struct ToolbarAlert: Identifiable, Equatable {
let id = UUID()
let type: ToolbarAlertType

static func == (lhs: ToolbarAlert, rhs: ToolbarAlert) -> Bool {
return lhs.id == rhs.id
}
}

enum ToolbarAlertType {
case link(handler: (String, URL) -> Void)
}

enum EditorToolbarStyle {
case main
case textEdition
Expand Down Expand Up @@ -170,15 +156,15 @@ enum EditorToolbarAction: Int {
case .bold:
return textAttributes.hasBold
case .italic:
return textAttributes.italic
return textAttributes.hasItalic
case .underline:
return textAttributes.underline
return textAttributes.hasUnderline
case .strikeThrough:
return textAttributes.strikethrough
return textAttributes.hasStrikethrough
case .unorderedList:
return textAttributes.unorderedList
return textAttributes.hasUnorderedList
case .link:
return textAttributes.link
return textAttributes.hasLink
case .editText, .ai, .addFile, .addPhoto, .takePhoto, .programMessage:
return false
}
Expand Down
15 changes: 10 additions & 5 deletions Mail/Views/New Message/ComposeEditor/EditorToolbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,20 @@ final class EditorToolbarView: UIToolbar {
case .bold:
textAttributes?.bold()
case .italic:
break
textAttributes?.italic()
case .underline:
break
textAttributes?.underline()
case .strikeThrough:
break
textAttributes?.strikethrough()
case .unorderedList:
break
textAttributes?.unorderedList()
case .link:
break
guard let textAttributes else { return }
if textAttributes.hasLink {
textAttributes.unlink()
} else {
mainButtonItemsHandler?(action)
}
case .ai, .addFile, .addPhoto, .takePhoto, .programMessage:
mainButtonItemsHandler?(action)
}
Expand Down

0 comments on commit c9ae7e9

Please sign in to comment.