moved progressmodel out of class

This commit is contained in:
X1nto 2021-02-16 12:26:40 +04:00
parent ab5f5182b1
commit d0e765fa39
9 changed files with 68 additions and 90 deletions

View File

@ -2,11 +2,9 @@ package com.vanced.manager.core.downloader
import android.content.Context
import com.vanced.manager.R
import com.vanced.manager.utils.*
import com.vanced.manager.utils.DownloadHelper.download
import com.vanced.manager.utils.DownloadHelper.downloadProgress
import com.vanced.manager.utils.PackageHelper.install
import com.vanced.manager.utils.baseInstallUrl
import com.vanced.manager.utils.microg
object MicrogDownloader {
@ -18,14 +16,14 @@ object MicrogDownloader {
download(url, "$baseInstallUrl/", folderName, fileName, context, onDownloadComplete = {
startMicrogInstall(context)
}, onError = {
downloadProgress.downloadingFile.postValue(context.getString(R.string.error_downloading, fileName))
downloadingFile.postValue(context.getString(R.string.error_downloading, fileName))
})
}
fun startMicrogInstall(context: Context) {
downloadProgress.installing.postValue(true)
downloadProgress.postReset()
installing.postValue(true)
postReset()
install("${context.getExternalFilesDir(folderName)}/$fileName", context)
}
}

View File

@ -6,7 +6,6 @@ import com.vanced.manager.utils.*
import com.vanced.manager.utils.AppUtils.musicRootPkg
import com.vanced.manager.utils.AppUtils.validateTheme
import com.vanced.manager.utils.DownloadHelper.download
import com.vanced.manager.utils.DownloadHelper.downloadProgress
import com.vanced.manager.utils.PackageHelper.downloadStockCheck
import com.vanced.manager.utils.PackageHelper.install
import com.vanced.manager.utils.PackageHelper.installMusicRoot
@ -59,13 +58,13 @@ object MusicDownloader {
"stock" -> startMusicInstall(context)
}
}, onError = {
downloadProgress.downloadingFile.postValue(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
downloadingFile.postValue(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
})
}
fun startMusicInstall(context: Context) {
downloadProgress.installing.postValue(true)
downloadProgress.postReset()
installing.postValue(true)
postReset()
if (variant == "root")
installMusicRoot(context)
else

View File

@ -10,7 +10,6 @@ import com.vanced.manager.utils.AppUtils.log
import com.vanced.manager.utils.AppUtils.validateTheme
import com.vanced.manager.utils.AppUtils.vancedRootPkg
import com.vanced.manager.utils.DownloadHelper.download
import com.vanced.manager.utils.DownloadHelper.downloadProgress
import com.vanced.manager.utils.PackageHelper.downloadStockCheck
import com.vanced.manager.utils.PackageHelper.installSplitApkFiles
import com.vanced.manager.utils.PackageHelper.installVancedRoot
@ -59,7 +58,7 @@ object VancedDownloader {
downloadSplits(context)
} catch (e: Exception) {
log("VMDownloader", e.stackTraceToString())
downloadProgress.downloadingFile.postValue(context.getString(R.string.error_downloading, "Vanced"))
downloadingFile.postValue(context.getString(R.string.error_downloading, "Vanced"))
}
}
@ -113,14 +112,14 @@ object VancedDownloader {
}
} else {
downloadProgress.downloadingFile.postValue(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
downloadingFile.postValue(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
}
})
}
fun startVancedInstall(context: Context, variant: String? = this.variant) {
downloadProgress.installing.postValue(true)
downloadProgress.postReset()
installing.postValue(true)
postReset()
FirebaseAnalytics.getInstance(context).logEvent(FirebaseAnalytics.Event.SELECT_ITEM) {
variant?.let { param("vanced_variant", it) }
theme?.let { param("vanced_theme", it) }

View File

@ -1,30 +0,0 @@
package com.vanced.manager.model
import androidx.lifecycle.MutableLiveData
import okhttp3.ResponseBody
import retrofit2.Call
open class ProgressModel {
val downloadProgress = MutableLiveData<Int>()
val downloadingFile = MutableLiveData<String>()
val installing = MutableLiveData<Boolean>()
var currentDownload: Call<ResponseBody>? = null
fun reset() {
downloadProgress.value = 0
downloadingFile.value = ""
}
fun postReset() {
downloadProgress.postValue(0)
downloadingFile.postValue("")
}
init {
installing.postValue(false)
postReset()
}
}

View File

@ -17,8 +17,7 @@ import com.vanced.manager.core.downloader.MusicDownloader.downloadMusic
import com.vanced.manager.core.downloader.VancedDownloader.downloadVanced
import com.vanced.manager.core.ui.base.BindingDialogFragment
import com.vanced.manager.databinding.DialogAppDownloadBinding
import com.vanced.manager.utils.DownloadHelper.downloadProgress
import com.vanced.manager.utils.applyAccent
import com.vanced.manager.utils.*
class AppDownloadDialog : BindingDialogFragment<DialogAppDownloadBinding>() {
@ -82,27 +81,25 @@ class AppDownloadDialog : BindingDialogFragment<DialogAppDownloadBinding>() {
}
private fun DialogAppDownloadBinding.bindDownloadProgress() {
with(downloadProgress) {
downloadProgress.observe(viewLifecycleOwner) {
appDownloadProgressbar.progress = it
}
installing.observe(viewLifecycleOwner) { installing ->
appDownloadProgressbar.isVisible = !installing
appInstallProgressbar.isVisible = installing
appDownloadFile.isVisible = !installing
appDownloadCancel.isEnabled = !installing
appDownloadCancel.setOnClickListener {
if (installing) {
return@setOnClickListener
}
currentDownload?.cancel()
downloadProgress.value = 0
dismiss()
downloadProgress.observe(viewLifecycleOwner) {
appDownloadProgressbar.progress = it
}
installing.observe(viewLifecycleOwner) { installing ->
appDownloadProgressbar.isVisible = !installing
appInstallProgressbar.isVisible = installing
appDownloadFile.isVisible = !installing
appDownloadCancel.isEnabled = !installing
appDownloadCancel.setOnClickListener {
if (installing) {
return@setOnClickListener
}
currentDownload?.cancel()
downloadProgress.value = 0
dismiss()
}
downloadingFile.observe(viewLifecycleOwner) {
appDownloadFile.text = it
}
}
downloadingFile.observe(viewLifecycleOwner) {
appDownloadFile.text = it
}
}

View File

@ -16,8 +16,9 @@ import com.vanced.manager.R
import com.vanced.manager.core.ui.base.BindingDialogFragment
import com.vanced.manager.databinding.DialogManagerUpdateBinding
import com.vanced.manager.utils.DownloadHelper.downloadManager
import com.vanced.manager.utils.DownloadHelper.downloadProgress
import com.vanced.manager.utils.applyAccent
import com.vanced.manager.utils.currentDownload
import com.vanced.manager.utils.downloadProgress
import com.vanced.manager.utils.manager
class ManagerUpdateDialog : BindingDialogFragment<DialogManagerUpdateBinding>() {
@ -68,10 +69,8 @@ class ManagerUpdateDialog : BindingDialogFragment<DialogManagerUpdateBinding>()
isCancelable = false
managerUpdateProgressbar.applyAccent()
managerUpdateCancel.setOnClickListener {
with(downloadProgress) {
downloadProgress.value = 0
currentDownload?.cancel()
}
downloadProgress.value = 0
currentDownload?.cancel()
dismiss()
}
bindDownloadProgress()
@ -79,11 +78,9 @@ class ManagerUpdateDialog : BindingDialogFragment<DialogManagerUpdateBinding>()
}
private fun DialogManagerUpdateBinding.bindDownloadProgress() {
with(downloadProgress) {
downloadProgress.observe(viewLifecycleOwner) {
managerUpdateProgressbar.progress = it
managerUpdateProgressbar.isVisible = it != 0
}
downloadProgress.observe(viewLifecycleOwner) {
managerUpdateProgressbar.progress = it
managerUpdateProgressbar.isVisible = it != 0
}
}

View File

@ -14,7 +14,6 @@ import com.vanced.manager.BuildConfig.APPLICATION_ID
import com.vanced.manager.R
import com.vanced.manager.ui.dialogs.AppDownloadDialog
import com.vanced.manager.ui.fragments.HomeFragment
import com.vanced.manager.utils.DownloadHelper.downloadProgress
import kotlinx.coroutines.*
import java.io.File
import java.io.IOException
@ -57,13 +56,13 @@ object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) {
fun sendCloseDialog(context: Context): Job {
return launch {
delay(700)
downloadProgress.installing.postValue(false)
installing.postValue(false)
LocalBroadcastManager.getInstance(context).sendBroadcast(Intent(AppDownloadDialog.CLOSE_DIALOG))
}
}
fun sendFailure(error: MutableList<String>, context: Context) {
sendFailure(error.joinToString(), context)
sendFailure(error.joinToString(" "), context)
}
fun sendFailure(error: String, context: Context): Job {

View File

@ -8,7 +8,6 @@ import android.os.Build
import androidx.core.content.FileProvider
import com.vanced.manager.R
import com.vanced.manager.library.network.providers.createService
import com.vanced.manager.model.ProgressModel
import com.vanced.manager.utils.AppUtils.log
import com.vanced.manager.utils.AppUtils.sendCloseDialog
import kotlinx.coroutines.CoroutineScope
@ -42,10 +41,10 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
onDownloadComplete: () -> Unit = {},
onError: (error: String) -> Unit = {}
) {
downloadProgress.downloadingFile.postValue(context.getString(R.string.downloading_file, fileName))
downloadingFile.postValue(context.getString(R.string.downloading_file, fileName))
val downloadInterface = createService(DownloadHelper::class, baseUrl)
val download = downloadInterface.download(url)
downloadProgress.currentDownload = download
currentDownload = download
download.enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
if (response.isSuccessful) {
@ -54,13 +53,13 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
onDownloadComplete()
} else {
onError("Could not save file")
downloadProgress.downloadProgress.postValue(0)
downloadProgress.postValue(0)
log("VMDownloader", "Failed to save file: $url")
}
}
} else {
onError(response.errorBody().toString())
downloadProgress.downloadProgress.postValue(0)
downloadProgress.postValue(0)
log("VMDownloader", "Failed to download file: $url")
}
}
@ -68,10 +67,10 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
if (call.isCanceled) {
log("VMDownloader", "Download canceled")
downloadProgress.downloadProgress.postValue(0)
downloadProgress.postValue(0)
} else {
onError(t.stackTraceToString())
downloadProgress.downloadProgress.postValue(0)
downloadProgress.postValue(0)
log("VMDownloader", "Failed to download file: $url")
}
}
@ -94,7 +93,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
while (inputStream.read(fileReader).also { read = it } != -1) {
outputStream.write(fileReader, 0, read)
downloadedBytes += read.toLong()
downloadProgress.downloadProgress.postValue((downloadedBytes * 100 / totalBytes).toInt())
downloadProgress.postValue((downloadedBytes * 100 / totalBytes).toInt())
}
outputStream.flush()
true
@ -109,8 +108,6 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
}
}
val downloadProgress = ProgressModel()
fun downloadManager(context: Context) {
val url = "https://github.com/YTVanced/VancedManager/releases/latest/download/manager.apk"
download(url,"https://github.com/YTVanced/VancedManager/", "manager", "manager.apk", context, onDownloadComplete = {
@ -133,7 +130,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
sendCloseDialog(context)
}
}, onError = {
downloadProgress.downloadingFile.postValue(
downloadingFile.postValue(
context.getString(
R.string.error_downloading,
"manager.apk"

View File

@ -0,0 +1,22 @@
package com.vanced.manager.utils
import androidx.lifecycle.MutableLiveData
import okhttp3.ResponseBody
import retrofit2.Call
val downloadProgress = MutableLiveData(0)
val downloadingFile = MutableLiveData("")
val installing = MutableLiveData(false)
var currentDownload: Call<ResponseBody>? = null
fun reset() {
downloadProgress.value = 0
downloadingFile.value = ""
}
fun postReset() {
downloadProgress.postValue(0)
downloadingFile.postValue("")
}