From c56a6d872a319fe3a94499219fcb29317cd198ab Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 18 Jul 2022 14:24:08 +0200 Subject: [PATCH] minor fixes --- .../libretube/activities/MainActivity.kt | 2 ++ .../github/libretube/dialogs/UpdateDialog.kt | 2 +- .../libretube/fragments/PlayerFragment.kt | 20 ++++++----- .../obj/{UpdateInfo.kt => VersionInfo.kt} | 2 +- .../libretube/preferences/MainSettings.kt | 36 +++++++++++++++++-- .../github/libretube/util/UpdateChecker.kt | 34 ++++++------------ app/src/main/res/values/array.xml | 16 ++++----- app/src/main/res/xml/player_settings.xml | 2 +- 8 files changed, 70 insertions(+), 44 deletions(-) rename app/src/main/java/com/github/libretube/obj/{UpdateInfo.kt => VersionInfo.kt} (86%) diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index fad02aa03..37a642a51 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -202,6 +202,8 @@ class MainActivity : AppCompatActivity() { break } } + } else { + playlistId = playlistId.replace("list=", "") } loadPlaylist(playlistId) diff --git a/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt b/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt index 62beaf79c..7b35766c3 100644 --- a/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt @@ -31,7 +31,7 @@ class UpdateAvailableDialog( } } -class NoUpdateAvailableDialog() : DialogFragment() { +class NoUpdateAvailableDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return activity?.let { diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 8fd09cfe2..487c12a4b 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -249,7 +249,7 @@ class PlayerFragment : Fragment() { playbackSpeed = PreferenceHelper.getString( PreferenceKeys.PLAYBACK_SPEED, - "1F" + "1" )!! fullscreenOrientationPref = PreferenceHelper.getString( @@ -480,6 +480,7 @@ class PlayerFragment : Fragment() { val playbackSpeedValues = context?.resources?.getStringArray(R.array.playbackSpeedValues)!! exoPlayer.setPlaybackSpeed(playbackSpeed.toFloat()) + Log.e(TAG, playbackSpeed) val speedIndex = playbackSpeedValues.indexOf(playbackSpeed) playerBinding.speedText.text = playbackSpeeds[speedIndex] @@ -1124,12 +1125,6 @@ class PlayerFragment : Fragment() { private val hideForwardButtonRunnable = Runnable { binding.forwardBTN.visibility = View.GONE } private val hideRewindButtonRunnable = Runnable { binding.rewindBTN.visibility = View.GONE } - private fun disableDoubleTapToSeek() { - // disable fast forward and rewind by double tapping - binding.forwardFL.visibility = View.GONE - binding.rewindFL.visibility = View.GONE - } - // enable seek bar preview private fun enableSeekbarPreview() { playerBinding.exoProgress.addListener(object : TimeBar.OnScrubListener { @@ -1456,6 +1451,7 @@ class PlayerFragment : Fragment() { // lock the player private fun lockPlayer(isLocked: Boolean) { + // isLocked is the current (old) state of the player lock val visibility = if (isLocked) View.VISIBLE else View.GONE playerBinding.exoTopBarRight.visibility = visibility @@ -1474,7 +1470,15 @@ class PlayerFragment : Fragment() { ) View.VISIBLE else View.GONE // disable double tap to seek when the player is locked - if (isLocked) enableDoubleTapToSeek() else disableDoubleTapToSeek() + if (isLocked) { + // enable fast forward and rewind by double tapping + binding.forwardFL.visibility = View.VISIBLE + binding.rewindFL.visibility = View.VISIBLE + } else { + // disable fast forward and rewind by double tapping + binding.forwardFL.visibility = View.GONE + binding.rewindFL.visibility = View.GONE + } } private fun isSubscribed(button: MaterialButton, channel_id: String) { diff --git a/app/src/main/java/com/github/libretube/obj/UpdateInfo.kt b/app/src/main/java/com/github/libretube/obj/VersionInfo.kt similarity index 86% rename from app/src/main/java/com/github/libretube/obj/UpdateInfo.kt rename to app/src/main/java/com/github/libretube/obj/VersionInfo.kt index 91808912b..e52d7ef79 100644 --- a/app/src/main/java/com/github/libretube/obj/UpdateInfo.kt +++ b/app/src/main/java/com/github/libretube/obj/VersionInfo.kt @@ -1,7 +1,7 @@ package com.github.libretube.obj // data class for the update info, required to return the data -data class UpdateInfo( +data class VersionInfo( val updateUrl: String, val tagName: String ) diff --git a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt index 0f3e4b067..2006db60d 100644 --- a/app/src/main/java/com/github/libretube/preferences/MainSettings.kt +++ b/app/src/main/java/com/github/libretube/preferences/MainSettings.kt @@ -8,9 +8,15 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import com.github.libretube.BuildConfig import com.github.libretube.R +import com.github.libretube.activities.SettingsActivity import com.github.libretube.dialogs.RequireRestartDialog +import com.github.libretube.dialogs.UpdateAvailableDialog import com.github.libretube.util.ThemeHelper import com.github.libretube.util.checkUpdate +import com.google.android.material.snackbar.Snackbar +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class MainSettings : PreferenceFragmentCompat() { val TAG = "SettingsFragment" @@ -78,9 +84,35 @@ class MainSettings : PreferenceFragmentCompat() { } val update = findPreference("update") - update?.title = getString(R.string.version, BuildConfig.VERSION_NAME) + + // set the version of the update preference + val versionString = if (BuildConfig.DEBUG) "${BuildConfig.VERSION_NAME} Debug" + else getString(R.string.version, BuildConfig.VERSION_NAME) + update?.title = versionString + update?.setOnPreferenceClickListener { - checkUpdate(childFragmentManager) + CoroutineScope(Dispatchers.IO).launch { + // check for update + val versionInfo = checkUpdate() + if (versionInfo?.tagName != "" && BuildConfig.VERSION_NAME != versionInfo?.tagName) { + // show the UpdateAvailableDialog if there's an update available + val updateAvailableDialog = UpdateAvailableDialog( + versionInfo!!.tagName, + versionInfo.updateUrl + ) + updateAvailableDialog.show(childFragmentManager, "UpdateAvailableDialog") + } else { + // otherwise show the no update available snackBar + val settingsActivity = activity as SettingsActivity + val snackBar = Snackbar + .make( + settingsActivity.binding.root, + R.string.app_uptodate, + Snackbar.LENGTH_SHORT + ) + snackBar.show() + } + } true } diff --git a/app/src/main/java/com/github/libretube/util/UpdateChecker.kt b/app/src/main/java/com/github/libretube/util/UpdateChecker.kt index 3513a636d..d42880d27 100644 --- a/app/src/main/java/com/github/libretube/util/UpdateChecker.kt +++ b/app/src/main/java/com/github/libretube/util/UpdateChecker.kt @@ -1,12 +1,8 @@ package com.github.libretube.util import android.util.Log -import androidx.fragment.app.FragmentManager -import com.github.libretube.BuildConfig import com.github.libretube.GITHUB_API_URL -import com.github.libretube.dialogs.NoUpdateAvailableDialog -import com.github.libretube.dialogs.UpdateAvailableDialog -import com.github.libretube.obj.UpdateInfo +import com.github.libretube.obj.VersionInfo import org.json.JSONArray import org.json.JSONObject import java.io.BufferedReader @@ -14,34 +10,25 @@ import java.io.InputStreamReader import java.net.URL import javax.net.ssl.HttpsURLConnection -fun checkUpdate(childFragmentManager: FragmentManager) { - var updateInfo: UpdateInfo? = UpdateInfo("", "") +fun checkUpdate(): VersionInfo? { + var versionInfo: VersionInfo? = VersionInfo("", "") // run http request as thread to make it async val thread = Thread { // otherwise crashes without internet try { - updateInfo = getUpdateInfo() + versionInfo = getUpdateInfo() } catch (e: Exception) { } } thread.start() // wait for the thread to finish thread.join() - // show the UpdateAvailableDialog if there's an update available - if (updateInfo?.tagName != "" && BuildConfig.VERSION_NAME != updateInfo?.tagName) { - val updateAvailableDialog = UpdateAvailableDialog( - updateInfo?.tagName!!, - updateInfo?.updateUrl!! - ) - updateAvailableDialog.show(childFragmentManager, "UpdateAvailableDialog") - } else { - // otherwise show the no update available dialog - val noUpdateAvailableDialog = NoUpdateAvailableDialog() - noUpdateAvailableDialog.show(childFragmentManager, "NoUpdateAvailableDialog") - } + + // return the information about the latest version + return versionInfo } -fun getUpdateInfo(): UpdateInfo? { +fun getUpdateInfo(): VersionInfo? { val latest = URL(GITHUB_API_URL) val json = StringBuilder() val urlConnection: HttpsURLConnection? @@ -59,14 +46,15 @@ fun getUpdateInfo(): UpdateInfo? { ) { val updateUrl = jsonRoot.getString("html_url") val jsonAssets: JSONArray = jsonRoot.getJSONArray("assets") + for (i in 0 until jsonAssets.length()) { val jsonAsset = jsonAssets.getJSONObject(i) if (jsonAsset.has("name")) { val name = jsonAsset.getString("name") if (name.endsWith(".apk")) { val tagName = jsonRoot.getString("name") - Log.i("", "Latest version: $tagName") - return UpdateInfo(updateUrl, tagName) + Log.i("VersionInfo", "Latest version: $tagName") + return VersionInfo(updateUrl, tagName) } } } diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index 155462ea9..71fab768f 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -638,14 +638,14 @@ 4x - 0.25F - 0.5F - 0.75F - 1F - 1.25F - 1.5F - 2F - 4F + 0.25 + 0.5 + 0.75 + 1 + 1.25 + 1.5 + 2 + 4 diff --git a/app/src/main/res/xml/player_settings.xml b/app/src/main/res/xml/player_settings.xml index 10563cdc6..3ff2570a2 100644 --- a/app/src/main/res/xml/player_settings.xml +++ b/app/src/main/res/xml/player_settings.xml @@ -28,7 +28,7 @@