Skip to content

Commit

Permalink
feat(WIP): Started creating NotSavedChanges dialog as confirmation fo…
Browse files Browse the repository at this point in the history
…r discarding or not changes

Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Apr 2, 2024
1 parent 543e750 commit 844a4d4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Warning
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
Expand All @@ -38,6 +45,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
Expand Down Expand Up @@ -94,6 +102,8 @@ fun ID3MetadataEditorPage(
}
}

var showNotSavedChangesDialog by remember { mutableStateOf(false) }

fun saveInMediaStore(): Boolean = viewModel.saveMetadata(
newMetadata = viewState.metadata?.copy(
propertyMap = propertiesCopy!!.toPropertyMap()
Expand Down Expand Up @@ -161,7 +171,7 @@ fun ID3MetadataEditorPage(
}

val artworkUri = parcelableSong.artworkPath
var showArtwork by remember { mutableStateOf(true) }

var showMediaStoreInfoDialog by remember { mutableStateOf(false) }

val audioStats by remember(actualPageState.metadata) {
Expand Down Expand Up @@ -399,4 +409,62 @@ fun ID3MetadataEditorPage(
}
}
}

if (showNotSavedChangesDialog) {
NotSavedChanges(
changelog = AnnotatedString("AnnotatedString(\"changesDiff.value!!\")"),
onDismissChanges = {
showNotSavedChangesDialog = false
navController.popBackStack()
},
onReturnToPage = {
showNotSavedChangesDialog = false
}
)
}
}

@Composable
private fun NotSavedChanges(
changelog: AnnotatedString, onDismissChanges: () -> Unit = {}, onReturnToPage: () -> Unit = {}
) {
AlertDialog(
onDismissRequest = onReturnToPage,
icon = {
Icon(
imageVector = Icons.Rounded.Warning,
contentDescription = stringResource(id = R.string.warning)
)
},
title = {
Text(text = stringResource(id = R.string.unsaved_changes))
},
text = {
Column(
modifier = Modifier
.heightIn(min = 200.dp, max = 400.dp)
.verticalScroll(rememberScrollState())
) {
Text(
text = changelog,
)
}
},
dismissButton = {
TextButton(
onClick = onReturnToPage,
) {
Text(text = stringResource(id = R.string.return_str))
}

},
confirmButton = {
Button(
onClick = onDismissChanges,
colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.error)
) {
Text(text = stringResource(id = R.string.discard_changes))
}
}
)
}
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
<string name="layout_type">Layout type</string>
<string name="reload_media_store">Reload MediaStore</string>
<string name="audio_details">Audio details</string>
<string name="discard_changes">Discard changes</string>
<string name="return_str">Return</string>
<string name="warning">Warning</string>
<string name="unsaved_changes">Unsaved changes</string>
<string name="unsaved_changes_info">The following fields have unsaved changes:</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ data class AudioFileMetadata(
releaseType = releaseType?.copyOf()
)
}

fun AudioFileMetadata.differences(other: AudioFileMetadata): MetadataChangesDiff {
val differences = mutableMapOf<String, Pair<Array<String>?, Array<String>?>>()

this::class.java.declaredFields.forEach { field ->
field.isAccessible = true
val thisValue = field.get(this) as? Array<String>
val otherValue = field.get(other) as? Array<String>

if (thisValue?.contentEquals(otherValue) == false) {
differences[field.name] = Pair(thisValue, otherValue)
}
}

return differences
}
}

fun toPropertyMap(): PropertyMap {
Expand Down Expand Up @@ -242,3 +258,5 @@ data class AudioFileMetadata(
return result
}
}

typealias MetadataChangesDiff = Map<String, Pair<Array<String>?, Array<String>?>>

0 comments on commit 844a4d4

Please sign in to comment.