Skip to content

Commit fb690b5

Browse files
added delete button to uploaded presets
1 parent ca6fde2 commit fb690b5

4 files changed

Lines changed: 89 additions & 8 deletions

File tree

app/src/main/java/com/better/nothing/music/vizualizer/logic/CommunityRepository.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ class CommunityRepository {
5353
throw e
5454
}
5555
}
56+
57+
suspend fun deletePreset(presetId: String) {
58+
try {
59+
Log.d("CommunityRepo", "Deleting preset: $presetId")
60+
database.child(presetId).removeValue().await()
61+
Log.d("CommunityRepo", "Delete successful")
62+
} catch (e: Exception) {
63+
Log.e("CommunityRepo", "Delete failed", e)
64+
throw e
65+
}
66+
}
5667

5768
suspend fun incrementDownloadCount(presetId: String) {
5869
val ref = database.child(presetId).child("downloads")

app/src/main/java/com/better/nothing/music/vizualizer/model/CommunityPreset.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ data class CommunityPreset(
88
val id: String = "",
99
val name: String = "",
1010
val author: String = "Anonymous",
11+
val authorId: String = "",
1112
val phoneModel: String = "",
1213
val zones: List<ZoneData> = emptyList(),
1314
val timestamp: Long = System.currentTimeMillis(),

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.compose.foundation.lazy.items
99
import androidx.compose.foundation.shape.RoundedCornerShape
1010
import androidx.compose.material.icons.Icons
1111
import androidx.compose.material.icons.automirrored.filled.ArrowBack
12+
import androidx.compose.material.icons.filled.Delete
1213
import androidx.compose.material.icons.filled.Download
1314
import androidx.compose.material3.*
1415
import androidx.compose.runtime.*
@@ -24,8 +25,10 @@ import java.util.*
2425
@Composable
2526
fun CommunityPresetsScreen(
2627
presets: List<CommunityPreset>?,
28+
currentUserId: String?,
2729
error: String?,
2830
onDownload: (CommunityPreset) -> Unit,
31+
onDelete: (CommunityPreset) -> Unit,
2932
onDismiss: () -> Unit
3033
) {
3134
Scaffold(
@@ -68,7 +71,12 @@ fun CommunityPresetsScreen(
6871
verticalArrangement = Arrangement.spacedBy(12.dp)
6972
) {
7073
items(presets) { preset ->
71-
PresetCard(preset, onDownload)
74+
PresetCard(
75+
preset = preset,
76+
onDownload = onDownload,
77+
onDelete = onDelete,
78+
isOwner = currentUserId != null && preset.authorId == currentUserId
79+
)
7280
}
7381
}
7482
}
@@ -77,7 +85,12 @@ fun CommunityPresetsScreen(
7785
}
7886

7987
@Composable
80-
fun PresetCard(preset: CommunityPreset, onDownload: (CommunityPreset) -> Unit) {
88+
fun PresetCard(
89+
preset: CommunityPreset,
90+
onDownload: (CommunityPreset) -> Unit,
91+
onDelete: (CommunityPreset) -> Unit,
92+
isOwner: Boolean
93+
) {
8194
Card(
8295
shape = RoundedCornerShape(24.dp),
8396
colors = CardDefaults.cardColors(containerColor = Color(0xFF1A1A1A)),
@@ -97,6 +110,38 @@ fun PresetCard(preset: CommunityPreset, onDownload: (CommunityPreset) -> Unit) {
97110
Text(stringResource(R.string.downloads_count, date, preset.downloads), fontSize = 12.sp, color = Color.DarkGray)
98111
}
99112

113+
if (isOwner) {
114+
var showConfirm by remember { mutableStateOf(false) }
115+
116+
if (showConfirm) {
117+
AlertDialog(
118+
onDismissRequest = { showConfirm = false },
119+
title = { Text("Delete Preset?") },
120+
text = { Text("Are you sure you want to delete this preset from the community?") },
121+
confirmButton = {
122+
TextButton(onClick = {
123+
onDelete(preset)
124+
showConfirm = false
125+
}) {
126+
Text("Delete", color = Color.Red)
127+
}
128+
},
129+
dismissButton = {
130+
TextButton(onClick = { showConfirm = false }) {
131+
Text("Cancel")
132+
}
133+
}
134+
)
135+
}
136+
137+
IconButton(
138+
onClick = { showConfirm = true },
139+
modifier = Modifier.padding(end = 8.dp)
140+
) {
141+
Icon(Icons.Default.Delete, contentDescription = "Delete", tint = Color.Gray)
142+
}
143+
}
144+
100145
IconButton(
101146
onClick = { onDownload(preset) },
102147
colors = IconButtonDefaults.iconButtonColors(

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
178178
private val _userNickname = MutableStateFlow("Anonymous")
179179
val userNickname = _userNickname.asStateFlow()
180180

181+
private val _userId = MutableStateFlow<String?>(null)
182+
val userId = _userId.asStateFlow()
183+
181184
private var lastStatsSyncMs = 0L
182185

183186
private val _isShowingLeaderboard = MutableStateFlow(false)
@@ -310,22 +313,23 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
310313
_totalFlashlightTime.value = prefs.getLong("total_flashlight_time", 0L)
311314
_userNickname.value = prefs.getString("user_nickname", "Anonymous") ?: "Anonymous"
312315

313-
var userId = prefs.getString("user_id", null)
314-
if (userId == null) {
315-
userId = java.util.UUID.randomUUID().toString()
316-
prefs.edit().putString("user_id", userId).apply()
316+
var uId = prefs.getString("user_id", null)
317+
if (uId == null) {
318+
uId = java.util.UUID.randomUUID().toString()
319+
prefs.edit().putString("user_id", uId).apply()
317320
}
321+
_userId.value = uId
318322

319323
viewModelScope.launch {
320324
try {
321-
val profile = userRepository.getUserProfile(userId)
325+
val profile = userRepository.getUserProfile(uId)
322326
if (profile != null) {
323327
_userProfile.value = profile
324328
_userNickname.value = profile.displayName
325329
} else {
326330
// Create initial profile
327331
val newProfile = UserProfile(
328-
userId = userId,
332+
userId = uId,
329333
displayName = _userNickname.value,
330334
totalVisualizedTime = _totalVisualizedTime.value
331335
)
@@ -1217,6 +1221,7 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
12171221
val preset = CommunityPreset(
12181222
name = name,
12191223
author = author,
1224+
authorId = _userId.value ?: "",
12201225
phoneModel = phoneModelForDevice(selectedDevice.value),
12211226
zones = zones.map { ZoneData.fromZoneSpec(it) }
12221227
)
@@ -1234,6 +1239,22 @@ internal class MainViewModel(application: Application) : AndroidViewModel(applic
12341239
}
12351240
}
12361241

1242+
fun deleteCommunityPreset(preset: CommunityPreset) {
1243+
viewModelScope.launch {
1244+
try {
1245+
communityRepository.deletePreset(preset.id)
1246+
withContext(Dispatchers.Main) {
1247+
Toast.makeText(ctx, "Preset deleted", Toast.LENGTH_SHORT).show()
1248+
}
1249+
} catch (e: Exception) {
1250+
Log.e("MainViewModel", "Failed to delete preset", e)
1251+
withContext(Dispatchers.Main) {
1252+
Toast.makeText(ctx, "Failed to delete: ${e.message}", Toast.LENGTH_SHORT).show()
1253+
}
1254+
}
1255+
}
1256+
}
1257+
12371258
fun downloadPreset(preset: CommunityPreset) {
12381259
saveCustomPreset(preset.name, preset.zones.map { it.toZoneSpec() }, preset.author)
12391260
analytics.logCommunityPresetDownloaded(preset.name, preset.author)
@@ -2390,6 +2411,7 @@ class MainActivity : ComponentActivity() {
23902411
val leaderboardEntries by viewModel.leaderboardEntries.collectAsStateWithLifecycle()
23912412
val communityPresets by viewModel.communityPresets.collectAsStateWithLifecycle()
23922413
val communityError by viewModel.communityError.collectAsStateWithLifecycle()
2414+
val userId by viewModel.userId.collectAsStateWithLifecycle()
23932415
val thanksMessage by viewModel.thanksMessage.collectAsStateWithLifecycle()
23942416
val latestAnnouncement by viewModel.latestAnnouncement.collectAsStateWithLifecycle()
23952417
val showAnnouncementModal by viewModel.showAnnouncementModal.collectAsStateWithLifecycle()
@@ -2518,8 +2540,10 @@ class MainActivity : ComponentActivity() {
25182540
) {
25192541
CommunityPresetsScreen(
25202542
presets = communityPresets,
2543+
currentUserId = userId,
25212544
error = communityError,
25222545
onDownload = viewModel::downloadPreset,
2546+
onDelete = viewModel::deleteCommunityPreset,
25232547
onDismiss = viewModel::hideCommunity
25242548
)
25252549
}

0 commit comments

Comments
 (0)