Skip to content

Commit

Permalink
perf: Improved song loading by moving memory-expensive functions to t…
Browse files Browse the repository at this point in the history
…he VM

Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Apr 5, 2024
1 parent f41eac4 commit 1c2c421
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -131,15 +137,17 @@ 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
)
}
}

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()
}
}
Expand Down

0 comments on commit 1c2c421

Please sign in to comment.