Skip to content

Commit

Permalink
feat: Add debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jun 20, 2023
1 parent d8df5d4 commit 091f313
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
19 changes: 12 additions & 7 deletions Mail/Views/New Message/AutocompletionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import Combine
import MailCore
import MailResources
import RealmSwift
Expand All @@ -24,8 +25,9 @@ import SwiftUI
struct AutocompletionView: View {
@State private var shouldAddUserProposal = false

@ObservedObject var cellRecipientsModel: CellRecipientsModel

@Binding var autocompletion: [Recipient]
@Binding var currentSearch: String
@Binding var addedRecipients: RealmSwift.List<Recipient>

let addRecipient: @MainActor (Recipient) -> Void
Expand All @@ -40,7 +42,7 @@ struct AutocompletionView: View {
AutocompletionCell(
addRecipient: addRecipient,
recipient: recipient,
highlight: currentSearch,
highlight: cellRecipientsModel.currentText,
alreadyAppend: addedRecipients.contains { $0.isSameRecipient(as: recipient) },
unknownRecipient: isUserProposal
)
Expand All @@ -52,9 +54,12 @@ struct AutocompletionView: View {
}
}
.onAppear {
updateAutocompletion(currentSearch)
updateAutocompletion(cellRecipientsModel.currentText)
}
// .onChange(of: currentSearch, perform: updateAutocompletion)
.onReceive(cellRecipientsModel.$currentText.debounce(for: .milliseconds(150), scheduler: DispatchQueue.main)) { currentValue in
updateAutocompletion("\(currentValue)")
}
.onChange(of: currentSearch, perform: updateAutocompletion)
}

private func updateAutocompletion(_ search: String) {
Expand All @@ -73,10 +78,10 @@ struct AutocompletionView: View {
let realResults = autocompleteRecipients.filter { !addedRecipients.map(\.email).contains($0.email) }

withAnimation {
shouldAddUserProposal = !(realResults.count == 1 && realResults.first?.email == currentSearch)
shouldAddUserProposal = !(realResults.count == 1 && realResults.first?.email == cellRecipientsModel.currentText)
if shouldAddUserProposal {
autocompleteRecipients
.append(Recipient(email: currentSearch, name: ""))
.append(Recipient(email: cellRecipientsModel.currentText, name: ""))
}

autocompletion = autocompleteRecipients
Expand All @@ -87,8 +92,8 @@ struct AutocompletionView: View {
struct AutocompletionView_Previews: PreviewProvider {
static var previews: some View {
AutocompletionView(
cellRecipientsModel: CellRecipientsModel(),
autocompletion: .constant([]),
currentSearch: .constant(""),
addedRecipients: .constant([PreviewHelper.sampleRecipient1].toRealmList())
) { _ in /* Preview */ }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ extension VerticalAlignment {
static let newMessageCellAlignment = VerticalAlignment(NewMessageCellAlignment.self)
}

class CellRecipientsModel: ObservableObject {
@Published var currentText = ""
}

struct ComposeMessageCellRecipients: View {
@State private var currentText = ""
@StateObject private var cellRecipientsModel = CellRecipientsModel()

@State private var autocompletion = [Recipient]()

@Binding var recipients: RealmSwift.List<Recipient>
Expand All @@ -52,7 +57,12 @@ struct ComposeMessageCellRecipients: View {
Text(type.title)
.textStyle(.bodySecondary)

RecipientField(currentText: $currentText, recipients: $recipients, focusedField: _focusedField, type: type) {
RecipientField(
currentText: $cellRecipientsModel.currentText,
recipients: $recipients,
focusedField: _focusedField,
type: type
) {
if let bestMatch = autocompletion.first {
addNewRecipient(bestMatch)
}
Expand All @@ -70,8 +80,8 @@ struct ComposeMessageCellRecipients: View {

if autocompletionType == type {
AutocompletionView(
cellRecipientsModel: cellRecipientsModel,
autocompletion: $autocompletion,
currentSearch: $currentText,
addedRecipients: $recipients,
addRecipient: addNewRecipient
)
Expand All @@ -81,7 +91,7 @@ struct ComposeMessageCellRecipients: View {
.onTapGesture {
focusedField = type
}
.onChange(of: currentText) { newValue in
.onChange(of: cellRecipientsModel.currentText) { newValue in
withAnimation {
if newValue.isEmpty {
autocompletionType = nil
Expand Down Expand Up @@ -109,7 +119,7 @@ struct ComposeMessageCellRecipients: View {
withAnimation {
$recipients.append(recipient)
}
currentText = ""
cellRecipientsModel.currentText = ""
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mail/Views/New Message/RecipientField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct RecipientField: View {
}

private func switchFocus() {
guard case .chip(let hash, let recipient) = focusedField else { return }
guard case let .chip(hash, recipient) = focusedField else { return }

if recipient == recipients.last {
focusedField = type
Expand Down

0 comments on commit 091f313

Please sign in to comment.