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.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.os.LocaleListCompat
|
import androidx.core.os.LocaleListCompat
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import androidx.preference.EditTextPreference
|
import androidx.preference.EditTextPreference
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
|
|
@ -27,6 +29,7 @@ import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.work.WorkInfo
|
import androidx.work.WorkInfo
|
||||||
import androidx.work.WorkManager
|
import androidx.work.WorkManager
|
||||||
import com.deniscerri.ytdl.R
|
import com.deniscerri.ytdl.R
|
||||||
|
import com.deniscerri.ytdl.database.viewmodel.ResultViewModel
|
||||||
import com.deniscerri.ytdl.databinding.NavOptionsItemBinding
|
import com.deniscerri.ytdl.databinding.NavOptionsItemBinding
|
||||||
import com.deniscerri.ytdl.ui.adapter.NavBarOptionsAdapter
|
import com.deniscerri.ytdl.ui.adapter.NavBarOptionsAdapter
|
||||||
import com.deniscerri.ytdl.util.NavbarUtil
|
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.UiUtil
|
||||||
import com.deniscerri.ytdl.util.UpdateUtil
|
import com.deniscerri.ytdl.util.UpdateUtil
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
|
|
||||||
class GeneralSettingsFragment : BaseSettingsFragment() {
|
class GeneralSettingsFragment : BaseSettingsFragment() {
|
||||||
override val title: Int = R.string.general
|
override val title: Int = R.string.general
|
||||||
private lateinit var preferences: SharedPreferences
|
private lateinit var preferences: SharedPreferences
|
||||||
|
private lateinit var resultViewModel: ResultViewModel
|
||||||
|
|
||||||
private var updateUtil: UpdateUtil? = null
|
private var updateUtil: UpdateUtil? = null
|
||||||
private var activeDownloadCount = 0
|
private var activeDownloadCount = 0
|
||||||
|
|
@ -49,6 +56,7 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
||||||
setPreferencesFromResource(R.xml.general_preferences, rootKey)
|
setPreferencesFromResource(R.xml.general_preferences, rootKey)
|
||||||
NavbarUtil.init(requireContext())
|
NavbarUtil.init(requireContext())
|
||||||
preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||||
|
resultViewModel = ViewModelProvider(this)[ResultViewModel::class.java]
|
||||||
updateUtil = UpdateUtil(requireContext())
|
updateUtil = UpdateUtil(requireContext())
|
||||||
val editor = preferences.edit()
|
val editor = preferences.edit()
|
||||||
|
|
||||||
|
|
@ -288,6 +296,13 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
||||||
|
|
||||||
findPreference<EditTextPreference>("api_key")?.isVisible = newValue == "yt_api"
|
findPreference<EditTextPreference>("api_key")?.isVisible = newValue == "yt_api"
|
||||||
findPreference<EditTextPreference>("custom_home_recommendation_url")?.isVisible = newValue == "custom"
|
findPreference<EditTextPreference>("custom_home_recommendation_url")?.isVisible = newValue == "custom"
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
withContext(Dispatchers.IO){
|
||||||
|
resultViewModel.deleteAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -295,6 +310,16 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
||||||
findPreference<EditTextPreference>("custom_home_recommendation_url")?.apply {
|
findPreference<EditTextPreference>("custom_home_recommendation_url")?.apply {
|
||||||
title = "[${getString(R.string.video_recommendations)}] ${getString(R.string.custom)}"
|
title = "[${getString(R.string.video_recommendations)}] ${getString(R.string.custom)}"
|
||||||
isVisible = preferences.getString("recommendations_home", "") == "custom"
|
isVisible = preferences.getString("recommendations_home", "") == "custom"
|
||||||
|
|
||||||
|
|
||||||
|
setOnPreferenceChangeListener { preference, newValue ->
|
||||||
|
lifecycleScope.launch {
|
||||||
|
withContext(Dispatchers.IO){
|
||||||
|
resultViewModel.deleteAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
findPreference<EditTextPreference>("api_key")?.apply {
|
findPreference<EditTextPreference>("api_key")?.apply {
|
||||||
|
|
@ -311,6 +336,13 @@ class GeneralSettingsFragment : BaseSettingsFragment() {
|
||||||
}else {
|
}else {
|
||||||
"${s}\n[${newValue}]"
|
"${s}\n[${newValue}]"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lifecycleScope.launch {
|
||||||
|
withContext(Dispatchers.IO){
|
||||||
|
resultViewModel.deleteAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import com.google.gson.reflect.TypeToken
|
||||||
import com.yausername.youtubedl_android.YoutubeDL
|
import com.yausername.youtubedl_android.YoutubeDL
|
||||||
import com.yausername.youtubedl_android.YoutubeDLRequest
|
import com.yausername.youtubedl_android.YoutubeDLRequest
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.processNextEventInCurrentThread
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
|
|
@ -28,6 +29,10 @@ class UpdateUtil(var context: Context) {
|
||||||
Pair<String, YoutubeDL.UpdateChannel>("master", YoutubeDL.UpdateChannel.MASTER)
|
Pair<String, YoutubeDL.UpdateChannel>("master", YoutubeDL.UpdateChannel.MASTER)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
private fun String.tagNameToVersionNumber() : Int {
|
||||||
|
return this.replace("-beta", "").replace(".", "").padEnd(10,'0').toInt()
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
@SuppressLint("UnspecifiedRegisterReceiverFlag")
|
||||||
fun tryGetNewVersion() : Result<GithubRelease> {
|
fun tryGetNewVersion() : Result<GithubRelease> {
|
||||||
try {
|
try {
|
||||||
|
|
@ -38,31 +43,41 @@ class UpdateUtil(var context: Context) {
|
||||||
return Result.failure(Error(context.getString(R.string.network_error)))
|
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 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
|
var isInLatest = true
|
||||||
if ((current < incoming) ||
|
|
||||||
(current > incoming && BuildConfig.VERSION_NAME.contains("beta", true) && !useBeta)){
|
var v: GithubRelease
|
||||||
isInLatest = false
|
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 (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)
|
return Result.success(v)
|
||||||
}catch (e: Exception){
|
}catch (e: Exception){
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue