diff --git a/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPage.kt b/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPage.kt index 9fa0494..9b3eca6 100644 --- a/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPage.kt +++ b/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPage.kt @@ -1,6 +1,7 @@ package com.bobbyesp.metadator.presentation.pages.utilities.tageditor import android.app.Activity +import androidx.activity.compose.BackHandler import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.IntentSenderRequest import androidx.activity.result.contract.ActivityResultContracts @@ -14,7 +15,6 @@ 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 @@ -45,7 +45,6 @@ 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 @@ -61,7 +60,7 @@ import com.bobbyesp.ui.components.others.MetadataTag import com.bobbyesp.ui.components.text.LargeCategoryTitle import com.bobbyesp.ui.components.text.MarqueeText import com.bobbyesp.ui.components.text.PreConfiguredOutlinedTextField -import com.bobbyesp.utilities.mediastore.AudioFileMetadata.Companion.toFileMetadata +import com.bobbyesp.utilities.mediastore.AudioFileMetadata.Companion.toAudioFileMetadata import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -109,6 +108,15 @@ fun ID3MetadataEditorPage( sendActivityIntent.launch(intent) } + BackHandler { + val hasChanges = propertiesCopy != viewState.metadata?.propertyMap?.toAudioFileMetadata() + if (hasChanges) { + showNotSavedChangesDialog = true + } else { + navController.popBackStack() + } + } + Scaffold( topBar = { TopAppBar(title = { @@ -163,7 +171,7 @@ fun ID3MetadataEditorPage( is ID3MetadataEditorPageViewModel.Companion.ID3MetadataEditorPageState.Success -> { SideEffect { - propertiesCopy = actualPageState.metadata.propertyMap.toFileMetadata() + propertiesCopy = actualPageState.metadata.propertyMap.toAudioFileMetadata() } val artworkUri = parcelableSong.artworkPath @@ -175,7 +183,7 @@ fun ID3MetadataEditorPage( } val songProperties by remember(actualPageState.metadata) { - mutableStateOf(actualPageState.metadata.propertyMap.toFileMetadata()) + mutableStateOf(actualPageState.metadata.propertyMap.toAudioFileMetadata()) } val scrollState = rememberScrollState() @@ -408,7 +416,6 @@ fun ID3MetadataEditorPage( if (showNotSavedChangesDialog) { NotSavedChanges( - changelog = AnnotatedString("AnnotatedString(\"changesDiff.value!!\")"), onDismissChanges = { showNotSavedChangesDialog = false navController.popBackStack() @@ -422,7 +429,7 @@ fun ID3MetadataEditorPage( @Composable private fun NotSavedChanges( - changelog: AnnotatedString, onDismissChanges: () -> Unit = {}, onReturnToPage: () -> Unit = {} + onDismissChanges: () -> Unit = {}, onReturnToPage: () -> Unit = {} ) { AlertDialog( onDismissRequest = onReturnToPage, @@ -436,15 +443,10 @@ private fun NotSavedChanges( Text(text = stringResource(id = R.string.unsaved_changes)) }, text = { - Column( - modifier = Modifier - .heightIn(min = 200.dp, max = 400.dp) - .verticalScroll(rememberScrollState()) - ) { - Text( - text = changelog, - ) - } + Text( + text = stringResource(id = R.string.unsaved_changes_info), + style = MaterialTheme.typography.bodyMedium + ) }, dismissButton = { TextButton( @@ -452,7 +454,6 @@ private fun NotSavedChanges( ) { Text(text = stringResource(id = R.string.return_str)) } - }, confirmButton = { Button( diff --git a/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPageViewModel.kt b/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPageViewModel.kt index d486e2a..8f15d7e 100644 --- a/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPageViewModel.kt +++ b/app/src/main/java/com/bobbyesp/metadator/presentation/pages/utilities/tageditor/ID3MetadataEditorPageViewModel.kt @@ -6,15 +6,10 @@ import android.content.Context import android.os.Build import android.util.Log import androidx.compose.runtime.mutableStateOf -import androidx.compose.ui.text.AnnotatedString -import androidx.compose.ui.text.SpanStyle -import androidx.compose.ui.text.font.FontWeight import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.bobbyesp.metadator.R import com.bobbyesp.utilities.mediastore.AudioFileMetadata import com.bobbyesp.utilities.mediastore.MediaStoreReceiver -import com.bobbyesp.utilities.mediastore.MetadataChangesDiff import com.kyant.taglib.AudioPropertiesReadStyle import com.kyant.taglib.Metadata import com.kyant.taglib.TagLib @@ -125,26 +120,6 @@ class ID3MetadataEditorPageViewModel @Inject constructor( } } - fun generateUnsavedChangesText( - context: Context, - changes: MetadataChangesDiff - ): AnnotatedString { - val builder = AnnotatedString.Builder() - val localizedInfoText = context.getString(R.string.unsaved_changes_info) - builder.append("$localizedInfoText \n") - - changes.forEach { (key, value) -> - val start = builder.length - builder.append("$key: ") - val end = builder.length - builder.addStyle(SpanStyle(fontWeight = FontWeight.Bold), start, end) - builder.append("${value.first?.joinToString()} -> ${value.second?.joinToString()}\n") - } - - return builder.toAnnotatedString() - } - - private fun updateState(state: ID3MetadataEditorPageState) { mutablePageViewState.update { it.copy( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7002092..a3016f8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,6 +43,6 @@ Return Warning Unsaved changes - The following fields have unsaved changes: + There are some metadata fields that has been changed and not saved. Do you really want to discard the changes? \ No newline at end of file diff --git a/app/utilities/src/main/java/com/bobbyesp/utilities/mediastore/AudioFileMetadata.kt b/app/utilities/src/main/java/com/bobbyesp/utilities/mediastore/AudioFileMetadata.kt index c5dcd11..e0d52e8 100644 --- a/app/utilities/src/main/java/com/bobbyesp/utilities/mediastore/AudioFileMetadata.kt +++ b/app/utilities/src/main/java/com/bobbyesp/utilities/mediastore/AudioFileMetadata.kt @@ -48,7 +48,7 @@ data class AudioFileMetadata( var releaseType: Array? = null ) { companion object { - fun PropertyMap.toFileMetadata(): AudioFileMetadata { + fun PropertyMap.toAudioFileMetadata(): AudioFileMetadata { return AudioFileMetadata( title = this["TITLE"], album = this["ALBUM"], @@ -122,22 +122,6 @@ data class AudioFileMetadata( releaseType = releaseType?.copyOf() ) } - - fun AudioFileMetadata.differences(other: AudioFileMetadata): MetadataChangesDiff { - val differences = mutableMapOf?, Array?>>() - - this::class.java.declaredFields.forEach { field -> - field.isAccessible = true - val thisValue = field.get(this) as? Array - val otherValue = field.get(other) as? Array - - if (thisValue?.contentEquals(otherValue) == false) { - differences[field.name] = Pair(thisValue, otherValue) - } - } - - return differences - } } fun toPropertyMap(): PropertyMap { @@ -258,5 +242,3 @@ data class AudioFileMetadata( return result } } - -typealias MetadataChangesDiff = Map?, Array?>>