From 96183984ef3e4d085037757d3206ec5ee6693d28 Mon Sep 17 00:00:00 2001 From: sim Date: Mon, 2 Dec 2024 10:09:19 +0000 Subject: [PATCH] Restore debug dialog --- .../nextpush/activities/MainViewModel.kt | 23 +++++++++++ .../nextpush/activities/ui/MainUi.kt | 39 ++++++++++++++++++- .../nextpush/activities/ui/MainUiState.kt | 3 +- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt index cebe486..f7e9c09 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt @@ -2,6 +2,8 @@ package org.unifiedpush.distributor.nextpush.activities import android.content.Context import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel @@ -26,6 +28,10 @@ class MainViewModel( var registrationsState by mutableStateOf(registrationsState) private set + private var lastDebugClickTime by mutableLongStateOf(0L) + + private var debugClickCount by mutableIntStateOf(0) + fun disableBatteryOptimization() { viewModelScope.launch { mainUiState = mainUiState.copy(requireBatteryOptimization = false) @@ -90,4 +96,21 @@ class MainViewModel( ) } } + + fun addDebugClick() { + val currentTime = System.currentTimeMillis() + if (currentTime - lastDebugClickTime < 500) { + debugClickCount++ + if (debugClickCount == 5) { + mainUiState = mainUiState.copy(showDebugInfo = true) + } + } else { + debugClickCount = 1 + } + lastDebugClickTime = currentTime + } + + fun dismissDebugInfo() { + mainUiState = mainUiState.copy(showDebugInfo = false) + } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt index da6e241..00e5739 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.combinedClickable +import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues @@ -11,6 +12,8 @@ import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.text.selection.SelectionContainer +import androidx.compose.material3.AlertDialog import androidx.compose.material3.Card import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.Icon @@ -19,6 +22,7 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color @@ -32,6 +36,7 @@ import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.activities.AppAction import org.unifiedpush.distributor.nextpush.activities.MainViewModel import org.unifiedpush.distributor.nextpush.activities.publishAction +import org.unifiedpush.distributor.nextpush.utils.getDebugInfo @Composable fun MainUi(viewModel: MainViewModel) { @@ -113,7 +118,13 @@ fun MainUiContent(viewModel: MainViewModel, innerPadding: PaddingValues) { Text( text = stringResource(R.string.main_applications_title), - style = MaterialTheme.typography.headlineMedium + style = MaterialTheme.typography.headlineMedium, + modifier = Modifier.clickable( + indication = null, + interactionSource = remember { MutableInteractionSource() } + ) { + viewModel.addDebugClick() + } ) } @@ -184,6 +195,32 @@ fun MainUiContent(viewModel: MainViewModel, innerPadding: PaddingValues) { } } } + if (viewModel.mainUiState.showDebugInfo) { + DebugDialog { + viewModel.dismissDebugInfo() + } + } +} + +@Composable +fun DebugDialog(onDismissRequest: () -> Unit) { + val text = getDebugInfo() + AlertDialog( + title = { Text("Debug") }, + text = { + SelectionContainer { + Text(text) + } + }, + onDismissRequest = onDismissRequest, + confirmButton = { + TextButton( + onClick = onDismissRequest + ) { + Text(stringResource(android.R.string.ok)) + } + } + ) } @Preview diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUiState.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUiState.kt index 0976ba0..01ac63d 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUiState.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUiState.kt @@ -6,7 +6,8 @@ import org.unifiedpush.distributor.nextpush.account.AccountFactory data class MainUiState( val requireBatteryOptimization: Boolean, - val accountName: String + val accountName: String, + val showDebugInfo: Boolean = false ) { constructor(context: Context) : this( requireBatteryOptimization = !(