Skip to content

Commit 1d0109c

Browse files
committed
wip
1 parent 08ab94c commit 1d0109c

File tree

4 files changed

+48
-121
lines changed

4 files changed

+48
-121
lines changed

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/hoc081098/datastoresample/ui/MainActivity.kt

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import androidx.activity.compose.setContent
55
import androidx.appcompat.app.AppCompatActivity
66
import androidx.compose.material.MaterialTheme
77
import androidx.compose.material.Surface
8+
import androidx.compose.runtime.collectAsState
9+
import androidx.compose.runtime.getValue
10+
import androidx.lifecycle.viewmodel.compose.viewModel
11+
import com.hoc081098.datastoresample.Locator
812
import com.hoc081098.datastoresample.ui.theme.DataStoreSampleTheme
913

1014
class MainActivity : AppCompatActivity() {
@@ -15,7 +19,13 @@ class MainActivity : AppCompatActivity() {
1519
DataStoreSampleTheme(darkTheme = true) {
1620
// A surface container using the 'background' color from the theme
1721
Surface(color = MaterialTheme.colors.background) {
18-
MainScreen()
22+
val viewModel = viewModel<MainViewModel>(factory = Locator.mainViewModelFactory)
23+
val state by viewModel.state.collectAsState()
24+
25+
MainScreen(
26+
state,
27+
viewModel::changeShowCompleted,
28+
)
1929
}
2030
}
2131
}

app/src/main/java/com/hoc081098/datastoresample/ui/MainScreen.kt

+27-97
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,33 @@ import androidx.compose.foundation.layout.Column
66
import androidx.compose.foundation.layout.PaddingValues
77
import androidx.compose.foundation.layout.Row
88
import androidx.compose.foundation.layout.Spacer
9-
import androidx.compose.foundation.layout.fillMaxHeight
109
import androidx.compose.foundation.layout.padding
1110
import androidx.compose.foundation.layout.preferredSize
1211
import androidx.compose.foundation.layout.width
1312
import androidx.compose.foundation.lazy.LazyColumn
1413
import androidx.compose.foundation.lazy.items
1514
import androidx.compose.material.Checkbox
15+
import androidx.compose.material.CircularProgressIndicator
1616
import androidx.compose.material.Divider
1717
import androidx.compose.material.MaterialTheme
1818
import androidx.compose.material.Scaffold
1919
import androidx.compose.material.Text
2020
import androidx.compose.material.TopAppBar
2121
import androidx.compose.runtime.Composable
22-
import androidx.compose.runtime.getValue
2322
import androidx.compose.ui.Modifier
2423
import androidx.compose.ui.res.painterResource
2524
import androidx.compose.ui.tooling.preview.Preview
2625
import androidx.compose.ui.unit.dp
27-
import androidx.lifecycle.viewmodel.compose.viewModel
28-
import com.hoc081098.datastoresample.Locator
2926
import com.hoc081098.datastoresample.R
27+
import com.hoc081098.datastoresample.domain.FilteredSortedTasks
3028
import com.hoc081098.datastoresample.domain.Task
3129
import com.hoc081098.datastoresample.ui.theme.DataStoreSampleTheme
3230

3331
@Composable
34-
fun MainScreen() {
35-
val viewModel = viewModel<MainViewModel>(factory = Locator.mainViewModelFactory)
36-
val state by viewModel.state
37-
32+
fun MainScreen(
33+
state: FilteredSortedTasks?,
34+
changeShowCompleted: (Boolean) -> Unit,
35+
) {
3836
Scaffold(
3937
topBar = {
4038
TopAppBar(
@@ -44,22 +42,26 @@ fun MainScreen() {
4442
)
4543
}
4644
) {
47-
Column {
48-
// MainTasksList(state.tasks)
45+
if (state == null) {
46+
CircularProgressIndicator()
47+
} else {
48+
Column {
49+
MainTasksList(state.tasks)
4950

50-
Row(modifier = Modifier.padding(all = 32.dp).wraps) {
51-
Image(
52-
painter = painterResource(id = R.drawable.ic_baseline_filter_list_24),
53-
contentDescription = null,
54-
modifier = Modifier.preferredSize(24.dp),
55-
)
51+
Row(modifier = Modifier.padding(all = 32.dp)) {
52+
Image(
53+
painter = painterResource(id = R.drawable.ic_baseline_filter_list_24),
54+
contentDescription = null,
55+
modifier = Modifier.preferredSize(24.dp),
56+
)
5657

57-
Spacer(modifier = Modifier.width(16.dp))
58+
Spacer(modifier = Modifier.width(16.dp))
5859

59-
Checkbox(
60-
checked = state.showCompleted,
61-
onCheckedChange = { viewModel.changeShowCompleted(it) }
62-
)
60+
Checkbox(
61+
checked = state.showCompleted,
62+
onCheckedChange = { changeShowCompleted(it) }
63+
)
64+
}
6365
}
6466
}
6567
}
@@ -98,82 +100,10 @@ fun TaskRow(task: Task) {
98100
@Preview
99101
@Composable
100102
fun MainScreenPreview() {
101-
/* val simpleDateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
102-
fun parseDate(date: String? = null): Date = date?.let(simpleDateFormat::parse) ?: Date()
103-
val tasks = listOf(
104-
Task(
105-
name = "Complete graduate project",
106-
deadline = parseDate("25-12-2020"),
107-
priority = TaskPriority.HIGH,
108-
),
109-
Task(
110-
name = "Learning Jetpack Compose",
111-
deadline = parseDate("01-01-2021"),
112-
priority = TaskPriority.MEDIUM,
113-
),
114-
Task(
115-
name = "Learning NestJs",
116-
deadline = parseDate("02-01-2021"),
117-
priority = TaskPriority.LOW,
118-
),
119-
Task(
120-
name = "Learn about Polymer",
121-
deadline = parseDate("10-10-2020"),
122-
priority = TaskPriority.LOW,
123-
),
124-
Task(
125-
name = "Learning Functional programming with Λrrow",
126-
deadline = parseDate("01-01-2022"),
127-
priority = TaskPriority.MEDIUM,
128-
),
129-
Task(
130-
name = "Learning Functional programming with Bow Swift",
131-
deadline = parseDate("01-01-2022"),
132-
priority = TaskPriority.MEDIUM,
133-
),
134-
Task(
135-
name = "Understand how to migrate to DataStore",
136-
deadline = parseDate(),
137-
priority = TaskPriority.HIGH,
138-
),
139-
Task(
140-
name = "Complete graduate project",
141-
deadline = parseDate("25-12-2020"),
142-
priority = TaskPriority.HIGH,
143-
),
144-
Task(
145-
name = "Learning Jetpack Compose",
146-
deadline = parseDate("01-01-2021"),
147-
priority = TaskPriority.MEDIUM,
148-
),
149-
Task(
150-
name = "Learning NestJs",
151-
deadline = parseDate("02-01-2021"),
152-
priority = TaskPriority.LOW,
153-
),
154-
Task(
155-
name = "Learn about Polymer",
156-
deadline = parseDate("10-10-2020"),
157-
priority = TaskPriority.LOW,
158-
),
159-
Task(
160-
name = "Learning Functional programming with Λrrow",
161-
deadline = parseDate("01-01-2022"),
162-
priority = TaskPriority.MEDIUM,
163-
),
164-
Task(
165-
name = "Learning Functional programming with Bow Swift",
166-
deadline = parseDate("01-01-2022"),
167-
priority = TaskPriority.MEDIUM,
168-
),
169-
Task(
170-
name = "Understand how to migrate to DataStore",
171-
deadline = parseDate(),
172-
priority = TaskPriority.HIGH,
173-
)
174-
)*/
175-
176103
DataStoreSampleTheme {
177-
MainScreen()
104+
MainScreen(
105+
null,
106+
{},
107+
)
178108
}
179109
}

app/src/main/java/com/hoc081098/datastoresample/ui/MainViewModel.kt

+9-22
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
package com.hoc081098.datastoresample.ui
22

33
import android.util.Log
4-
import androidx.compose.runtime.State
5-
import androidx.compose.runtime.mutableStateOf
64
import androidx.lifecycle.ViewModel
75
import androidx.lifecycle.ViewModelProvider
86
import androidx.lifecycle.viewModelScope
97
import com.hoc081098.datastoresample.domain.ChangeShowCompleted
108
import com.hoc081098.datastoresample.domain.FilterSortTasks
119
import com.hoc081098.datastoresample.domain.FilteredSortedTasks
12-
import com.hoc081098.datastoresample.domain.SortOrder
13-
import kotlinx.coroutines.flow.catch
14-
import kotlinx.coroutines.flow.launchIn
15-
import kotlinx.coroutines.flow.onEach
10+
import kotlinx.coroutines.flow.SharingStarted
11+
import kotlinx.coroutines.flow.StateFlow
12+
import kotlinx.coroutines.flow.stateIn
1613
import kotlinx.coroutines.launch
1714

1815
class MainViewModel(
1916
filterSortTasks: FilterSortTasks,
2017
private val _changeShowCompleted: ChangeShowCompleted,
2118
) : ViewModel() {
22-
private val _state = mutableStateOf(
23-
FilteredSortedTasks(
24-
tasks = emptyList(),
25-
showCompleted = true,
26-
sortOrder = SortOrder.NONE
19+
val state: StateFlow<FilteredSortedTasks?> = filterSortTasks()
20+
.stateIn(
21+
viewModelScope,
22+
SharingStarted.Eagerly,
23+
null as FilteredSortedTasks?,
2724
)
28-
)
29-
30-
val state: State<FilteredSortedTasks> get() = _state
3125

3226
init {
3327
Log.d("MainViewModel", "$this::init")
34-
35-
filterSortTasks()
36-
.onEach { _state.value = it }
37-
.catch { }
38-
.launchIn(viewModelScope)
3928
}
4029

4130
override fun onCleared() {
@@ -44,9 +33,7 @@ class MainViewModel(
4433
}
4534

4635
fun changeShowCompleted(showCompleted: Boolean) {
47-
viewModelScope.launch {
48-
_changeShowCompleted(showCompleted)
49-
}
36+
viewModelScope.launch { _changeShowCompleted(showCompleted) }
5037
}
5138

5239
class Factory(

0 commit comments

Comments
 (0)