Skip to content

Commit

Permalink
Refactor: Suspend scanStorage function to fix ANR (#523)
Browse files Browse the repository at this point in the history
* refactor: suspend scanStorage function

* lint: run KtlintFormat
  • Loading branch information
anilbeesetti authored Aug 21, 2023
1 parent 412bd4f commit 646a3ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import java.nio.ByteBuffer
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

val VIDEO_COLLECTION_URI: Uri
Expand Down Expand Up @@ -198,19 +199,29 @@ fun Context.showToast(string: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, string, duration).show()
}

fun Context.scanPaths(paths: List<String>, callback: ((String?, Uri?) -> Unit)? = null) {
MediaScannerConnection.scanFile(this, paths.toTypedArray(), arrayOf("video/*"), callback)
suspend fun Context.scanPaths(
paths: List<String>,
callback: ((String?, Uri?) -> Unit)? = null
) = withContext(Dispatchers.IO) {
MediaScannerConnection.scanFile(
this@scanPaths,
paths.toTypedArray(),
arrayOf("video/*"),
callback
)
}

fun Context.scanPath(file: File) {
if (file.isDirectory) {
file.listFiles()?.forEach { scanPath(it) }
} else {
scanPaths(listOf(file.path))
suspend fun Context.scanPath(file: File) {
withContext(Dispatchers.IO) {
if (file.isDirectory) {
file.listFiles()?.forEach { scanPath(it) }
} else {
scanPaths(listOf(file.path))
}
}
}

fun Context.scanStorage(callback: ((String?, Uri?) -> Unit)? = null) {
suspend fun Context.scanStorage(callback: ((String?, Uri?) -> Unit)? = null) = withContext(Dispatchers.IO) {
val storagePath = Environment.getExternalStorageDirectory()?.path
if (storagePath != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
Expand All @@ -21,6 +22,7 @@ import dev.anilbeesetti.nextplayer.core.ui.components.ClickablePreferenceItem
import dev.anilbeesetti.nextplayer.core.ui.components.NextTopAppBar
import dev.anilbeesetti.nextplayer.core.ui.designsystem.NextIcons
import dev.anilbeesetti.nextplayer.settings.composables.PreferenceSubtitle
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -30,6 +32,7 @@ fun MediaLibraryPreferencesScreen(
) {
val scrollBehaviour = TopAppBarDefaults.pinnedScrollBehavior()
val context = LocalContext.current
val scope = rememberCoroutineScope()

Scaffold(
modifier = Modifier.nestedScroll(scrollBehaviour.nestedScrollConnection),
Expand Down Expand Up @@ -61,7 +64,7 @@ fun MediaLibraryPreferencesScreen(
)
forceRescanStorageSetting(
onClick = {
context.scanStorage()
scope.launch { context.scanStorage() }
context.showToast(
string = context.getString(R.string.scanning_storage),
duration = Toast.LENGTH_LONG
Expand Down

0 comments on commit 646a3ac

Please sign in to comment.