Skip to content

Commit 32bbdaf

Browse files
committed
Refactor edit task UI code to use domain model instead of DTO (#909)
1 parent 85345a7 commit 32bbdaf

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

features/tasks/src/main/kotlin/com/edricchan/studybuddy/features/tasks/edit/ui/compat/EditTaskFragment.kt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ import androidx.lifecycle.lifecycleScope
1414
import com.edricchan.studybuddy.exts.android.showToast
1515
import com.edricchan.studybuddy.exts.datetime.format
1616
import com.edricchan.studybuddy.exts.datetime.toLocalDateTime
17-
import com.edricchan.studybuddy.exts.firebase.toLocalDateTime
1817
import com.edricchan.studybuddy.exts.firebase.toTimestamp
1918
import com.edricchan.studybuddy.exts.material.picker.setCalendarConstraints
2019
import com.edricchan.studybuddy.exts.material.picker.setSelection
2120
import com.edricchan.studybuddy.exts.material.picker.setStart
2221
import com.edricchan.studybuddy.exts.material.picker.showMaterialDatePicker
2322
import com.edricchan.studybuddy.exts.material.textfield.editTextStrValue
2423
import com.edricchan.studybuddy.features.tasks.R
25-
import com.edricchan.studybuddy.features.tasks.data.model.TodoItem
2624
import com.edricchan.studybuddy.features.tasks.databinding.FragEditTaskBinding
25+
import com.edricchan.studybuddy.features.tasks.domain.model.TaskItem
2726
import com.edricchan.studybuddy.features.tasks.edit.vm.EditTaskViewModel
2827
import com.edricchan.studybuddy.features.tasks.edit.vm.EditTaskViewModel.TaskState
2928
import com.edricchan.studybuddy.ui.common.fragment.ViewBindingFragment
@@ -48,7 +47,7 @@ class EditTaskFragment : ViewBindingFragment<FragEditTaskBinding>(FragEditTaskBi
4847
lateinit var auth: FirebaseAuth
4948

5049
private var taskInstant: Instant? = null
51-
private lateinit var todoItem: TodoItem
50+
private lateinit var taskItem: TaskItem
5251

5352
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
5453
super.onViewCreated(view, savedInstanceState)
@@ -126,12 +125,12 @@ class EditTaskFragment : ViewBindingFragment<FragEditTaskBinding>(FragEditTaskBi
126125
is TaskState.Success -> binding.apply {
127126
progressBar.isVisible = false
128127
scrollView.isVisible = true
129-
todoItem = state.item
130-
with(todoItem) {
131-
title?.let { textInputTitle.editText?.setText(it) }
128+
taskItem = state.item
129+
with(taskItem) {
130+
textInputTitle.editText?.setText(title)
132131
content?.let { textInputContent.editText?.setText(it) }
133-
done?.let { checkboxMarkAsDone.isChecked = it }
134-
taskInstant = dueDate?.toInstant()
132+
checkboxMarkAsDone.isChecked = isCompleted
133+
taskInstant = dueDate
135134
dueDate?.let {
136135
// We need to convert it to a LocalDateTime as Instants don't support
137136
// temporal units bigger than days - see the `Instant#isSupported` Javadocs
@@ -161,27 +160,27 @@ class EditTaskFragment : ViewBindingFragment<FragEditTaskBinding>(FragEditTaskBi
161160

162161
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
163162
if (menuItem.itemId == R.id.action_save) {
164-
val taskItemUpdates = buildMap<TodoItem.Field, Any> {
163+
val taskItemUpdates = buildMap<TaskItem.Field, Any> {
165164
binding.also {
166-
if (it.textInputTitle.editTextStrValue != todoItem.title) {
167-
this[TodoItem.Field.Title] = it.textInputTitle.editTextStrValue
165+
if (it.textInputTitle.editTextStrValue != taskItem.title) {
166+
this[TaskItem.Field.Title] = it.textInputTitle.editTextStrValue
168167
}
169-
if (it.textInputContent.editTextStrValue != todoItem.content) {
170-
this[TodoItem.Field.Content] = it.textInputContent.editTextStrValue
168+
if (it.textInputContent.editTextStrValue != taskItem.content) {
169+
this[TaskItem.Field.Content] = it.textInputContent.editTextStrValue
171170
}
172-
this[TodoItem.Field.IsDone] = it.checkboxMarkAsDone.isChecked
173-
this[TodoItem.Field.Tags] = it.textInputTags.editTextStrValue.split(
171+
this[TaskItem.Field.IsCompleted] = it.checkboxMarkAsDone.isChecked
172+
this[TaskItem.Field.Tags] = it.textInputTags.editTextStrValue.split(
174173
Regex("""\s*,\s*""")
175174
).filter(String::isNotBlank)
176175
}
177176
taskInstant?.let {
178-
if (todoItem.dueDate?.toInstant() != it) {
179-
this[TodoItem.Field.DueDate] = it.toTimestamp()
177+
if (taskItem.dueDate != it) {
178+
this[TaskItem.Field.DueDate] = it.toTimestamp()
180179
}
181180
// When taskInstant is set to null, this means that the user
182181
// wants to clear the due-date of the item
183182
} ?: run {
184-
this[TodoItem.Field.DueDate] = FieldValue.delete()
183+
this[TaskItem.Field.DueDate] = FieldValue.delete()
185184
}
186185
}
187186

features/tasks/src/main/kotlin/com/edricchan/studybuddy/features/tasks/edit/vm/EditTaskViewModel.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import androidx.lifecycle.ViewModel
55
import androidx.lifecycle.viewModelScope
66
import androidx.navigation.toRoute
77
import com.edricchan.studybuddy.core.compat.navigation.CompatDestination
8-
import com.edricchan.studybuddy.features.tasks.data.model.TodoItem
98
import com.edricchan.studybuddy.features.tasks.data.repo.TaskRepository
10-
import com.edricchan.studybuddy.features.tasks.data.repo.update
9+
import com.edricchan.studybuddy.features.tasks.domain.model.TaskItem
1110
import dagger.hilt.android.lifecycle.HiltViewModel
1211
import kotlinx.coroutines.flow.SharingStarted
1312
import kotlinx.coroutines.flow.map
@@ -22,13 +21,13 @@ class EditTaskViewModel @Inject constructor(
2221
) : ViewModel() {
2322
sealed interface TaskState {
2423
data object Loading : TaskState
25-
data class Success(val item: TodoItem) : TaskState
24+
data class Success(val item: TaskItem) : TaskState
2625
data object DoesNotExist : TaskState
2726
}
2827

2928
val taskId = savedStateHandle.toRoute<CompatDestination.Task.Edit>().taskId
3029

31-
val taskDetail = repo.observeTask(taskId)
30+
val taskDetail = repo.observeTaskById(taskId)
3231
.map {
3332
if (it == null) return@map TaskState.DoesNotExist
3433
TaskState.Success(it)
@@ -39,14 +38,12 @@ class EditTaskViewModel @Inject constructor(
3938
)
4039

4140
fun updateTask(
42-
data: Map<TodoItem.Field, Any>,
41+
data: Map<TaskItem.Field, Any>,
4342
onSuccess: () -> Unit,
4443
onFailure: (Throwable) -> Unit
4544
) {
4645
viewModelScope.launch {
47-
repo.runCatching {
48-
repo.update(taskId, data)
49-
}
46+
repo.runCatching { updateTask(taskId, data) }
5047
.onSuccess { onSuccess() }
5148
.onFailure(onFailure)
5249
}

0 commit comments

Comments
 (0)