Skip to content

Commit

Permalink
refactor: Rename observable object for debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jun 20, 2023
1 parent 5292452 commit 51b6156
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
14 changes: 7 additions & 7 deletions Mail/Views/New Message/AutocompletionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import SwiftUI
struct AutocompletionView: View {
@State private var shouldAddUserProposal = false

@ObservedObject var cellRecipientsModel: CellRecipientsModel
@ObservedObject var textDebounce: TextDebounce

@Binding var autocompletion: [Recipient]
@Binding var addedRecipients: RealmSwift.List<Recipient>
Expand All @@ -42,7 +42,7 @@ struct AutocompletionView: View {
AutocompletionCell(
addRecipient: addRecipient,
recipient: recipient,
highlight: cellRecipientsModel.currentText,
highlight: textDebounce.text,
alreadyAppend: addedRecipients.contains { $0.isSameRecipient(as: recipient) },
unknownRecipient: isUserProposal
)
Expand All @@ -54,9 +54,9 @@ struct AutocompletionView: View {
}
}
.onAppear {
updateAutocompletion(cellRecipientsModel.currentText)
updateAutocompletion(textDebounce.text)
}
.onReceive(cellRecipientsModel.$currentText.debounce(for: .milliseconds(150), scheduler: DispatchQueue.main)) { currentValue in
.onReceive(textDebounce.$text.debounce(for: .milliseconds(150), scheduler: DispatchQueue.main)) { currentValue in
updateAutocompletion("\(currentValue)")
}
}
Expand All @@ -77,10 +77,10 @@ struct AutocompletionView: View {
let realResults = autocompleteRecipients.filter { !addedRecipients.map(\.email).contains($0.email) }

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

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

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

struct ComposeMessageCellRecipients: View {
@StateObject private var cellRecipientsModel = CellRecipientsModel()
@StateObject private var textDebounce = TextDebounce()

@State private var autocompletion = [Recipient]()

Expand All @@ -58,7 +58,7 @@ struct ComposeMessageCellRecipients: View {
.textStyle(.bodySecondary)

RecipientField(
currentText: $cellRecipientsModel.currentText,
currentText: $textDebounce.text,
recipients: $recipients,
focusedField: _focusedField,
type: type
Expand All @@ -80,7 +80,7 @@ struct ComposeMessageCellRecipients: View {

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

Expand Down

0 comments on commit 51b6156

Please sign in to comment.