Skip to content

Commit

Permalink
Deleted inefficient changes parser
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Apr 5, 2024
1 parent 4f23f39 commit f41eac4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -408,7 +416,6 @@ fun ID3MetadataEditorPage(

if (showNotSavedChangesDialog) {
NotSavedChanges(
changelog = AnnotatedString("AnnotatedString(\"changesDiff.value!!\")"),
onDismissChanges = {
showNotSavedChangesDialog = false
navController.popBackStack()
Expand All @@ -422,7 +429,7 @@ fun ID3MetadataEditorPage(

@Composable
private fun NotSavedChanges(
changelog: AnnotatedString, onDismissChanges: () -> Unit = {}, onReturnToPage: () -> Unit = {}
onDismissChanges: () -> Unit = {}, onReturnToPage: () -> Unit = {}
) {
AlertDialog(
onDismissRequest = onReturnToPage,
Expand All @@ -436,23 +443,17 @@ 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(
onClick = onReturnToPage,
) {
Text(text = stringResource(id = R.string.return_str))
}

},
confirmButton = {
Button(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
<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>
<string name="unsaved_changes_info">There are some metadata fields that has been changed and not saved. Do you really want to discard the changes?</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data class AudioFileMetadata(
var releaseType: Array<String>? = null
) {
companion object {
fun PropertyMap.toFileMetadata(): AudioFileMetadata {
fun PropertyMap.toAudioFileMetadata(): AudioFileMetadata {
return AudioFileMetadata(
title = this["TITLE"],
album = this["ALBUM"],
Expand Down Expand Up @@ -122,22 +122,6 @@ 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 @@ -258,5 +242,3 @@ data class AudioFileMetadata(
return result
}
}

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

0 comments on commit f41eac4

Please sign in to comment.