Skip to content

Commit

Permalink
perf: Rewritten the metadata saver
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Jul 28, 2024
1 parent d4e2345 commit 357b7c1
Show file tree
Hide file tree
Showing 13 changed files with 1,021 additions and 113 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 84 additions & 56 deletions app/src/main/java/com/bobbyesp/metadator/presentation/Navigation.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.bobbyesp.metadator.presentation

import android.annotation.SuppressLint
import android.app.Activity
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.BoxWithConstraints
Expand Down Expand Up @@ -39,6 +44,7 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -85,13 +91,14 @@ import com.bobbyesp.metadator.presentation.pages.mediaplayer.MediaplayerViewMode
import com.bobbyesp.metadator.presentation.pages.mediaplayer.player.CollapsedPlayerHeight
import com.bobbyesp.metadator.presentation.pages.mediaplayer.player.PlayerAnimationSpec
import com.bobbyesp.metadator.presentation.pages.settings.SettingsPage
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.ID3MetadataEditorPage
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.ID3MetadataEditorPageViewModel
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.rework.MetadataEditorPage
import com.bobbyesp.metadator.presentation.pages.utilities.tageditor.rework.MetadataEditorVM
import com.bobbyesp.ui.components.bottomsheet.draggable.rememberDraggableBottomSheetState
import com.bobbyesp.ui.components.tags.RoundedTag
import com.bobbyesp.ui.motion.animatedComposable
import com.bobbyesp.ui.motion.slideInVerticallyComposable
import com.bobbyesp.utilities.navigation.parcelableType
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlin.reflect.typeOf

Expand Down Expand Up @@ -149,18 +156,14 @@ fun Navigator() {
}

val animatedBottom by animateDpAsState(
targetValue = targetBottom,
label = "Animated bottom insets for player sheet"
targetValue = targetBottom, label = "Animated bottom insets for player sheet"
)

val playerAwareWindowInsets = remember(
bottomInset,
mediaPlayerSheetState.isDismissed,
animatedBottom
bottomInset, mediaPlayerSheetState.isDismissed, animatedBottom
) {
val insetsBottom = animatedBottom
windowsInsets
.only(WindowInsetsSides.Horizontal + WindowInsetsSides.Top)
windowsInsets.only(WindowInsetsSides.Horizontal + WindowInsetsSides.Top)
.add(WindowInsets(bottom = insetsBottom))
}

Expand Down Expand Up @@ -229,11 +232,11 @@ fun Navigator() {
}
},
icon = {
Icon(
imageVector = destinationInfo?.icon
?: Icons.Rounded.Square,
contentDescription = destinationInfo?.title
?.let { stringResource(id = it) })
Icon(imageVector = destinationInfo?.icon
?: Icons.Rounded.Square,
contentDescription = destinationInfo?.title?.let {
stringResource(id = it)
})
},
modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
)
Expand All @@ -246,8 +249,7 @@ fun Navigator() {
.padding(horizontal = 12.dp),
) {
Row(
modifier = Modifier,
verticalAlignment = Alignment.CenterVertically
modifier = Modifier, verticalAlignment = Alignment.CenterVertically
) {
Text(
modifier = Modifier.padding(16.dp),
Expand All @@ -262,19 +264,17 @@ fun Navigator() {
shape = MaterialTheme.shapes.small
)
}
Surface(
onClick = {
navController.navigate(Route.SettingsNavigator.Settings)
scope.launch {
drawerState.close()
}
},
Surface(onClick = {
navController.navigate(Route.SettingsNavigator.Settings)
scope.launch {
drawerState.close()
}
},
modifier = Modifier
.semantics { role = Role.Tab }
.height(56.dp)
.fillMaxWidth(),
color = Color.Transparent
) {
color = Color.Transparent) {
Row(
Modifier.padding(start = 16.dp, end = 24.dp),
verticalAlignment = Alignment.CenterVertically
Expand All @@ -297,31 +297,27 @@ fun Navigator() {
}
},
) {
Scaffold(
modifier = Modifier.windowInsetsPadding(
insets = WindowInsets(
left = WindowInsets.safeDrawing.getLeft(
density,
layoutDirection = LocalLayoutDirection.current
),
right = WindowInsets.safeDrawing.getRight(
density,
layoutDirection = LocalLayoutDirection.current
),
Scaffold(modifier = Modifier.windowInsetsPadding(
insets = WindowInsets(
left = WindowInsets.safeDrawing.getLeft(
density, layoutDirection = LocalLayoutDirection.current
),
right = WindowInsets.safeDrawing.getRight(
density, layoutDirection = LocalLayoutDirection.current
),
)
), snackbarHost = {
SnackbarHost(
hostState = snackbarHostState
) { dataReceived ->
Snackbar(
modifier = Modifier,
snackbarData = dataReceived,
containerColor = MaterialTheme.colorScheme.inverseSurface,
contentColor = MaterialTheme.colorScheme.inverseOnSurface,
)
),
snackbarHost = {
SnackbarHost(
hostState = snackbarHostState
) { dataReceived ->
Snackbar(
modifier = Modifier,
snackbarData = dataReceived,
containerColor = MaterialTheme.colorScheme.inverseSurface,
contentColor = MaterialTheme.colorScheme.inverseOnSurface,
)
}
}) {
}
}) {
NavHost(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -354,15 +350,47 @@ fun Navigator() {
slideInVerticallyComposable<Route.UtilitiesNavigator.TagEditor>(
typeMap = mapOf(typeOf<ParcelableSong>() to parcelableType<ParcelableSong>()),
) {
val song =
it.toRoute<Route.UtilitiesNavigator.TagEditor>()
val song = it.toRoute<Route.UtilitiesNavigator.TagEditor>()

val viewModel = hiltViewModel<ID3MetadataEditorPageViewModel>()
val viewModel = hiltViewModel<MetadataEditorVM>()
val state = viewModel.state.collectAsStateWithLifecycle()

ID3MetadataEditorPage(
viewModel = viewModel,
parcelableSong = song.selectedSong
)
val securityErrorHandler =
rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
viewModel.savePropertyMap(
audioPath = song.selectedSong.localSongPath
)
navController.popBackStack()
}
}

LaunchedEffect(true) {
viewModel.eventFlow.collectLatest { event ->
Log.i("MetadataEditor", "Event received: $event")
when (event) {
is MetadataEditorVM.Companion.UiEvent.RequestPermission -> {
val intent =
IntentSenderRequest.Builder(event.intent)
.build()
securityErrorHandler.launch(intent)
}

MetadataEditorVM.Companion.UiEvent.SaveSuccess -> {
navController.popBackStack()
}
}
}
}

MetadataEditorPage(
state = state,
receivedSong = song.selectedSong,
) { receivedEvent ->
viewModel.onEvent(receivedEvent)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,41 +157,42 @@ fun ID3MetadataEditorPage(
}


BottomSheetScaffold(topBar = {
TopAppBar(title = {
Column(modifier = Modifier.fillMaxWidth()) {
MarqueeText(
text = stringResource(id = R.string.viewing_metadata),
style = MaterialTheme.typography.bodyLarge,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
}, navigationIcon = {
CloseButton { navController.popBackStack() }
}, actions = {
IconButton(onClick = {
scope.launch {
scaffoldState.bottomSheetState.partialExpand()
BottomSheetScaffold(
topBar = {
TopAppBar(title = {
Column(modifier = Modifier.fillMaxWidth()) {
MarqueeText(
text = stringResource(id = R.string.viewing_metadata),
style = MaterialTheme.typography.bodyLarge,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
}) {
Icon(
imageVector = Icons.Rounded.Downloading,
contentDescription = stringResource(
id = R.string.retrieve_song_info
}, navigationIcon = {
CloseButton { navController.popBackStack() }
}, actions = {
IconButton(onClick = {
scope.launch {
scaffoldState.bottomSheetState.partialExpand()
}
}) {
Icon(
imageVector = Icons.Rounded.Downloading,
contentDescription = stringResource(
id = R.string.retrieve_song_info
)
)
)
}
TextButton(onClick = {
val isInfoSavedInMediaStore = saveInMediaStore()
if (isInfoSavedInMediaStore) {
navController.popBackStack()
}
}) {
Text(text = stringResource(id = R.string.save))
}
}, scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior())
},
TextButton(onClick = {
val isInfoSavedInMediaStore = saveInMediaStore()
if (isInfoSavedInMediaStore) {
navController.popBackStack()
}
}) {
Text(text = stringResource(id = R.string.save))
}
}, scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior())
},
modifier = Modifier.fillMaxSize(),
scaffoldState = scaffoldState,
sheetPeekHeight = 148.dp,
Expand Down
Loading

0 comments on commit 357b7c1

Please sign in to comment.