Skip to content

Commit

Permalink
Show watch added/removed huds
Browse files Browse the repository at this point in the history
- change backup success hud
  • Loading branch information
mNizhurin authored and Dianaaiym committed Jun 23, 2022
1 parent 19bc9ac commit 17a847c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class BackupConfirmKeyViewController: KeyboardAwareViewController {
self?.passphraseCell.inputText = nil
}
subscribe(disposeBag, viewModel.successSignal) { [weak self] in
HudHelper.instance.show(banner: .addedToWallet)
HudHelper.instance.show(banner: .success)
self?.dismiss(animated: true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extension CoinPageViewModel {
switch service.walletState {
case .supported(let added):
if added {
hudRelay.accept(.alreadyAddedToWatchlist)
hudRelay.accept(.alreadyAddedToWallet)
} else {
service.addWallet()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class MarketListViewController: ThemeViewController {
self.listViewModel = listViewModel

super.init()

if let watchViewModel = listViewModel as? IMarketListWatchViewModel {
subscribe(disposeBag, watchViewModel.favoriteDriver) { HudHelper.instance.show(banner: .addedToWatchlist) }
subscribe(disposeBag, watchViewModel.unfavoriteDriver) { HudHelper.instance.show(banner: .removedFromWatchlist) }
subscribe(disposeBag, watchViewModel.failDriver) { error in HudHelper.instance.show(banner: .error(string: error.localized)) }
}
}

required init?(coder: NSCoder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,59 @@ import CurrencyKit
import MarketKit

protocol IMarketListWatchViewModel: IMarketListViewModel {
var favoriteDriver: Driver<()> { get }
var unfavoriteDriver: Driver<()> { get }
var failDriver: Driver<String> { get }

func isFavorite(index: Int) -> Bool?
func favorite(index: Int)
func unfavorite(index: Int)
}

class MarketListWatchViewModel<Service: IMarketListService, Decorator: IMarketListDecorator>: MarketListViewModel<Service, Decorator> {
private let disposeBag = DisposeBag()

private let watchlistToggleService: MarketWatchlistToggleService

private let favoriteRelay = PublishRelay<()>()
private let unfavoriteRelay = PublishRelay<()>()
private let failRelay = PublishRelay<String>()

init(service: Service, watchlistToggleService: MarketWatchlistToggleService, decorator: Decorator) {
self.watchlistToggleService = watchlistToggleService

super.init(service: service, decorator: decorator)

subscribe(disposeBag, watchlistToggleService.statusObservable) { [weak self] in self?.handle(status: $0) }
}

private func handle(status: MarketWatchlistToggleService.State) {
switch status {
case .favorite:
favoriteRelay.accept(())
case .unfavorite:
unfavoriteRelay.accept(())
case .fail:
failRelay.accept("watch_coin.fail_to_find_uuid")
}
}

}

extension MarketListWatchViewModel: IMarketListWatchViewModel {

var favoriteDriver: Driver<()> {
favoriteRelay.asDriver(onErrorJustReturn: ())
}

var unfavoriteDriver: Driver<()> {
unfavoriteRelay.asDriver(onErrorJustReturn: ())
}

var failDriver: Driver<String> {
failRelay.asDriver(onErrorJustReturn: ())
}

func isFavorite(index: Int) -> Bool? {
watchlistToggleService.isFavorite(index: index)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import RxSwift

class MarketWatchlistToggleService {
private let coinUidService: IMarketListCoinUidService
private let favoritesManager: FavoritesManager

private let statusSubject = PublishSubject<State>()

init(coinUidService: IMarketListCoinUidService, favoritesManager: FavoritesManager) {
self.coinUidService = coinUidService
self.favoritesManager = favoritesManager
Expand All @@ -11,6 +15,10 @@ class MarketWatchlistToggleService {

extension MarketWatchlistToggleService {

var statusObservable: Observable<State> {
statusSubject.asObservable()
}

func isFavorite(index: Int) -> Bool? {
guard let coinUid = coinUidService.coinUid(index: index) else {
return nil
Expand All @@ -21,18 +29,34 @@ extension MarketWatchlistToggleService {

func favorite(index: Int) {
guard let coinUid = coinUidService.coinUid(index: index) else {
statusSubject.onNext(.fail)
return
}

favoritesManager.add(coinUid: coinUid)

statusSubject.onNext(.favorite)
}

func unfavorite(index: Int) {
guard let coinUid = coinUidService.coinUid(index: index) else {
statusSubject.onNext(.fail)
return
}

favoritesManager.remove(coinUid: coinUid)

statusSubject.onNext(.unfavorite)
}

}

extension MarketWatchlistToggleService {

enum State {
case favorite
case unfavorite
case fail
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension HudHelper {
case addedToWatchlist
case removedFromWatchlist
case addedToWallet
case alreadyAddedToWatchlist
case alreadyAddedToWallet
case notSupportedYet
case copied
case saved
Expand All @@ -36,7 +36,7 @@ extension HudHelper {
case .addedToWatchlist: image = UIImage(named: "star_24")
case .removedFromWatchlist: image = UIImage(named: "star_off_24")
case .addedToWallet: image = UIImage(named: "add_to_wallet_2_24")
case .alreadyAddedToWatchlist: image = UIImage(named: "warning_2_24")
case .alreadyAddedToWallet: image = UIImage(named: "warning_2_24")
case .notSupportedYet: image = UIImage(named: "warning_2_24")
case .copied: image = UIImage(named: "copy_24")
case .saved: image = UIImage(named: "download_24")
Expand All @@ -58,7 +58,7 @@ extension HudHelper {

var color: UIColor {
switch self {
case .addedToWatchlist, .alreadyAddedToWatchlist, .notSupportedYet, .sent, .swapped, .approved, .attention: return .themeJacob
case .addedToWatchlist, .alreadyAddedToWallet, .notSupportedYet, .sent, .swapped, .approved, .attention: return .themeJacob
case .removedFromWatchlist, .deleted, .noInternet, .disconnectedWalletConnect, .error: return .themeLucian
case .addedToWallet, .copied, .saved, .success, .created, .enabled: return .themeRemus
case .disconnectingWalletConnect, .enabling, .sending, .swapping, .approving: return .themeGray
Expand All @@ -70,7 +70,7 @@ extension HudHelper {
case .addedToWatchlist: return "alert.added_to_watchlist".localized
case .removedFromWatchlist: return "alert.removed_from_watchlist".localized
case .addedToWallet: return "alert.added_to_wallet".localized
case .alreadyAddedToWatchlist: return "alert.already_added_to_wallet".localized
case .alreadyAddedToWallet: return "alert.already_added_to_wallet".localized
case .notSupportedYet: return "alert.not_supported_yet".localized
case .copied: return "alert.copied".localized
case .saved: return "alert.saved".localized
Expand Down

0 comments on commit 17a847c

Please sign in to comment.