Skip to content

Commit

Permalink
Merge branch 'master' into swiftui-app
Browse files Browse the repository at this point in the history
# Conflicts:
#	Mail/Views/Thread/WebView.swift
  • Loading branch information
PhilippeWeidmann committed Jul 7, 2023
2 parents b1ce193 + 1207787 commit c2477b4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/siteline/SwiftUI-Introspect",
"state" : {
"revision" : "8bf15ad33a529359200bd419a72ca2dda841089b",
"version" : "0.8.0"
"revision" : "adb9e7a69fd75322dcdee0c20f2fb6640d6f0087",
"version" : "0.9.0"
}
},
{
Expand Down
12 changes: 6 additions & 6 deletions Mail/Helpers/RichTextEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import WebKit
struct RichTextEditor: UIViewRepresentable {
typealias UIViewType = MailEditorView

@ObservedObject var model: RichTextEditorModel
@Binding var model: RichTextEditorModel
@Binding var body: String
@Binding var isShowingCamera: Bool
@Binding var isShowingFileSelection: Bool
Expand All @@ -37,12 +37,12 @@ struct RichTextEditor: UIViewRepresentable {
let blockRemoteContent: Bool
var alert: ObservedObject<NewMessageAlert>.Wrapper

init(model: RichTextEditorModel, body: Binding<String>,
init(model: Binding<RichTextEditorModel>, body: Binding<String>,
alert: ObservedObject<NewMessageAlert>.Wrapper,
isShowingCamera: Binding<Bool>, isShowingFileSelection: Binding<Bool>, isShowingPhotoLibrary: Binding<Bool>,
becomeFirstResponder: Binding<Bool>,
blockRemoteContent: Bool) {
_model = ObservedObject(wrappedValue: model)
_model = model
_body = body
self.alert = alert
_isShowingCamera = isShowingCamera
Expand Down Expand Up @@ -146,9 +146,9 @@ extension SQTextEditorView {
}
}

class RichTextEditorModel: ObservableObject {
@Published var cursorPosition: CGFloat = 0
@Published var height: CGFloat = 0
struct RichTextEditorModel {
var cursorPosition: CGFloat = 0
var height: CGFloat = 0
}

class MailEditorView: SQTextEditorView {
Expand Down
6 changes: 3 additions & 3 deletions Mail/Views/New Message/ComposeMessageBodyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ struct ComposeMessageBodyView: View {
@State private var isShowingFileSelection = false
@State private var isShowingPhotoLibrary = false

@StateObject private var editorModel = RichTextEditorModel()

@ObservedRealmObject var draft: Draft

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

Expand All @@ -48,7 +47,7 @@ struct ComposeMessageBodyView: View {
AttachmentsHeaderView(attachmentsManager: attachmentsManager)

RichTextEditor(
model: editorModel,
model: $editorModel,
body: $draft.body,
alert: $alert,
isShowingCamera: $isShowingCamera,
Expand Down Expand Up @@ -87,6 +86,7 @@ struct ComposeMessageBodyView_Previews: PreviewProvider {
@Environment(\.dismiss) var dismiss

ComposeMessageBodyView(draft: Draft(),
editorModel: .constant(RichTextEditorModel()),
isLoadingContent: .constant(false),
editorFocus: .constant(false),
attachmentsManager: AttachmentsManager(
Expand Down
68 changes: 45 additions & 23 deletions Mail/Views/New Message/ComposeMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ struct ComposeMessageView: View {
@State private var autocompletionType: ComposeViewFieldType?
@State private var editorFocus = false

@State private var editorModel = RichTextEditorModel()
@State private var scrollView: UIScrollView?

@StateObject private var attachmentsManager: AttachmentsManager
@StateObject private var alert = NewMessageAlert()

Expand Down Expand Up @@ -116,6 +119,29 @@ struct ComposeMessageView: View {
NavigationView {
composeMessage
}
.task {
do {
isLoadingContent = true
try await draftContentManager.prepareCompleteDraft()
attachmentsManager.completeUploadedAttachments()
isLoadingContent = false
} catch {
// Unable to get signatures, "An error occurred" and close modal.
IKSnackBar.showSnackBar(message: MailError.unknownError.localizedDescription)
dismiss()
}
}
.onAppear {
switch messageReply?.replyMode {
case .reply, .replyAll:
focusedField = .editor
default:
focusedField = .to
}
}
.onDisappear {
draftManager.syncDraft(mailboxManager: mailboxManager)
}
.interactiveDismissDisabled()
.customAlert(isPresented: $alert.isShowing) {
switch alert.state {
Expand Down Expand Up @@ -144,6 +170,7 @@ struct ComposeMessageView: View {
if autocompletionType == nil && !isLoadingContent {
ComposeMessageBodyView(
draft: draft,
editorModel: $editorModel,
isLoadingContent: $isLoadingContent,
editorFocus: $editorFocus,
attachmentsManager: attachmentsManager,
Expand All @@ -154,38 +181,33 @@ struct ComposeMessageView: View {
}
}
}
.task {
do {
isLoadingContent = true
try await draftContentManager.prepareCompleteDraft()
attachmentsManager.completeUploadedAttachments()
isLoadingContent = false
} catch {
// Unable to get signatures, "An error occurred" and close modal.
IKSnackBar.showSnackBar(message: MailError.unknownError.localizedDescription)
dismiss()
}
}
.background(MailResourcesAsset.backgroundColor.swiftUIColor)
.onAppear {
switch messageReply?.replyMode {
case .reply, .replyAll:
focusedField = .editor
default:
focusedField = .to
}
}
.onDisappear {
draftManager.syncDraft(mailboxManager: mailboxManager)
}
.overlay {
if isLoadingContent {
progressView
}
}
.introspectScrollView { scrollView in
guard self.scrollView != scrollView else { return }
self.scrollView = scrollView
scrollView.keyboardDismissMode = .interactive
}
.onChange(of: editorModel.height) { _ in
guard let scrollView else { return }

let fullSize = scrollView.contentSize.height
let realPosition = (fullSize - editorModel.height) + editorModel.cursorPosition

guard realPosition >= 0 else { return }
let rect = CGRect(x: 0, y: realPosition, width: 1, height: 1)
scrollView.scrollRectToVisible(rect, animated: true)
}
.onChange(of: autocompletionType) { newValue in
guard newValue != nil else { return }

let rectTop = CGRect(x: 0, y: 0, width: 1, height: 1)
scrollView?.scrollRectToVisible(rectTop, animated: true)
}
.navigationTitle(MailResourcesStrings.Localizable.buttonNewMessage)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
Expand Down
31 changes: 21 additions & 10 deletions Mail/Views/Thread/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,35 @@ extension WKWebView {
}

final class WebViewController: UIViewController {
var openURL: OpenURLAction!
var model: WebViewModel!
var messageUid: String!
var openURL: OpenURLAction?
var model: WebViewModel?
var messageUid: String?

private let widthSubject = PassthroughSubject<Double, Never>()
private var widthSubscriber: AnyCancellable?

override func loadView() {
view = model.webView
view.translatesAutoresizingMaskIntoConstraints = false
view = UIView()
guard let webView = model?.webView else {
return
}
// In some cases the UIWebView was still owned by an other UIViewController
webView.removeFromSuperview()
view.addSubview(webView)

webView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
webView.heightAnchor.constraint(equalTo: view.heightAnchor),
webView.widthAnchor.constraint(equalTo: view.widthAnchor)
])

setUpWebView(model.webView)
setUpWebView(webView)

widthSubscriber = widthSubject
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.sink { newWidth in
.sink { [weak self] newWidth in
Task {
try await self.normalizeMessageWidth(webViewWidth: CGFloat(newWidth))
try await self?.normalizeMessageWidth(webViewWidth: CGFloat(newWidth))
}
}
}
Expand Down Expand Up @@ -91,7 +102,7 @@ final class WebViewController: UIViewController {
}

private func normalizeMessageWidth(webViewWidth width: CGFloat) async throws {
try await model.webView.evaluateJavaScript(.normalizeMessageWidth(width, messageUid ?? ""))
try await model?.webView.evaluateJavaScript(.normalizeMessageWidth(width, messageUid ?? ""))
}
}

Expand All @@ -114,7 +125,7 @@ extension WebViewController: WKNavigationDelegate {
) {
if let url = navigationAction.request.url, Constants.isMailTo(url) {
decisionHandler(.cancel)
openURL(url)
openURL?(url)
return
}

Expand Down

0 comments on commit c2477b4

Please sign in to comment.