mirror of
https://codeberg.org/mi6e4ka/openstore.git
synced 2026-07-10 00:12:06 +00:00
fix(apk installer): some fixes for apk installer and working "copy link" buttton
This commit is contained in:
parent
18b3791983
commit
5ce2bebaca
3 changed files with 32 additions and 17 deletions
|
|
@ -42,15 +42,11 @@ class ApkInstaller(private val context: Context) {
|
|||
onError: (Throwable) -> Unit,
|
||||
onCancel: () -> Unit,
|
||||
): () -> Unit {
|
||||
|
||||
val downloadRunner = CoroutineScope(Dispatchers.IO).launch {
|
||||
val totalBytesToDownload = files.sumOf { it.size }
|
||||
val totalBytesDownloaded = AtomicLong(0)
|
||||
|
||||
val downloadedFiles = mutableListOf<File>()
|
||||
|
||||
try {
|
||||
|
||||
val downloadJobs = files.map { file ->
|
||||
async {
|
||||
val fileName = file.url.split("/").last()
|
||||
|
|
@ -101,7 +97,6 @@ class ApkInstaller(private val context: Context) {
|
|||
}
|
||||
val resultingFiles = downloadJobs.awaitAll()
|
||||
|
||||
// Если мы здесь, значит все файлы скачаны успешно
|
||||
withContext(Dispatchers.Main) {
|
||||
onSuccess(resultingFiles)
|
||||
}
|
||||
|
|
@ -111,12 +106,11 @@ class ApkInstaller(private val context: Context) {
|
|||
file.delete()
|
||||
}
|
||||
}
|
||||
|
||||
// Обрабатываем ошибки
|
||||
println("STOP ${e is CancellationException}")
|
||||
if (e is CancellationException) {
|
||||
withContext(Dispatchers.Main) { onCancel() }
|
||||
onCancel()
|
||||
} else {
|
||||
withContext(Dispatchers.Main) { onError(e) }
|
||||
onError(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,14 +128,9 @@ fun DetailsScreen(
|
|||
actions = {
|
||||
IconButton(onClick = {
|
||||
scope.launch {
|
||||
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clip = ClipData.newPlainText("", state.downloadLink)
|
||||
clipboard.setPrimaryClip(clip)
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
||||
Toast.makeText(context, R.string.copied_to_buffer, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
viewModel.downloadFileToFolder(context)
|
||||
}
|
||||
}, enabled = state.downloadLink != null) {
|
||||
}, enabled = !(state.splitOnly ?: true)) {
|
||||
Icon(painterResource(R.drawable.apk_install_24px), null)
|
||||
}
|
||||
IconButton(onClick = {
|
||||
|
|
@ -144,7 +139,7 @@ fun DetailsScreen(
|
|||
setSelector(Intent(Intent.ACTION_VIEW, "http://".toUri()))
|
||||
}
|
||||
context.startActivity(intent)
|
||||
}) {
|
||||
}, enabled = state.item != null) {
|
||||
Icon(Icons.Default.OpenInBrowser, null)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,18 @@
|
|||
package dev.mi6e4ka.openstore.ui.screen.details
|
||||
|
||||
import android.app.DownloadManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.content.ContextCompat.getSystemService
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewModelScope
|
||||
|
|
@ -24,6 +29,7 @@ import kotlinx.coroutines.flow.asStateFlow
|
|||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.File
|
||||
import androidx.core.net.toUri
|
||||
|
||||
data class DetailsState(
|
||||
val isLoading: Boolean = true,
|
||||
|
|
@ -69,6 +75,7 @@ class DetailsViewModel(private val itemId: String, private val apkInstaller: Apk
|
|||
val installedVersionCode = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
packageInfo.longVersionCode
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
packageInfo.versionCode
|
||||
}) as Long
|
||||
if (installedVersionCode < item.versionCode) {
|
||||
|
|
@ -187,6 +194,25 @@ class DetailsViewModel(private val itemId: String, private val apkInstaller: Apk
|
|||
fun updateInstallStatus(isInstalled: Boolean) {
|
||||
_state.update { it.copy(isInstalled = isInstalled) }
|
||||
}
|
||||
|
||||
fun downloadFileToFolder(context: Context) {
|
||||
val fileName = "${state.value.item?.packageName}-${state.value.item?.versionName}.apk"
|
||||
val request = DownloadManager.Request(state.value.plainApkUrl?.toUri())
|
||||
.setTitle(fileName)
|
||||
.setDescription("Скачивание...")
|
||||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)
|
||||
|
||||
val downloadManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
context.getSystemService(DownloadManager::class.java)
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
||||
}
|
||||
downloadManager.enqueue(request)
|
||||
Toast.makeText(context, "Загрузка началась...", Toast.LENGTH_SHORT).show()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class DetailsViewModelFactory(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue