Skip to content

Commit 7599813

Browse files
committed
apply checking status
1 parent b172664 commit 7599813

File tree

6 files changed

+110
-7
lines changed

6 files changed

+110
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
package io.github.mrtry.todolist.app.todo.viewmodel
22

3+
import androidx.lifecycle.LifecycleOwner
4+
import androidx.lifecycle.MutableLiveData
5+
import io.github.mrtry.todolist.R
6+
import io.github.mrtry.todolist.app.todo.ui.navigator.ToDoNavigator
7+
import io.github.mrtry.todolist.misc.extension.observeNonNull
8+
import io.github.mrtry.todolist.misc.extension.requireValue
9+
import io.github.mrtry.todolist.todo.domainservice.ToDoDomainService
310
import io.github.mrtry.todolist.todo.entity.ToDo
11+
import kotlinx.coroutines.CancellationException
12+
import kotlinx.coroutines.CoroutineScope
13+
import kotlinx.coroutines.launch
14+
import timber.log.Timber
415

5-
data class ToDoListItemViewModel(
6-
val task: ToDo
7-
)
16+
class ToDoListItemViewModel(
17+
todo: ToDo,
18+
lifecycleOwner: LifecycleOwner,
19+
private val navigator: ToDoNavigator,
20+
private val domainService: ToDoDomainService,
21+
private val coroutineScope: CoroutineScope
22+
) {
23+
val todo: MutableLiveData<ToDo> = MutableLiveData(todo)
24+
val isComplete: MutableLiveData<Boolean> = MutableLiveData(todo.isComplete)
25+
val isSaving: MutableLiveData<Boolean> = MutableLiveData(false)
26+
27+
private var currentIsCompleteState: Boolean = isComplete.requireValue()
28+
29+
init {
30+
isComplete.observeNonNull(lifecycleOwner) {
31+
this.todo.value = this.todo.requireValue().copy(isComplete = it)
32+
}
33+
34+
this.todo.observeNonNull(lifecycleOwner) {
35+
if (it.isComplete == currentIsCompleteState) return@observeNonNull
36+
37+
coroutineScope.launch {
38+
isSaving.value = true
39+
40+
try {
41+
domainService.save(it)
42+
currentIsCompleteState = isComplete.requireValue()
43+
} catch (e: Exception) {
44+
if (e is CancellationException) return@launch
45+
46+
Timber.e(e)
47+
navigator.showSnackBar(R.string.to_do_activity_error_update_task_failed)
48+
isComplete.value = currentIsCompleteState
49+
} finally {
50+
isSaving.value = false
51+
}
52+
}
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
package io.github.mrtry.todolist.app.todo.viewmodel.converter
22

3+
import androidx.lifecycle.LifecycleOwner
4+
import io.github.mrtry.todolist.app.todo.ui.navigator.ToDoNavigator
35
import io.github.mrtry.todolist.app.todo.viewmodel.ToDoListItemViewModel
46
import io.github.mrtry.todolist.di.scope.ActivityScope
7+
import io.github.mrtry.todolist.todo.domainservice.ToDoDomainService
58
import io.github.mrtry.todolist.todo.entity.ToDo
9+
import kotlinx.coroutines.CoroutineScope
610
import javax.inject.Inject
711

812
@ActivityScope
913
class ToDoListItemViewModelConverter
10-
@Inject constructor() {
14+
@Inject constructor(
15+
private val lifecycleOwner: LifecycleOwner,
16+
private val navigator: ToDoNavigator,
17+
private val domainService: ToDoDomainService,
18+
private val coroutineScope: CoroutineScope
19+
) {
1120
fun convert(entity: ToDo): ToDoListItemViewModel =
1221
ToDoListItemViewModel(
13-
entity
22+
entity,
23+
lifecycleOwner,
24+
navigator,
25+
domainService,
26+
coroutineScope
1427
)
1528
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.mrtry.todolist.misc.extension
2+
3+
import androidx.lifecycle.LifecycleOwner
4+
import androidx.lifecycle.LiveData
5+
import androidx.lifecycle.Observer
6+
7+
fun <T> LiveData<T>.observeNonNull(owner: LifecycleOwner, observer: (T) -> Unit) {
8+
this.observe(owner, Observer {
9+
if (it != null) {
10+
observer(it)
11+
}
12+
})
13+
}
14+
15+
fun <T> LiveData<T>.requireValue(): T {
16+
value ?: throw IllegalStateException("Value has not been set yet.")
17+
return value!!
18+
}

app/src/main/res/layout/list_item_to_do.xml

+23-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<data>
66

7+
<import type="android.view.View" />
8+
79
<variable
810
name="viewModel"
911
type="io.github.mrtry.todolist.app.todo.viewmodel.ToDoListItemViewModel" />
@@ -20,7 +22,9 @@
2022
android:layout_marginStart="@dimen/list_item_check_box_margin_start"
2123
app:layout_constraintStart_toStartOf="parent"
2224
app:layout_constraintTop_toTopOf="parent"
23-
app:layout_constraintBottom_toBottomOf="parent" />
25+
app:layout_constraintBottom_toBottomOf="parent"
26+
android:enabled="@{!safeUnbox(viewModel.isSaving())}"
27+
android:checked="@={viewModel.isComplete}" />
2428

2529
<TextView
2630
android:id="@+id/title"
@@ -31,7 +35,7 @@
3135
app:layout_constraintEnd_toEndOf="parent"
3236
app:layout_constraintTop_toTopOf="parent"
3337
app:layout_constraintBottom_toBottomOf="parent"
34-
android:text="@{viewModel.task.title}"
38+
android:text="@{viewModel.todo.title}"
3539
android:ellipsize="end"
3640
android:maxLines="1" />
3741

@@ -41,5 +45,22 @@
4145
android:layout_height="@dimen/list_item_divider_height"
4246
app:layout_constraintBottom_toBottomOf="parent"
4347
android:background="?android:attr/dividerVertical" />
48+
49+
<View
50+
android:id="@+id/progress_background"
51+
android:layout_width="match_parent"
52+
android:layout_height="match_parent"
53+
android:background="@color/disabled_view_backgroud"
54+
android:visibility="@{safeUnbox(viewModel.isSaving()) ? View.VISIBLE :View.GONE}" />
55+
56+
<ProgressBar
57+
android:layout_width="@dimen/banner_add_task_progress_container"
58+
android:layout_height="@dimen/banner_add_task_progress_container"
59+
app:layout_constraintTop_toTopOf="@id/progress_background"
60+
app:layout_constraintBottom_toBottomOf="@id/progress_background"
61+
app:layout_constraintStart_toStartOf="@id/progress_background"
62+
app:layout_constraintEnd_toEndOf="@id/progress_background"
63+
android:visibility="@{safeUnbox(viewModel.isSaving()) ? View.VISIBLE :View.GONE}" />
64+
4465
</androidx.constraintlayout.widget.ConstraintLayout>
4566
</layout>

app/src/main/res/values/colors.xml

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
<color name="colorPrimary">#6200EE</color>
44
<color name="colorPrimaryDark">#3700B3</color>
55
<color name="colorAccent">#03DAC5</color>
6+
7+
<color name="disabled_view_backgroud">#26000000</color>
68
</resources>

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<string name="app_name">ToDoList</string>
33
<string name="to_do_activity_empty_state_text">タスクがまだ登録されていません</string>
44
<string name="to_do_activity_error_add_task_failed">登録に失敗しました。再度お試しください。</string>
5+
<string name="to_do_activity_error_update_task_failed">更新に失敗しました。再度お試しください。</string>
56
<string name="to_do_activity_error_get_task_failed">取得に失敗しました。再度お試しください。</string>
67
</resources>

0 commit comments

Comments
 (0)