Skip to content

Commit

Permalink
Refactor several chunks of code
Browse files Browse the repository at this point in the history
  • Loading branch information
MateusRodCosta committed Nov 1, 2024
1 parent 9cbcd10 commit 431aa9f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,32 @@ class DetailsActivity : ComponentActivity() {
}

private fun handleIntent(intent: Intent?) {
var fileUri: Uri? = null
if (intent?.action == Intent.ACTION_SEND) {
fileUri =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) intent.getParcelableExtra(
Intent.EXTRA_STREAM, Uri::class.java
)
else @Suppress("DEPRECATION") intent.getParcelableExtra(Intent.EXTRA_STREAM)
Log.d("fileUri ACTION_SEND", fileUri.toString())
}
// ACTION_VIEW intents interceptor
if (intent?.action == Intent.ACTION_VIEW) {
fileUri = intent.data
Log.d("fileUri ACTION_VIEW", fileUri.toString())
}

if (fileUri != null) uriData =
getUriData(contentResolver, fileUri, getPreview = showFilePreview)
if (uriData != null) {
createFile = registerForActivityResult(
CreateDocumentWithInitialUri(uriData?.type ?: "*/*", defaultSaveLocation)
) { uri ->
if (uri == null) {
if (skipFileDetails) finish()
} else {
lifecycleScope.launch {
handleFileSave(uri, fileUri)
}
val fileUri: Uri? = if (intent?.action == Intent.ACTION_SEND) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) intent.getParcelableExtra(
Intent.EXTRA_STREAM, Uri::class.java
)
else @Suppress("DEPRECATION") intent.getParcelableExtra(Intent.EXTRA_STREAM)
} else if (intent?.action == Intent.ACTION_VIEW) intent.data
else null
Log.d("fileUri", "Action: ${intent?.action}, uri: $fileUri")

if (fileUri == null) return
uriData = getUriData(contentResolver, fileUri, getPreview = showFilePreview)
if(uriData == null) return
createFile = registerForActivityResult(
CreateDocumentWithInitialUri(uriData?.type ?: "*/*", defaultSaveLocation)
) { uri ->
if (uri == null) {
if (skipFileDetails) finish()
} else {
lifecycleScope.launch {
handleFileSave(uri, fileUri)
}
}
}
}

private suspend fun handleFileSave(uri: Uri?, fileUri: Uri?) {
if (uri == null || fileUri == null) return
private suspend fun handleFileSave(uri: Uri, fileUri: Uri) {
return withContext(Dispatchers.IO) {
val isSuccess = saveFile(baseContext, uri, fileUri)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class MainActivity : ComponentActivity() {
LaunchedEffect(key1 = Unit) {
if (isAppPreference) navController.navigate("settings")
}

AppNavigation(navController, settingsViewModel, windowSizeClass)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class SampleUriDataProvider : PreviewParameterProvider<UriData?> {
"21. Setting Sail, Coming Home (End Theme).flac", "audio/flac", 35280673, null
),
UriData("03. Lonely Rolling Star (Missing You).flac", "audio/flac", 41123343, null),
UriData(null, null, null, null),
null,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ package com.mateusrodcosta.apps.share2storage.model
import android.graphics.Bitmap

data class UriData(
val displayName: String?,
val type: String?,
val size: Long?,
val displayName: String,
val type: String,
val size: Long,
val previewImage: Bitmap?
)
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,15 @@ fun FileInfo(uriData: UriData) {
) {
FileInfoLine(
label = stringResource(R.string.file_name),
content = uriData.displayName ?: stringResource(R.string.unknown)
content = uriData.displayName
)
FileInfoLine(
label = stringResource(R.string.file_type),
content = uriData.type ?: stringResource(R.string.unknown)
content = uriData.type
)
FileInfoLine(
label = stringResource(R.string.file_size),
content = if (uriData.size != null) Formatter.formatFileSize(
LocalContext.current, uriData.size
) else stringResource(R.string.unknown)
content = Formatter.formatFileSize(LocalContext.current, uriData.size)
)
}
}
Expand All @@ -223,7 +221,7 @@ fun FileInfoLine(label: String, content: String) {

@Composable
fun FilePreview(uriData: UriData) {
val mimeType = uriData.type ?: "*/*"
val mimeType = uriData.type
val fallbackFileIcon = if (mimeType.startsWith("image/")) Icons.Outlined.Image
else if (mimeType.startsWith("audio/")) Icons.Outlined.AudioFile
else if (mimeType.startsWith("video/")) Icons.Outlined.VideoFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,26 @@ object Utils {
const val BUFFER_SIZE: Int = 1024
}

fun getUriData(contentResolver: ContentResolver, uri: Uri?, getPreview: Boolean): UriData? {
if (uri == null) return null
val type = contentResolver.getType(uri)
var displayName: String? = null
var size: Long? = null
fun getUriData(contentResolver: ContentResolver, uri: Uri, getPreview: Boolean): UriData? {
val type = contentResolver.getType(uri) ?: "*/*"
val displayName: String?
val size: Long?

val cursor = contentResolver.query(uri, null, null, null, null)
if (cursor != null) {/*
* Get the column indexes of the data in the Cursor,
* move to the first row in the Cursor, get the data,
* and display it.
*/
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
displayName = cursor.getString(nameIndex)
size = cursor.getLong(sizeIndex)

cursor.close()
}
if (cursor == null) return null

/*
* Get the column indexes of the data in the Cursor,
* move to the first row in the Cursor, get the data,
* and display it.
*/
val nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
cursor.moveToFirst()
displayName = cursor.getString(nameIndex)
size = cursor.getLong(sizeIndex)

cursor.close()

var bitmap: Bitmap? = null
if (getPreview) {
Expand Down Expand Up @@ -138,7 +139,7 @@ fun saveFile(
bos.flush()
bos.close()
}
} catch (ignored: Exception) {
} catch (_: Exception) {
}
}
return !hasError
Expand Down

0 comments on commit 431aa9f

Please sign in to comment.