-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace CFlows with StateFlows using SKIE
- Loading branch information
1 parent
e0941f7
commit a4faf32
Showing
24 changed files
with
267 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
androidApp/src/main/java/com/mirego/kmp/boilerplate/Greeting.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
androidApp/src/main/java/com/mirego/kmp/boilerplate/viewmodels/LifecycleViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.mirego.kmp.boilerplate.viewmodels | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
|
||
|
||
/** | ||
* Convenience viewModel builder which creates the common ViewModel using the provided initializer | ||
* and wraps it into an androidx.lifecycle.ViewModel() for proper cancellation. | ||
*/ | ||
@Composable | ||
inline fun <reified VM : ViewModel> lifecycleViewModel(crossinline initializer: () -> VM): VM { | ||
val factory = viewModelFactory { | ||
initializer { | ||
LifecycleViewModel(vm = initializer()) | ||
} | ||
} | ||
return viewModel<LifecycleViewModel<VM>>(factory = factory).vm | ||
} | ||
|
||
/** | ||
* Wraps our common ViewModel into an androidx.lifecycle.ViewModel() to cancel work when cleared. | ||
*/ | ||
class LifecycleViewModel<VM : ViewModel>(val vm: VM) : androidx.lifecycle.ViewModel() { | ||
override fun onCleared() = vm.cancel() | ||
} |
26 changes: 26 additions & 0 deletions
26
androidApp/src/main/java/com/mirego/kmp/boilerplate/views/ExampleView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.mirego.kmp.boilerplate.views | ||
|
||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import com.mirego.kmp.boilerplate.previews.PreviewContext | ||
import com.mirego.kmp.boilerplate.viewmodels.example.ExampleViewModel | ||
|
||
@Composable | ||
fun ExampleView(viewModel: ExampleViewModel) { | ||
val state: ExampleViewModel.State by viewModel.state.collectAsStateWithLifecycle() | ||
|
||
Text(text = state.greeting) | ||
} | ||
|
||
@Preview(showSystemUi = true) | ||
@Composable | ||
fun PreviewGreeting() { | ||
PreviewContext { viewModelFactory -> | ||
ExampleView( | ||
viewModel = viewModelFactory.example() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Shared | ||
import SwiftUI | ||
|
||
struct ExampleView: View { | ||
|
||
private let viewModel: ExampleViewModel | ||
|
||
@State private var state: ExampleViewModelState | ||
|
||
init(viewModel: ExampleViewModel) { | ||
self.viewModel = viewModel | ||
self.state = viewModel.state.value | ||
} | ||
|
||
var body: some View { | ||
VStack { | ||
Text(state.greeting) | ||
} | ||
.task { | ||
await withTaskCancellationHandler { | ||
for await state in viewModel.state { | ||
self.state = state | ||
} | ||
} onCancel: { | ||
viewModel.cancel() | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
PreviewContext { viewModelFactory in | ||
ExampleView( | ||
viewModel: viewModelFactory.example() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import Shared | ||
import SwiftUI | ||
|
||
@main | ||
struct IOSApp: App { | ||
let viewModelFactory = ViewModelFactory() | ||
|
||
var body: some Scene { | ||
WindowGroup { | ||
GreetingView() | ||
ExampleView( | ||
viewModel: viewModelFactory.example() | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.