Skip to content
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

Add ViewModel logic #12

Merged
merged 4 commits into from
Jul 28, 2022
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
#
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ android.namespace = "io.github.droidkaigi.confsched2022"
dependencies {
implementation(projects.featureHome)
implementation(projects.coreUi)

implementation(libs.hiltNavigationCompose)
}
Binary file added build-logic/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions build-logic/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package io.github.droidkaigi.confsched2022

import kotlinx.collections.immutable.ImmutableList

class SessionGroup(val sessions: ImmutableList<Session>)
class Sessions(val sessions: ImmutableList<Session>)

This file was deleted.

1 change: 1 addition & 0 deletions feature-home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
implementation(projects.coreUi)
implementation(projects.coreModel)

implementation(libs.hiltNavigationCompose)
implementation(libs.androidxCoreKtx)
implementation(libs.composeUi)
implementation(libs.composeMaterial)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package io.github.droidkaigi.confsched2022

import androidx.compose.foundation.clickable
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import io.github.droidkaigi.confsched2022.ui.theme.DroidKaigi2022Theme

@Composable
fun Sessions() {
Text(text = "Hello!")
val hiltViewModel = hiltViewModel<SessionsViewModel>()
val state by hiltViewModel.state
Text(
text = "Hello! $state",
modifier = Modifier.clickable {
hiltViewModel.onToggle()
}
)
}

@Preview(showBackground = true)
@Composable
fun SessionsPreview() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.github.droidkaigi.confsched2022

class SessionsUiModel(
val sessionListState: SessionListState
data class SessionsUiModel(
val sessionsState: SessionsState,
val isFilterOn: Boolean
) {
sealed interface SessionListState {
class Loaded() : SessionListState
object Loading : SessionListState
sealed interface SessionsState {
class Loaded(sessions: Sessions) : SessionsState
object Loading : SessionsState
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionClock
import app.cash.molecule.RecompositionClock.ContextClock
import app.cash.molecule.launchMolecule
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.droidkaigi.confsched2022.SessionsUiModel.SessionListState
import io.github.droidkaigi.confsched2022.SessionsUiModel.SessionsState
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope

@HiltViewModel
class SessionsViewModel @Inject constructor() : ViewModel() {
val filter = mutableStateOf(false)
val state = viewModelScope.moleculeComposeState {
SessionsUiModel(SessionListState.Loading)

private val moleculeScope =
CoroutineScope(viewModelScope.coroutineContext + AndroidUiDispatcher.Main)

val state = moleculeScope.moleculeComposeState(clock = ContextClock) {
SessionsUiModel(SessionsState.Loading, isFilterOn = filter.value)
}

fun onToggle() {
println("toggle")
filter.value = !filter.value
}
}

fun <T> CoroutineScope.moleculeComposeState(
clock: RecompositionClock,
body: @Composable () -> T,
): State<T> {
var mutableState: MutableState<T>? = null

launchMolecule<T>(
RecompositionClock.ContextClock,
launchMolecule(
clock,
emitter = { value ->
val outputFlow = mutableState
if (outputFlow != null) {
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ androidDesugarJdkLibs = "1.2.0"
compose = "1.2.0"
composeCompiler = "1.2.0"
androidxLifecycle = "2.5.0"
androidxActivity = "1.5.0"
androidxActivity = "1.6.0-alpha05"
hiltNavigationCompose = "1.0.0"
junit = "4.13.2"
androidxTestExt = "1.1.3"
androidxTestEspresso = "3.4.0"
Expand Down Expand Up @@ -41,6 +42,7 @@ kotlinxCollectionsImmutable = { module = "org.jetbrains.kotlinx:kotlinx-collecti
androidxCoreKtx = { module = "androidx.core:core-ktx", version.ref = "androidxCore" }
androidxLifecycleLifecycleRuntimeKtx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "androidxLifecycle" }
androidxActivityActivityCompose = { module = "androidx.activity:activity-compose", version.ref = "androidxActivity" }
hiltNavigationCompose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
junit = { module = "junit:junit", version.ref = "junit" }
androidxTestExtJunit = { module = "androidx.test.ext:junit", version.ref = "androidxTestExt" }
androidxTestEspressoEspressoCore = { module = "androidx.test.espresso:espresso-core", version.ref = "androidxTestEspresso" }
Expand Down