Skip to content

Commit

Permalink
Use alternative image url if needed for coins in widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
esen committed Jun 20, 2024
1 parent 06b61a6 commit 9f863d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
6 changes: 4 additions & 2 deletions UnstoppableWallet/Widget/Misc/ApiProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ApiProvider {
func topCoins(limit: Int) async throws -> [Coin] {
let parameters: Parameters = [
"limit": limit,
"fields": "uid,name,code",
"fields": "uid,name,code,image",
"order_by_rank": "true",
]

Expand All @@ -48,7 +48,7 @@ class ApiProvider {
func coinWithPrice(uid: String, currencyCode: String) async throws -> Coin {
let parameters: Parameters = [
"uids": uid,
"fields": "uid,name,code,price,price_change_24h,price_change_1d",
"fields": "uid,name,code,price,price_change_24h,price_change_1d,image",
"currency": currencyCode.lowercased(),
]

Expand Down Expand Up @@ -102,6 +102,7 @@ struct Coin: ImmutableMappable {
let priceChange1w: Decimal?
let priceChange1m: Decimal?
let priceChange3m: Decimal?
let imageUrl: String?

init(map: Map) throws {
uid = try map.value("uid")
Expand All @@ -115,6 +116,7 @@ struct Coin: ImmutableMappable {
priceChange1w = try? map.value("price_change_1w", using: Transform.stringToDecimalTransform)
priceChange1m = try? map.value("price_change_1m", using: Transform.stringToDecimalTransform)
priceChange3m = try? map.value("price_change_3m", using: Transform.stringToDecimalTransform)
imageUrl = try? map.value("image")
}
}

Expand Down
22 changes: 15 additions & 7 deletions UnstoppableWallet/Widget/Misc/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import SwiftUI

extension Coin {
var image: Image? {
let iconUrl = "https://cdn.blocksdecoded.com/coin-icons/32px/\(uid)@3x.png"

guard let url = URL(string: iconUrl) else { return nil }
guard let data = try? Data(contentsOf: url) else { return nil }
guard let uiImage = UIImage(data: data) else { return nil }

return Image(uiImage: uiImage)
do {
let iconUrl = "https://cdn.blocksdecoded.com/coin-icons/32px/\(uid)@3x.png"
return try image(url: iconUrl)
} catch {
guard let alternativeUrl = imageUrl else { return nil }
return try? image(url: alternativeUrl)
}
}

func formattedPrice(currency: Currency) -> String {
Expand All @@ -27,6 +27,14 @@ extension Coin {
return priceChange >= 0 ? .up : .down
}

private func image(url: String) throws -> Image? {
guard let url = URL(string: url) else { return nil }
let data = try Data(contentsOf: url)

guard let uiImage = UIImage(data: data) else { return nil }
return Image(uiImage: uiImage)
}

private func priceChange(timePeriod: WatchlistTimePeriod) -> Decimal? {
switch timePeriod {
case .hour24: return priceChange24h
Expand Down

0 comments on commit 9f863d9

Please sign in to comment.