Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/java/com/cashpilot/android/model/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ data class Settings(
val apiKey: String = "",
val heartbeatIntervalSeconds: Int = 30,
val enabledSlugs: Set<String> = KnownApps.all.map { it.slug }.toSet(),
val setupCompleted: Boolean = false,
)
12 changes: 3 additions & 9 deletions app/src/main/java/com/cashpilot/android/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,17 @@ class MainActivity : ComponentActivity() {
CashPilotTheme {
Surface(modifier = Modifier.fillMaxSize()) {
val settings by viewModel.settings.collectAsState()
val hasNotif by viewModel.hasNotificationAccess.collectAsState()
val hasUsage by viewModel.hasUsageAccess.collectAsState()
var showSettings by rememberSaveable { mutableStateOf(false) }
var setupDismissed by rememberSaveable { mutableStateOf(false) }

val needsSetup = !setupDismissed &&
(settings.serverUrl.isBlank() || settings.apiKey.isBlank() || !hasNotif || !hasUsage)

// Handle system Back from Settings → return to Dashboard
BackHandler(enabled = showSettings && !needsSetup) {
BackHandler(enabled = showSettings && settings.setupCompleted) {
showSettings = false
}

when {
needsSetup -> SetupScreen(
!settings.setupCompleted -> SetupScreen(
viewModel = viewModel,
onComplete = { setupDismissed = true },
onComplete = {},
)
showSettings -> SettingsScreen(
viewModel = viewModel,
Expand Down
89 changes: 50 additions & 39 deletions app/src/main/java/com/cashpilot/android/ui/screen/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import androidx.compose.foundation.layout.fillMaxSize
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.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand All @@ -38,6 +41,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.cashpilot.android.R
import com.cashpilot.android.model.KnownApps
Expand All @@ -47,6 +51,7 @@ import com.cashpilot.android.ui.MainViewModel
@Composable
fun SettingsScreen(viewModel: MainViewModel, onBack: () -> Unit) {
val settings by viewModel.settings.collectAsState()
val hasBattery by viewModel.hasBatteryOptOut.collectAsState()
val context = LocalContext.current

// Local state for text fields — avoids per-keystroke DataStore writes
Expand Down Expand Up @@ -122,35 +127,34 @@ fun SettingsScreen(viewModel: MainViewModel, onBack: () -> Unit) {
)
}

// Permissions section
// Battery optimization
item {
Spacer(modifier = Modifier.height(8.dp))
Text("Permissions", style = MaterialTheme.typography.titleMedium)
Text(
"Grant these permissions for full app detection.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
item {
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
OutlinedButton(
onClick = { openNotificationListenerSettings(context) },
modifier = Modifier.fillMaxWidth(),
) {
Text("Grant Notification Access")
}
OutlinedButton(
onClick = { openUsageAccessSettings(context) },
modifier = Modifier.fillMaxWidth(),
if (hasBattery) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text("Grant Usage Access")
Icon(
Icons.Default.CheckCircle,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(20.dp),
)
Spacer(Modifier.width(8.dp))
Text(
stringResource(R.string.battery_unrestricted),
style = MaterialTheme.typography.bodyMedium,
)
}
} else {
OutlinedButton(
onClick = { openBatteryOptimizationSettings(context) },
modifier = Modifier.fillMaxWidth(),
) {
Text("Disable Battery Optimization")
Text(stringResource(R.string.battery_disable_optimization))
}
}
}
Expand Down Expand Up @@ -197,12 +201,24 @@ fun SettingsScreen(viewModel: MainViewModel, onBack: () -> Unit) {
onClick = { openUrl(context, "https://github.com/GeiserX/CashPilot-android") },
modifier = Modifier.fillMaxWidth(),
) {
Icon(
painter = painterResource(R.drawable.ic_github),
contentDescription = null,
modifier = Modifier.size(18.dp),
)
Spacer(Modifier.width(8.dp))
Text(stringResource(R.string.about_github_android))
}
OutlinedButton(
onClick = { openUrl(context, "https://github.com/GeiserX/CashPilot") },
modifier = Modifier.fillMaxWidth(),
) {
Icon(
painter = painterResource(R.drawable.ic_github),
contentDescription = null,
modifier = Modifier.size(18.dp),
)
Spacer(Modifier.width(8.dp))
Text(stringResource(R.string.about_github_server))
}
Button(
Expand All @@ -226,23 +242,18 @@ private fun openUrl(context: Context, url: String) {
)
}

private fun openNotificationListenerSettings(context: Context) {
context.startActivity(
Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
}

private fun openUsageAccessSettings(context: Context) {
context.startActivity(
Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
}

private fun openBatteryOptimizationSettings(context: Context) {
context.startActivity(
Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
try {
context.startActivity(
Intent(
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
android.net.Uri.parse("package:${context.packageName}"),
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
} catch (_: Exception) {
context.startActivity(
Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
}
}
16 changes: 7 additions & 9 deletions app/src/main/java/com/cashpilot/android/ui/screen/SetupScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ fun SetupScreen(viewModel: MainViewModel, onComplete: () -> Unit) {

val serverDone = localUrl.isNotBlank() && localKey.isNotBlank()

val finishSetup = {
viewModel.updateSettings { it.copy(serverUrl = localUrl, apiKey = localKey, setupCompleted = true) }
onComplete()
}

Scaffold { padding ->
Column(
modifier = Modifier
Expand Down Expand Up @@ -186,22 +191,15 @@ fun SetupScreen(viewModel: MainViewModel, onComplete: () -> Unit) {
Spacer(Modifier.height(8.dp))

Button(
onClick = {
// Persist any pending text field values
viewModel.updateSettings { it.copy(serverUrl = localUrl, apiKey = localKey) }
onComplete()
},
onClick = finishSetup,
modifier = Modifier.fillMaxWidth(),
enabled = serverDone && hasNotif && hasUsage,
) {
Text(stringResource(R.string.setup_continue))
}

TextButton(
onClick = {
viewModel.updateSettings { it.copy(serverUrl = localUrl, apiKey = localKey) }
onComplete()
},
onClick = finishSetup,
modifier = Modifier.fillMaxWidth(),
) {
Text(stringResource(R.string.setup_skip))
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/cashpilot/android/util/SettingsStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
Expand All @@ -20,6 +21,7 @@ object SettingsStore {
private val API_KEY = stringPreferencesKey("api_key")
private val HEARTBEAT_INTERVAL = intPreferencesKey("heartbeat_interval")
private val ENABLED_SLUGS = stringSetPreferencesKey("enabled_slugs")
private val SETUP_COMPLETED = booleanPreferencesKey("setup_completed")

fun settings(context: Context): Flow<Settings> =
context.dataStore.data.map { prefs ->
Expand All @@ -28,6 +30,9 @@ object SettingsStore {
apiKey = prefs[API_KEY] ?: "",
heartbeatIntervalSeconds = prefs[HEARTBEAT_INTERVAL] ?: 30,
enabledSlugs = prefs[ENABLED_SLUGS] ?: KnownApps.all.map { it.slug }.toSet(),
// Migration: mark as completed only if both URL and key were configured before this field existed
setupCompleted = prefs[SETUP_COMPLETED]
?: (prefs[SERVER_URL]?.isNotEmpty() == true && prefs[API_KEY]?.isNotEmpty() == true),
)
}

Expand All @@ -38,12 +43,15 @@ object SettingsStore {
apiKey = prefs[API_KEY] ?: "",
heartbeatIntervalSeconds = prefs[HEARTBEAT_INTERVAL] ?: 30,
enabledSlugs = prefs[ENABLED_SLUGS] ?: KnownApps.all.map { it.slug }.toSet(),
setupCompleted = prefs[SETUP_COMPLETED]
?: (prefs[SERVER_URL]?.isNotEmpty() == true && prefs[API_KEY]?.isNotEmpty() == true),
)
val updated = transform(current)
prefs[SERVER_URL] = updated.serverUrl
prefs[API_KEY] = updated.apiKey
prefs[HEARTBEAT_INTERVAL] = updated.heartbeatIntervalSeconds
prefs[ENABLED_SLUGS] = updated.enabledSlugs
prefs[SETUP_COMPLETED] = updated.setupCompleted
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_github.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.477,2 2,6.477 2,12c0,4.418 2.865,8.166 6.839,9.489 0.5,0.092 0.682,-0.217 0.682,-0.482 0,-0.237 -0.008,-0.866 -0.013,-1.7 -2.782,0.603 -3.369,-1.342 -3.369,-1.342 -0.454,-1.155 -1.11,-1.462 -1.11,-1.462 -0.908,-0.62 0.069,-0.608 0.069,-0.608 1.003,0.07 1.531,1.03 1.531,1.03 0.892,1.529 2.341,1.087 2.91,0.831 0.091,-0.646 0.349,-1.086 0.635,-1.337 -2.22,-0.253 -4.555,-1.11 -4.555,-4.943 0,-1.091 0.39,-1.984 1.029,-2.683 -0.103,-0.253 -0.446,-1.27 0.098,-2.647 0,0 0.84,-0.269 2.75,1.025A9.564,9.564 0,0 1,12 6.844a9.59,9.59 0,0 1,2.504 0.337c1.909,-1.294 2.747,-1.025 2.747,-1.025 0.546,1.377 0.202,2.394 0.1,2.647 0.64,0.699 1.028,1.592 1.028,2.683 0,3.842 -2.339,4.687 -4.566,4.935 0.359,0.309 0.678,0.919 0.678,1.852 0,1.336 -0.012,2.415 -0.012,2.743 0,0.267 0.18,0.578 0.688,0.48C19.138,20.163 22,16.418 22,12c0,-5.523 -4.477,-10 -10,-10z" />
</vector>
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
<string name="state_last_active">Last: %s</string>
<string name="notification_active">Notification active</string>

<!-- Battery -->
<string name="battery_unrestricted">Battery optimization disabled</string>
<string name="battery_disable_optimization">Disable Battery Optimization</string>

<!-- Warnings -->
<string name="cleartext_warning">Insecure: API key and app data will be sent unencrypted. Use https:// if possible.</string>

<!-- About section -->
<string name="about">About</string>
<string name="about_github_android">GitHub — CashPilot Android</string>
<string name="about_github_server">GitHub — CashPilot Server</string>
<string name="about_github_android">CashPilot Android</string>
<string name="about_github_server">CashPilot Server</string>
<string name="about_donate">Sponsor / Donate</string>
</resources>
Loading