Skip to content

Commit 32bbfa1

Browse files
extended local preset deletion
1 parent fb690b5 commit 32bbfa1

2 files changed

Lines changed: 87 additions & 18 deletions

File tree

app/src/main/java/com/better/nothing/music/vizualizer/ui/GlyphSettingsScreen.kt

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,13 @@ import androidx.compose.foundation.layout.widthIn
3737
import androidx.compose.material.icons.Icons
3838
import androidx.compose.material.icons.filled.CloudDownload
3939
import androidx.compose.material.icons.filled.FolderOpen
40+
import androidx.compose.material.icons.filled.Delete
4041
import androidx.compose.material.icons.filled.Public
4142
import androidx.compose.material.icons.filled.Star
4243
import androidx.compose.material.icons.filled.Sync
4344
import androidx.compose.foundation.layout.PaddingValues
4445
import androidx.compose.material3.*
45-
import androidx.compose.runtime.Composable
46-
import androidx.compose.runtime.LaunchedEffect
47-
import androidx.compose.runtime.getValue
48-
import androidx.compose.runtime.key
49-
import androidx.compose.runtime.mutableIntStateOf
50-
import androidx.compose.runtime.mutableStateOf
51-
import androidx.compose.runtime.remember
46+
import androidx.compose.runtime.*
5247
import androidx.compose.ui.Alignment
5348
import androidx.compose.ui.Modifier
5449
import androidx.compose.ui.geometry.Offset
@@ -85,6 +80,29 @@ internal fun GlyphsScreen(
8580
presets.firstOrNull { it.key == selectedPreset } ?: presets.firstOrNull()
8681
}
8782

83+
var showDeleteConfirm by remember { mutableStateOf<String?>(null) }
84+
85+
if (showDeleteConfirm != null) {
86+
AlertDialog(
87+
onDismissRequest = { showDeleteConfirm = null },
88+
title = { Text("Delete Preset?") },
89+
text = { Text("Are you sure you want to delete the local preset '${showDeleteConfirm}'?") },
90+
confirmButton = {
91+
TextButton(onClick = {
92+
showDeleteConfirm?.let { viewModel.deleteCustomPreset(it) }
93+
showDeleteConfirm = null
94+
}) {
95+
Text("Delete", color = Color.Red)
96+
}
97+
},
98+
dismissButton = {
99+
TextButton(onClick = { showDeleteConfirm = null }) {
100+
Text("Cancel")
101+
}
102+
}
103+
)
104+
}
105+
88106
Column(
89107
modifier = Modifier
90108
.fillMaxSize()
@@ -245,17 +263,36 @@ internal fun GlyphsScreen(
245263
)
246264
),
247265
) {
248-
Crossfade(
249-
targetState = selectedInfo?.description,
250-
label = "desc_fade",
251-
animationSpec = spring(stiffness = Spring.StiffnessMedium)
252-
) { description ->
253-
Text(
254-
text = description ?: stringResource(R.string.glyph_no_config),
255-
style = MaterialTheme.typography.bodyLarge.copy(lineHeight = 22.sp),
256-
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.9f),
257-
modifier = Modifier.fillMaxWidth(),
258-
)
266+
Row(
267+
modifier = Modifier.fillMaxWidth(),
268+
verticalAlignment = Alignment.CenterVertically
269+
) {
270+
Crossfade(
271+
targetState = selectedInfo?.description,
272+
label = "desc_fade",
273+
animationSpec = spring(stiffness = Spring.StiffnessMedium),
274+
modifier = Modifier.weight(1f)
275+
) { description ->
276+
Text(
277+
text = description ?: stringResource(R.string.glyph_no_config),
278+
style = MaterialTheme.typography.bodyLarge.copy(lineHeight = 22.sp),
279+
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.9f),
280+
modifier = Modifier.fillMaxWidth(),
281+
)
282+
}
283+
284+
if (selectedInfo?.description?.startsWith("Custom:") == true) {
285+
IconButton(
286+
onClick = { showDeleteConfirm = selectedInfo.key },
287+
modifier = Modifier.padding(start = 8.dp)
288+
) {
289+
Icon(
290+
Icons.Default.Delete,
291+
contentDescription = "Delete Local Preset",
292+
tint = MaterialTheme.colorScheme.error.copy(alpha = 0.7f)
293+
)
294+
}
295+
}
259296
}
260297
}
261298

app/src/main/java/com/better/nothing/music/vizualizer/ui/MainActivity.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,38 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
12551255
}
12561256
}
12571257

1258+
fun deleteCustomPreset(presetKey: String) {
1259+
viewModelScope.launch(Dispatchers.IO) {
1260+
try {
1261+
val file = File(ctx.filesDir, "zones.config")
1262+
if (!file.exists()) return@launch
1263+
1264+
val json = JSONObject(file.readText())
1265+
if (json.has(presetKey)) {
1266+
json.remove(presetKey)
1267+
file.writeText(json.toString(2))
1268+
1269+
// Remove from favorites if present
1270+
val currentFavs = _favoritePresets.value.toMutableSet()
1271+
if (currentFavs.remove(presetKey)) {
1272+
_favoritePresets.value = currentFavs
1273+
ctx.getSharedPreferences("viz_prefs", Context.MODE_PRIVATE)
1274+
.edit().putStringSet("favorite_presets", currentFavs).apply()
1275+
}
1276+
1277+
refreshPresetsInternal()
1278+
MainActivity.serviceStatic?.reloadConfig()
1279+
1280+
withContext(Dispatchers.Main) {
1281+
Toast.makeText(ctx, "Preset deleted", Toast.LENGTH_SHORT).show()
1282+
}
1283+
}
1284+
} catch (e: Exception) {
1285+
Log.e("MainViewModel", "Failed to delete custom preset", e)
1286+
}
1287+
}
1288+
}
1289+
12581290
fun downloadPreset(preset: CommunityPreset) {
12591291
saveCustomPreset(preset.name, preset.zones.map { it.toZoneSpec() }, preset.author)
12601292
analytics.logCommunityPresetDownloaded(preset.name, preset.author)

0 commit comments

Comments
 (0)