From 1c2c421991ddbfa1af6393da88e59ef69835ba49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Font=C3=A1n?= Date: Fri, 5 Apr 2024 23:04:04 +0200 Subject: [PATCH] perf: Improved song loading by moving memory-expensive functions to the VM MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gabriel Fontán --- .../tageditor/ID3MetadataEditorPage.kt | 27 +++---------------- .../ID3MetadataEditorPageViewModel.kt | 14 +++++++--- 2 files changed, 14 insertions(+), 27 deletions(-) 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 9b3eca6..4067921 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,7 +1,6 @@ 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 @@ -35,7 +34,6 @@ import androidx.compose.material3.TextButton import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.SideEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -60,7 +58,6 @@ 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.toAudioFileMetadata import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -108,15 +105,6 @@ fun ID3MetadataEditorPage( sendActivityIntent.launch(intent) } - BackHandler { - val hasChanges = propertiesCopy != viewState.metadata?.propertyMap?.toAudioFileMetadata() - if (hasChanges) { - showNotSavedChangesDialog = true - } else { - navController.popBackStack() - } - } - Scaffold( topBar = { TopAppBar(title = { @@ -170,21 +158,12 @@ fun ID3MetadataEditorPage( } is ID3MetadataEditorPageViewModel.Companion.ID3MetadataEditorPageState.Success -> { - SideEffect { - propertiesCopy = actualPageState.metadata.propertyMap.toAudioFileMetadata() - } - - val artworkUri = parcelableSong.artworkPath - var showMediaStoreInfoDialog by remember { mutableStateOf(false) } - val audioStats by remember(actualPageState.metadata) { - mutableStateOf(actualPageState.metadata.audioProperties) - } + val artworkUri = parcelableSong.artworkPath - val songProperties by remember(actualPageState.metadata) { - mutableStateOf(actualPageState.metadata.propertyMap.toAudioFileMetadata()) - } + val songProperties = viewState.audioFileMetadata!! + val audioStats = viewState.audioProperties!! val scrollState = rememberScrollState() 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 8f15d7e..5b8bcd6 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 @@ -9,7 +9,9 @@ import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.bobbyesp.utilities.mediastore.AudioFileMetadata +import com.bobbyesp.utilities.mediastore.AudioFileMetadata.Companion.toAudioFileMetadata import com.bobbyesp.utilities.mediastore.MediaStoreReceiver +import com.kyant.taglib.AudioProperties import com.kyant.taglib.AudioPropertiesReadStyle import com.kyant.taglib.Metadata import com.kyant.taglib.TagLib @@ -36,6 +38,8 @@ class ID3MetadataEditorPageViewModel @Inject constructor( data class PageViewState( val metadata: Metadata? = null, + val audioFileMetadata: AudioFileMetadata? = null, + val audioProperties: AudioProperties? = null, val state: ID3MetadataEditorPageState = ID3MetadataEditorPageState.Loading, ) @@ -65,7 +69,9 @@ class ID3MetadataEditorPageViewModel @Inject constructor( updateMetadata(metadata) - updateState(ID3MetadataEditorPageState.Success(metadata)) + updateState(ID3MetadataEditorPageState.Success) + + propertiesCopy.value = metadata.propertyMap.toAudioFileMetadata() } }.onFailure { error -> Log.e( @@ -131,7 +137,9 @@ class ID3MetadataEditorPageViewModel @Inject constructor( private fun updateMetadata(metadata: Metadata? = null) { mutablePageViewState.update { it.copy( - metadata = metadata + metadata = metadata, + audioFileMetadata = metadata?.propertyMap?.toAudioFileMetadata(), + audioProperties = metadata?.audioProperties ) } } @@ -139,7 +147,7 @@ class ID3MetadataEditorPageViewModel @Inject constructor( companion object { sealed class ID3MetadataEditorPageState { data object Loading : ID3MetadataEditorPageState() - data class Success(val metadata: Metadata) : ID3MetadataEditorPageState() + data object Success : ID3MetadataEditorPageState() data class Error(val throwable: Throwable) : ID3MetadataEditorPageState() } }