improve autoplay next episode

* add animations to show/hide next episode button
* add option to disable/enable autoplay
This commit is contained in:
2020-11-15 13:39:33 +01:00
parent c8d80ddc9f
commit d4fa726f9c
9 changed files with 161 additions and 14 deletions

View File

@ -1,5 +1,7 @@
package org.mosad.teapod
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.SuppressLint
import android.net.Uri
import android.os.Build
@ -28,6 +30,7 @@ import org.mosad.teapod.util.Episode
import org.mosad.teapod.util.Media
import java.util.concurrent.TimeUnit
class PlayerActivity : AppCompatActivity() {
private lateinit var player: SimpleExoPlayer
@ -157,7 +160,7 @@ class PlayerActivity : AppCompatActivity() {
else -> View.VISIBLE
}
if (state == ExoPlayer.STATE_ENDED && nextEpisode != null) {
if (state == ExoPlayer.STATE_ENDED && nextEpisode != null && Preferences.autoplay) {
playNextEpisode()
}
@ -219,8 +222,14 @@ class PlayerActivity : AppCompatActivity() {
if (remainingTime in 0..20000) {
withContext(Dispatchers.Main) {
// if the next ep button is not visible, make it visible
if (!button_next_ep.isVisible && nextEpisode != null) {
button_next_ep.visibility = View.VISIBLE // TODO animation
if (!button_next_ep.isVisible && nextEpisode != null && Preferences.autoplay) {
showButtonNextEp()
}
}
} else {
withContext(Dispatchers.Main) {
if (button_next_ep.isVisible) {
hideButtonNextEp()
}
}
}
@ -253,7 +262,7 @@ class PlayerActivity : AppCompatActivity() {
// update the gui
exo_text_title.text = nextEp.title
button_next_ep.visibility = View.GONE // TODO animation
hideButtonNextEp()
player.clearMediaItems() //remove previous item
val mediaSource = HlsMediaSource.Factory(dataSourceFactory)
@ -316,6 +325,36 @@ class PlayerActivity : AppCompatActivity() {
}
}
/**
* show the next episode button
* TODO improve the show animation
*/
private fun showButtonNextEp() {
button_next_ep.visibility = View.VISIBLE
button_next_ep.alpha = 0.0f
button_next_ep.animate()
.alpha(1.0f)
.setListener(null)
}
/**
* hide the next episode button
* TODO improve the hide animation
*/
private fun hideButtonNextEp() {
button_next_ep.animate()
.alpha(0.0f)
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
button_next_ep.visibility = View.GONE
}
})
}
inner class PlayerGestureListener : GestureDetector.SimpleOnGestureListener() {
/**

View File

@ -7,6 +7,8 @@ object Preferences {
var preferSecondary = false
internal set
var autoplay = true
internal set
fun savePreferSecondary(context: Context, preferSecondary: Boolean) {
val sharedPref = context.getSharedPreferences(
@ -22,6 +24,20 @@ object Preferences {
this.preferSecondary = preferSecondary
}
fun saveAutoplay(context: Context, autoplay: Boolean) {
val sharedPref = context.getSharedPreferences(
context.getString(R.string.preference_file_key),
Context.MODE_PRIVATE
)
with(sharedPref.edit()) {
putBoolean(context.getString(R.string.save_key_autoplay), autoplay)
apply()
}
this.autoplay = autoplay
}
/**
* initially load the stored values
*/
@ -34,6 +50,9 @@ object Preferences {
preferSecondary = sharedPref.getBoolean(
context.getString(R.string.save_key_prefer_secondary), false
)
autoplay = sharedPref.getBoolean(
context.getString(R.string.save_key_autoplay), true
)
}

View File

@ -28,6 +28,7 @@ class AccountFragment : Fragment() {
text_account_login.text = EncryptedPreferences.login
text_info_about_desc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
switch_secondary.isChecked = Preferences.preferSecondary
switch_autoplay.isChecked = Preferences.autoplay
initActions()
}
@ -57,6 +58,10 @@ class AccountFragment : Fragment() {
switch_secondary.setOnClickListener {
Preferences.savePreferSecondary(requireContext(), switch_secondary.isChecked)
}
switch_autoplay.setOnClickListener {
Preferences.saveAutoplay(requireContext(), switch_autoplay.isChecked)
}
}
private fun showLoginDialog(firstTry: Boolean) {

View File

@ -6,12 +6,15 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_library.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.mosad.teapod.MainActivity
import org.mosad.teapod.R
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.util.decoration.MediaItemDecoration
import org.mosad.teapod.util.adapter.MediaItemAdapter
import org.mosad.teapod.util.decoration.MediaItemDecoration
class LibraryFragment : Fragment() {