fix the updater

This commit is contained in:
Bnyro 2022-11-12 19:37:33 +01:00
parent 30b74926de
commit 9d1b79748d

View File

@ -1,6 +1,7 @@
package com.github.libretube.ui.preferences package com.github.libretube.ui.preferences
import android.os.Bundle import android.os.Bundle
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.preference.Preference import androidx.preference.Preference
import com.github.libretube.BuildConfig import com.github.libretube.BuildConfig
@ -9,7 +10,6 @@ import com.github.libretube.api.RetrofitInstance
import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.UpdateDialog import com.github.libretube.ui.dialogs.UpdateDialog
import com.github.libretube.util.NetworkHelper
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -96,56 +96,36 @@ class MainSettings : BasePreferenceFragment() {
// checking for update: yes -> dialog, no -> snackBar // checking for update: yes -> dialog, no -> snackBar
update?.setOnPreferenceClickListener { update?.setOnPreferenceClickListener {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
if (!NetworkHelper.isNetworkAvailable(requireContext())) {
(activity as? SettingsActivity)?.binding?.let {
Snackbar.make(
it.root,
R.string.unknown_error,
Snackbar.LENGTH_SHORT
)
.show()
}
return@launch
}
// check for update // check for update
val updateInfo = try { val updateInfo = try {
RetrofitInstance.externalApi.getUpdateInfo() RetrofitInstance.externalApi.getUpdateInfo()
} catch (e: Exception) { } catch (e: Exception) {
showSnackBar(R.string.server_error)
return@launch return@launch
} }
if (updateInfo.name == null) {
// request failed if (BuildConfig.VERSION_NAME != updateInfo.name) {
(activity as? SettingsActivity)?.binding?.let {
Snackbar.make(
it.root,
R.string.unknown_error,
Snackbar.LENGTH_SHORT
)
.show()
}
} else if (BuildConfig.VERSION_NAME != updateInfo.name) {
// show the UpdateAvailableDialog if there's an update available // show the UpdateAvailableDialog if there's an update available
val updateAvailableDialog = UpdateDialog(updateInfo) UpdateDialog(updateInfo).show(
updateAvailableDialog.show(
childFragmentManager, childFragmentManager,
UpdateDialog::class.java.name UpdateDialog::class.java.name
) )
} else { } else {
// otherwise show the no update available snackBar // otherwise show the no update available snackBar
(activity as? SettingsActivity)?.binding?.let { showSnackBar(R.string.app_uptodate)
Snackbar.make(
it.root,
R.string.unknown_error,
Snackbar.LENGTH_SHORT
)
.show()
}
} }
} }
true true
} }
} }
private fun showSnackBar(@StringRes text: Int) {
(activity as? SettingsActivity)?.binding?.let {
Snackbar.make(it.root, text, Snackbar.LENGTH_SHORT)
.show()
}
}
private fun navigateToSettingsFragment(newFragment: Fragment) { private fun navigateToSettingsFragment(newFragment: Fragment) {
parentFragmentManager.beginTransaction() parentFragmentManager.beginTransaction()
.replace(R.id.settings, newFragment) .replace(R.id.settings, newFragment)