Skip to content

Commit 99173dd

Browse files
LoadableViewObservable can expose elements by UUID
1 parent a63273f commit 99173dd

5 files changed

Lines changed: 53 additions & 17 deletions

File tree

Sources/LoadableView/Protocols/LoadableView/BaseLoadableViewModel+Refresh.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ public extension BaseLoadableViewModel {
1515
refresh()
1616
}
1717

18-
func refresh(on changePublisher: AnyPublisher<Void, Never>, showLoading: Bool) -> AnyCancellable {
19-
changePublisher.sink { [weak self] in
20-
self?.refresh(showLoading: showLoading)
18+
func refresh(on changePublisher: AnyPublisher<ObservationType, Never>, showLoading: Bool) -> AnyCancellable {
19+
changePublisher.sink { [weak self] observationType in
20+
switch observationType {
21+
case .void:
22+
self?.refresh(showLoading: showLoading)
23+
case .id(let id):
24+
self?._refresh(ifID: id, showLoading: showLoading)
25+
}
2126
}
2227
}
2328

24-
func refresh(on changePublisher: AnyPublisher<Void, Never>) -> AnyCancellable {
29+
func refresh(on changePublisher: AnyPublisher<ObservationType, Never>) -> AnyCancellable {
2530
refresh(on: changePublisher, showLoading: false)
2631
}
2732

28-
func refresh(on changePublishers: [AnyPublisher<Void, Never>], showLoading: Bool = false) -> AnyCancellable {
33+
func refresh(on changePublishers: [AnyPublisher<ObservationType, Never>], showLoading: Bool = false) -> AnyCancellable {
2934
refresh(on: Publishers.MergeMany(changePublishers).eraseToAnyPublisher(), showLoading: showLoading)
3035
}
3136

32-
func refresh(on changePublisher: AnyPublisher<Void, Never>..., showLoading: Bool = false) -> AnyCancellable {
37+
func refresh(on changePublisher: AnyPublisher<ObservationType, Never>..., showLoading: Bool = false) -> AnyCancellable {
3338
refresh(on: changePublisher, showLoading: showLoading)
3439
}
3540

Sources/LoadableView/Protocols/LoadableView/BaseLoadableViewModel.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ public protocol BaseLoadableViewModel: ObservableObject, AnyObject {
1313
var viewState: ViewState<Element> { get set }
1414
var overlayState: CurrentValueSubject<Overlay, Never> { get }
1515

16+
// MARK: - Optional implementation
17+
func cancel() async
18+
19+
func onLoadingChange(isLoading: Bool)
20+
21+
func shouldRefresh(_ oldItem: Element?, newItem: Element) async -> Bool
22+
23+
func shouldAnimate(_ oldItem: Element?, newItem: Element) async -> Bool
24+
1625
// MARK: - Don't implement
1726
func onAppear()
1827
func onDisappear()
1928
func setError(_ error: Error?)
2029
func setIsLoading(_ isLoading: Bool)
2130

22-
// MARK: - Don't implement
2331
func refresh(showLoading: Bool)
24-
func refresh(on changePublisher: AnyPublisher<Void, Never>, showLoading: Bool) -> AnyCancellable
25-
26-
// MARK: - Optional implementation
27-
func cancel() async
32+
func refresh(on changePublisher: AnyPublisher<ObservationType, Never>, showLoading: Bool) -> AnyCancellable
2833

29-
func onLoadingChange(isLoading: Bool)
30-
31-
func shouldRefresh(_ oldItem: Element?, newItem: Element) async -> Bool
32-
33-
func shouldAnimate(_ oldItem: Element?, newItem: Element) async -> Bool
34+
// Note: - Used internally for observing LoadableViewObservables
35+
func _refresh(ifID id: UUID, showLoading: Bool)
3436
}
3537

3638
public extension BaseLoadableViewModel {

Sources/LoadableView/Protocols/LoadableView/IDedLoadableViewModel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,11 @@ public extension IDedLoadableViewModel {
125125
func shouldAnimate(_ oldItem: Element?, newItem: Element) async -> Bool {
126126
return true
127127
}
128+
129+
func _refresh(ifID id: UUID, showLoading: Bool) {
130+
guard self.id == id as? Self.ID else {
131+
return
132+
}
133+
self.refresh(showLoading: showLoading)
134+
}
128135
}

Sources/LoadableView/Protocols/LoadableView/LoadableViewModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ public extension LoadableViewModel {
7676
func shouldAnimate(_ oldItem: Element?, newItem: Element) async -> Bool {
7777
return true
7878
}
79+
80+
func _refresh(ifID id: UUID, showLoading: Bool) {
81+
self.refresh(showLoading: showLoading)
82+
}
7983
}

Sources/LoadableView/Protocols/LoadableViewObservable.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44
import Foundation
55
import Combine
66

7+
public enum ObservationType {
8+
case void, id(UUID)
9+
}
10+
711
public protocol LoadableViewObservable {
812
associatedtype Change
913
where Change: Hashable
10-
nonisolated func publisher(for observableChange: Set<Change>) -> AnyPublisher<(), Never>
14+
nonisolated func publisher(for observableChange: Set<Change>) -> AnyPublisher<ObservationType, Never>
15+
}
16+
17+
public extension Publisher where Output == Void, Failure == Never {
18+
var viewModelObservable: AnyPublisher<ObservationType, Never> {
19+
map { .void }
20+
.eraseToAnyPublisher()
21+
}
22+
}
23+
24+
public extension Publisher where Output == UUID, Failure == Never {
25+
func viewModelObservable(_ id: UUID) -> AnyPublisher<ObservationType, Never> {
26+
map { .id($0) }
27+
.eraseToAnyPublisher()
28+
}
1129
}

0 commit comments

Comments
 (0)