-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add comparison between compose state and stateflow
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
initScreenData/app/src/main/java/io/github/aniokrait/androidtoybox/ui/screen/SampleScreen.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,8 @@ | ||
package io.github.aniokrait.androidtoybox.ui.screen | ||
|
||
import androidx.compose.runtime.Composable | ||
|
||
@Composable | ||
fun SampleScreen() { | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...nData/app/src/main/java/io/github/aniokrait/androidtoybox/ui/viewmodel/SampleViewModel.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 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 = "取得終了" | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
stateComparison/app/src/main/java/io/github/aniokrait/androidtoybox/MainActivity.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,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") | ||
} | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...parison/app/src/main/java/io/github/aniokrait/androidtoybox/ui/viewmodel/MainViewModel.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,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 | ||
} | ||
} |