-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add watchlist swipe actions to new Market module
- Loading branch information
Showing
12 changed files
with
177 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 28 additions & 21 deletions
49
UnstoppableWallet/UnstoppableWallet/Modules/Favorites/FavoritesManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,65 @@ | ||
import RxCocoa | ||
import RxSwift | ||
import Combine | ||
import WidgetKit | ||
|
||
class FavoritesManager { | ||
private let storage: FavoriteCoinRecordStorage | ||
private let sharedStorage: SharedLocalStorage | ||
|
||
private let coinUidsUpdatedRelay = PublishRelay<Void>() | ||
private let coinUidsSubject = PassthroughSubject<Set<String>, Never>() | ||
|
||
var coinUids: Set<String> { | ||
didSet { | ||
coinUidsSubject.send(coinUids) | ||
syncSharedStorage() | ||
} | ||
} | ||
|
||
init(storage: FavoriteCoinRecordStorage, sharedStorage: SharedLocalStorage) { | ||
self.storage = storage | ||
self.sharedStorage = sharedStorage | ||
|
||
do { | ||
let records = try storage.favoriteCoinRecords() | ||
coinUids = Set(records.map(\.coinUid)) | ||
} catch { | ||
coinUids = Set() | ||
} | ||
|
||
syncSharedStorage() | ||
} | ||
|
||
private func syncSharedStorage() { | ||
sharedStorage.set(value: allCoinUids, for: AppWidgetConstants.keyFavoriteCoinUids) | ||
sharedStorage.set(value: Array(coinUids), for: AppWidgetConstants.keyFavoriteCoinUids) | ||
WidgetCenter.shared.reloadTimelines(ofKind: AppWidgetConstants.watchlistWidgetKind) | ||
} | ||
} | ||
|
||
extension FavoritesManager { | ||
var coinUidsUpdatedObservable: Observable<Void> { | ||
coinUidsUpdatedRelay.asObservable() | ||
} | ||
|
||
var allCoinUids: [String] { | ||
storage.favoriteCoinRecords.map(\.coinUid) | ||
var coinUidsPublisher: AnyPublisher<Set<String>, Never> { | ||
coinUidsSubject.eraseToAnyPublisher() | ||
} | ||
|
||
func add(coinUid: String) { | ||
storage.save(favoriteCoinRecord: FavoriteCoinRecord(coinUid: coinUid)) | ||
coinUidsUpdatedRelay.accept(()) | ||
syncSharedStorage() | ||
coinUids.insert(coinUid) | ||
try? storage.save(favoriteCoinRecord: FavoriteCoinRecord(coinUid: coinUid)) | ||
} | ||
|
||
func add(coinUids: [String]) { | ||
storage.save(favoriteCoinRecords: coinUids.map { FavoriteCoinRecord(coinUid: $0) }) | ||
coinUidsUpdatedRelay.accept(()) | ||
syncSharedStorage() | ||
self.coinUids.formUnion(coinUids) | ||
try? storage.save(favoriteCoinRecords: coinUids.map { FavoriteCoinRecord(coinUid: $0) }) | ||
} | ||
|
||
func removeAll() { | ||
storage.deleteAll() | ||
coinUids = Set() | ||
try? storage.deleteAll() | ||
} | ||
|
||
func remove(coinUid: String) { | ||
storage.deleteFavoriteCoinRecord(coinUid: coinUid) | ||
coinUidsUpdatedRelay.accept(()) | ||
syncSharedStorage() | ||
coinUids.remove(coinUid) | ||
try? storage.deleteFavoriteCoinRecord(coinUid: coinUid) | ||
} | ||
|
||
func isFavorite(coinUid: String) -> Bool { | ||
storage.favoriteCoinRecordExists(coinUid: coinUid) | ||
coinUids.contains(coinUid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
UnstoppableWallet/UnstoppableWallet/Modules/Market/Favorites/FavoritesModifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import SwiftUI | ||
|
||
struct FavoritesModifier: ViewModifier { | ||
@ObservedObject var viewModel: FavoritesViewModel | ||
let coinUid: String | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.swipeActions { | ||
if viewModel.coinUids.contains(coinUid) { | ||
Button { | ||
viewModel.remove(coinUid: coinUid) | ||
} label: { | ||
Image("star_off_24").renderingMode(.template) | ||
} | ||
.tint(.themeLucian) | ||
} else { | ||
Button { | ||
viewModel.add(coinUid: coinUid) | ||
} label: { | ||
Image("star_24").renderingMode(.template) | ||
} | ||
.tint(.themeJacob) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
func favoriteSwipeActions(viewModel: FavoritesViewModel, coinUid: String) -> some View { | ||
modifier(FavoritesModifier(viewModel: viewModel, coinUid: coinUid)) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
UnstoppableWallet/UnstoppableWallet/Modules/Market/Favorites/FavoritesViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Combine | ||
|
||
class FavoritesViewModel: ObservableObject { | ||
private let favoritesManager = App.shared.favoritesManager | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
@Published var coinUids: Set<String> | ||
|
||
init() { | ||
coinUids = favoritesManager.coinUids | ||
|
||
favoritesManager.coinUidsPublisher | ||
.sink { [weak self] in self?.coinUids = $0 } | ||
.store(in: &cancellables) | ||
} | ||
} | ||
|
||
extension FavoritesViewModel { | ||
func add(coinUid: String) { | ||
favoritesManager.add(coinUid: coinUid) | ||
} | ||
|
||
func remove(coinUid: String) { | ||
favoritesManager.remove(coinUid: coinUid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.