@@ -20,23 +20,12 @@ import android.os.Bundle
2020import androidx.activity.ComponentActivity
2121import androidx.activity.compose.setContent
2222import androidx.activity.viewModels
23- import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
2423import androidx.compose.runtime.Composable
25- import androidx.compose.runtime.collectAsState
24+ import androidx.compose.runtime.getValue
2625import androidx.compose.ui.tooling.preview.Preview
27- import androidx.lifecycle.flowWithLifecycle
28- import androidx.lifecycle.lifecycleScope
29- import androidx.window.core.layout.WindowWidthSizeClass
30- import androidx.window.layout.FoldingFeature
31- import androidx.window.layout.WindowInfoTracker
26+ import androidx.lifecycle.compose.collectAsStateWithLifecycle
3227import com.example.reply.data.local.LocalEmailsDataProvider
3328import com.example.reply.ui.theme.ReplyTheme
34- import com.example.reply.ui.utils.DevicePosture
35- import com.example.reply.ui.utils.isBookPosture
36- import com.example.reply.ui.utils.isSeparating
37- import kotlinx.coroutines.flow.SharingStarted
38- import kotlinx.coroutines.flow.map
39- import kotlinx.coroutines.flow.stateIn
4029
4130class MainActivity : ComponentActivity () {
4231
@@ -45,42 +34,12 @@ class MainActivity : ComponentActivity() {
4534 override fun onCreate (savedInstanceState : Bundle ? ) {
4635 super .onCreate(savedInstanceState)
4736
48- /* *
49- * Flow of [DevicePosture] that emits every time there's a change in the windowLayoutInfo
50- */
51- val devicePostureFlow = WindowInfoTracker .getOrCreate(this ).windowLayoutInfo(this )
52- .flowWithLifecycle(this .lifecycle)
53- .map { layoutInfo ->
54- val foldingFeature =
55- layoutInfo.displayFeatures
56- .filterIsInstance<FoldingFeature >()
57- .firstOrNull()
58- when {
59- isBookPosture(foldingFeature) ->
60- DevicePosture .BookPosture (foldingFeature.bounds)
61-
62- isSeparating(foldingFeature) ->
63- DevicePosture .Separating (foldingFeature.bounds, foldingFeature.orientation)
64-
65- else -> DevicePosture .NormalPosture
66- }
67- }
68- .stateIn(
69- scope = lifecycleScope,
70- started = SharingStarted .Eagerly ,
71- initialValue = DevicePosture .NormalPosture
72- )
73-
7437 setContent {
7538 ReplyTheme {
76- val windowAdaptiveInfo = currentWindowAdaptiveInfo()
77- val devicePosture = devicePostureFlow.collectAsState().value
78- val uiState = viewModel.uiState.collectAsState().value
39+ val uiState by viewModel.uiState.collectAsStateWithLifecycle()
7940 ReplyApp (
80- windowAdaptiveInfo.windowSizeClass.windowWidthSizeClass,
81- devicePosture,
82- uiState,
83- viewModel::setSelectedEmail
41+ replyHomeUIState = uiState,
42+ onEmailClick = viewModel::setSelectedEmail
8443 )
8544 }
8645 }
@@ -93,8 +52,6 @@ fun ReplyAppPreview() {
9352 ReplyTheme {
9453 ReplyApp (
9554 replyHomeUIState = ReplyHomeUIState (emails = LocalEmailsDataProvider .allEmails),
96- windowSize = WindowWidthSizeClass .COMPACT ,
97- foldingDevicePosture = DevicePosture .NormalPosture ,
9855 onEmailClick = {}
9956 )
10057 }
@@ -106,8 +63,6 @@ fun ReplyAppPreviewTablet() {
10663 ReplyTheme {
10764 ReplyApp (
10865 replyHomeUIState = ReplyHomeUIState (emails = LocalEmailsDataProvider .allEmails),
109- windowSize = WindowWidthSizeClass .MEDIUM ,
110- foldingDevicePosture = DevicePosture .NormalPosture ,
11166 onEmailClick = {}
11267 )
11368 }
@@ -119,8 +74,6 @@ fun ReplyAppPreviewDesktop() {
11974 ReplyTheme {
12075 ReplyApp (
12176 replyHomeUIState = ReplyHomeUIState (emails = LocalEmailsDataProvider .allEmails),
122- windowSize = WindowWidthSizeClass .EXPANDED ,
123- foldingDevicePosture = DevicePosture .NormalPosture ,
12477 onEmailClick = {}
12578 )
12679 }
0 commit comments