Skip to content

Commit

Permalink
Implemented Pin support to Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
aritra-tech committed Nov 18, 2023
1 parent 8054f4d commit 550a69c
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.aritra.notify.components.appbar

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material.icons.outlined.PushPin
import androidx.compose.material.icons.outlined.RemoveCircleOutline
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.aritra.notify.domain.models.Note

@Composable
fun SelectedModeBottomBar(selectedNotes: List<Note>, onDeleteClick: () -> Unit, onPinNote: (isAlreadyPinned:Boolean) -> Unit) {
BottomAppBar(actions = {
Row(
horizontalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxWidth()
) {
IconButton(onClick = { onDeleteClick.invoke() }) {
Icon(
imageVector = Icons.Outlined.Delete,
contentDescription = null
)
}
if (selectedNotes.size == 1) {
if (selectedNotes[0].isPinned) {
IconButton(onClick = { onPinNote.invoke(true) }) {
Icon(
imageVector = Icons.Outlined.RemoveCircleOutline,
contentDescription = null
)
}
} else {
IconButton(onClick = { onPinNote.invoke(false) }) {
Icon(
imageVector = Icons.Outlined.PushPin,
contentDescription = null
)
}
}
}
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.aritra.notify.components.appbar
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.DeleteOutline
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand Down Expand Up @@ -39,13 +38,5 @@ fun SelectionModeTopAppBar(selectedItems: List<Int>, onDeleteClick: () -> Unit,
fontFamily = FontFamily(Font(R.font.poppins_medium))
)
)
}, actions = {
IconButton(onDeleteClick) {
Icon(
imageVector = Icons.Outlined.DeleteOutline,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground
)
}
})
}
33 changes: 23 additions & 10 deletions app/src/main/java/com/aritra/notify/components/note/GridNoteCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.PushPin
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
Expand Down Expand Up @@ -84,16 +85,28 @@ fun GridNoteCard(
.fillMaxSize()
.padding(16.dp)
) {
if (isSelected) {
Icon(
imageVector = Icons.Default.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.size(24.dp)
.align(Alignment.TopEnd)

)
Row(
modifier = Modifier
.align(Alignment.TopEnd)
) {
if (notesModel.isPinned) {
Icon(
imageVector = Icons.Default.PushPin,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.size(24.dp)
)
}
if (isSelected) {
Icon(
imageVector = Icons.Default.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.size(24.dp)
)
}
}

Column(
Expand Down
42 changes: 28 additions & 14 deletions app/src/main/java/com/aritra/notify/components/note/NoteCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccessTime
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.PushPin
import androidx.compose.material3.AssistChipDefaults
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedAssistChip
Expand All @@ -28,8 +29,8 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand Down Expand Up @@ -62,7 +63,7 @@ fun NotesCard(
onClick: () -> Unit,
onLongClick: () -> Unit,
) {
val painter = rememberSaveable { mutableStateOf(noteModel.image) }
val painter by rememberUpdatedState(newValue = noteModel.image)
val context = LocalContext.current

OutlinedCard(
Expand All @@ -86,29 +87,42 @@ fun NotesCard(
.fillMaxSize()
.padding(16.dp)
) {
if (isSelected) {
Icon(
imageVector = Icons.Default.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.size(24.dp)
.align(Alignment.TopEnd)
)
Row(
modifier = Modifier
.align(Alignment.TopEnd)
) {
if (noteModel.isPinned) {
Icon(
imageVector = Icons.Default.PushPin,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.size(24.dp)
)
}
if (isSelected) {
Icon(
imageVector = Icons.Default.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier
.size(24.dp)
)
}
}
Column(
modifier = Modifier
.fillMaxWidth()
) {
if (painter.value.isNotEmpty()) {
if (painter.isNotEmpty()) {
Row(
modifier = Modifier
.height(80.dp)
.clip(RoundedCornerShape(10.dp))
.horizontalScroll(rememberScrollState()),
horizontalArrangement = Arrangement.spacedBy(6.dp)
) {
painter.value.forEach {
painter.forEach {
AsyncImage(
model = ImageRequest.Builder(context)
.data(it ?: "")
Expand Down
2 changes: 2 additions & 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 @@ -23,6 +23,8 @@ data class Note(
var isMovedToTrash: Boolean = false,
var reminderDateTime: LocalDateTime? = null,
var isReminded: Boolean = false,
@ColumnInfo(defaultValue = "false")
var isPinned: Boolean = false
) : Parcelable

data class ReminderDateTimeInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
import com.aritra.notify.R
import com.aritra.notify.components.actions.BackPressHandler
import com.aritra.notify.components.actions.LayoutToggleButton
import com.aritra.notify.components.appbar.SelectedModeBottomBar
import com.aritra.notify.components.note.GridNoteCard
import com.aritra.notify.components.note.NotesCard
import com.aritra.notify.components.appbar.SelectionModeTopAppBar
Expand Down Expand Up @@ -140,7 +141,11 @@ fun NoteScreen(
snapshotFlow { listState.layoutInfo }
.collect {
if (it.visibleItemsInfo.isNotEmpty()) {
shouldHideBottomBar.invoke(shouldHideSearchBarForList)
if (isInSelectionMode) {
shouldHideBottomBar.invoke(!isInSelectionMode)
} else {
shouldHideBottomBar.invoke(shouldHideSearchBarForList)
}
shouldHideSearchBar = shouldHideSearchBarForList
}
}
Expand All @@ -149,7 +154,10 @@ fun NoteScreen(
snapshotFlow { staggeredGState.layoutInfo }
.collect {
if (it.visibleItemsInfo.isNotEmpty()) {
shouldHideBottomBar.invoke(shouldHideSearchBarForGrid)
if (isInSelectionMode)
shouldHideBottomBar.invoke(!isInSelectionMode)
else
shouldHideBottomBar.invoke(shouldHideSearchBarForGrid)
shouldHideSearchBar = shouldHideSearchBarForGrid
}
}
Expand Down Expand Up @@ -241,6 +249,49 @@ fun NoteScreen(
}
}
},
bottomBar = {
if (isInSelectionMode) {
SelectedModeBottomBar(
selectedNotes = listOfAllNotes.filter { note -> note.id in selectedNoteIds },
onPinNote = {currentStatus ->
val selectedNotes = listOfAllNotes.filter { note -> note.id in selectedNoteIds }
if (selectedNotes.size == 1) {
val selectedNote: Note = selectedNotes[0]
selectedNote.isPinned = !currentStatus
viewModel.updateNote(selectedNote){
resetSelectionMode()
}
}
},
onDeleteClick = {
val selectedNotes =
listOfAllNotes.filter { note -> note.id in selectedNoteIds }

viewModel.deleteListOfNote(selectedNotes)

deletedNotes.addAll(selectedNotes)
resetSelectionMode()

scope.launch {
val snackBarResult = snackBarHostState.showSnackbar(
message = "Notes moved to trash",
actionLabel = "Undo",
duration = SnackbarDuration.Short,
withDismissAction = false
)

when (snackBarResult) {
SnackbarResult.ActionPerformed -> {
addEditViewModel.insertListOfNote(deletedNotes) {}
}

SnackbarResult.Dismissed -> {
}
}
}
})
}
},
content = {
Surface(
modifier = Modifier.padding(it)
Expand All @@ -259,6 +310,8 @@ fun NoteScreen(
itemsIndexed(
listOfAllNotes.filter { note ->
note.title.contains(searchQuery, true)
}.sortedByDescending {note ->
note.isPinned
}
) { _, notesModel ->
val isSelected = selectedNoteIds.contains(notesModel.id)
Expand Down Expand Up @@ -302,6 +355,8 @@ fun NoteScreen(
items(
listOfAllNotes.filter { note ->
note.title.contains(searchQuery, true)
}.sortedByDescending { note ->
note.isPinned
},
key = { it.id },
contentType = { it.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.aritra.notify.domain.models.TrashNote
import com.aritra.notify.domain.repository.NoteRepository
import com.aritra.notify.domain.repository.trash.TrashNoteRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDateTime
Expand Down Expand Up @@ -58,4 +59,13 @@ class NoteScreenViewModel @Inject constructor(
}
}
}

fun updateNote(note:Note, onSuccess: (updated: Boolean) -> Unit) = viewModelScope.launch(Dispatchers.IO){
homeRepository.updateNoteInRoom(
note
)
withContext(Dispatchers.Main) {
onSuccess(true)
}
}
}

0 comments on commit 550a69c

Please sign in to comment.