Skip to content

Commit

Permalink
Market watchlist updates
Browse files Browse the repository at this point in the history
- replace name field with market cap value
- adopt widgets for new Watchlist manager
  • Loading branch information
ealymbaev committed May 20, 2024
1 parent 4d52c7c commit cf95eff
Show file tree
Hide file tree
Showing 57 changed files with 1,109 additions and 983 deletions.
110 changes: 64 additions & 46 deletions UnstoppableWallet/UnstoppableWallet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions UnstoppableWallet/UnstoppableWallet/Core/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class App {
let networkManager: NetworkManager
let guidesManager: GuidesManager
let termsManager: TermsManager
let favoritesManager: FavoritesManager
let watchlistManager: WatchlistManager
let contactManager: ContactBookManager
let subscriptionManager: SubscriptionManager

Expand Down Expand Up @@ -160,8 +160,7 @@ class App {
guidesManager = GuidesManager(networkManager: networkManager)
termsManager = TermsManager(userDefaultsStorage: userDefaultsStorage)

let favoriteCoinRecordStorage = FavoriteCoinRecordStorage(dbPool: dbPool)
favoritesManager = FavoritesManager(storage: favoriteCoinRecordStorage, sharedStorage: sharedLocalStorage)
watchlistManager = WatchlistManager(storage: sharedLocalStorage)

contactManager = ContactBookManager(localStorage: localStorage, ubiquityContainerIdentifier: AppConfig.privateCloudContainer, helper: ContactBookHelper(), logger: logger)
subscriptionManager = SubscriptionManager(userDefaultsStorage: userDefaultsStorage, marketKit: marketKit)
Expand Down Expand Up @@ -281,7 +280,7 @@ class App {
accountManager: accountManager,
accountFactory: accountFactory,
walletManager: walletManager,
favoritesManager: favoritesManager,
watchlistManager: watchlistManager,
evmSyncSourceManager: evmSyncSourceManager,
btcBlockchainManager: btcBlockchainManager,
restoreSettingsManager: restoreSettingsManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Combine
import WidgetKit

class WatchlistManager {
private let keyCoinUids = "watchlist-coin-uids"
private let keySortBy = "watchlist-sort-by"
private let keyTimePeriod = "watchlist-time-period"
private let keyShowSignals = "watchlist-show-signals"

private let storage: SharedLocalStorage

private let coinUidsSubject = PassthroughSubject<[String], Never>()

var coinUids: [String] {
didSet {
coinUidSet = Set(coinUids)
coinUidsSubject.send(coinUids)

storage.set(value: coinUids, for: keyCoinUids)

WidgetCenter.shared.reloadTimelines(ofKind: AppWidgetConstants.watchlistWidgetKind)
}
}

private var coinUidSet: Set<String>

var sortBy: WatchlistSortBy {
didSet {
storage.set(value: sortBy.rawValue, for: keySortBy)
WidgetCenter.shared.reloadTimelines(ofKind: AppWidgetConstants.watchlistWidgetKind)
}
}

var timePeriod: WatchlistTimePeriod {
didSet {
storage.set(value: timePeriod.rawValue, for: keyTimePeriod)
WidgetCenter.shared.reloadTimelines(ofKind: AppWidgetConstants.watchlistWidgetKind)
}
}

var showSignals: Bool {
didSet {
storage.set(value: showSignals, for: keyShowSignals)
// WidgetCenter.shared.reloadTimelines(ofKind: AppWidgetConstants.watchlistWidgetKind)
}
}

init(storage: SharedLocalStorage) {
self.storage = storage

coinUids = storage.value(for: keyCoinUids) ?? []
coinUidSet = Set(coinUids)

let sortByRaw: String? = storage.value(for: keySortBy)
sortBy = sortByRaw.flatMap { WatchlistSortBy(rawValue: $0) } ?? .manual

let timePeriodRaw: String? = storage.value(for: keyTimePeriod)
timePeriod = timePeriodRaw.flatMap { WatchlistTimePeriod(rawValue: $0) } ?? .day1

showSignals = storage.value(for: keyShowSignals) ?? true

WidgetCenter.shared.reloadTimelines(ofKind: AppWidgetConstants.watchlistWidgetKind)
}
}

extension WatchlistManager {
var coinUidsPublisher: AnyPublisher<[String], Never> {
coinUidsSubject.eraseToAnyPublisher()
}

func add(coinUid: String) {
guard !coinUids.contains(coinUid) else {
return
}

coinUids.append(coinUid)
}

func add(coinUids: [String]) {
let coinUids = coinUids.filter { !self.coinUids.contains($0) }
self.coinUids.append(contentsOf: coinUids)
}

func removeAll() {
coinUids = []
}

func remove(coinUid: String) {
guard let index = coinUids.firstIndex(of: coinUid) else {
return
}

coinUids.remove(at: index)
}

func isWatched(coinUid: String) -> Bool {
coinUidSet.contains(coinUid)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,6 @@ enum StorageMigrator {
try db.drop(table: CoinRecord_v19.databaseTableName)
}

migrator.registerMigration("recreateFavoriteCoins") { db in
if try db.tableExists("favorite_coins") {
try db.drop(table: "favorite_coins")
}

try db.create(table: "favorite_coins_v20") { t in
t.column("coinType", .text).notNull()
}
}

migrator.registerMigration("createActiveAccount") { db in
try db.create(table: ActiveAccount_v_0_36.databaseTableName) { t in
t.column(ActiveAccount_v_0_36.Columns.uniqueId.name, .text).notNull()
Expand Down Expand Up @@ -512,12 +502,6 @@ enum StorageMigrator {
}
}

migrator.registerMigration("newStructureForFavoriteCoins") { db in
try db.create(table: FavoriteCoinRecord.databaseTableName) { t in
t.column(FavoriteCoinRecord.Columns.coinUid.name, .text).primaryKey()
}
}

migrator.registerMigration("createWalletConnectV2Sessions") { db in
try db.create(table: WalletConnectSession.databaseTableName) { t in
t.column(WalletConnectSession.Columns.accountId.name, .text).notNull()
Expand Down Expand Up @@ -823,6 +807,20 @@ enum StorageMigrator {
}
}

migrator.registerMigration("Migrate watchlist coin uids to local storage") { db in
if try db.tableExists(FavoriteCoinRecord_v_0_38.databaseTableName) {
let records = try FavoriteCoinRecord_v_0_38.fetchAll(db)
let coinUids = Array(Set(records.map(\.coinUid)))

if !coinUids.isEmpty {
let sharedLocalStorage = SharedLocalStorage()
sharedLocalStorage.set(value: coinUids.sorted(), for: "watchlist-coin-uids")
}

try db.drop(table: FavoriteCoinRecord_v_0_38.databaseTableName)
}
}

try migrator.migrate(dbPool)
}

Expand Down
12 changes: 6 additions & 6 deletions UnstoppableWallet/UnstoppableWallet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>OneInchCommissionAddress</key>
<string>${one_inch_commission_address}</string>
<key>OneInchCommission</key>
<string>${one_inch_commission}</string>
<key>ArbiscanApiKey</key>
<string>${arbiscan_api_key}</string>
<key>BscscanApiKey</key>
Expand Down Expand Up @@ -113,6 +109,12 @@
</array>
<key>OfficeMode</key>
<string>${OfficeMode}</string>
<key>OneInchApiKey</key>
<string>${one_inch_api_key}</string>
<key>OneInchCommission</key>
<string>${one_inch_commission}</string>
<key>OneInchCommissionAddress</key>
<string>${one_inch_commission_address}</string>
<key>OpenSeaApiKey</key>
<string>${open_sea_api_key}</string>
<key>OptimismEtherscanApiKey</key>
Expand Down Expand Up @@ -162,7 +164,5 @@
<string>${unstoppable_domains_api_key}</string>
<key>WallectConnectV2ProjectKey</key>
<string>${wallet_connect_v2_project_key}</string>
<key>OneInchApiKey</key>
<string>${one_inch_api_key}</string>
</dict>
</plist>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GRDB

class FavoriteCoinRecord: Record {
class FavoriteCoinRecord_v_0_38: Record {
let coinUid: String

init(coinUid: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum WatchlistSortBy: String, CaseIterable {
case manual
case highestCap
case lowestCap
case gainers
case losers
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum WatchlistTimePeriod: String, CaseIterable {
case day1 = "1d"
case week1 = "1w"
case month1 = "1m"
case month3 = "3m"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AppBackupProvider {
private let accountManager: AccountManager
private let accountFactory: AccountFactory
private let walletManager: WalletManager
private let favoritesManager: FavoritesManager
private let watchlistManager: WatchlistManager
private let evmSyncSourceManager: EvmSyncSourceManager
private let btcBlockchainManager: BtcBlockchainManager
private let restoreSettingsManager: RestoreSettingsManager
Expand All @@ -27,7 +27,7 @@ class AppBackupProvider {
init(accountManager: AccountManager,
accountFactory: AccountFactory,
walletManager: WalletManager,
favoritesManager: FavoritesManager,
watchlistManager: WatchlistManager,
evmSyncSourceManager: EvmSyncSourceManager,
btcBlockchainManager: BtcBlockchainManager,
restoreSettingsManager: RestoreSettingsManager,
Expand All @@ -46,7 +46,7 @@ class AppBackupProvider {
self.accountManager = accountManager
self.accountFactory = accountFactory
self.walletManager = walletManager
self.favoritesManager = favoritesManager
self.watchlistManager = watchlistManager
self.evmSyncSourceManager = evmSyncSourceManager
self.btcBlockchainManager = btcBlockchainManager
self.restoreSettingsManager = restoreSettingsManager
Expand Down Expand Up @@ -125,7 +125,7 @@ class AppBackupProvider {
let syncSources = EvmSyncSourceManager.SyncSourceBackup(selected: selected, custom: [])
return RawFullBackup(
accounts: accounts,
watchlistIds: Array(favoritesManager.coinUids),
watchlistIds: watchlistManager.coinUids,
contacts: contactManager.backupContactBook?.contacts ?? [],
settings: settings(evmSyncSources: syncSources),
customSyncSources: custom
Expand Down Expand Up @@ -186,7 +186,7 @@ extension AppBackupProvider {
for wallet in raw.accounts {
restore(raws: [wallet])
}
favoritesManager.add(coinUids: raw.watchlistIds)
watchlistManager.add(coinUids: raw.watchlistIds)

if !raw.contacts.isEmpty {
try? contactManager.restore(contacts: raw.contacts, mergePolitics: .replace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UIKit

enum CoinPageModule {
static func view(fullCoin: FullCoin) -> some View {
let viewModel = CoinPageViewModelNew(fullCoin: fullCoin, favoritesManager: App.shared.favoritesManager)
let viewModel = CoinPageViewModelNew(fullCoin: fullCoin, watchlistManager: App.shared.watchlistManager)

let overviewView = CoinOverviewModule.view(coinUid: fullCoin.coin.uid)
let analyticsView = CoinAnalyticsModule.view(fullCoin: fullCoin)
Expand All @@ -26,7 +26,7 @@ enum CoinPageModule {

let service = CoinPageService(
fullCoin: fullCoin,
favoritesManager: App.shared.favoritesManager
watchlistManager: App.shared.watchlistManager
)

let viewModel = CoinPageViewModel(service: service)
Expand Down
Loading

0 comments on commit cf95eff

Please sign in to comment.