Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.moscow.tudee.data.service

import android.util.Log
import com.moscow.tudee.data.local.dao.CategoryDao
import com.moscow.tudee.data.local.dao.TaskDao
import com.moscow.tudee.data.local.mapper.toCategory
Expand Down Expand Up @@ -55,7 +54,10 @@ class TasksServicesImpl(
return tasks.toTasksBySingleCategory(category)
}

override suspend fun getTasksByCategoryAndStatus(categoryId: Long, status: Task.Status): List<Task> {
override suspend fun getTasksByCategoryAndStatus(
categoryId: Long,
status: Task.Status
): List<Task> {
val tasks = taskDao.getTasksByCategoryAndStatus(categoryId, status.name)
val category = categoryDao.getCategoryById(categoryId)
?: throw IllegalStateException("No Category for id=$categoryId")
Expand All @@ -72,7 +74,7 @@ class TasksServicesImpl(
return taskEntity.toTask(domainCategory)
}

override suspend fun changeTaskStatus(taskId: Long,updatedStatus: Task.Status) {
override suspend fun changeTaskStatus(taskId: Long, updatedStatus: Task.Status) {
taskDao.updateTaskStatus(taskId, updatedStatus.name)
}

Expand All @@ -84,7 +86,17 @@ class TasksServicesImpl(
}

override suspend fun updateTask(task: Task) {
task.id?.let { taskId ->
taskDao.getTaskById(taskId)?.let { oldTask ->
categoryDao.getCategoryById(oldTask.categoryId)?.let { oldCategory ->
categoryDao.decrementTaskCount(oldCategory.id)
}
}
}

taskDao.updateTask(task.toTaskEntity())

categoryDao.incrementTaskCount(task.category.id)
}

override suspend fun deleteTask(taskId: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.repeatOnLifecycle
import coil.compose.rememberAsyncImagePainter
import com.moscow.tudee.R
import com.moscow.tudee.presentation.base.ObserveAsEvent
Expand All @@ -46,6 +49,13 @@ fun CategoryScreen(
viewModel.onReturnedFromEditWithMessage(messageId)
}

val lifecycleOwner = LocalLifecycleOwner.current
LaunchedEffect(lifecycleOwner.lifecycle) {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
viewModel.getAllCategories()
}
}

val uiState by viewModel.uiState.collectAsStateWithLifecycle()
ObserveAsEvent(viewModel.uiEvent) { event ->
when (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CategoryViewModel(
}
}

private fun getAllCategories() {
fun getAllCategories() {
launchWithResult(
action = { categoryServices.getCategories() },
onSuccess = ::onGetAllCategoriesSuccess,
Expand Down
Loading