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
Expand Up @@ -94,7 +94,7 @@ private fun NavBarIcon(
) {
val backgroundColor = if (isSelected) Theme.colors.primaryVariant else Theme.colors.surfaceHigh

val tintColor = if (isSelected) Theme.colors.primary else Theme.colors.hint
val tintColor = if (isSelected) Theme.colors.primary else Theme.colors.hint.copy(alpha = 1f)


Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fun MainNavGraph(
navController.previousBackStackEntry
?.savedStateHandle
?.set("result_message", messageId)
navController.popBackStack()

}
, navigateBack ={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.moscow.tudee.presentation.screen.home
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -10,14 +9,18 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
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.compose.ui.zIndex
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.repeatOnLifecycle
import com.moscow.tudee.R
import com.moscow.tudee.domain.entity.Task
import com.moscow.tudee.presentation.component.AddTaskBottomSheet
Expand Down Expand Up @@ -48,6 +51,15 @@ fun HomeScreen(
}
}
}

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

HomeContent(
uiState.value,
viewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.moscow.tudee.presentation.screen.home

import com.moscow.tudee.domain.entity.Task
import com.moscow.tudee.domain.entity.Task.Status
import com.moscow.tudee.domain.service.CategoryServices
Expand Down Expand Up @@ -29,37 +30,56 @@ class HomeViewModel(
}


private fun loadTasks() {
fun loadTasks() {
launchWithResult(
action = { tasksServices.getTasksByDate(Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date).map { it.toTaskUi() } },
onSuccess = ::onSuccessLoadingTasks ,
onError = { handleHomeError(it) },
onStart = { startLoading() },
action = {
tasksServices.getTasksByDate(
Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
).map { it.toTaskUi() }
},
onSuccess = ::onSuccessLoadingTasks,
onError = { handleHomeError(it) },
onStart = { startLoading() },
onFinally = { endLoading() }
)
updateSlider()
}

private fun getTodayDate(){
updateState { it.copy(formattedDate = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toJavaLocalDate().format(DateTimeFormatter.ofPattern("dd MMM yyyy")).toString()) }
private fun getTodayDate() {
updateState {
it.copy(
formattedDate = Clock.System.now()
.toLocalDateTime(TimeZone.currentSystemDefault()).date.toJavaLocalDate()
.format(DateTimeFormatter.ofPattern("dd MMM yyyy")).toString()
)
}
}
private fun onSuccessLoadingTasks(tasks:List<TaskUi>){
val groupedTasks = tasks.groupBy{ it.status }

private fun onSuccessLoadingTasks(tasks: List<TaskUi>) {
val groupedTasks = tasks.groupBy { it.status }

updateState {
it.copy(
todoTasks = groupedTasks[Status.TODO].orEmpty() ,
todoTasks = groupedTasks[Status.TODO].orEmpty(),
doneTasks = groupedTasks[Status.DONE].orEmpty(),
inProgressTasks = groupedTasks[Status.IN_PROGRESS].orEmpty(),
sliderState = updateSliderState( uiState.value.doneTasks.size ,uiState.value.todoTasks.size ,uiState.value.inProgressTasks.size)
sliderState = updateSliderState(
uiState.value.doneTasks.size,
uiState.value.todoTasks.size,
uiState.value.inProgressTasks.size
)

)
}

updateSlider()
}

private fun updateSliderState(doneTasks:Int, todoTasks:Int,inProgressTasks:Int): HomeState.SliderState {
private fun updateSliderState(
doneTasks: Int,
todoTasks: Int,
inProgressTasks: Int
): HomeState.SliderState {
val totalTasks = todoTasks + inProgressTasks + doneTasks
return when {
totalTasks == 0 -> HomeState.SliderState.NOTHING_ON_YOUR_LIST
Expand All @@ -70,18 +90,17 @@ class HomeViewModel(
}

}
private fun getCategories() {
if(uiState.value.categories.isEmpty()) {
launchWithResult(
action = { categoryServices.getCategories() },
onSuccess = { response ->
updateState { it.copy(categories = response.map { it.toCategoryUi() }) }
},
onError = { handleHomeError(it) },
onStart = { startLoading() },
onFinally = { endLoading() }
)
}

fun getCategories() {
launchWithResult(
action = { categoryServices.getCategories() },
onSuccess = { response ->
updateState { it.copy(categories = response.map { it.toCategoryUi() }) }
},
onError = { handleHomeError(it) },
onStart = { startLoading() },
onFinally = { endLoading() }
)
}

override fun onFloatingActionButtonClick() {
Expand Down Expand Up @@ -197,7 +216,7 @@ class HomeViewModel(
private fun getNextStatus(currentStatus: Task.Status): Task.Status {
return when (currentStatus) {
Status.TODO -> Status.IN_PROGRESS
Status.IN_PROGRESS ->Status.DONE
Status.IN_PROGRESS -> Status.DONE
Status.DONE -> Status.DONE
}
}
Expand Down
Loading