Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
deletion/restoration feature added.
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsmanmeet committed Dec 11, 2023
1 parent add4a2b commit 3a9064f
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fun ActionDialogBox(
if(isDialogShowing.value){
AlertDialog(
onDismissRequest = {
onDismissClick()
isDialogShowing.value = false
},
confirmButton = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fun addTodoDialog(
},
confirmButton = {
Button(onClick = {
if (enteredText1.isNotEmpty() && descriptionText.isNotEmpty()){
if (enteredText1.isNotEmpty()){
val todo = Todo(
ID = null,
title = enteredText1,
Expand Down Expand Up @@ -216,7 +216,7 @@ fun addTodoDialog(
isRepeating = false
}
else{
Toast.makeText(context,"Fill in all fields",Toast.LENGTH_SHORT).show()
Toast.makeText(context,"Task name is required.",Toast.LENGTH_SHORT).show()
}
}
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.thatsmanmeet.taskyapp.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import com.thatsmanmeet.taskyapp.room.Todo
import com.thatsmanmeet.taskyapp.room.TodoViewModel
import com.thatsmanmeet.taskyapp.room.deletedtodo.DeletedTodo
import com.thatsmanmeet.taskyapp.room.deletedtodo.DeletedTodoViewModel


@Composable
fun DeletedTodoItem(
deletedTodo: DeletedTodo,
todoViewModel: TodoViewModel,
deletedTodoViewModel: DeletedTodoViewModel,
modifier: Modifier = Modifier
) {
val isShowing = remember {
mutableStateOf(false)
}
Row(
modifier = modifier
.fillMaxWidth()
.padding(10.dp)
.heightIn(60.dp)
.clip(RoundedCornerShape(10.dp))
.clickable {
isShowing.value = true
}
.background(MaterialTheme.colorScheme.inverseOnSurface),
verticalAlignment = Alignment.CenterVertically
) {
Box(modifier = modifier.fillMaxWidth().padding(10.dp)){
Text(
text = deletedTodo.title!!,
fontSize = 16.sp,
textDecoration = if(deletedTodo.isCompleted) TextDecoration.LineThrough else TextDecoration.None
)
}
}
if(isShowing.value){
ActionDialogBox(
isDialogShowing = isShowing,
title = "Choose Action",
message = "Do you want to restore or delete this task?",
confirmButtonText = "Restore" ,
dismissButtonText = "Delete",
onConfirmClick = {
todoViewModel.insertTodo(Todo(
ID = deletedTodo.ID,
title = deletedTodo.title,
todoDescription = deletedTodo.todoDescription,
isCompleted = deletedTodo.isCompleted,
date = deletedTodo.date,
time = deletedTodo.time,
notificationID = deletedTodo.notificationID,
isRecurring = deletedTodo.isRecurring
))
deletedTodoViewModel.deleteDeletedTodo(deletedTodo)
},
onDismissClick = {
deletedTodoViewModel.deleteDeletedTodo(deletedTodo)
},

)
}
}
73 changes: 42 additions & 31 deletions app/src/main/java/com/thatsmanmeet/taskyapp/components/TaskList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SwipeToDismiss
import androidx.compose.material3.rememberDismissState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.movableContentOf
import androidx.compose.runtime.mutableStateOf
Expand All @@ -40,6 +41,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.thatsmanmeet.taskyapp.room.Todo
import com.thatsmanmeet.taskyapp.room.TodoViewModel
import com.thatsmanmeet.taskyapp.room.deletedtodo.DeletedTodo
import com.thatsmanmeet.taskyapp.room.deletedtodo.DeletedTodoViewModel
import com.thatsmanmeet.taskyapp.screens.CurrentDateTimeComparator
import com.thatsmanmeet.taskyapp.screens.cancelNotification
import com.thatsmanmeet.taskyapp.screens.scheduleNotification
Expand All @@ -56,6 +59,7 @@ fun TaskList(
state: LazyListState,
list : List<Todo>,
todoViewModel: TodoViewModel,
deletedTodoViewModel: DeletedTodoViewModel,
onClick : (Int) -> Unit,
searchText: String,
coroutineScope: CoroutineScope,
Expand Down Expand Up @@ -96,39 +100,37 @@ fun TaskList(

if(dismissState.isDismissed(direction = DismissDirection.EndToStart)){
isSwipeDeleteDialogShowing.value = true
ActionDialogBox(
isDialogShowing = isSwipeDeleteDialogShowing,
title = "Delete Task?",
message = "Do you want to delete this task?",
confirmButtonText = "Delete",
dismissButtonText = "Cancel",
onConfirmClick = {
todoViewModel.deleteTodo(currentItem)
},
onDismissClick = {
isSwipeDeleteDialogShowing.value = false
coroutineScope.launch {
dismissState.reset()
}
},
confirmButtonColor = Color(0xFFF75F5F),
confirmButtonContentColor = Color.White
)
// LaunchedEffect(Unit){
// val result = snackbarHostState.showSnackbar(
// message = "Task will be deleted soon!",
// actionLabel = "Undo",
// duration = SnackbarDuration.Short
// )
// when(result){
// SnackbarResult.Dismissed -> {
// todoViewModel.deleteTodo(currentItem)
// }
// SnackbarResult.ActionPerformed -> {
deletedTodoViewModel.insertDeletedTodo(DeletedTodo(
ID = currentItem.ID,
title = currentItem.title,
todoDescription = currentItem.todoDescription,
isCompleted = currentItem.isCompleted,
date = currentItem.date,
time = currentItem.time,
isRecurring = currentItem.isRecurring,
notificationID = currentItem.notificationID,
todoDeletionDate = getDate30DaysLater(currentItem.date!!)
))
todoViewModel.deleteTodo(currentItem)
cancelNotification(context,currentItem)
// ActionDialogBox(
// isDialogShowing = isSwipeDeleteDialogShowing,
// title = "Delete Task?",
// message = "Do you want to delete this task?",
// confirmButtonText = "Delete",
// dismissButtonText = "Cancel",
// onConfirmClick = {
// todoViewModel.deleteTodo(currentItem)
// },
// onDismissClick = {
// isSwipeDeleteDialogShowing.value = false
// coroutineScope.launch {
// dismissState.reset()
// }
// }
// }
// },
// confirmButtonColor = Color(0xFFF75F5F),
// confirmButtonContentColor = Color.White
// )
}

if(dismissState.isDismissed(direction = DismissDirection.StartToEnd)){
Expand Down Expand Up @@ -214,4 +216,13 @@ fun TaskList(
}
}
}
}

fun getDate30DaysLater(enteredDate:String):String{
val sdf = SimpleDateFormat("dd/MM/yyyy",Locale.getDefault())
val date = sdf.parse(enteredDate)
val calendar = Calendar.getInstance()
calendar.time = date!!
calendar.add(Calendar.DAY_OF_MONTH,30)
return sdf.format(calendar.time)
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/thatsmanmeet/taskyapp/screens/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.thatsmanmeet.taskyapp.notification.channelID
import com.thatsmanmeet.taskyapp.receiver.RepeatingTasksReceiver
import com.thatsmanmeet.taskyapp.room.Todo
import com.thatsmanmeet.taskyapp.room.TodoViewModel
import com.thatsmanmeet.taskyapp.room.deletedtodo.DeletedTodoViewModel
import com.thatsmanmeet.taskyapp.ui.theme.TaskyTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand All @@ -54,6 +55,7 @@ fun MyApp(
val activity = LocalContext.current as Activity
val context = LocalContext.current
val todoViewModel = TodoViewModel(activity.application)
val deletedTodoViewModel = DeletedTodoViewModel(activity.application)
val listState = rememberLazyListState()
val selectedItem = rememberSaveable {
mutableIntStateOf(-1)
Expand Down Expand Up @@ -246,6 +248,7 @@ fun MyApp(
state = listState,
list = todoListFromFlow,
todoViewModel = todoViewModel,
deletedTodoViewModel = deletedTodoViewModel,
onClick = {index->
selectedItem.intValue = index
openEditDialog.value = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.thatsmanmeet.taskyapp.screens

import android.app.Activity
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
Expand All @@ -18,7 +20,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
Expand All @@ -30,7 +31,9 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.thatsmanmeet.taskyapp.components.DeletedTodoItem
import com.thatsmanmeet.taskyapp.datastore.SettingsStore
import com.thatsmanmeet.taskyapp.room.TodoViewModel
import com.thatsmanmeet.taskyapp.room.deletedtodo.DeletedTodoViewModel
import com.thatsmanmeet.taskyapp.ui.theme.TaskyTheme

Expand All @@ -45,6 +48,7 @@ fun DeletedTodoScreen(
val settingStore = SettingsStore(context)
val savedThemeKey = settingStore.getThemeModeKey.collectAsState(initial = "")
val deletedTodoViewModel = DeletedTodoViewModel(activity.application)
val todoViewModel = TodoViewModel(activity.application)
val deletedTodoList = deletedTodoViewModel.getAllDeletedTodos.collectAsState(initial = emptyList())

TaskyTheme(darkTheme = when (savedThemeKey.value) {
Expand Down Expand Up @@ -84,13 +88,23 @@ fun DeletedTodoScreen(
) {
Box(
modifier = modifier
.fillMaxWidth()
.height(100.dp)
.padding(16.dp)
.fillMaxWidth()
.height(100.dp)
.padding(16.dp)
){
Text(text = "Deleted Todos will be automatically deleted in 30 days.", fontSize = 15.sp)
}
LazyColumn{
items(deletedTodoList.value){deletedTodo->
DeletedTodoItem(
deletedTodo = deletedTodo,
todoViewModel = todoViewModel,
deletedTodoViewModel = deletedTodoViewModel
)
Spacer(modifier = modifier.height(5.dp))
}

}
}
}
}
Expand Down

0 comments on commit 3a9064f

Please sign in to comment.