From 9c5e9c8e4381aaa2886d0ad03070b2a57a937cce Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 23 Jul 2022 19:15:36 +0530 Subject: [PATCH] Improvements to key list activity (#2030) --- app/build.gradle.kts | 1 + .../app/passwordstore/ui/pgp/PGPKeyList.kt | 16 ++++++++- .../ui/pgp/PGPKeyListActivity.kt | 21 +++++++++-- .../res/drawable/ic_arrow_back_black_24dp.xml | 15 ++++++++ ui-compose/build.gradle.kts | 1 - .../kotlin/app/passwordstore/ui/APSAppBar.kt | 36 +++++++++++++++++++ .../passwordstore/ui/compose/theme/Theme.kt | 15 +++----- .../passwordstore/ui/compose/theme/utils.kt | 27 ++++++++++++++ 8 files changed, 117 insertions(+), 15 deletions(-) rename {ui-compose/src/main/kotlin => app/src/main/java}/app/passwordstore/ui/pgp/PGPKeyList.kt (78%) create mode 100644 app/src/main/res/drawable/ic_arrow_back_black_24dp.xml create mode 100644 ui-compose/src/main/kotlin/app/passwordstore/ui/APSAppBar.kt create mode 100644 ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 8d0bc6f6..32291d33 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -76,6 +76,7 @@ dependencies { implementation(libs.androidx.recyclerviewSelection) implementation(libs.androidx.security) implementation(libs.androidx.swiperefreshlayout) + implementation(libs.compose.ui.tooling) implementation(libs.dagger.hilt.android) implementation(libs.kotlin.coroutines.android) diff --git a/ui-compose/src/main/kotlin/app/passwordstore/ui/pgp/PGPKeyList.kt b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt similarity index 78% rename from ui-compose/src/main/kotlin/app/passwordstore/ui/pgp/PGPKeyList.kt rename to app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt index ccc6fa01..fd2369e0 100644 --- a/ui-compose/src/main/kotlin/app/passwordstore/ui/pgp/PGPKeyList.kt +++ b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyList.kt @@ -9,11 +9,12 @@ import androidx.compose.foundation.lazy.items import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import app.passwordstore.crypto.GpgIdentifier @Composable -public fun KeyList( +fun KeyList( identifiers: List, onItemClick: (identifier: GpgIdentifier) -> Unit, modifier: Modifier = Modifier, @@ -37,3 +38,16 @@ private fun KeyItem( } Box(modifier = modifier.padding(16.dp).fillMaxWidth()) { Text(text = label) } } + +@Preview +@Composable +private fun KeyListPreview() { + KeyList( + identifiers = + listOfNotNull( + GpgIdentifier.fromString("john.doe@example.com"), + GpgIdentifier.fromString("0xB950AE2813841585") + ), + onItemClick = {} + ) +} diff --git a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt index 3cce4060..4394cff6 100644 --- a/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt +++ b/app/src/main/java/app/passwordstore/ui/pgp/PGPKeyListActivity.kt @@ -6,10 +6,17 @@ import androidx.activity.compose.setContent import androidx.activity.viewModels import androidx.compose.foundation.layout.padding import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import app.passwordstore.R +import app.passwordstore.ui.APSAppBar import app.passwordstore.ui.compose.theme.APSTheme +import app.passwordstore.ui.compose.theme.decideColorScheme import app.passwordstore.util.viewmodel.PGPKeyListViewModel import dagger.hilt.android.AndroidEntryPoint @@ -22,8 +29,18 @@ class PGPKeyListActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { - APSTheme { - Scaffold { paddingValues -> + val context = LocalContext.current + APSTheme(colors = decideColorScheme(context)) { + Scaffold( + topBar = { + APSAppBar( + title = stringResource(R.string.activity_label_pgp_key_manager), + navigationIcon = painterResource(R.drawable.ic_arrow_back_black_24dp), + onNavigationIconClick = { finish() }, + backgroundColor = MaterialTheme.colorScheme.surface, + ) + }, + ) { paddingValues -> PGPKeyList(viewModel = viewModel, modifier = Modifier.padding(paddingValues)) } } diff --git a/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml b/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml new file mode 100644 index 00000000..6cf467d6 --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/ui-compose/build.gradle.kts b/ui-compose/build.gradle.kts index 8dfee593..8f874f62 100644 --- a/ui-compose/build.gradle.kts +++ b/ui-compose/build.gradle.kts @@ -20,5 +20,4 @@ dependencies { api(libs.compose.foundation.layout) api(libs.compose.material3) api(libs.compose.ui.core) - implementation(projects.cryptoPgpainless) } diff --git a/ui-compose/src/main/kotlin/app/passwordstore/ui/APSAppBar.kt b/ui-compose/src/main/kotlin/app/passwordstore/ui/APSAppBar.kt new file mode 100644 index 00000000..6900b25f --- /dev/null +++ b/ui-compose/src/main/kotlin/app/passwordstore/ui/APSAppBar.kt @@ -0,0 +1,36 @@ +package app.passwordstore.ui + +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.SmallTopAppBar +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBarDefaults +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.painter.Painter + +@Composable +public fun APSAppBar( + title: String, + backgroundColor: Color, + navigationIcon: Painter?, + onNavigationIconClick: (() -> Unit)?, + modifier: Modifier = Modifier, +) { + SmallTopAppBar( + title = { Text(text = title) }, + navigationIcon = { + if (navigationIcon != null) { + IconButton(onClick = { onNavigationIconClick?.invoke() }) { + Icon( + painter = navigationIcon, + contentDescription = null, + ) + } + } + }, + colors = TopAppBarDefaults.smallTopAppBarColors(containerColor = backgroundColor), + modifier = modifier, + ) +} diff --git a/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt b/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt index 9bd8003e..695ff6cc 100644 --- a/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt +++ b/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/Theme.kt @@ -1,12 +1,12 @@ package app.passwordstore.ui.compose.theme -import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.ColorScheme import androidx.compose.material3.MaterialTheme import androidx.compose.material3.darkColorScheme import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable -private val LightThemeColors = +internal val LightThemeColors = lightColorScheme( primary = md_theme_light_primary, onPrimary = md_theme_light_onPrimary, @@ -34,7 +34,7 @@ private val LightThemeColors = inverseOnSurface = md_theme_light_inverseOnSurface, inverseSurface = md_theme_light_inverseSurface, ) -private val DarkThemeColors = +internal val DarkThemeColors = darkColorScheme( primary = md_theme_dark_primary, onPrimary = md_theme_dark_onPrimary, @@ -65,15 +65,8 @@ private val DarkThemeColors = @Composable public fun APSTheme( - useDarkTheme: Boolean = isSystemInDarkTheme(), + colors: ColorScheme, content: @Composable () -> Unit, ) { - val colors = - if (!useDarkTheme) { - LightThemeColors - } else { - DarkThemeColors - } - MaterialTheme(colorScheme = colors, typography = AppTypography, content = content) } diff --git a/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt b/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt new file mode 100644 index 00000000..096862dc --- /dev/null +++ b/ui-compose/src/main/kotlin/app/passwordstore/ui/compose/theme/utils.kt @@ -0,0 +1,27 @@ +package app.passwordstore.ui.compose.theme + +import android.content.Context +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.ColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.runtime.Composable + +@Composable +public fun decideColorScheme(context: Context): ColorScheme { + val isDarkTheme = isSystemInDarkTheme() + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + if (isDarkTheme) { + dynamicDarkColorScheme(context) + } else { + dynamicLightColorScheme(context) + } + } else { + if (isDarkTheme) { + DarkThemeColors + } else { + LightThemeColors + } + } +}