Skip to content

Commit 4612f24

Browse files
committed
[Feature] Add a dynamic List to display downloaded coins
1 parent ec45939 commit 4612f24

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// HomeViewModel.swift
3+
// Crypto
4+
//
5+
// Created by Jethro Liu on 2025/04/19.
6+
//
7+
8+
import Foundation
9+
10+
@Observable class HomeViewModel {
11+
var allCoins: [CoinModel] = []
12+
var portfolioCoins: [CoinModel] = []
13+
14+
init() {
15+
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
16+
self.allCoins.append(DeveloperPreview.instance.coin)
17+
self.portfolioCoins.append(DeveloperPreview.instance.coin)
18+
}
19+
}
20+
21+
}

Crypto/Crypto/Core/Home/Views/HomeView.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import SwiftUI
99

1010
struct HomeView: View {
1111

12+
@Environment(HomeViewModel.self) var vm
1213
@State private var showPortfolio: Bool = false
1314

1415
var body: some View {
@@ -18,6 +19,18 @@ struct HomeView: View {
1819

1920
VStack {
2021
homeHeader
22+
23+
columnTitles
24+
25+
if !showPortfolio {
26+
allCoinsList
27+
.transition(.move(edge: .leading))
28+
}
29+
if showPortfolio {
30+
portfolioCoinsList
31+
.transition(.move(edge: .trailing))
32+
}
33+
2134
Spacer(minLength: 0)
2235
}
2336
}
@@ -29,6 +42,7 @@ struct HomeView: View {
2942
HomeView()
3043
.toolbarVisibility(.hidden)
3144
}
45+
.environment(dev.homeVM)
3246
}
3347

3448
extension HomeView {
@@ -56,4 +70,39 @@ extension HomeView {
5670
}
5771
.padding(.horizontal)
5872
}
73+
74+
private var allCoinsList: some View {
75+
List {
76+
ForEach(vm.allCoins) { coin in
77+
CoinRowView(coin: coin, showHoldingsColumn: false)
78+
.listRowInsets(.init(top: 10, leading: 0, bottom: 10, trailing: 10))
79+
}
80+
}
81+
.listStyle(.plain)
82+
}
83+
84+
private var portfolioCoinsList: some View {
85+
List {
86+
ForEach(vm.allCoins) { coin in
87+
CoinRowView(coin: coin, showHoldingsColumn: true)
88+
.listRowInsets(.init(top: 10, leading: 0, bottom: 10, trailing: 10))
89+
}
90+
}
91+
.listStyle(.plain)
92+
}
93+
94+
private var columnTitles: some View {
95+
HStack {
96+
Text("Coin")
97+
Spacer()
98+
if showPortfolio {
99+
Text("Holdings")
100+
}
101+
Text("Price")
102+
.frame(width: UIScreen.main.bounds.width / 3.5, alignment: .trailing)
103+
}
104+
.font(.caption)
105+
.foregroundStyle(Color.theme.secondaryText)
106+
.padding(.horizontal)
107+
}
59108
}

Crypto/Crypto/CryptoApp.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import SwiftUI
99

1010
@main
1111
struct CryptoApp: App {
12+
13+
@State private var vm = HomeViewModel()
14+
1215
var body: some Scene {
1316
WindowGroup {
1417
NavigationStack {
1518
HomeView()
1619
.toolbarVisibility(.hidden)
1720
}
21+
.environment(vm)
1822
}
1923
}
2024
}

Crypto/Crypto/Extensions/PreviewProvider.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class DeveloperPreview {
2525
static let instance = DeveloperPreview()
2626
private init() { }
2727

28+
let homeVM = HomeViewModel()
29+
2830
let coin = CoinModel(
2931
id: "bitcoin",
3032
symbol: "btc",

0 commit comments

Comments
 (0)