small fixes
This commit is contained in:
parent
a52478b874
commit
481b3ea092
2 changed files with 67 additions and 20 deletions
|
|
@ -14,6 +14,8 @@ import android.provider.Settings
|
|||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.os.LocaleListCompat
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.ListPreference
|
||||
|
|
@ -27,6 +29,7 @@ import androidx.recyclerview.widget.RecyclerView
|
|||
import androidx.work.WorkInfo
|
||||
import androidx.work.WorkManager
|
||||
import com.deniscerri.ytdl.R
|
||||
import com.deniscerri.ytdl.database.viewmodel.ResultViewModel
|
||||
import com.deniscerri.ytdl.databinding.NavOptionsItemBinding
|
||||
import com.deniscerri.ytdl.ui.adapter.NavBarOptionsAdapter
|
||||
import com.deniscerri.ytdl.util.NavbarUtil
|
||||
|
|
@ -34,12 +37,16 @@ import com.deniscerri.ytdl.util.ThemeUtil
|
|||
import com.deniscerri.ytdl.util.UiUtil
|
||||
import com.deniscerri.ytdl.util.UpdateUtil
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Locale
|
||||
|
||||
|
||||
class GeneralSettingsFragment : BaseSettingsFragment() {
|
||||
override val title: Int = R.string.general
|
||||
private lateinit var preferences: SharedPreferences
|
||||
private lateinit var resultViewModel: ResultViewModel
|
||||
|
||||
private var updateUtil: UpdateUtil? = null
|
||||
private var activeDownloadCount = 0
|
||||
|
|
@ -49,6 +56,7 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
|||
setPreferencesFromResource(R.xml.general_preferences, rootKey)
|
||||
NavbarUtil.init(requireContext())
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
resultViewModel = ViewModelProvider(this)[ResultViewModel::class.java]
|
||||
updateUtil = UpdateUtil(requireContext())
|
||||
val editor = preferences.edit()
|
||||
|
||||
|
|
@ -288,6 +296,13 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
|||
|
||||
findPreference<EditTextPreference>("api_key")?.isVisible = newValue == "yt_api"
|
||||
findPreference<EditTextPreference>("custom_home_recommendation_url")?.isVisible = newValue == "custom"
|
||||
|
||||
lifecycleScope.launch {
|
||||
withContext(Dispatchers.IO){
|
||||
resultViewModel.deleteAll()
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
@ -295,6 +310,16 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
|||
findPreference<EditTextPreference>("custom_home_recommendation_url")?.apply {
|
||||
title = "[${getString(R.string.video_recommendations)}] ${getString(R.string.custom)}"
|
||||
isVisible = preferences.getString("recommendations_home", "") == "custom"
|
||||
|
||||
|
||||
setOnPreferenceChangeListener { preference, newValue ->
|
||||
lifecycleScope.launch {
|
||||
withContext(Dispatchers.IO){
|
||||
resultViewModel.deleteAll()
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
findPreference<EditTextPreference>("api_key")?.apply {
|
||||
|
|
@ -311,6 +336,13 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
|||
}else {
|
||||
"${s}\n[${newValue}]"
|
||||
}
|
||||
|
||||
lifecycleScope.launch {
|
||||
withContext(Dispatchers.IO){
|
||||
resultViewModel.deleteAll()
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import com.google.gson.reflect.TypeToken
|
|||
import com.yausername.youtubedl_android.YoutubeDL
|
||||
import com.yausername.youtubedl_android.YoutubeDLRequest
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.processNextEventInCurrentThread
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.InputStreamReader
|
||||
import java.net.HttpURLConnection
|
||||
|
|
@ -28,6 +29,10 @@ class UpdateUtil(var context: Context) {
|
|||
Pair<String, YoutubeDL.UpdateChannel>("master", YoutubeDL.UpdateChannel.MASTER)
|
||||
)
|
||||
|
||||
private fun String.tagNameToVersionNumber() : Int {
|
||||
return this.replace("-beta", "").replace(".", "").padEnd(10,'0').toInt()
|
||||
}
|
||||
|
||||
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
||||
fun tryGetNewVersion() : Result<GithubRelease> {
|
||||
try {
|
||||
|
|
@ -38,31 +43,41 @@ class UpdateUtil(var context: Context) {
|
|||
return Result.failure(Error(context.getString(R.string.network_error)))
|
||||
}
|
||||
|
||||
val currentVersion = BuildConfig.VERSION_NAME
|
||||
val currentVerNumber = currentVersion.tagNameToVersionNumber()
|
||||
|
||||
val useBeta = sharedPreferences.getBoolean("update_beta", false)
|
||||
val v: GithubRelease
|
||||
if (useBeta){
|
||||
val tmp = res.firstOrNull { it.tag_name.contains("beta", true) && res.indexOf(it) == 0 }
|
||||
v = tmp ?: res.first()
|
||||
}else{
|
||||
v = res.first { !it.tag_name.contains("beta", true) }
|
||||
}
|
||||
|
||||
val current = BuildConfig.VERSION_NAME.replace("-beta", "").replace(".", "").padEnd(10,'0').toInt()
|
||||
val incoming = v.tag_name.removePrefix("v").replace("-beta", "").replace(".", "").padEnd(10,'0').toInt()
|
||||
|
||||
|
||||
var isInLatest = true
|
||||
if ((current < incoming) ||
|
||||
(current > incoming && BuildConfig.VERSION_NAME.contains("beta", true) && !useBeta)){
|
||||
isInLatest = false
|
||||
|
||||
var v: GithubRelease
|
||||
if (useBeta) {
|
||||
v = res.firstOrNull { it.tag_name.contains("beta", true) } ?: res.first()
|
||||
val stableV = res.first { !it.tag_name.contains("beta", true) }
|
||||
|
||||
val incomingVerNumber = v.tag_name.removePrefix("v").tagNameToVersionNumber()
|
||||
val incomingStableVerNumber = stableV.tag_name.removePrefix("v").tagNameToVersionNumber()
|
||||
|
||||
//if in beta but latest stable higher
|
||||
if (currentVerNumber < incomingStableVerNumber) {
|
||||
isInLatest = false
|
||||
v = stableV
|
||||
}else{
|
||||
isInLatest = currentVerNumber >= incomingVerNumber
|
||||
}
|
||||
}else {
|
||||
v = res.first { !it.tag_name.contains("beta", true) }
|
||||
val incomingVerNumber = v.tag_name.removePrefix("v").tagNameToVersionNumber()
|
||||
|
||||
//if current version is beta but wants to downgrade to stable, allow it
|
||||
isInLatest = if (currentVersion.contains("beta", true)) {
|
||||
false
|
||||
}else {
|
||||
currentVerNumber >= incomingVerNumber
|
||||
}
|
||||
}
|
||||
|
||||
if (skippedVersions.contains(v.tag_name)) isInLatest = true
|
||||
|
||||
if (isInLatest){
|
||||
return Result.failure(Error(context.getString(R.string.you_are_in_latest_version)))
|
||||
}
|
||||
|
||||
if (isInLatest) return Result.failure(Error(context.getString(R.string.you_are_in_latest_version)))
|
||||
return Result.success(v)
|
||||
}catch (e: Exception){
|
||||
e.printStackTrace()
|
||||
|
|
|
|||
Loading…
Reference in a new issue