Skip to content

Commit

Permalink
Optimize search & change some functions logic
Browse files Browse the repository at this point in the history
[Tooltip is compatible with compose-bom:2024.09.x]
[Change NavBar, NavHost & some functions logic]
[Fixed check task refresh error in searching]
[Support back action in search interface]
[Update dependencies]
  • Loading branch information
Z-Siqi committed Sep 19, 2024
1 parent 3d12174 commit 39b2db1
Show file tree
Hide file tree
Showing 13 changed files with 668 additions and 414 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

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

10 changes: 5 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ android {
dependencies {

implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.5")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.6")
implementation("androidx.activity:activity-compose:1.9.2")
implementation(platform("androidx.compose:compose-bom:2024.09.00"))
implementation(platform("androidx.compose:compose-bom:2024.09.02"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3-android:1.3.0")
implementation("androidx.compose.foundation:foundation-android:1.7.0")
implementation("androidx.compose.foundation:foundation-android:1.7.2")
implementation("androidx.work:work-runtime-ktx:2.9.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.2.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.09.00"))
androidTestImplementation(platform("androidx.compose:compose-bom:2024.09.02"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
// Navigation
implementation("androidx.navigation:navigation-compose:2.8.0")
implementation("androidx.navigation:navigation-compose:2.8.1")
// Room
ksp("androidx.room:room-compiler:2.6.1")
implementation("androidx.room:room-runtime:2.6.1")
Expand Down
195 changes: 85 additions & 110 deletions app/src/main/java/com/sqz/checklist/ui/MainLayout.kt
Original file line number Diff line number Diff line change
@@ -1,64 +1,54 @@
package com.sqz.checklist.ui

import android.content.Context
import android.util.Log
import android.view.SoundEffectConstants
import android.view.View
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddCircle
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.DividerDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MediumTopAppBar
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TooltipBox
import androidx.compose.material3.TooltipDefaults
import androidx.compose.material3.TooltipScope
import androidx.compose.material3.TooltipState
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.material3.TopAppBarState
import androidx.compose.material3.VerticalDivider
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.sqz.checklist.R
import com.sqz.checklist.ui.main.NavBar
import com.sqz.checklist.ui.main.NavExtendedButtonData
import com.sqz.checklist.ui.main.task.TaskLayoutViewModel
import com.sqz.checklist.ui.main.task.history.TaskHistory
import com.sqz.checklist.ui.main.task.history.TaskHistoryNavBar
import com.sqz.checklist.ui.main.task.history.TaskHistoryViewModel
import com.sqz.checklist.ui.main.task.layout.TaskLayout
import com.sqz.checklist.ui.main.task.layout.taskExtendedNavButton
import com.sqz.checklist.ui.material.TextTooltipBox
import java.time.LocalDate
import java.time.format.DateTimeFormatter
Expand All @@ -67,99 +57,97 @@ import java.util.Locale
enum class MainLayoutNav {
TaskLayout,
TaskHistory,
Unknown,
}

/** Top level of MainLayout **/
@Composable
fun MainLayout(context: Context, view: View) {
fun MainLayout(context: Context, view: View, modifier: Modifier = Modifier) {
val navController = rememberNavController()
NavHost(
navController = navController,
startDestination = MainLayoutNav.TaskLayout.name
) {
composable(MainLayoutNav.TaskLayout.name) {
TaskLayout(toTaskHistory = {
navController.navigate(MainLayoutNav.TaskHistory.name)
}, context = context, view = view)
}
composable(MainLayoutNav.TaskHistory.name) {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
TaskHistory(navBack = {
if (currentRoute != MainLayoutNav.TaskLayout.name) {
navController.popBackStack()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route

val taskLayoutViewModel: TaskLayoutViewModel = viewModel()
val taskHistoryViewModel: TaskHistoryViewModel = viewModel()

val mainNavigationBar = @Composable {
@Suppress("OPT_IN_USAGE_FUTURE_ERROR") val extendedButtonData =
when (currentRoute) {
// TaskLayout Extended Nav Button function
MainLayoutNav.TaskLayout.name -> taskExtendedNavButton(
view, context, taskLayoutViewModel
)
// The else should never happen, never be called
else -> NavExtendedButtonData()
}
NavBar(
extendedButtonData = extendedButtonData,
selected = { index -> index.name == currentRoute },
onNavClick = { index ->
if (index.name != currentRoute) navController.navigate(index.name) {
popUpTo(0)
}
})
},
modifier = modifier
)
}
val taskHistoryNavBar = @Composable {
TaskHistoryNavBar(view = view, historyState = taskHistoryViewModel)
}

val nulLog = { Log.d("MainLayout", "Navigation bar is disable") }
val nul = @Composable { Spacer(modifier = modifier).also { nulLog() } }
ContentLayout(
bottomBar = when (currentRoute) {
MainLayoutNav.TaskHistory.name -> taskHistoryNavBar
MainLayoutNav.Unknown.name -> nul
else -> mainNavigationBar
}
) {
NavHost(
navController = navController,
startDestination = MainLayoutNav.TaskLayout.name
) {
composable(MainLayoutNav.TaskLayout.name) {
TaskLayout(
toTaskHistory = { navController.navigate(MainLayoutNav.TaskHistory.name) },
context = context, view = view,
taskState = taskLayoutViewModel
)
}
composable(MainLayoutNav.TaskHistory.name) {
TaskHistory(
navBack = { navController.popBackStack() },
historyState = taskHistoryViewModel
)
}
}
}
}

/** MainLayout NavigationBar **/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NavBar(
icon: @Composable () -> Unit,
label: @Composable () -> Unit,
tooltipContent: @Composable (TooltipScope.() -> Unit),
tooltipState: TooltipState,
onClick: () -> Unit,
modifier: Modifier = Modifier
) {
data class Items(
val text: String,
val icon: Int
)

var selectedItem by remember { mutableIntStateOf(0) }
val items = listOf(Items(stringResource(R.string.tasks), R.drawable.task_icon))
NavigationBar(
modifier = modifier,
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
private fun ContentLayout(
modifier: Modifier = Modifier,
topBar: @Composable () -> Unit = {},
bottomBar: @Composable () -> Unit = {},
navigationRail: @Composable () -> Unit = {},
floatingActionButton: @Composable () -> Unit = {},
content: @Composable () -> Unit = {}
) = Row {
Surface(
modifier = modifier.weight(1f)
) {
Spacer(modifier = modifier.weight(0.5f))
items.forEachIndexed { index, item ->
NavigationBarItem(
modifier = modifier.weight(1f),
colors = NavigationBarItemDefaults.colors(
indicatorColor = MaterialTheme.colorScheme.inversePrimary,
selectedIconColor = MaterialTheme.colorScheme.inverseSurface,
disabledIconColor = MaterialTheme.colorScheme.primary
),
icon = {
Icon(
painter = painterResource(id = item.icon),
contentDescription = item.text,
modifier = modifier.size(24.dp, 24.dp)
)
},
label = { Text(item.text) },
selected = selectedItem == index,
onClick = { selectedItem = index }
)
}
Spacer(modifier = modifier.weight(0.5f))
VerticalDivider(
modifier = modifier.height(50.dp), color = if (isSystemInDarkTheme()) {
MaterialTheme.colorScheme.onSurface
} else DividerDefaults.color
)
Row(modifier = modifier.weight(1f), verticalAlignment = Alignment.CenterVertically) {
TooltipBox(
positionProvider = TooltipDefaults.rememberPlainTooltipPositionProvider(),
tooltip = tooltipContent,
state = tooltipState
) {
NavigationBarItem(
colors = NavigationBarItemDefaults.colors(MaterialTheme.colorScheme.primary),
icon = icon,
label = label,
selected = false,
onClick = onClick
)
Scaffold(
topBar = topBar,
bottomBar = bottomBar,
floatingActionButton = floatingActionButton,
) { paddingValues ->
Surface(modifier = modifier.padding(paddingValues)) {
content()
}
}
}
Surface { navigationRail() }
}

/** MainLayout Top App Bar **/
Expand Down Expand Up @@ -271,16 +259,3 @@ private fun topBarContent(pattern: String): String {
val formatter = DateTimeFormatter.ofPattern(pattern, Locale.getDefault())
return currentDate.format(formatter)
}

@OptIn(ExperimentalMaterial3Api::class)
@Preview(showBackground = true)
@Composable
private fun Preview() {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
Scaffold(
topBar = { TopBar(scrollBehavior, rememberTopAppBarState(), {}, LocalView.current) },
bottomBar = {
NavBar({ Icon(Icons.Filled.AddCircle, "") }, { Text("Add") }, {}, TooltipState(), {})
}
) { Modifier.padding(it) }
}
Loading

0 comments on commit 39b2db1

Please sign in to comment.