Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(task): delete task #56

Merged
merged 8 commits into from
Aug 14, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ google-services.json

# Android Profiling
*.hprof

# iOS
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.loryblu.core.ui.components

import androidx.annotation.StringRes
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp

@Composable
fun LBOutlinedExcludeButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
buttonColors: ButtonColors,
@StringRes textRes: Int
) {
OutlinedButton(
onClick = onClick,
colors = buttonColors,
shape = RoundedCornerShape(10.dp),
border = BorderStroke(
width = 1.dp,
color = MaterialTheme.colorScheme.error
),
modifier = modifier
) {
Text(text = stringResource(id = textRes))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.loryblu.core.ui.components

import androidx.annotation.StringRes
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource

@Composable
fun LBTextButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
buttonColors: ButtonColors,
@StringRes textRes: Int
) {
TextButton(
onClick = onClick,
colors = buttonColors,
modifier = modifier
) {
Text(text = stringResource(id = textRes))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ interface LogbookApi {
taskId: Int
): Flow<ApiResponse>

fun deleteTask(taskId: Int) : Flow<ApiResponse>

suspend fun getUserTasks(): Flow<ApiResponseWithData<List<LogbookTask>>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.loryblu.data.logbook.remote.model.LogbookTaskRequest
import com.loryblu.data.logbook.util.toListOfLogbookTask
import io.ktor.client.HttpClient
import io.ktor.client.request.bearerAuth
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.patch
Expand All @@ -24,7 +25,7 @@ import kotlinx.coroutines.flow.flow
class LogbookApiImpl(
private val client: HttpClient,
private val session: Session
): LogbookApi {
) : LogbookApi {

override suspend fun createTask(logbookTaskRequest: LogbookTaskRequest) = flow {
emit(ApiResponse.Loading)
Expand Down Expand Up @@ -62,11 +63,29 @@ class LogbookApiImpl(
}
}

override fun deleteTask(
taskId: Int
): Flow<ApiResponse> = flow {
emit(ApiResponse.Loading)
try {
emit(
client.delete(HttpRoutes.TASK) {
parameter("taskId", taskId)
parameter("childrenId", session.getChildId().toString())
bearerAuth(session.getToken())
}.toApiResponse()
)
} catch (e: Exception) {
e.printStackTrace()
emit(ApiResponse.ErrorDefault)
}
}

override suspend fun getUserTasks(): Flow<ApiResponseWithData<List<LogbookTask>>> = flow {
val nameOfWeekDays = arrayOf("sun", "mon", "tue", "wed", "thu", "fri", "sat")

emit(ApiResponseWithData.Loading())
try{
try {
emit(
client.get(HttpRoutes.TASK) {
parameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.loryblu.feature.logbook.model.LogbookTaskModel
import com.loryblu.feature.logbook.ui.home.LogbookHomeViewModel
import com.loryblu.feature.logbook.ui.task.LogbookTaskViewModel
import com.loryblu.feature.logbook.ui.task.edit.LogbookEditTaskViewModel
import com.loryblu.feature.logbook.useCases.DeleteTaskUseCase
import com.loryblu.feature.logbook.useCases.DeleteTaskUseCaseImpl
import com.loryblu.feature.logbook.useCases.EditTaskUseCase
import com.loryblu.feature.logbook.useCases.EditTaskUseCaseImpl
import com.loryblu.feature.logbook.useCases.GetUserTaskByDayOfWeek
Expand All @@ -16,11 +18,12 @@ import org.koin.dsl.module

val logbookModule = module {
viewModel { LogbookTaskViewModel(get(), get(), get()) }
viewModel { LogbookHomeViewModel(get())}
viewModel { LogbookHomeViewModel(get(), get(), get())}
viewModel { LogbookEditTaskViewModel(get(), get(), get(), get()) }

single<GetUserTaskByDayOfWeek> { GetUserTaskByDayOfWeekImpl(get()) }
single<GetUserTaskById> { GetUserTaskByIdImpl(get())}
single<EditTaskUseCase> { EditTaskUseCaseImpl(get()) }
single<LogbookTaskModel> { LogbookTaskModel(CategoryItem.Routine, "", "", listOf()) }
single<DeleteTaskUseCase> { DeleteTaskUseCaseImpl(get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fun NavGraphBuilder.logbookNavigation(
val dayOfWeek = data.dayOfWeek.value
viewModel.selectADayOfWeek(dayOfWeek, 3)

val hasUpdateInTaskList = backStack.arguments?.getBoolean("UPDATE_ANIMATION") ?: false
val hasUpdateInTaskList =
backStack.arguments?.getBoolean("UPDATE_ANIMATION") ?: false

if (hasUpdateInTaskList) {
viewModel.selectADayOfWeek(
Expand All @@ -84,6 +85,14 @@ fun NavGraphBuilder.logbookNavigation(
selectADay = { day, shift ->
viewModel.selectADayOfWeek(day, shift)
},
onDeleteTaskConfirmed = { logbookTask, deleteOption, selectedDay, shiftSelected ->
viewModel.deleteTask(
logbookTask = logbookTask,
deleteOption = deleteOption,
dayOfWeekInt = selectedDay,
shift = shiftSelected,
)
},
)
}

Expand Down Expand Up @@ -272,7 +281,7 @@ fun NavGraphBuilder.logbookNavigation(
EditTaskScreen(
taskItems = taskItems,
cardClicked = cardClicked,
onCardClick = { cardClicked = it },
onCardClick = { cardClicked = it },
onBackButtonClicked = { navController.navigateUp() },
onNextScreenClicked = {
viewModel.setSelectedTask(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fun TaskCardComponent(
taskItem: LogbookTask,
parentAccess: Boolean,
onEditTaskClicked: (taskId: Int) -> Unit,
onDeleteTaskClicked: (taskId: Int) -> Unit,
) {
ElevatedCard(
modifier
Expand All @@ -55,7 +56,13 @@ fun TaskCardComponent(
categoryText = stringResource(id = taskItem.itemOfCategory.category.text),
parentAccess = parentAccess
)
TaskContent(modifier = Modifier.height(126.dp), taskItem, parentAccess, onEditTaskClicked)
TaskContent(
modifier = Modifier.height(126.dp),
taskItem = taskItem,
parentAccess = parentAccess,
onEditTask = onEditTaskClicked,
onDeleteTask = onDeleteTaskClicked
)
TaskName(
modifier = Modifier.height(46.dp),
taskName = stringResource(id = taskItem.itemOfCategory.text)
Expand All @@ -79,7 +86,7 @@ fun TitleAndDragButton(modifier: Modifier = Modifier, categoryText: String, pare
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
)
if(parentAccess) {
if (parentAccess) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.End,
Expand All @@ -90,7 +97,10 @@ fun TitleAndDragButton(modifier: Modifier = Modifier, categoryText: String, pare
containerColor = Color.Transparent,
)
) {
Image(painter = painterResource(id = R.drawable.drag_icon), contentDescription = "")
Image(
painter = painterResource(id = R.drawable.drag_icon),
contentDescription = ""
)
}
}
}
Expand All @@ -102,7 +112,8 @@ fun TaskContent(
modifier: Modifier = Modifier,
taskItem: LogbookTask,
parentAccess: Boolean,
onEditTask: (taskId: Int) -> Unit
onEditTask: (taskId: Int) -> Unit,
onDeleteTask: (taskId: Int) -> Unit,
) {
Row(
modifier
Expand Down Expand Up @@ -151,7 +162,7 @@ fun TaskContent(
}
Button(
modifier = Modifier.fillMaxWidth(),
onClick = {},
onClick = { onDeleteTask(taskItem.id) },
colors = ButtonDefaults.buttonColors(
containerColor = LBSoftBlue,
contentColor = Color.Black
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.loryblu.feature.logbook.ui.home

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.loryblu.core.network.di.Session
import com.loryblu.core.network.model.ApiResponse
import com.loryblu.core.network.model.ApiResponseWithData
import com.loryblu.data.logbook.local.ShiftItem
import com.loryblu.data.logbook.remote.model.LogbookTask
import com.loryblu.feature.logbook.ui.task.delete.DeleteOption
import com.loryblu.feature.logbook.ui.task.delete.mutableDialogStateOf
import com.loryblu.feature.logbook.useCases.DeleteTaskUseCase
import com.loryblu.feature.logbook.useCases.GetUserTaskByDayOfWeek
import com.loryblu.feature.logbook.utils.intToDayOfWeek
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class LogbookHomeViewModel(
private val getUserTaskByDayOfWeek: GetUserTaskByDayOfWeek
private val getUserTaskByDayOfWeek: GetUserTaskByDayOfWeek,
private val deleteTaskUseCase: DeleteTaskUseCase,
private val session: Session
) : ViewModel() {

private val _userTasks =
Expand All @@ -23,17 +28,53 @@ class LogbookHomeViewModel(
var lastDayOfWeek = 0
var lastShift = 0

val deletedTaskDialogState = mutableDialogStateOf<Pair<LogbookTask, DeleteOption>?>(null)

fun selectADayOfWeek(dayOfWeekInt: Int, shift: Int, force: Boolean = false) =
viewModelScope.launch {
lastDayOfWeek = dayOfWeekInt
lastShift = shift

getUserTaskByDayOfWeek.invoke(
dayOfWeek = intToDayOfWeek(dayOfWeekInt),
shift = shift,
force = force
).collect {
_userTasks.value = it
forceGetUserTaskByDayOfWeek(
dayOfWeekInt = dayOfWeekInt, shift = shift
)
}

fun deleteTask(
logbookTask: LogbookTask,
deleteOption: DeleteOption,
dayOfWeekInt: Int,
shift: Int,
) = viewModelScope.launch {
deleteTaskUseCase
.invoke(
logbookTask = logbookTask,
deleteOption = deleteOption,
childrenId = session.getChildId()
)
.collect() { response ->
if (response is ApiResponse.Success) {
OdisBy marked this conversation as resolved.
Show resolved Hide resolved
deletedTaskDialogState.showDialog(
Pair(logbookTask, deleteOption)
)
}
}

forceGetUserTaskByDayOfWeek(
dayOfWeekInt = dayOfWeekInt, shift = shift
)
}

private suspend fun forceGetUserTaskByDayOfWeek(
dayOfWeekInt: Int,
shift: Int,
) {
getUserTaskByDayOfWeek.invoke(
dayOfWeek = intToDayOfWeek(dayOfWeekInt),
shift = shift,
force = true
).collect {
_userTasks.value = it
}
}
}
Loading
Loading