Skip to content

Commit

Permalink
Merge pull request #219 from logickoder/todo_list
Browse files Browse the repository at this point in the history
feat(checklist): implemented the checklist
  • Loading branch information
aritra-tech committed Nov 3, 2023
2 parents e0f8f4f + 185af67 commit ddfb57b
Show file tree
Hide file tree
Showing 14 changed files with 380 additions and 76 deletions.
30 changes: 18 additions & 12 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fun AddEditBottomBar(
showDrawingScreen: () -> Unit,
showCameraSheet: () -> Unit,
onReminderDateTime: () -> Unit,
addTodo: () -> Unit,
) {
var showSheet by remember { mutableStateOf(false) }
val sheetState = rememberModalBottomSheetState()
Expand Down Expand Up @@ -145,6 +146,14 @@ fun AddEditBottomBar(
showSheet = false
}
)
BottomSheetOptions(
text = stringResource(R.string.add_todo),
icon = painterResource(id = R.drawable.add_box_icon),
onClick = {
addTodo()
showSheet = false
}
)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ fun AddEditTopBar(
{
if (isNew) {
onBackPress()
} else if (description.isBlank()) {
Toast.makeText(
context,
"Your note cannot be blank",
Toast.LENGTH_SHORT
).show()
} else {
if (description.isBlank()) {
Toast.makeText(
context,
"Your note cannot be blank",
Toast.LENGTH_SHORT
).show()
} else {
saveNote()
}
saveNote()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.aritra.notify.data.converters

import android.net.Uri
import androidx.room.TypeConverter
import com.aritra.notify.domain.models.Todo
import com.aritra.notify.utils.fromJson
import com.aritra.notify.utils.toString
import com.google.gson.Gson

object CollectionConverter {

@TypeConverter
fun fromUriListToString(value: List<Uri?>): String {
return Gson().toJson(value.map { it?.toString() ?: "" })
}

@TypeConverter
fun fromStringToUriList(value: String): List<Uri?> {
return try {
val stringList = Gson().fromJson<List<String>>(value) // using extension function
stringList?.map { Uri.parse(it) } ?: emptyList()
} catch (e: Exception) {
emptyList()
}
}

@TypeConverter
fun fromTodoListToString(value: List<Todo>?): String? {
return Gson().toString(value)
}

@TypeConverter
fun fromStringToTodoList(value: String?): List<Todo>? {
return Gson().fromJson(value)
}
}

This file was deleted.

9 changes: 7 additions & 2 deletions app/src/main/java/com/aritra/notify/data/db/NoteDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.aritra.notify.data.converters.CollectionConverter
import com.aritra.notify.data.converters.DateTypeConverter
import com.aritra.notify.data.converters.ListConverter
import com.aritra.notify.data.converters.LocalDateTimeConverter
import com.aritra.notify.data.converters.UriConverter
import com.aritra.notify.data.dao.NoteDao
Expand All @@ -15,7 +15,12 @@ import com.aritra.notify.domain.models.Note
import com.aritra.notify.domain.models.TrashNote

@Database(entities = [Note::class, TrashNote::class], version = 5)
@TypeConverters(DateTypeConverter::class, UriConverter::class, ListConverter::class, LocalDateTimeConverter::class)
@TypeConverters(
DateTypeConverter::class,
UriConverter::class,
CollectionConverter::class,
LocalDateTimeConverter::class
)
abstract class NoteDatabase : RoomDatabase() {

abstract fun noteDao(): NoteDao
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/aritra/notify/domain/models/Note.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ data class Note(
var note: String = "",
var dateTime: Date? = null,
var image: List<Uri?> = emptyList(),
var checklist: List<Todo> = emptyList(),
@ColumnInfo(defaultValue = "false")
var isMovedToTrash: Boolean = false,
var reminderDateTime: LocalDateTime? = null,
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/aritra/notify/domain/models/Todo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.aritra.notify.domain.models

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* @property title the title of the todo
* @property isChecked the status of the todo
*/
@Parcelize
data class Todo(
val title: String = "",
val isChecked: Boolean = false,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import com.aritra.notify.domain.models.Todo
import com.aritra.notify.ui.screens.notes.homeScreen.NoteScreenViewModel
import java.time.LocalDateTime

Expand All @@ -38,13 +39,14 @@ fun AddEditRoute(
navController.popBackStack()
}
}
val saveNote: (String, String, List<Uri>) -> Unit = remember(note, isNew) {
{ title, description, images ->
val saveNote: (String, String, List<Uri>, List<Todo>) -> Unit = remember(note, isNew) {
{ title, description, images, checklist ->
if (isNew) {
viewModel.insertNote(
title = title,
description = description,
images = images,
checklist = checklist,
onSuccess = {
navigateBack()
Toast.makeText(context, "Successfully Saved!", Toast.LENGTH_SHORT).show()
Expand All @@ -55,12 +57,11 @@ fun AddEditRoute(
title = title,
description = description,
images = images,
checklist = checklist,
onSuccess = { updated ->
navigateBack()
if (updated) {
navigateBack()
Toast.makeText(context, "Successfully Updated!", Toast.LENGTH_SHORT).show()
} else {
navigateBack()
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.aritra.notify.ui.screens.notes.addEditScreen

import android.net.Uri
import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -53,6 +52,7 @@ import com.aritra.notify.components.dialog.DateTimeDialog
import com.aritra.notify.components.dialog.TextDialog
import com.aritra.notify.components.drawing.DrawingScreen
import com.aritra.notify.domain.models.Note
import com.aritra.notify.domain.models.Todo
import com.aritra.notify.ui.theme.NotifyTheme
import com.aritra.notify.utils.formatReminderDateTime
import java.time.LocalDateTime
Expand All @@ -64,7 +64,7 @@ fun AddEditScreen(
isNew: Boolean,
modifier: Modifier = Modifier,
navigateBack: () -> Unit,
saveNote: (String, String, List<Uri>) -> Unit,
saveNote: (String, String, List<Uri>, List<Todo>) -> Unit,
deleteNote: (() -> Unit) -> Unit,
onUpdateReminderDateTime: (LocalDateTime?) -> Unit,
) {
Expand All @@ -76,8 +76,14 @@ fun AddEditScreen(
var description by remember {
mutableStateOf(note.note)
}
var showAddTodo by remember {
mutableStateOf(false)
}
val images = remember {
mutableStateListOf(*note.image.filterNotNull().toTypedArray())
mutableStateListOf<Uri>()
}
val checklist = remember {
mutableStateListOf<Todo>()
}
val cancelDialogState = remember {
mutableStateOf(false)
Expand All @@ -91,19 +97,25 @@ fun AddEditScreen(
var openDrawingScreen by remember {
mutableStateOf(false)
}

var shouldShowDialogDateTime by remember {
mutableStateOf(false)
}

// Makes sure that the title is updated when the note is updated
LaunchedEffect(note.title) {
title = note.title
}

// Makes sure that the description is updated when the note is updated
LaunchedEffect(note.note) {
description = note.note
}
LaunchedEffect(note.image) {
images.clear()
images.addAll(note.image.filterNotNull())
}
LaunchedEffect(note.checklist) {
checklist.clear()
checklist.addAll(note.checklist.sortedBy { it.isChecked })
}

Scaffold(
modifier = modifier,
Expand All @@ -118,9 +130,7 @@ fun AddEditScreen(
navigateBack
},
saveNote = {
Log.e("AddEditScreen app bar", title)
Log.e("AddEditScreen app bar", description)
saveNote(title, description, images)
saveNote(title, description, images, checklist)
},
deleteNote = deleteNote
)
Expand Down Expand Up @@ -225,6 +235,27 @@ fun AddEditScreen(
}, modifier = Modifier)
}

NoteChecklist(
checklist = checklist,
showAddTodo = showAddTodo,
onAdd = {
checklist.add(Todo(title = it))
},
onDelete = {
checklist.removeAt(it)
},
onUpdate = { index, newTitle ->
checklist[index] = checklist[index].copy(title = newTitle)
},
onToggle = { index ->
checklist[index] = checklist[index].copy(isChecked = !checklist[index].isChecked)
checklist.sortBy { it.isChecked }
},
hideAddTodo = {
showAddTodo = false
}
)

DescriptionTextField(
scrollOffset = descriptionScrollOffset,
contentSize = contentSize,
Expand Down Expand Up @@ -255,6 +286,9 @@ fun AddEditScreen(
},
onReminderDateTime = {
shouldShowDialogDateTime = true
},
addTodo = {
showAddTodo = true
}
)
}
Expand Down Expand Up @@ -283,6 +317,7 @@ fun AddEditScreen(
}
)
}

TextDialog(
title = stringResource(R.string.are_you_sure),
description = stringResource(R.string.the_text_change_will_not_be_saved),
Expand Down Expand Up @@ -316,7 +351,7 @@ private fun AddEditScreenPreview() = NotifyTheme {
),
isNew = true,
navigateBack = {},
saveNote = { _, _, _ -> },
saveNote = { _, _, _, _ -> },
deleteNote = {},
onUpdateReminderDateTime = {}
)
Expand Down
Loading

0 comments on commit ddfb57b

Please sign in to comment.