passwords,photos: split oversized ui files (350-line lint)
- Commit
d338030ea11ebfc68ba7a9845a75523cba2ba584- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-21 19:22 UTC (1 month ago)
- Parents
a7609dd
Diff
passwords/src/main/java/com/vayunmathur/passwords/ui/KdbxSyncSection.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/KdbxSyncSection.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/KdbxSyncSection.ktnew file mode 100644index 0000000000..0b35afa797--- /dev/null+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/KdbxSyncSection.kt@@ -0,0 +1,266 @@+package com.vayunmathur.passwords.ui++import android.content.Intent+import android.net.Uri+import android.text.format.DateFormat+import androidx.activity.compose.rememberLauncherForActivityResult+import androidx.activity.result.contract.ActivityResultContracts+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.height+import androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberCoroutineScope+import androidx.compose.runtime.setValue+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.input.PasswordVisualTransformation+import androidx.compose.ui.unit.dp+import com.vayunmathur.library.ui.AlertDialog+import com.vayunmathur.library.ui.DropdownMenu+import com.vayunmathur.library.ui.DropdownMenuItem+import com.vayunmathur.library.ui.ExperimentalMaterial3Api+import com.vayunmathur.library.ui.OutlinedTextField+import com.vayunmathur.library.ui.R as UiR+import com.vayunmathur.library.ui.SettingsDivider+import com.vayunmathur.library.ui.SettingsRow+import com.vayunmathur.library.ui.SettingsSection+import com.vayunmathur.library.ui.SettingsSwitchRow+import com.vayunmathur.library.ui.Text+import com.vayunmathur.library.ui.TextButton+import com.vayunmathur.passwords.R+import com.vayunmathur.passwords.sync.KdbxPasswordHelper+import com.vayunmathur.passwords.sync.KdbxSyncScheduler+import com.vayunmathur.passwords.sync.KdbxSyncSettings+import kotlinx.coroutines.launch+import java.util.Date++@OptIn(ExperimentalMaterial3Api::class)+@Composable+fun KdbxSyncSection() {+ val context = LocalContext.current+ val scope = rememberCoroutineScope()++ val enabled by KdbxSyncSettings.enabledFlow(context).collectAsState(false)+ val documentUri by KdbxSyncSettings.documentUriFlow(context).collectAsState("")+ val lastSync by KdbxSyncSettings.lastSyncFlow(context).collectAsState(0L)+ val status by KdbxSyncSettings.statusFlow(context).collectAsState("")+ val error by KdbxSyncSettings.errorFlow(context).collectAsState("")+ val quarantine by KdbxSyncSettings.quarantineFlow(context).collectAsState("")+ val quarantineAt by KdbxSyncSettings.quarantineAtFlow(context).collectAsState(0L)++ // Re-read on every recomposition rather than remembering: the dialog below writes it.+ var passwordSaved by remember { mutableStateOf(KdbxPasswordHelper(context).isKeyGenerated()) }+ var showPasswordDialog by remember { mutableStateOf(false) }+ var fileMenuExpanded by remember { mutableStateOf(false) }++ fun persistPermission(uri: Uri) {+ runCatching {+ context.contentResolver.takePersistableUriPermission(+ uri,+ Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION,+ )+ }+ scope.launch { KdbxSyncSettings.setDocumentUri(context, uri.toString()) }+ }++ val openLauncher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->+ uri?.let(::persistPermission)+ }+ val createLauncher = rememberLauncherForActivityResult(+ ActivityResultContracts.CreateDocument("application/octet-stream"),+ ) { uri -> uri?.let(::persistPermission) }++ Spacer(Modifier.height(24.dp))++ SettingsSection(title = stringResource(R.string.sync_section)) {+ SettingsSwitchRow(+ title = stringResource(R.string.sync_enable),+ supportingText = stringResource(R.string.sync_enable_description),+ checked = enabled,+ onCheckedChange = { checked ->+ scope.launch {+ KdbxSyncSettings.setEnabled(context, checked)+ if (checked) {+ KdbxSyncScheduler.schedulePeriodic(context)+ KdbxSyncScheduler.syncNow(context)+ } else {+ KdbxSyncScheduler.cancel(context)+ }+ }+ },+ )+ SettingsDivider()++ Box {+ SettingsRow(+ title = stringResource(R.string.sync_file),+ supportingText = documentUri.ifBlank { stringResource(R.string.sync_file_none) },+ onClick = { fileMenuExpanded = true },+ )+ DropdownMenu(+ expanded = fileMenuExpanded,+ onDismissRequest = { fileMenuExpanded = false },+ ) {+ DropdownMenuItem(+ text = { Text(stringResource(R.string.sync_choose_file)) },+ onClick = {+ fileMenuExpanded = false+ openLauncher.launch(arrayOf("application/octet-stream", "application/x-keepass", "*/*"))+ },+ )+ DropdownMenuItem(+ text = { Text(stringResource(R.string.sync_create_file)) },+ onClick = {+ fileMenuExpanded = false+ createLauncher.launch("passwords.kdbx")+ },+ )+ }+ }+ SettingsDivider()++ SettingsRow(+ title = stringResource(R.string.sync_vault_password),+ supportingText = stringResource(+ if (passwordSaved) UiR.string.saved else R.string.sync_vault_password_unset,+ ),+ onClick = { showPasswordDialog = true },+ )+ SettingsDivider()++ SettingsRow(+ title = stringResource(R.string.sync_last),+ supportingText = syncStatusText(lastSync, status, error),+ )+ SettingsDivider()++ if (status == KdbxSyncSettings.STATUS_QUARANTINED) {+ QuarantineSection(+ reason = quarantine,+ quarantinedAt = quarantineAt,+ onResolveKeepFile = {+ scope.launch {+ KdbxSyncSettings.clearQuarantine(context)+ KdbxSyncScheduler.syncNow(context)+ }+ },+ )+ SettingsDivider()+ }++ SettingsRow(+ title = stringResource(R.string.sync_now),+ enabled = enabled,+ onClick = { KdbxSyncScheduler.syncNow(context) },+ )+ }++ if (showPasswordDialog) {+ VaultPasswordDialog(+ onDismiss = { showPasswordDialog = false },+ onSave = { value ->+ KdbxPasswordHelper(context).storePassphrase(value)+ passwordSaved = true+ showPasswordDialog = false+ },+ )+ }+}++@Composable+private fun syncStatusText(lastSync: Long, status: String, error: String): String {+ val context = LocalContext.current+ val whenText = if (lastSync == 0L) {+ stringResource(R.string.sync_never)+ } else {+ DateFormat.getMediumDateFormat(context).format(Date(lastSync)) + " " ++ DateFormat.getTimeFormat(context).format(Date(lastSync))+ }+ if (status != KdbxSyncSettings.STATUS_ERROR && status != KdbxSyncSettings.STATUS_QUARANTINED) return whenText+ if (status == KdbxSyncSettings.STATUS_QUARANTINED) {+ return "$whenText\n${stringResource(R.string.sync_error_quarantined)}"+ }+ val reason = when (error) {+ "file_missing" -> stringResource(R.string.sync_error_file_missing)+ "wrong_password" -> stringResource(R.string.sync_error_wrong_password)+ "verify_failed" -> stringResource(R.string.sync_error_verify_failed)+ else -> stringResource(R.string.sync_error_generic, error)+ }+ return "$whenText\n$reason"+}++@Composable+private fun QuarantineSection(+ reason: String,+ quarantinedAt: Long,+ onResolveKeepFile: () -> Unit,+) {+ val context = LocalContext.current+ var showResolveDialog by remember { mutableStateOf(false) }+ val whenText = if (quarantinedAt == 0L) {+ ""+ } else {+ DateFormat.getMediumDateFormat(context).format(Date(quarantinedAt)) + " " ++ DateFormat.getTimeFormat(context).format(Date(quarantinedAt))+ }++ SettingsRow(+ title = stringResource(R.string.sync_quarantine_title),+ supportingText = stringResource(R.string.sync_quarantine_description, whenText),+ onClick = { showResolveDialog = true },+ )++ if (showResolveDialog) {+ AlertDialog(+ onDismissRequest = { showResolveDialog = false },+ title = { Text(stringResource(R.string.sync_quarantine_title)) },+ text = { Text(stringResource(R.string.sync_quarantine_dialog, reason)) },+ confirmButton = {+ TextButton(+ onClick = {+ showResolveDialog = false+ onResolveKeepFile()+ },+ ) {+ Text(stringResource(R.string.sync_quarantine_resolve))+ }+ },+ dismissButton = {+ TextButton(onClick = { showResolveDialog = false }) {+ Text(stringResource(UiR.string.cancel))+ }+ },+ )+ }+}++@Composable+private fun VaultPasswordDialog(onDismiss: () -> Unit, onSave: (String) -> Unit) {+ var value by remember { mutableStateOf("") }+ AlertDialog(+ onDismissRequest = onDismiss,+ title = { Text(stringResource(R.string.sync_vault_password)) },+ text = {+ OutlinedTextField(+ value = value,+ onValueChange = { value = it },+ singleLine = true,+ visualTransformation = PasswordVisualTransformation(),+ label = { Text(stringResource(R.string.sync_vault_password)) },+ )+ },+ confirmButton = {+ TextButton(onClick = { onSave(value) }, enabled = value.isNotEmpty()) {+ Text(stringResource(UiR.string.save))+ }+ },+ dismissButton = {+ TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.cancel)) }+ },+ )+}
passwords/src/main/java/com/vayunmathur/passwords/ui/SettingsPage.kt
diff --git a/passwords/src/main/java/com/vayunmathur/passwords/ui/SettingsPage.kt b/passwords/src/main/java/com/vayunmathur/passwords/ui/SettingsPage.ktindex e3fd753a19..2202c0906f 100644--- a/passwords/src/main/java/com/vayunmathur/passwords/ui/SettingsPage.kt+++ b/passwords/src/main/java/com/vayunmathur/passwords/ui/SettingsPage.kt@@ -1,8 +1,6 @@ package com.vayunmathur.passwords.ui -import android.content.Intent import android.net.Uri-import android.text.format.DateFormat import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.Arrangement@@ -11,44 +9,28 @@ 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 com.vayunmathur.library.ui.AlertDialog import com.vayunmathur.library.ui.Button import com.vayunmathur.library.ui.CircularProgressIndicator import com.vayunmathur.library.ui.DetailScaffold import com.vayunmathur.library.ui.DropdownMenu import com.vayunmathur.library.ui.DropdownMenuItem import com.vayunmathur.library.ui.ExperimentalMaterial3Api-import com.vayunmathur.library.ui.OutlinedTextField import com.vayunmathur.library.ui.R as UiR-import com.vayunmathur.library.ui.SettingsDivider-import com.vayunmathur.library.ui.SettingsRow-import com.vayunmathur.library.ui.SettingsSection-import com.vayunmathur.library.ui.SettingsSwitchRow import com.vayunmathur.library.ui.Text-import com.vayunmathur.library.ui.TextButton import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember-import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier-import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource-import androidx.compose.ui.text.input.PasswordVisualTransformation-import com.vayunmathur.passwords.R import androidx.compose.ui.unit.dp+import com.vayunmathur.passwords.R import com.vayunmathur.library.ui.BackupButtons import com.vayunmathur.library.ui.appBarScrollBehavior-import com.vayunmathur.passwords.sync.KdbxPasswordHelper-import com.vayunmathur.passwords.sync.KdbxSyncScheduler-import com.vayunmathur.passwords.sync.KdbxSyncSettings import com.vayunmathur.passwords.domain.ImportSource-import com.vayunmathur.passwords.platform.KdbxBackupFormat import com.vayunmathur.passwords.platform.PasswordsViewModel-import kotlinx.coroutines.launch-import java.util.Date @OptIn(ExperimentalMaterial3Api::class) @Composable@@ -124,228 +106,3 @@ fun SettingsPage( KdbxSyncSection() } }--@Composable-private fun KdbxSyncSection() {- val context = LocalContext.current- val scope = rememberCoroutineScope()-- val enabled by KdbxSyncSettings.enabledFlow(context).collectAsState(false)- val documentUri by KdbxSyncSettings.documentUriFlow(context).collectAsState("")- val lastSync by KdbxSyncSettings.lastSyncFlow(context).collectAsState(0L)- val status by KdbxSyncSettings.statusFlow(context).collectAsState("")- val error by KdbxSyncSettings.errorFlow(context).collectAsState("")- val quarantine by KdbxSyncSettings.quarantineFlow(context).collectAsState("")- val quarantineAt by KdbxSyncSettings.quarantineAtFlow(context).collectAsState(0L)-- // Re-read on every recomposition rather than remembering: the dialog below writes it.- var passwordSaved by remember { mutableStateOf(KdbxPasswordHelper(context).isKeyGenerated()) }- var showPasswordDialog by remember { mutableStateOf(false) }- var fileMenuExpanded by remember { mutableStateOf(false) }-- fun persistPermission(uri: Uri) {- runCatching {- context.contentResolver.takePersistableUriPermission(- uri,- Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION,- )- }- scope.launch { KdbxSyncSettings.setDocumentUri(context, uri.toString()) }- }-- val openLauncher = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->- uri?.let(::persistPermission)- }- val createLauncher = rememberLauncherForActivityResult(- ActivityResultContracts.CreateDocument("application/octet-stream"),- ) { uri -> uri?.let(::persistPermission) }-- Spacer(Modifier.height(24.dp))-- SettingsSection(title = stringResource(R.string.sync_section)) {- SettingsSwitchRow(- title = stringResource(R.string.sync_enable),- supportingText = stringResource(R.string.sync_enable_description),- checked = enabled,- onCheckedChange = { checked ->- scope.launch {- KdbxSyncSettings.setEnabled(context, checked)- if (checked) {- KdbxSyncScheduler.schedulePeriodic(context)- KdbxSyncScheduler.syncNow(context)- } else {- KdbxSyncScheduler.cancel(context)- }- }- },- )- SettingsDivider()-- Box {- SettingsRow(- title = stringResource(R.string.sync_file),- supportingText = documentUri.ifBlank { stringResource(R.string.sync_file_none) },- onClick = { fileMenuExpanded = true },- )- DropdownMenu(- expanded = fileMenuExpanded,- onDismissRequest = { fileMenuExpanded = false },- ) {- DropdownMenuItem(- text = { Text(stringResource(R.string.sync_choose_file)) },- onClick = {- fileMenuExpanded = false- openLauncher.launch(arrayOf("application/octet-stream", "application/x-keepass", "*/*"))- },- )- DropdownMenuItem(- text = { Text(stringResource(R.string.sync_create_file)) },- onClick = {- fileMenuExpanded = false- createLauncher.launch("passwords.kdbx")- },- )- }- }- SettingsDivider()-- SettingsRow(- title = stringResource(R.string.sync_vault_password),- supportingText = stringResource(- if (passwordSaved) UiR.string.saved else R.string.sync_vault_password_unset,- ),- onClick = { showPasswordDialog = true },- )- SettingsDivider()-- SettingsRow(- title = stringResource(R.string.sync_last),- supportingText = syncStatusText(lastSync, status, error),- )- SettingsDivider()-- if (status == KdbxSyncSettings.STATUS_QUARANTINED) {- QuarantineSection(- reason = quarantine,- quarantinedAt = quarantineAt,- onResolveKeepFile = {- scope.launch {- KdbxSyncSettings.clearQuarantine(context)- KdbxSyncScheduler.syncNow(context)- }- },- )- SettingsDivider()- }-- SettingsRow(- title = stringResource(R.string.sync_now),- enabled = enabled,- onClick = { KdbxSyncScheduler.syncNow(context) },- )- }-- if (showPasswordDialog) {- VaultPasswordDialog(- onDismiss = { showPasswordDialog = false },- onSave = { value ->- KdbxPasswordHelper(context).storePassphrase(value)- passwordSaved = true- showPasswordDialog = false- },- )- }-}--@Composable-private fun syncStatusText(lastSync: Long, status: String, error: String): String {- val context = LocalContext.current- val whenText = if (lastSync == 0L) {- stringResource(R.string.sync_never)- } else {- DateFormat.getMediumDateFormat(context).format(Date(lastSync)) + " " +- DateFormat.getTimeFormat(context).format(Date(lastSync))- }- if (status != KdbxSyncSettings.STATUS_ERROR && status != KdbxSyncSettings.STATUS_QUARANTINED) return whenText- if (status == KdbxSyncSettings.STATUS_QUARANTINED) {- return "$whenText\n${stringResource(R.string.sync_error_quarantined)}"- }- val reason = when (error) {- "file_missing" -> stringResource(R.string.sync_error_file_missing)- "wrong_password" -> stringResource(R.string.sync_error_wrong_password)- "verify_failed" -> stringResource(R.string.sync_error_verify_failed)- else -> stringResource(R.string.sync_error_generic, error)- }- return "$whenText\n$reason"-}--@Composable-private fun QuarantineSection(- reason: String,- quarantinedAt: Long,- onResolveKeepFile: () -> Unit,-) {- val context = LocalContext.current- var showResolveDialog by remember { mutableStateOf(false) }- val whenText = if (quarantinedAt == 0L) {- ""- } else {- DateFormat.getMediumDateFormat(context).format(Date(quarantinedAt)) + " " +- DateFormat.getTimeFormat(context).format(Date(quarantinedAt))- }-- SettingsRow(- title = stringResource(R.string.sync_quarantine_title),- supportingText = stringResource(R.string.sync_quarantine_description, whenText),- onClick = { showResolveDialog = true },- )-- if (showResolveDialog) {- AlertDialog(- onDismissRequest = { showResolveDialog = false },- title = { Text(stringResource(R.string.sync_quarantine_title)) },- text = { Text(stringResource(R.string.sync_quarantine_dialog, reason)) },- confirmButton = {- TextButton(- onClick = {- showResolveDialog = false- onResolveKeepFile()- },- ) {- Text(stringResource(R.string.sync_quarantine_resolve))- }- },- dismissButton = {- TextButton(onClick = { showResolveDialog = false }) {- Text(stringResource(UiR.string.cancel))- }- },- )- }-}--@Composable-private fun VaultPasswordDialog(onDismiss: () -> Unit, onSave: (String) -> Unit) {- var value by remember { mutableStateOf("") }- AlertDialog(- onDismissRequest = onDismiss,- title = { Text(stringResource(R.string.sync_vault_password)) },- text = {- OutlinedTextField(- value = value,- onValueChange = { value = it },- singleLine = true,- visualTransformation = PasswordVisualTransformation(),- label = { Text(stringResource(R.string.sync_vault_password)) },- )- },- confirmButton = {- TextButton(onClick = { onSave(value) }, enabled = value.isNotEmpty()) {- Text(stringResource(UiR.string.save))- }- },- dismissButton = {- TextButton(onClick = onDismiss) { Text(stringResource(UiR.string.cancel)) }- },- )-}
photos/src/main/java/com/vayunmathur/photos/ui/GalleryPinchZoom.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPinchZoom.kt b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPinchZoom.ktnew file mode 100644index 0000000000..f73d64e1b4--- /dev/null+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryPinchZoom.kt@@ -0,0 +1,29 @@+package com.vayunmathur.photos.ui++import androidx.compose.foundation.gestures.awaitEachGesture+import androidx.compose.foundation.gestures.calculateZoom+import androidx.compose.ui.Modifier+import androidx.compose.ui.input.pointer.PointerEventPass+import androidx.compose.ui.input.pointer.changedToUp+import androidx.compose.ui.input.pointer.pointerInput++/**+ * Pinch-to-zoom gesture that drives the gallery grid column count.+ * Extracted from GalleryScreen.kt (FileLength: `ui` package files cap at 350 lines).+ */+internal fun Modifier.pinchToZoomColumns(getColumnCount: () -> Float, setColumnCount: (Float) -> Unit): Modifier =+ pointerInput(Unit) {+ awaitEachGesture {+ while (true) {+ val event = awaitPointerEvent(PointerEventPass.Initial)+ if (event.changes.size > 1) {+ val zoom = event.calculateZoom()+ if (zoom != 1f) {+ setColumnCount((getColumnCount() / zoom).coerceIn(2f, 8f))+ event.changes.forEach { it.consume() }+ }+ }+ if (event.changes.all { it.changedToUp() }) break+ }+ }+ }
photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.ktindex e299b28397..8be09ac85a 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/GalleryScreen.kt@@ -9,8 +9,6 @@ import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.IntentSenderRequest import androidx.activity.result.contract.ActivityResultContracts-import androidx.compose.foundation.gestures.awaitEachGesture-import androidx.compose.foundation.gestures.calculateZoom import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column@@ -47,9 +45,6 @@ import androidx.compose.runtime.setValue import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import androidx.compose.ui.Modifier-import androidx.compose.ui.input.pointer.PointerEventPass-import androidx.compose.ui.input.pointer.changedToUp-import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalResources import androidx.compose.ui.res.stringResource@@ -332,20 +327,3 @@ fun GalleryScreen( ) } }--internal fun Modifier.pinchToZoomColumns(getColumnCount: () -> Float, setColumnCount: (Float) -> Unit): Modifier =- pointerInput(Unit) {- awaitEachGesture {- while (true) {- val event = awaitPointerEvent(PointerEventPass.Initial)- if (event.changes.size > 1) {- val zoom = event.calculateZoom()- if (zoom != 1f) {- setColumnCount((getColumnCount() / zoom).coerceIn(2f, 8f))- event.changes.forEach { it.consume() }- }- }- if (event.changes.all { it.changedToUp() }) break- }- }- }