Skip to content

Commit

Permalink
add comparison between compose state and stateflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Aniokrait committed Jul 30, 2023
1 parent 3f1236f commit fe38b66
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.aniokrait.androidtoybox.ui.screen

import androidx.compose.runtime.Composable

@Composable
fun SampleScreen() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.aniokrait.androidtoybox.ui.viewmodel

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@HiltViewModel
class SampleViewModel: ViewModel() {
private val _state: MutableState<String> = mutableStateOf("")
val state: State<String> = _state
fun superLongAsyncMethod() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
delay(10000000000)
}
_state.value = "取得終了"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.aniokrait.androidtoybox

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import io.github.aniokrait.androidtoybox.ui.viewmodel.MainViewModel

class MainActivity: ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val vm: MainViewModel = viewModel()
val composeCount = vm.composeCount
val flowCount by vm.flowCount.collectAsState()

Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = { vm.generateNewComposeCount() }) {
Text("Compose Stateのカウント: $composeCount")
}

Button(onClick = { vm.generateNewFlowCount() }) {
Text("flowCountのカウント: $flowCount")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.github.aniokrait.androidtoybox.ui.viewmodel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlin.random.Random

class MainViewModel constructor(
private val savedStateHandle: SavedStateHandle
): ViewModel() {
//StateFlowの宣言
// private val _flowCount = MutableStateFlow(0)
// val flowCount = _flowCount.asStateFlow()
//StateFlowの宣言(savedStateHandleを使う場合)
val flowCount = savedStateHandle.getStateFlow("count", 0)

//Compose Stateの宣言
var composeCount by mutableStateOf(
// 0
//savedStateHandleを使う場合
savedStateHandle.get<Int>("count") ?: 0
)
private set

fun generateNewComposeCount() {
val newCount = Random.nextInt(1000)
//savedStateHandleを使う場合
savedStateHandle["count"] = newCount
//savedStateHandleを使う場合もCompose Stateへの代入は必要
composeCount = newCount
}

fun generateNewFlowCount() {
val newCount = Random.nextInt(1000)
// _flowCount.value = newCount
//savedStateHandleを使う場合
savedStateHandle["count"] = newCount
}
}

0 comments on commit fe38b66

Please sign in to comment.