Skip to content

Commit

Permalink
Handle file saving asynchronously with coroutines
Browse files Browse the repository at this point in the history
New dependencies:

Coroutines: 1.7.3
Lifecycle Runtime Ktx: 2.8.1
  • Loading branch information
MateusRodCosta committed Jun 5, 2024
1 parent 5f34bf7 commit cab1d95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ android {
}

dependencies {
implementation(libs.kotlinx.coroutines)
implementation(libs.bundles.androidx.ktx)
implementation(libs.androidx.core.splashscreen)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
Expand All @@ -29,13 +31,20 @@ import androidx.activity.result.ActivityResultLauncher
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.mateusrodcosta.apps.share2storage.model.UriData
import com.mateusrodcosta.apps.share2storage.screens.DetailsScreen
import com.mateusrodcosta.apps.share2storage.utils.CreateDocumentWithInitialUri
import com.mateusrodcosta.apps.share2storage.utils.SharedPreferenceKeys
import com.mateusrodcosta.apps.share2storage.utils.getUriData
import com.mateusrodcosta.apps.share2storage.utils.saveFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.time.DurationUnit
import kotlin.time.toDuration

class DetailsActivity : ComponentActivity() {
private lateinit var createFile: ActivityResultLauncher<String>
Expand Down Expand Up @@ -111,18 +120,33 @@ class DetailsActivity : ComponentActivity() {
createFile = registerForActivityResult(
CreateDocumentWithInitialUri(uriData?.type ?: "*/*", defaultSaveLocation)
) { uri ->
if (uri == null || fileUri == null) return@registerForActivityResult
val isSuccess = saveFile(baseContext, uri, fileUri)
lifecycleScope.launch {
handleFileSave(uri, fileUri)
}
}
}
}

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

Handler(Looper.getMainLooper()).post {
Toast.makeText(
baseContext, if (isSuccess) {
R.string.toast_saved_file_success
} else {
R.string.toast_saved_file_failure
}, Toast.LENGTH_LONG
).show()
if (skipFileDetails) finish()
}

if (skipFileDetails) {
delay(1.toDuration(DurationUnit.SECONDS))
finish()
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ fun saveFile(
}
val output = context.contentResolver.openOutputStream(targeturi)

val BUFFER_SIZE = 1024
val bufferSize = 1024
bis = BufferedInputStream(input)
bos = BufferedOutputStream(output)
val buf = ByteArray(BUFFER_SIZE)
val buf = ByteArray(bufferSize)
bis.read(buf)
do {
bos.write(buf)
Expand Down
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ kotlin = "1.9.24"
androidxComposeCompiler = "1.5.14"
ktx = "1.13.1"
preference-ktx = "1.2.1"
lifecycle-runtime-ktx = "2.8.1"
splashscreen = "1.0.1"
composeBom = "2024.05.00"
activityCompose = "1.9.0"
navigationCompose = "2.7.7"
junit = "4.13.2"
androidxTestRunner = "1.5.2"
androidxTestRules = "1.5.0"
coroutines = "1.7.3"

[libraries]
kotlinx-coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "coroutines" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }
androidx-preference-ktx = { group = "androidx.preference", name = "preference-ktx", version.ref = "preference-ktx" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle-runtime-ktx" }
androidx-core-splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "splashscreen" }
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
compose-material3 = { group = "androidx.compose.material3", name = "material3" }
Expand All @@ -31,7 +35,7 @@ androidx-test-runner = { group = "androidx.test", name="runner", version.ref = "
androidx-test-rules = { group = "androidx.test", name="rules", version.ref = "androidxTestRules" }

[bundles]
androidx-ktx = ["androidx.core.ktx", "androidx.preference.ktx"]
androidx-ktx = ["androidx.core.ktx", "androidx.preference.ktx", "androidx.lifecycle.runtime.ktx"]
compose = ["compose.material3", "compose.material3.window.size.classes", "compose.ui.tooling.preview", "compose.ui.material.icons.extended"]
compose-debug = ["compose.ui.tooling", "compose.ui.test.manifest"]
androidx-compose-integration = ["androidx.activity.compose", "androidx.navigation.compose"]
Expand Down

0 comments on commit cab1d95

Please sign in to comment.