2022-04-15 13:32:16 +02:00
package org.mosad.teapod.ui.activity.player.fragment
2020-12-20 20:21:27 +01:00
2022-04-15 13:32:16 +02:00
import android.content.DialogInterface
2020-12-20 20:21:27 +01:00
import android.graphics.Color
import android.graphics.Typeface
2022-04-15 13:32:16 +02:00
import android.os.Bundle
2020-12-20 20:21:27 +01:00
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
2022-04-15 13:32:16 +02:00
import android.view.View
2020-12-20 20:21:27 +01:00
import android.view.ViewGroup
2023-02-19 14:21:46 +01:00
import android.widget.LinearLayout
2020-12-20 20:21:27 +01:00
import android.widget.TextView
2020-12-26 20:09:35 +01:00
import androidx.core.view.children
2022-04-15 13:32:16 +02:00
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
2020-12-20 20:21:27 +01:00
import org.mosad.teapod.R
import org.mosad.teapod.databinding.PlayerLanguageSettingsBinding
2021-02-06 19:02:12 +01:00
import org.mosad.teapod.ui.activity.player.PlayerViewModel
2022-04-15 13:32:16 +02:00
import org.mosad.teapod.util.hideBars
2020-12-27 20:11:01 +01:00
import java.util.*
2020-12-20 20:21:27 +01:00
2022-04-15 13:32:16 +02:00
class LanguageSettingsDialogFragment : DialogFragment ( ) {
private lateinit var model : PlayerViewModel
private lateinit var binding : PlayerLanguageSettingsBinding
2023-02-19 14:21:46 +01:00
private var selectedSubtitleLocale = Locale . ROOT
private var selectedAudioLocale = Locale . ROOT
2022-04-15 13:32:16 +02:00
companion object {
const val TAG = " LanguageSettingsDialogFragment "
}
override fun onCreate ( savedInstanceState : Bundle ? ) {
super . onCreate ( savedInstanceState )
setStyle ( STYLE _NO _TITLE , R . style . FullScreenDialogStyle )
model = ViewModelProvider ( requireActivity ( ) ) [ PlayerViewModel :: class . java ]
2023-02-19 14:21:46 +01:00
selectedSubtitleLocale = model . currentSubtitleLocale
2022-04-15 13:32:16 +02:00
}
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = PlayerLanguageSettingsBinding . inflate ( inflater , container , false )
return binding . root
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2023-02-19 14:21:46 +01:00
var selectedSubtitleView : TextView ? = null
2023-01-25 19:51:38 +01:00
model . currentStreams . data [ 0 ] . adaptive _hls . keys . forEach { languageTag ->
2022-04-15 13:32:16 +02:00
val locale = Locale . forLanguageTag ( languageTag )
2023-02-19 14:21:46 +01:00
val subtitleView = addLanguage ( binding . linearSubtitleLanguages , locale ) { v ->
selectedSubtitleLocale = locale
updateSelectedLanguage ( binding . linearSubtitleLanguages , v as TextView )
}
// if the view is the currently selected one, highlight it
if ( locale == model . currentSubtitleLocale ) {
selectedSubtitleView = subtitleView
updateSelectedLanguage ( binding . linearSubtitleLanguages , subtitleView )
}
}
val currentAudioLocal = Locale . forLanguageTag ( model . currentVersion . audioLocale )
var selectedAudioView : TextView ? = null
2023-04-16 16:24:28 +02:00
model . currentEpisode . versions ?. forEach { version ->
2023-02-19 14:21:46 +01:00
val locale = Locale . forLanguageTag ( version . audioLocale )
val audioView = addLanguage ( binding . linearAudioLanguages , locale ) { v ->
selectedAudioLocale = locale
updateSelectedLanguage ( binding . linearAudioLanguages , v as TextView )
}
// if the view is the currently selected one, highlight it
if ( locale == currentAudioLocal ) {
selectedAudioView = audioView
updateSelectedLanguage ( binding . linearAudioLanguages , audioView )
2021-12-29 20:51:53 +01:00
}
2020-12-26 20:09:35 +01:00
}
2020-12-20 20:21:27 +01:00
2022-04-15 13:32:16 +02:00
binding . buttonCloseLanguageSettings . setOnClickListener { dismiss ( ) }
binding . buttonCancel . setOnClickListener { dismiss ( ) }
2020-12-26 20:09:35 +01:00
binding . buttonSelect . setOnClickListener {
2023-02-19 14:21:46 +01:00
model . setLanguage ( selectedAudioLocale , selectedSubtitleLocale )
2022-04-15 13:32:16 +02:00
dismiss ( )
2020-12-26 20:09:35 +01:00
}
2022-04-15 13:32:16 +02:00
// initially hide the status and navigation bar
hideBars ( requireDialog ( ) . window , binding . root )
2022-11-05 11:24:16 +01:00
// scroll to the position of the view, if it's the selected language
2023-02-19 14:21:46 +01:00
binding . scrollSubtitleLanguages . post {
binding . scrollSubtitleLanguages . scrollTo ( 0 , selectedSubtitleView ?. top ?: 0 )
}
binding . scrollAudioLanguages . post {
binding . scrollSubtitleLanguages . scrollTo ( 0 , selectedAudioView ?. top ?: 0 )
2022-11-05 11:24:16 +01:00
}
2022-04-15 13:32:16 +02:00
}
override fun onDismiss ( dialog : DialogInterface ) {
super . onDismiss ( dialog )
model . player . play ( )
2020-12-20 20:21:27 +01:00
}
2023-02-19 14:21:46 +01:00
private fun addLanguage ( linear : LinearLayout , locale : Locale , onClick : View . OnClickListener ) : TextView {
2020-12-20 20:21:27 +01:00
val text = TextView ( context ) . apply {
height = 96
gravity = Gravity . CENTER _VERTICAL
2021-12-29 20:51:53 +01:00
text = if ( locale == Locale . ROOT ) context . getString ( R . string . no _subtitles ) else locale . displayLanguage
2020-12-20 20:21:27 +01:00
setTextSize ( TypedValue . COMPLEX _UNIT _SP , 16f )
2023-04-15 22:35:19 +02:00
setTextColor ( context . resources . getColor ( R . color . player _text , context . theme ) )
2023-02-19 14:21:46 +01:00
setPadding ( 75 , 0 , 0 , 0 )
2020-12-20 20:21:27 +01:00
setOnClickListener ( onClick )
}
2023-02-19 14:21:46 +01:00
linear . addView ( text )
return text
2020-12-20 20:21:27 +01:00
}
2023-02-19 14:21:46 +01:00
/ * *
* Highlights the selected audio / subtitle language
*
* @param languageLayout The audio / subtitle Layout to update
* @param selected The newly selected language TextView
* /
private fun updateSelectedLanguage ( languageLayout : LinearLayout , selected : TextView ) {
2020-12-26 20:09:35 +01:00
// rest all tf to not selected style
2023-02-19 14:21:46 +01:00
languageLayout . children . forEach { child ->
2020-12-26 20:09:35 +01:00
if ( child is TextView ) {
child . apply {
2023-04-15 22:35:19 +02:00
setTextColor ( context . resources . getColor ( R . color . player _text , context . theme ) )
2020-12-26 20:09:35 +01:00
setTypeface ( null , Typeface . NORMAL )
setCompoundDrawablesRelativeWithIntrinsicBounds ( 0 , 0 , 0 , 0 )
setPadding ( 75 , 0 , 0 , 0 )
}
}
}
// set selected to selected style
selected . apply {
2022-04-15 13:32:16 +02:00
setTextColor ( context . resources . getColor ( R . color . player _white , context . theme ) )
2020-12-26 20:09:35 +01:00
setTypeface ( null , Typeface . BOLD )
setCompoundDrawablesRelativeWithIntrinsicBounds ( R . drawable . ic _baseline _check _24 , 0 , 0 , 0 )
setPadding ( 0 , 0 , 0 , 0 )
compoundDrawablesRelative . getOrNull ( 0 ) ?. setTint ( Color . WHITE )
compoundDrawablePadding = 12
}
}
2022-04-15 16:28:15 +02:00
}