Skip to content

Commit

Permalink
Add search option to top bar tooltip
Browse files Browse the repository at this point in the history
[Delete unused code]
[Add search option to top bar tooltip]
  • Loading branch information
Z-Siqi committed Sep 23, 2024
1 parent aa44dd2 commit 21db3dd
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 88 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/com/sqz/checklist/ui/main/TopBarMenuContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import com.sqz.checklist.R

@Composable
fun NavTooltipContent(
textRid: Int,
onDismissRequest: () -> Unit,
onClickToTaskHistory: () -> Unit,
onClickToSearch: () -> Unit,
view: View,
modifier: Modifier = Modifier,
expanded: Boolean = false,
Expand All @@ -44,7 +44,14 @@ fun NavTooltipContent(
onClickToTaskHistory()
view.playSoundEffect(SoundEffectConstants.CLICK)
},
text = { Text(text = stringResource(textRid)) }
text = { Text(text = stringResource(R.string.task_history)) }
)
DropdownMenuItem(
onClick = {
onClickToSearch()
view.playSoundEffect(SoundEffectConstants.CLICK)
},
text = { Text(text = stringResource(R.string.search)) }
)
}
}
Expand All @@ -54,5 +61,5 @@ fun NavTooltipContent(
@Preview(showBackground = true)
@Composable
private fun Preview() {
NavTooltipContent(R.string.app_name, {}, {}, LocalView.current, expanded = true)
NavTooltipContent({}, {}, {}, LocalView.current, expanded = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale

data class SelectData(
val selectedId: Int = -0,
val onSelect: Boolean = false,
val hideSelected: Boolean = false // Hide in a short time before remove (for animation)
)

/**
* Task history screen layout.
**/
Expand Down Expand Up @@ -85,6 +91,7 @@ fun TaskHistory(
modifier = modifier.padding(paddingValues),
color = MaterialTheme.colorScheme.surfaceContainer
) {
val selectState = historyState.selectState.collectAsState().value
LazyColumn(
modifier = modifier.fillMaxSize()
) {
Expand All @@ -93,12 +100,11 @@ fun TaskHistory(
}
items(item, key = { it.id }) {
ItemBox(
id = it.id,
description = it.description,
createDate = it.createDate,
hide = historyState.hideSelected && historyState.selectedId == it.id,
view = view,
historyState = historyState
item = it,
selectState = selectState,
onItemClick = { historyState.setSelectTask(it.id) },
hide = selectState.hideSelected && selectState.selectedId == it.id,
view = view
)
}
item {
Expand Down Expand Up @@ -157,15 +163,14 @@ private fun HistoryTopBar(

@Composable
private fun ItemBox(
id: Int,
description: String,
createDate: LocalDate,
item: Task,
selectState: SelectData,
hide: Boolean,
onItemClick: () -> Unit,
modifier: Modifier = Modifier,
historyState: TaskHistoryViewModel = viewModel(),
view: View
) {
val border = if (historyState.selectedId == id) {
val border = if (selectState.selectedId == item.id) {
BorderStroke(3.dp, MaterialTheme.colorScheme.tertiary)
} else BorderStroke(1.dp, MaterialTheme.colorScheme.surfaceDim)
Column(
Expand All @@ -187,13 +192,11 @@ private fun ItemBox(
val formatter = DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.getDefault())
val padding = modifier.padding(bottom = 8.dp, top = 12.dp, start = 12.dp, end = 11.dp)
val onClick = modifier.clickable {
historyState.selectTask(id)
onItemClick()
view.playSoundEffect(SoundEffectConstants.CLICK)
}
Box(
modifier = modifier.fillMaxSize() then onClick
) {
val time = stringResource(R.string.task_creation_time, createDate.format(formatter))
Box(modifier = modifier.fillMaxSize() then onClick) {
val time = stringResource(R.string.task_creation_time, item.createDate.format(formatter))
Box(modifier = padding) {
Column {
Column(
Expand All @@ -203,7 +206,7 @@ private fun ItemBox(
horizontalAlignment = Alignment.Start
) {
Text(
text = description,
text = item.description,
modifier = modifier.padding(top = 0.dp),
fontSize = 21.sp,
overflow = TextOverflow.Ellipsis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,31 @@ import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.sqz.checklist.R
import com.sqz.checklist.ui.material.WarningAlertDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun TaskHistoryNavBar(
view: View,
modifier: Modifier = Modifier,
historyState: TaskHistoryViewModel,
) {
val coroutineScope = rememberCoroutineScope()
val selectState = historyState.selectState.collectAsState().value

var deleteAllView by rememberSaveable { mutableStateOf(false) }
var redoAllView by rememberSaveable { mutableStateOf(false) }
if (deleteAllView) {
WarningAlertDialog(
onDismissRequest = { deleteAllView = false },
onConfirmButtonClick = {
historyState.doAllTask(TaskHistoryViewModel.DoAllTaskAction.Delete)
historyState.updateTaskHistoryData()
historyState.doAllTask(TaskHistoryViewModel.DoTaskAction.Delete)
deleteAllView = false
},
textString = stringResource(R.string.delete_all_history)
Expand All @@ -51,8 +48,7 @@ fun TaskHistoryNavBar(
WarningAlertDialog(
onDismissRequest = { redoAllView = false },
onConfirmButtonClick = {
historyState.doAllTask(TaskHistoryViewModel.DoAllTaskAction.Redo)
historyState.updateTaskHistoryData()
historyState.doAllTask(TaskHistoryViewModel.DoTaskAction.Redo)
redoAllView = false
},
textString = stringResource(R.string.redo_all_history)
Expand All @@ -75,17 +71,12 @@ fun TaskHistoryNavBar(
colors = colors,
icon = { Icon(imageVector = Icons.Filled.Delete, contentDescription = deleteText) },
label = { Text(text = deleteText) },
selected = historyState.onSelect,
selected = selectState.onSelect,
onClick = {
if (historyState.onSelect) {
coroutineScope.launch {
historyState.hideSelected = true
delay(80)
historyState.deleteTask(historyState.selectedId)
delay(20)
historyState.resetSelect()
}
} else {
if (selectState.onSelect) historyState.removeFromHistory(
TaskHistoryViewModel.DoTaskAction.Delete,
selectState.selectedId
) else {
deleteAllView = true
}
view.playSoundEffect(SoundEffectConstants.CLICK)
Expand All @@ -96,16 +87,12 @@ fun TaskHistoryNavBar(
colors = colors,
icon = { Icon(imageVector = Icons.Filled.Refresh, contentDescription = redoText) },
label = { Text(text = redoText) },
selected = historyState.onSelect,
selected = selectState.onSelect,
onClick = {
if (historyState.onSelect) {
coroutineScope.launch {
historyState.hideSelected = true
historyState.changeTaskVisibilityAsUndo(historyState.selectedId)
delay(100)
historyState.resetSelect()
}
} else {
if (selectState.onSelect) historyState.removeFromHistory(
TaskHistoryViewModel.DoTaskAction.Redo,
selectState.selectedId
) else {
redoAllView = true
}
view.playSoundEffect(SoundEffectConstants.CLICK)
Expand All @@ -114,6 +101,6 @@ fun TaskHistoryNavBar(
Spacer(modifier = modifier.weight(0.5f))
}
LaunchedEffect(true) {
if (historyState.onSelect) historyState.resetSelect()
if (selectState.onSelect) historyState.resetSelectState()
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.sqz.checklist.ui.main.task.history

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sqz.checklist.MainActivity
import com.sqz.checklist.database.Task
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.time.LocalDate
Expand All @@ -28,19 +26,59 @@ class TaskHistoryViewModel : ViewModel() {
}

/** Redo or Delete all task from history **/
fun doAllTask(doAllTaskAction: DoAllTaskAction) = viewModelScope.launch {
fun doAllTask(doAllTaskAction: DoTaskAction) = viewModelScope.launch {
when (doAllTaskAction) {
DoAllTaskAction.Redo -> MainActivity.taskDatabase.taskDao().setAllNotHistory()
DoAllTaskAction.Delete -> MainActivity.taskDatabase.taskDao().deleteAllHistory()
DoTaskAction.Redo -> MainActivity.taskDatabase.taskDao().setAllNotHistory()
DoTaskAction.Delete -> MainActivity.taskDatabase.taskDao().deleteAllHistory()
}
// Update to LazyColumn
updateTaskHistoryData()
}

enum class DoAllTaskAction { Redo, Delete }
enum class DoTaskAction { Redo, Delete }

/** Control the History Selection State **/
private val _selectState = MutableStateFlow(SelectData())
val selectState: StateFlow<SelectData> = _selectState.asStateFlow()
fun setSelectTask(id: Int) {
_selectState.update {
if (!it.onSelect) {
it.copy(
selectedId = id,
onSelect = true
)
} else if (it.selectedId == id) {
it.copy(
selectedId = -0,
onSelect = false
)
} else {
it.copy(selectedId = id)
}
}
}

/** Reset Select State as Default **/
fun resetSelectState() {
_selectState.value = SelectData()
}

/** Delete or Undo to history as id **/
fun removeFromHistory(action: DoTaskAction, id: Int) = viewModelScope.launch {
_selectState.update { // Hide before remove is for animation
it.copy(hideSelected = true)
}
delay(80)
when (action) {
DoTaskAction.Delete -> deleteTask(id)
DoTaskAction.Redo -> changeTaskVisibilityAsUndo(id)
}
delay(20)
resetSelectState()
}

/** Delete action **/
fun deleteTask(id: Int) = viewModelScope.launch {
private fun deleteTask(id: Int) = viewModelScope.launch {
// Actions
MainActivity.taskDatabase.taskDao().delete(
Task(id = id, description = "", createDate = LocalDate.MIN)
Expand All @@ -51,35 +89,12 @@ class TaskHistoryViewModel : ViewModel() {
}

/** Undo to history **/
fun changeTaskVisibilityAsUndo(id: Int) = viewModelScope.launch { // Actions
private fun changeTaskVisibilityAsUndo(id: Int) = viewModelScope.launch {
// Actions
MainActivity.taskDatabase.taskDao().setHistory(0, id)
MainActivity.taskDatabase.taskDao().setHistoryId(0, id)
arrangeHistoryId()
// Update to LazyColumn
updateTaskHistoryData()
}

/** Select task by id **/
fun selectTask(id: Int) {
if (!onSelect) {
selectedId = id
onSelect = true
} else if (selectedId == id) {
selectedId = -0
onSelect = false
} else {
selectedId = id
}
}

var selectedId by mutableIntStateOf(-0)
var onSelect by mutableStateOf(false)
var hideSelected by mutableStateOf(false)

/** Reset select state (selectedId, onSelect, hideSelected) **/
fun resetSelect() {
this.selectedId = -0
this.onSelect = false
this.hideSelected = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ fun TaskLayout(
modifier = modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
var menu by rememberSaveable { mutableStateOf(false) }
NavTooltipContent(textRid = R.string.task_history, onClickToTaskHistory = {
taskState.checkTaskAction = false
menu = false
toTaskHistory()
}, onDismissRequest = { menu = false }, expanded = menu, view = view)
NavTooltipContent(
onClickToTaskHistory = {
taskState.checkTaskAction = false
menu = false
toTaskHistory()
},
onClickToSearch = {
val it = NavExtendedConnectData(searchState = true)
taskState.updateNavConnector(it, it)
menu = false
},
onDismissRequest = { menu = false }, expanded = menu, view = view
)
TopBar(scrollBehavior, topBarState, onClick = { menu = true }, view)
}
) { paddingValues ->
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.5.0" apply false
id("com.android.application") version "8.5.2" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
}

0 comments on commit 21db3dd

Please sign in to comment.