Skip to content

Commit

Permalink
chore: update TitleBar to use TopAppBar
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 committed Jul 4, 2024
1 parent 21a0add commit a8add58
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package io.github.jd1378.otphelper.ui.components

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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.github.jd1378.otphelper.R
import io.github.jd1378.otphelper.utils.mirroringBackIcon

Expand All @@ -33,24 +30,27 @@ private fun Up(upPress: () -> Unit) {
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TitleBar(
upPress: () -> Unit,
text: String,
showBackBtn: Boolean = true,
content: @Composable () -> Unit = {}
content: @Composable () -> Unit = {},
) {
Row(
Modifier.fillMaxWidth().statusBarsPadding().padding(top = 5.dp),
verticalAlignment = Alignment.CenterVertically,
) {
if (showBackBtn) {
Up(upPress)
} else {
Box(Modifier.width(10.dp).height(56.dp))
}
Text(text = text, fontSize = 20.sp, modifier = Modifier.padding(end = 15.dp))
Spacer(Modifier.weight(1f))
content()
}
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())

TopAppBar(
scrollBehavior = scrollBehavior,
colors =
TopAppBarDefaults.topAppBarColors(
scrolledContainerColor = MaterialTheme.colorScheme.surface),
title = { Text(text) },
navigationIcon = {
if (showBackBtn) {
Up(upPress)
}
},
actions = { content() },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ fun Permissions(
if (setupMode) {
Button(
modifier =
Modifier.fillMaxWidth()
.padding(start = 40.dp, end = dimensionResource(R.dimen.padding_small)),
Modifier.padding(
start = 40.dp, end = dimensionResource(R.dimen.padding_small)),
onClick = { onNavigateToRoute(MainDestinations.LANGUAGE_SELECTION_ROUTE) }) {
Text(text = stringResource(R.string.language))
}
Expand All @@ -111,72 +111,73 @@ fun Permissions(
}
}
}
}) { padding ->
Column(
Modifier.padding(padding)
.padding(top = 10.dp)
.padding(horizontal = dimensionResource(R.dimen.padding_small))
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
Text(
stringResource(R.string.permissions_desc),
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
fontSize = 15.sp)
},
) { padding ->
Column(
Modifier.padding(padding)
.padding(top = 10.dp)
.padding(horizontal = dimensionResource(R.dimen.padding_small))
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
Text(
stringResource(R.string.permissions_desc),
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
fontSize = 15.sp)

TodoItem(
stringResource(R.string.permission_todo_post_notifications),
actionText = stringResource(R.string.grant),
enabled = !uiState.hasNotifPerm,
checked = uiState.hasNotifPerm) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
TodoItem(
stringResource(R.string.permission_todo_read_notifications),
checked = uiState.hasNotifListenerPerm) {
viewModel.onOpenReadNotificationsPressed(context)
}
TodoItem(
stringResource(R.string.permission_todo_remain_open),
checked = uiState.isIgnoringBatteryOptimizations) {
viewModel.onOpenBatteryOptimizationsPressed(context)
}
TodoItem(
stringResource(R.string.permission_todo_post_notifications),
actionText = stringResource(R.string.grant),
enabled = !uiState.hasNotifPerm,
checked = uiState.hasNotifPerm) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}
}
TodoItem(
stringResource(R.string.permission_todo_read_notifications),
checked = uiState.hasNotifListenerPerm) {
viewModel.onOpenReadNotificationsPressed(context)
}
TodoItem(
stringResource(R.string.permission_todo_remain_open),
checked = uiState.isIgnoringBatteryOptimizations) {
viewModel.onOpenBatteryOptimizationsPressed(context)
}

if (uiState.hasAutostartSettings) {
Text(
stringResource(R.string.perm_extra_desc),
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
fontSize = 15.sp)
if (uiState.hasAutostartSettings) {
Text(
stringResource(R.string.perm_extra_desc),
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
fontSize = 15.sp)

TodoItem(
stringResource(R.string.permission_todo_autostart),
intermediate = true,
checked = true) {
viewModel.onOpenAutostartPressed(context)
}
}
TodoItem(
stringResource(R.string.permission_todo_autostart),
intermediate = true,
checked = true) {
viewModel.onOpenAutostartPressed(context)
}
}

if (uiState.hasRestrictedSettings) {
Text(
stringResource(R.string.perm_restricted_desc),
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
fontSize = 15.sp)
if (uiState.hasRestrictedSettings) {
Text(
stringResource(R.string.perm_restricted_desc),
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
fontSize = 15.sp)

TodoItem(
stringResource(R.string.permission_todo_allow_restricted_settings),
intermediate = true,
checked = true) {
viewModel.onOpenAppSettings(context)
}
}
}
TodoItem(
stringResource(R.string.permission_todo_allow_restricted_settings),
intermediate = true,
checked = true) {
viewModel.onOpenAppSettings(context)
}
}
}
}
}

0 comments on commit a8add58

Please sign in to comment.