Skip to content

Commit

Permalink
Merge pull request #968 from WalletConnect/feature/wcm-recent-wallets
Browse files Browse the repository at this point in the history
[WCM] Store recent wallets in UserDefaults
  • Loading branch information
radeknovis authored Jul 20, 2023
2 parents 8dbc1d1 + aedb0eb commit 41187ff
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
68 changes: 65 additions & 3 deletions Sources/WalletConnectModal/Modal/ModalViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ final class ModalViewModel: ObservableObject {
}

var filteredWallets: [Listing] {
if searchTerm.isEmpty { return wallets }
if searchTerm.isEmpty { return sortByRecent(wallets) }

return wallets.filter { $0.name.lowercased().contains(searchTerm.lowercased()) }
return sortByRecent(
wallets.filter { $0.name.lowercased().contains(searchTerm.lowercased()) }
)
}

private var disposeBag = Set<AnyCancellable>()
Expand Down Expand Up @@ -120,6 +122,8 @@ final class ModalViewModel: ObservableObject {
}

func onListingTap(_ listing: Listing) {
setLastTimeUsed(listing.id)

navigateToDeepLink(
universalLink: listing.mobile.universal ?? "",
nativeLink: listing.mobile.native ?? ""
Expand All @@ -146,7 +150,6 @@ final class ModalViewModel: ObservableObject {

func onCopyButton() {


guard let uri else {
toast = Toast(style: .error, message: "No uri found")
return
Expand Down Expand Up @@ -190,13 +193,72 @@ final class ModalViewModel: ObservableObject {

return lhs < rhs
}

loadRecentWallets()
}
} catch {
toast = Toast(style: .error, message: error.localizedDescription)
}
}
}

// MARK: - Recent Wallets

private extension ModalViewModel {

func sortByRecent(_ input: [Listing]) -> [Listing] {
input.sorted { lhs, rhs in
guard let lhsLastTimeUsed = lhs.lastTimeUsed else {
return false
}

guard let rhsLastTimeUsed = rhs.lastTimeUsed else {
return true
}

return lhsLastTimeUsed > rhsLastTimeUsed
}
}

func loadRecentWallets() {
RecentWalletsStorage().recentWallets.forEach { wallet in

guard let lastTimeUsed = wallet.lastTimeUsed else {
return
}

// Consider Recent only for 3 days
if abs(lastTimeUsed.timeIntervalSinceNow) > (24 * 60 * 60 * 3) {
return
}

setLastTimeUsed(wallet.id, date: lastTimeUsed)
}
}

func saveRecentWallets() {
RecentWalletsStorage().recentWallets = Array(wallets.filter {
$0.lastTimeUsed != nil
}.prefix(5))
}

func setLastTimeUsed(_ walletId: String, date: Date = Date()) {
guard let index = wallets.firstIndex(where: {
$0.id == walletId
}) else {
return
}

var copy = wallets[index]
copy.lastTimeUsed = date
wallets[index] = copy

saveRecentWallets()
}
}

// MARK: - Deeplinking

private extension ModalViewModel {
enum DeeplinkErrors: LocalizedError {
case noWalletLinkFound
Expand Down
31 changes: 31 additions & 0 deletions Sources/WalletConnectModal/Modal/RecentWalletStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation

final class RecentWalletsStorage {
private let defaults: UserDefaults

init(defaults: UserDefaults = .standard) {
self.defaults = defaults
}

var recentWallets: [Listing] {
get {
guard
let data = defaults.data(forKey: "recentWallets"),
let wallets = try? JSONDecoder().decode([Listing].self, from: data)
else {
return []
}

return wallets
}
set {
guard
let walletsData = try? JSONEncoder().encode(newValue)
else {
return
}

defaults.set(walletsData, forKey: "recentWallets")
}
}
}
2 changes: 1 addition & 1 deletion Sources/WalletConnectModal/Modal/Screens/WalletList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct WalletList: View {
.minimumScaleFactor(0.4)

Text("RECENT")
.opacity(0)
.opacity(wallet.lastTimeUsed != nil ? 1 : 0)
.font(.system(size: 10))
.foregroundColor(.foreground3)
.padding(.horizontal, 12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Listing: Codable, Hashable, Identifiable {
let imageId: String
let app: App
let mobile: Mobile
var lastTimeUsed: Date?

private enum CodingKeys: String, CodingKey {
case id
Expand All @@ -21,6 +22,7 @@ struct Listing: Codable, Hashable, Identifiable {
case imageId = "image_id"
case app
case mobile
case lastTimeUsed
}

struct App: Codable, Hashable {
Expand Down

0 comments on commit 41187ff

Please sign in to comment.