Skip to content

Commit

Permalink
feat(ComposeMessageView): User can select identity
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jul 18, 2023
1 parent bd41af0 commit 26fbee2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
2 changes: 0 additions & 2 deletions Mail/Views/New Message/ComposeMessageBodyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ struct ComposeMessageBodyView: View {
@ObservedRealmObject var draft: Draft

@Binding var editorModel: RichTextEditorModel
@Binding var isLoadingContent: Bool
@Binding var editorFocus: Bool

@ObservedObject var attachmentsManager: AttachmentsManager
Expand Down Expand Up @@ -87,7 +86,6 @@ struct ComposeMessageBodyView_Previews: PreviewProvider {

ComposeMessageBodyView(draft: Draft(),
editorModel: .constant(RichTextEditorModel()),
isLoadingContent: .constant(false),
editorFocus: .constant(false),
attachmentsManager: AttachmentsManager(
draft: Draft(),
Expand Down
2 changes: 1 addition & 1 deletion Mail/Views/New Message/ComposeMessageHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ComposeMessageHeaderView: View {

var body: some View {
VStack(spacing: 0) {
ComposeMessageCellStaticText(
ComposeMessageSenderMenu(
autocompletionType: autocompletionType,
type: .from,
text: mailboxManager.mailbox.email
Expand Down
1 change: 0 additions & 1 deletion Mail/Views/New Message/ComposeMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ struct ComposeMessageView: View {
ComposeMessageBodyView(
draft: draft,
editorModel: $editorModel,
isLoadingContent: $isLoadingContent,
editorFocus: $editorFocus,
attachmentsManager: attachmentsManager,
alert: alert,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@

import RealmSwift
import MailCore
import MailResources
import SwiftUI

struct ComposeMessageCellStaticText: View {
struct ComposeMessageSenderMenu: View {
@EnvironmentObject private var draftContentManager: DraftContentManager

@State private var selectedSignature: Signature?

@ObservedResults(Signature.self) private var signatures
Expand All @@ -29,6 +32,10 @@ struct ComposeMessageCellStaticText: View {
let type: ComposeViewFieldType
let text: String

private var canSelectSignature: Bool {
signatures.count > 1
}

var body: some View {
if autocompletionType == nil {
VStack(spacing: 0) {
Expand All @@ -43,19 +50,31 @@ struct ComposeMessageCellStaticText: View {
withAnimation {
self.selectedSignature = signature
}
draftContentManager.updateSignature(with: signature)
} label: {
Text("\(signature.fullName) (\(signature.name))")
Label {
Text("\(signature.fullName) (\(signature.name))")
} icon: {
if signature == selectedSignature {
MailResourcesAsset.check.swiftUIImage
}
}

Text(signature.senderIdn)
}

}
} label: {
Text("\(selectedSignature.fullName) <\(selectedSignature.senderIdn)> (\(selectedSignature.name))")
.textStyle(.bodyAccent)
.textStyle(.body)
.lineLimit(1)
.frame(maxWidth: .infinity, alignment: .leading)
}

if canSelectSignature {
ChevronIcon(style: .down)
}
}
.disabled(!canSelectSignature)
}
}
.padding(.vertical, UIConstants.composeViewHeaderCellLargeVerticalSpacing)
Expand All @@ -71,6 +90,6 @@ struct ComposeMessageCellStaticText: View {

struct ComposeMessageStaticText_Previews: PreviewProvider {
static var previews: some View {
ComposeMessageCellStaticText(autocompletionType: nil, type: .from, text: "myaddress@email.com")
ComposeMessageSenderMenu(autocompletionType: nil, type: .from, text: "myaddress@email.com")
}
}
25 changes: 25 additions & 0 deletions MailCore/Cache/DraftContentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Foundation
import Sentry
import SwiftSoup

public class DraftContentManager: ObservableObject {
struct CompleteDraftResult {
Expand Down Expand Up @@ -48,6 +49,30 @@ public class DraftContentManager: ObservableObject {
)
}

public func updateSignature(with newSignature: Signature) {
let realm = mailboxManager.getRealm()
guard let liveIncompleteDraft = realm.object(ofType: Draft.self, forPrimaryKey: incompleteDraft.localUUID) else {
return
}

do {
let parsedBody = try SwiftSoup.parse(liveIncompleteDraft.body)
// If we find the old signature, we replace it with the new one
// else, we append the signature at the end of the document
if let foundSignatureDiv = try parsedBody.select(".editorUserSignature").first {
try foundSignatureDiv.html(newSignature.content)
} else {
try parsedBody.body()?.append(newSignature.content)
}

try? realm.write {
liveIncompleteDraft.identityId = "\(newSignature.id)"
liveIncompleteDraft.body = try parsedBody.outerHtml()
}
} catch {
}
}

private func loadCompleteDraftBody() async throws -> CompleteDraftResult {
var completeDraftBody: String
var attachments = [Attachment]()
Expand Down

0 comments on commit 26fbee2

Please sign in to comment.