Skip to content

Commit

Permalink
Wrap search in scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHearst committed Aug 17, 2022
1 parent 9312410 commit ecad02b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
4 changes: 1 addition & 3 deletions MTGWizard/Components/SearchView/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ struct SearchView: View {
}

func handleSubmit() {
Task {
await viewModel.search()
}
viewModel.scheduleSearch()
}
}
23 changes: 20 additions & 3 deletions MTGWizard/Components/SearchView/SearchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class SearchViewModel: ObservableObject, SearchPersistable {
private var storageLocation: StorageLocation
private var debounceTimer: Timer?
private var pageNumber = 1
private var task: Task<Void, Never>?

@AppStorage("HideAlchemy") var hideAlchemy = false
@AppStorage("DefaultFormat") var defaultFormat: Format?
Expand All @@ -37,7 +38,7 @@ final class SearchViewModel: ObservableObject, SearchPersistable {
@Published var showFilters = false {
didSet {
guard !showFilters else { return }
Task { await search() }
scheduleSearch()
}
}

Expand All @@ -53,14 +54,22 @@ final class SearchViewModel: ObservableObject, SearchPersistable {
self.searchText = state.searchTerm
self.filters = state.filters

Task { await search() }
scheduleSearch()
}

// MARK: Search
func debounceSearch() {
debounceTimer?.invalidate()
debounceTimer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { _ in
Task { await self.search() }
self.scheduleSearch()
}
}

func scheduleSearch() {
debounceTimer?.invalidate()
task?.cancel()
task = Task {
await search()
}
}

Expand Down Expand Up @@ -144,4 +153,12 @@ final class SearchViewModel: ObservableObject, SearchPersistable {

isLoadingMore = false
}

deinit {
// TODO: Is this necessary?
debounceTimer?.invalidate()
debounceTimer = nil
task?.cancel()
task = nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21256.2" systemVersion="21G72" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Card" representedClassName="Card" syncable="YES" codeGenerationType="class">
<attribute name="data" optional="YES" attributeType="String"/>
<relationship name="onList" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="CardList" inverseName="hasCard" inverseEntity="CardList"/>
</entity>
<entity name="CardList" representedClassName="CardList" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="name" optional="YES" attributeType="String"/>
<relationship name="hasCard" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Card" inverseName="onList" inverseEntity="Card"/>
</entity>
</model>

0 comments on commit ecad02b

Please sign in to comment.