refactor(Utils): save to downloads better

This commit is contained in:
rushiiMachine 2025-06-25 22:54:15 -07:00
parent f8e99bad89
commit eefb7108dc
No known key found for this signature in database
GPG key ID: DCBE5952BB3B6420

View file

@ -28,18 +28,19 @@ fun Context.copyToClipboard(text: String) {
) )
} }
fun Context.saveFile(name: String, text: String): File? { fun Context.saveFile(name: String, text: String): Boolean {
val basePath = Environment.getExternalStorageDirectory() val downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File(basePath, "/Download/$name") val file = File(downloads, name)
val base = downloads.parentFile!!
return try { return try {
file.writeText(text) file.writeText(text)
showToast(R.string.installer_file_save_success, file.relativeTo(basePath)) showToast(R.string.installer_file_save_success, file.toRelativeString(base))
file true
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e(BuildConfig.TAG, Log.getStackTraceString(e)) Log.e(BuildConfig.TAG, Log.getStackTraceString(e))
showToast(R.string.installer_file_save_failed, file.relativeTo(basePath)) showToast(R.string.installer_file_save_failed, file.toRelativeString(base))
null false
} }
} }