From 8125e01638a03c11c92c46c613412ac05bc908e9 Mon Sep 17 00:00:00 2001 From: X1nto Date: Sun, 24 May 2020 17:15:47 +0400 Subject: [PATCH] enhanced split apk downloading --- .../vanced/manager/core/base/BaseFragment.kt | 55 +++++++++++-------- .../vanced/manager/ui/core/ThemedActivity.kt | 17 ++---- app/src/main/res/values/arrays.xml | 6 +- app/src/main/res/xml/preferences.xml | 2 +- 4 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt b/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt index aeeb77e5..e1cf65bd 100644 --- a/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt +++ b/app/src/main/java/com/vanced/manager/core/base/BaseFragment.kt @@ -10,7 +10,9 @@ import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.navigation.findNavController import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable import io.reactivex.rxkotlin.subscribeBy +import zlc.season.rxdownload4.delete import zlc.season.rxdownload4.download import zlc.season.rxdownload4.file import zlc.season.rxdownload4.task.Task @@ -18,6 +20,8 @@ import zlc.season.rxdownload4.utils.getFileNameFromUrl open class BaseFragment : Fragment() { + private var disposable: Disposable? = null + fun openUrl(Url: String, color: Int) { val builder = CustomTabsIntent.Builder() builder.setToolbarColor(ContextCompat.getColor(requireContext(), color)) @@ -44,34 +48,39 @@ open class BaseFragment : Fragment() { else -> return } - activity?.filesDir?.path?.let { - Task(url = url, saveName = getFileNameFromUrl(url), savePath = it) - .download() - .observeOn(AndroidSchedulers.mainThread()) - .subscribeBy( - onNext = { progress -> - loadBar.visibility = View.VISIBLE - loadBar.progress = progress.percent().toInt() - }, - onComplete = { - //if (isInstalling) { - //So we should implement installation here. - //That will be done later - //} - view?.findNavController()?.navigate(navigate) - }, - onError = { throwable -> - Toast.makeText(requireContext(), throwable.toString(), Toast.LENGTH_SHORT).show() - } + val task = activity?.filesDir?.path?.let { Task(url = url, saveName = getFileNameFromUrl(url), savePath = it) } - ) + if (task?.file()?.exists()!!) + task.delete() + + disposable = task.download() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeBy( + onNext = { progress -> + loadBar.visibility = View.VISIBLE + loadBar.progress = progress.percent().toInt() + }, + onComplete = { + view?.findNavController()?.navigate(navigate) + //if (isInstalling) { + //So we should implement installation here. + //That will be done later + //} + }, + onError = { throwable -> + Toast.makeText(requireContext(), throwable.toString(), Toast.LENGTH_SHORT).show() + } + ) } - } fun downloadEn() { val url = "https://x1nto.github.io/VancedFiles/Splits/Language/split_config.en.apk" - activity?.filesDir?.path?.let { - Task(url = url, saveName = getFileNameFromUrl(url), savePath = it) + + val task = activity?.filesDir?.path?.let { Task(url = url, saveName = getFileNameFromUrl(url), savePath = it) } + if (task?.file()?.exists()!!) + task.delete() + + disposable = task .download() .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( diff --git a/app/src/main/java/com/vanced/manager/ui/core/ThemedActivity.kt b/app/src/main/java/com/vanced/manager/ui/core/ThemedActivity.kt index 2cc4f76d..5ee40976 100644 --- a/app/src/main/java/com/vanced/manager/ui/core/ThemedActivity.kt +++ b/app/src/main/java/com/vanced/manager/ui/core/ThemedActivity.kt @@ -21,7 +21,7 @@ open class ThemedActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { pref = PreferenceManager.getDefaultSharedPreferences(this) - currentTheme = pref.getString("theme_mode", "").toString() + currentTheme = pref.getString("theme_mode", "Light").toString() setFinalTheme(currentTheme) super.onCreate(savedInstanceState) @@ -33,7 +33,7 @@ open class ThemedActivity : AppCompatActivity() { override fun onResume() { super.onResume() - val theme = pref.getString("theme_mode", "LIGHT") + val theme = pref.getString("theme_mode", "Light") //if for some weird reasons we get invalid //theme, recreate activity @@ -57,20 +57,15 @@ open class ThemedActivity : AppCompatActivity() { private fun setFinalTheme(currentTheme: String) { val currentAccent = pref.getString("accent_color", "Blue").toString() when (currentTheme) { - "LIGHT" -> setLightAccent(currentAccent) - "DARK" -> setDarkAccent(currentAccent) - "FOLLOW" -> { + "Light" -> setLightAccent(currentAccent) + "Dark" -> setDarkAccent(currentAccent) + "Follow" -> { when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { Configuration.UI_MODE_NIGHT_YES -> setDarkAccent(currentAccent) Configuration.UI_MODE_NIGHT_NO -> setLightAccent(currentAccent) } } - else -> { - when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { - Configuration.UI_MODE_NIGHT_YES -> setDarkAccent("Blue") - Configuration.UI_MODE_NIGHT_NO -> setLightAccent("Blue") - } - } + else -> setLightAccent("Blue") } } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 0e3d3110..874eb39d 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -8,9 +8,9 @@ - LIGHT - DARK - FOLLOW + Light + Dark + Follow diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 5b3a7201..13cec585 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -33,7 +33,7 @@ android:summary="@string/theme_summary" android:entries="@array/theme_modes" android:entryValues="@array/theme_modes_values" - android:defaultValue="FOLLOW"/> + android:defaultValue="LIGHT"/>