2021-01-21 18:22:53 +01:00
package org.mosad.teapod.util
2022-11-26 17:46:25 +01:00
import android.content.Intent
2022-04-15 13:32:16 +02:00
import android.view.View
import android.view.Window
2021-01-21 18:22:53 +01:00
import android.widget.TextView
2022-04-15 13:32:16 +02:00
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
2022-11-26 17:46:25 +01:00
import androidx.fragment.app.Fragment
import org.mosad.teapod.R
2022-01-05 00:28:49 +01:00
import org.mosad.teapod.parser.crunchyroll.Collection
2023-02-19 16:53:54 +01:00
import org.mosad.teapod.parser.crunchyroll.Collection2
2022-01-06 18:39:23 +01:00
import org.mosad.teapod.parser.crunchyroll.Item
2023-02-19 16:53:54 +01:00
import org.mosad.teapod.parser.crunchyroll.PlayheadObject
2022-11-26 17:46:25 +01:00
import org.mosad.teapod.ui.activity.player.PlayerActivity
2022-03-04 20:29:37 +01:00
import java.util.*
2021-01-21 18:22:53 +01:00
2022-11-26 17:46:25 +01:00
/ * *
* Create a Intent for PlayerActivity with season and episode id .
*
* @param seasonId The ID of the season the episode to be played is in
* @param episodeId The ID of the episode to play
* /
fun Fragment . playerIntent ( seasonId : String , episodeId : String ) = Intent ( context , PlayerActivity :: class . java ) . apply {
putExtra ( getString ( R . string . intent _season _id ) , seasonId )
putExtra ( getString ( R . string . intent _episode _id ) , episodeId )
}
2021-01-21 18:22:53 +01:00
fun TextView . setDrawableTop ( drawable : Int ) {
this . setCompoundDrawablesWithIntrinsicBounds ( 0 , drawable , 0 , 0 )
}
2021-12-31 16:03:15 +01:00
fun < T > concatenate ( vararg lists : List < T > ) : List < T > {
return listOf ( * lists ) . flatten ( )
}
2022-01-05 00:28:49 +01:00
// TODO move to correct location
2022-01-06 18:39:23 +01:00
fun Collection < Item > . toItemMediaList ( ) : List < ItemMedia > {
2022-01-05 00:28:49 +01:00
return this . items . map {
ItemMedia ( it . id , it . title , it . images . poster _wide [ 0 ] [ 0 ] . source )
}
}
2023-07-21 11:39:48 +02:00
fun Collection2 < Item > . toItemMediaList ( ) : List < ItemMedia > {
return this . data . map {
ItemMedia ( it . id , it . title , it . images . poster _wide [ 0 ] [ 0 ] . source )
}
}
2022-04-10 17:39:30 +02:00
@JvmName ( " toItemMediaListItem " )
fun List < Item > . toItemMediaList ( ) : List < ItemMedia > {
return this . map {
ItemMedia ( it . id , it . title , it . images . poster _wide [ 0 ] [ 0 ] . source )
}
}
2022-03-04 20:29:37 +01:00
fun Locale . toDisplayString ( fallback : String ) : String {
return if ( this . displayLanguage . isNotEmpty ( ) && this . displayCountry . isNotEmpty ( ) ) {
" ${this.displayLanguage} ( ${this.displayCountry} ) "
} else if ( this . displayCountry . isNotEmpty ( ) ) {
this . displayLanguage
} else {
fallback
}
}
2022-04-15 13:32:16 +02:00
2023-02-19 16:53:54 +01:00
fun Collection2 < PlayheadObject > . toPlayheadsMap ( ) : Map < String , PlayheadObject > {
return this . data . associateBy { it . contentId }
}
2022-04-15 13:32:16 +02:00
fun hideBars ( window : Window ? , root : View ) {
if ( window != null ) {
WindowCompat . setDecorFitsSystemWindows ( window , false )
WindowInsetsControllerCompat ( window , root ) . let { controller ->
controller . hide ( WindowInsetsCompat . Type . systemBars ( ) or WindowInsetsCompat . Type . navigationBars ( ) )
2022-04-15 16:28:15 +02:00
controller . systemBarsBehavior = WindowInsetsControllerCompat . BEHAVIOR _SHOW _TRANSIENT _BARS _BY _SWIPE
2022-04-15 13:32:16 +02:00
}
}
}