mirror of
https://github.com/metabolist/metatext.git
synced 2025-01-03 18:48:40 +00:00
Refactoring
This commit is contained in:
parent
725438cc9e
commit
11e8f1b3f8
1 changed files with 10 additions and 16 deletions
|
@ -9,7 +9,6 @@ class StatusListViewModel: ObservableObject {
|
||||||
@Published private(set) var loading = false
|
@Published private(set) var loading = false
|
||||||
private(set) var maintainScrollPositionOfStatusID: String?
|
private(set) var maintainScrollPositionOfStatusID: String?
|
||||||
|
|
||||||
@Published private var filterRegularExpression: String?
|
|
||||||
private var statuses = [String: Status]()
|
private var statuses = [String: Status]()
|
||||||
private let statusListService: StatusListService
|
private let statusListService: StatusListService
|
||||||
private var statusViewModelCache = [Status: (StatusViewModel, AnyCancellable)]()
|
private var statusViewModelCache = [Status: (StatusViewModel, AnyCancellable)]()
|
||||||
|
@ -18,22 +17,9 @@ class StatusListViewModel: ObservableObject {
|
||||||
init(statusListService: StatusListService) {
|
init(statusListService: StatusListService) {
|
||||||
self.statusListService = statusListService
|
self.statusListService = statusListService
|
||||||
|
|
||||||
statusListService.filters
|
|
||||||
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
||||||
.map { $0.regularExpression() }
|
|
||||||
.assign(to: &$filterRegularExpression)
|
|
||||||
|
|
||||||
statusListService.statusSections
|
statusListService.statusSections
|
||||||
.map {
|
.combineLatest(statusListService.filters.map { $0.regularExpression() })
|
||||||
$0.map {
|
.map(Self.filter(statusSections:regularExpression:))
|
||||||
$0.filter { [weak self] in
|
|
||||||
guard let filterRegularExpression = self?.filterRegularExpression else { return true }
|
|
||||||
|
|
||||||
return $0.filterableContent.range(of: filterRegularExpression,
|
|
||||||
options: [.regularExpression, .caseInsensitive]) == nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.handleEvents(receiveOutput: { [weak self] in
|
.handleEvents(receiveOutput: { [weak self] in
|
||||||
self?.determineIfScrollPositionShouldBeMaintained(newStatusSections: $0)
|
self?.determineIfScrollPositionShouldBeMaintained(newStatusSections: $0)
|
||||||
self?.cleanViewModelCache(newStatusSections: $0)
|
self?.cleanViewModelCache(newStatusSections: $0)
|
||||||
|
@ -91,6 +77,14 @@ extension StatusListViewModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension StatusListViewModel {
|
private extension StatusListViewModel {
|
||||||
|
static func filter(statusSections: [[Status]], regularExpression: String?) -> [[Status]] {
|
||||||
|
guard let regEx = regularExpression else { return statusSections }
|
||||||
|
|
||||||
|
return statusSections.map {
|
||||||
|
$0.filter { $0.filterableContent.range(of: regEx, options: [.regularExpression, .caseInsensitive]) == nil }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func determineIfScrollPositionShouldBeMaintained(newStatusSections: [[Status]]) {
|
func determineIfScrollPositionShouldBeMaintained(newStatusSections: [[Status]]) {
|
||||||
maintainScrollPositionOfStatusID = nil // clear old value
|
maintainScrollPositionOfStatusID = nil // clear old value
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue