Skip to content

Use SKIE Observing to collect state flows in SwiftUI #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Fruitties/iosApp/iosApp/CartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import shared
struct CartView : View {
let mainViewModel: MainViewModel

// The ViewModel exposes a StateFlow that we access in SwiftUI with SKIE Observing.
// https://skie.touchlab.co/features/flows-in-swiftui

@State
private var expanded = false

Expand Down Expand Up @@ -54,7 +57,8 @@ struct CartDetailsView: View {
let mainViewModel: MainViewModel

var body: some View {


// https://skie.touchlab.co/features/flows-in-swiftui
Observing(self.mainViewModel.cartUiState) { cartUIState in
VStack {
ForEach(cartUIState.cartDetails, id: \.fruittie.id) { item in
Expand Down
27 changes: 11 additions & 16 deletions Fruitties/iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,22 @@ import Foundation
struct ContentView: View {
var mainViewModel: MainViewModel

// The ViewModel exposes a StateFlow.
Comment on lines 23 to -24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It would be helpful to add a comment explaining why the @State property homeUIState was removed. This provides context for the change and clarifies the motivation behind using Observing instead.

// We collect() the StateFlow into State, which can be used in SwiftUI.
// https://skie.touchlab.co/features/flows-in-swiftui
@State
var homeUIState: HomeUiState = HomeUiState(fruitties: [])

var body: some View {
Text("Fruitties").font(.largeTitle).fontWeight(.bold)
CartView(mainViewModel: mainViewModel)
ScrollView {
LazyVStack {
ForEach(homeUIState.fruitties, id: \.self) { value in
FruittieView(fruittie: value, addToCart: { fruittie in
Task {
self.mainViewModel.addItemToCart(fruittie: fruittie)
}
})
// https://skie.touchlab.co/features/flows-in-swiftui
Observing(self.mainViewModel.homeUiState) { homeUIState in
ScrollView {
LazyVStack {
ForEach(homeUIState.fruitties, id: \.self) { value in
FruittieView(fruittie: value, addToCart: { fruittie in
Task {
self.mainViewModel.addItemToCart(fruittie: fruittie)
}
})
}
}
}
// https://skie.touchlab.co/features/flows-in-swiftui
.collect(flow: self.mainViewModel.homeUiState, into: $homeUIState)
}
}
}
Expand Down
Loading