코인게코 API를 이용하기 위한 모델
CoinModel.swift
import Foundation
// CoinGecko API info
/*
url: https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=250&page=1&sparkline=true&price_change_percentage=24h
*/
struct CoinModel: Codable, Identifiable {
let id, symbol, name: String
let image: String
let currentPrice: Double
let marketCapRank: Double?
let marketCap, fullyDilutedValuation: Double?
let totalVolume, high24H, low24H: Double?
let priceChange24H, priceChangePercentage24H: Double
let marketCapChange24H,marketCapChangePercentage24H: Double?
let circulatingSupply, totalSupply, maxSupply, ath: Double?
let athChangePercentage: Double?
let athDate: String?
let atl, atlChangePercentage: Double?
let atlDate: String?
let lastUpdated: String?
let sparklineIn7D: SparklineIn7D?
let priceChangePercentage24HInCurrency: Double?
let currentHoldings: Double?
enum CodingKeys: String, CodingKey {
case id, symbol, name, image
case currentPrice = "current_price"
case marketCap = "market_cap"
case marketCapRank = "market_cap_rank"
case fullyDilutedValuation = "fully_diluted_valuation"
case totalVolume = "total_volume"
case high24H = "high_24h"
case low24H = "low_24h"
case priceChange24H = "price_change_24h"
case priceChangePercentage24H = "price_change_percentage_24h"
case marketCapChange24H = "market_cap_change_24h"
case marketCapChangePercentage24H = "market_cap_change_percentage_24h"
case circulatingSupply = "circulating_supply"
case totalSupply = "total_supply"
case maxSupply = "max_supply"
case ath
case athChangePercentage = "ath_change_percentage"
case athDate = "ath_date"
case atl
case atlChangePercentage = "atl_change_percentage"
case atlDate = "atl_date"
case lastUpdated = "last_updated"
case sparklineIn7D = "sparkline_in_7d"
case priceChangePercentage24HInCurrency = "price_change_percentage_24h_in_currency"
case currentHoldings
}
func updateHoldings(amount: Double) -> CoinModel {
return CoinModel(id: id, symbol: symbol, name: name, image: image, currentPrice: currentPrice, marketCapRank: marketCapRank, marketCap: marketCap, fullyDilutedValuation: fullyDilutedValuation, totalVolume: totalVolume, high24H: high24H, low24H: low24H, priceChange24H: priceChange24H, priceChangePercentage24H: priceChangePercentage24H, marketCapChange24H: marketCapChange24H, marketCapChangePercentage24H: marketCapChangePercentage24H, circulatingSupply: circulatingSupply, totalSupply: totalSupply, maxSupply: maxSupply, ath: ath, athChangePercentage: athChangePercentage, athDate: athDate, atl: atl, atlChangePercentage: atlChangePercentage, atlDate: atlDate, lastUpdated: lastUpdated, sparklineIn7D: sparklineIn7D, priceChangePercentage24HInCurrency: priceChangePercentage24HInCurrency, currentHoldings: amount)
}
var currentHoldingsValue: Double {
return (currentHoldings ?? 0) * currentPrice
}
var rank: Int {
return Int(marketCapRank ?? 0)
}
}
struct SparklineIn7D: Codable {
let price: [Double]?
}
updateHodings, currentHodingsValue는 유저 포트폴리오에 사용된다.
'Project > SwiftUI 블록와이드' 카테고리의 다른 글
[SwiftUI Project] 코인 데이터 받아오기 with Combine (0) | 2022.09.13 |
---|---|
[SwiftUI Project] HomeViewModel과 HomeView 연결, UI 작업 (0) | 2022.09.13 |
[SwiftUI Project] CoinRowView와 Double Formatter (0) | 2022.09.12 |
[SwiftUI Project] PreviewProvider Extension (0) | 2022.09.12 |
[SwiftUI Project] 프로젝트 셋업, 컬러세팅, Header View 만들기 (0) | 2022.09.12 |
댓글