Skip to content

Commit

Permalink
fix: API level lower than TIRAMISU's instantly crashed the app
Browse files Browse the repository at this point in the history
fix: Now not loading MediaStore until the permissions are explicitly given

Signed-off-by: Gabriel Fontán <[email protected]>
  • Loading branch information
BobbyESP committed Jul 28, 2024
1 parent 357b7c1 commit cdad156
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 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.

Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ fun Navigator() {
animatedComposable<Route.MetadatorNavigator.Home> {
val songsState =
mediaStoreViewModel.songs.collectAsStateWithLifecycle()
HomePage(songs = songsState)
HomePage(songs = songsState, onEvent = mediaStoreViewModel::onEvent)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bobbyesp.metadator.presentation.pages

import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bobbyesp.utilities.mediastore.MediaStoreReceiver.Advanced.observeSongs
Expand All @@ -27,20 +28,31 @@ class MediaStorePageViewModel @Inject constructor(
private val mediaStoreSongsFlow =
applicationContext.contentResolver.observeSongs()

init {
fun reloadMediaStore() {
viewModelScope.launch(Dispatchers.IO) {
_songs.update { ResourceState.Loading() }
mediaStoreSongsFlow.collectLatest { songs ->
_songs.update { ResourceState.Success(songs) }
}
}
}

fun reloadMediaStore() {
viewModelScope.launch(Dispatchers.IO) {
_songs.update { ResourceState.Loading() }
mediaStoreSongsFlow.collectLatest { songs ->
_songs.update { ResourceState.Success(songs) }
fun onEvent(event: Events) {
when (event) {
is Events.StartObservingMediaStore -> {
Log.i("MediaStorePageViewModel", "Start observing media store")
viewModelScope.launch(Dispatchers.IO) {
mediaStoreSongsFlow.collectLatest { songs ->
_songs.update { ResourceState.Success(songs) }
}
}
}
}
}

companion object {
interface Events {
data object StartObservingMediaStore : Events
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.material3.SegmentedButtonDefaults
import androidx.compose.material3.SingleChoiceSegmentedButtonRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand All @@ -57,6 +58,7 @@ import com.bobbyesp.metadator.presentation.common.LocalDrawerState
import com.bobbyesp.metadator.presentation.common.LocalNavController
import com.bobbyesp.metadator.presentation.common.Route
import com.bobbyesp.metadator.presentation.pages.MediaStorePage
import com.bobbyesp.metadator.presentation.pages.MediaStorePageViewModel
import com.bobbyesp.ui.components.dropdown.AnimatedDropdownMenu
import com.bobbyesp.ui.components.dropdown.DropdownItemContainer
import com.bobbyesp.ui.components.text.AutoResizableText
Expand All @@ -69,25 +71,33 @@ import com.bobbyesp.utilities.ui.permission.PermissionRequestHandler
import com.bobbyesp.utilities.ui.permission.toPermissionType
import com.bobbyesp.utilities.ui.rememberForeverLazyGridState
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.launch
import okhttp3.internal.toImmutableList

@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
@Composable
fun HomePage(
modifier: Modifier = Modifier, songs: State<ResourceState<List<Song>>>
modifier: Modifier = Modifier,
songs: State<ResourceState<List<Song>>>,
onEvent: (MediaStorePageViewModel.Companion.Events) -> Unit = {}
) {
val context = LocalContext.current as Activity
val currentApiVersion = Build.VERSION.SDK_INT
val readAudioFiles = when {
currentApiVersion < Build.VERSION_CODES.TIRAMISU -> Manifest.permission.READ_EXTERNAL_STORAGE
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU -> Manifest.permission.READ_EXTERNAL_STORAGE

else -> Manifest.permission.READ_MEDIA_AUDIO
}

val storagePermissionState = rememberPermissionState(permission = readAudioFiles)

LaunchedEffect(storagePermissionState.status.isGranted) {
if (storagePermissionState.status.isGranted && songs.value !is ResourceState.Success) {
onEvent(MediaStorePageViewModel.Companion.Events.StartObservingMediaStore)
}
}

val navController = LocalNavController.current
val drawerState = LocalDrawerState.current
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -210,10 +220,9 @@ fun HomePage(
}
}
}
//for scrolling up to the top

}) { paddingValues ->
PermissionRequestHandler(permissionState = storagePermissionState,
PermissionRequestHandler(
permissionState = storagePermissionState,
deniedContent = { shouldShowRationale ->
PermissionNotGrantedDialog(
neededPermissions = listOf(readAudioFiles.toPermissionType()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class MetadataEditorVM @Inject constructor(
val mutablePropertiesMap: SnapshotStateMap<String, String> = mutableStateMapOf()
)

suspend fun loadTrackMetadata(path: String) {
private suspend fun loadTrackMetadata(path: String) {
updateState(ScreenState.Loading)
mutableState.value.mutablePropertiesMap.clear()
runCatching {
Expand Down Expand Up @@ -103,7 +103,7 @@ class MetadataEditorVM @Inject constructor(
}
}

suspend fun loadAudioMetadata(songFd: ParcelFileDescriptor): Metadata? {
private suspend fun loadAudioMetadata(songFd: ParcelFileDescriptor): Metadata? {
val fd = songFd.dup()?.detachFd()
?: throw NullAudioFileDescriptorException(isAudioProperties = false)

Expand All @@ -113,7 +113,7 @@ class MetadataEditorVM @Inject constructor(
}


suspend fun loadAudioProperties(
private suspend fun loadAudioProperties(
songFd: ParcelFileDescriptor,
readStyle: AudioPropertiesReadStyle = AudioPropertiesReadStyle.Average
): AudioProperties? {
Expand Down Expand Up @@ -160,7 +160,7 @@ class MetadataEditorVM @Inject constructor(
}
}

fun savePictures(
private fun savePictures(
context: Context = this.context,
imagesUri: List<Uri> = emptyList(),
audioPath: String,
Expand All @@ -180,8 +180,8 @@ class MetadataEditorVM @Inject constructor(
val picture = Picture(
data = byteArray,
mimeType = mimeType,
description = "Song image $index - Metadator",
pictureType = "Cover (front)"
description = "Audio image $index - Metadator",
pictureType = "Front cover"
)

mutablePicturesList.add(picture)
Expand Down Expand Up @@ -223,8 +223,8 @@ class MetadataEditorVM @Inject constructor(
val picture = Picture(
data = byteArray,
mimeType = mimeType,
description = "Song cover - Metadator",
pictureType = "Cover (front)"
description = "Audio image $index - Metadator",
pictureType = "Front cover"
)

mutablePicturesList.add(picture)
Expand Down Expand Up @@ -276,7 +276,7 @@ class MetadataEditorVM @Inject constructor(
}
}

fun emitUiEvent(event: UiEvent) {
private fun emitUiEvent(event: UiEvent) {
viewModelScope.launch {
_eventFlow.emit(event)
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ hilt-ext = "1.2.0"
room = "2.6.1"

#High performance Key-value storage
mmkv = "1.3.6"
mmkv = "1.3.9"

#Firebase, GMS...
firebaseAuth = "23.0.0"
Expand Down

0 comments on commit cdad156

Please sign in to comment.