@ -18,7 +18,7 @@ import kotlinx.coroutines.launch
@@ -18,7 +18,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.withContext
data class RadioSessionBody ( val radio _type : String , val custom _radio : Int )
data class RadioSessionBody ( val radio _type : String , var custom _radio : Int ? = null )
data class RadioSession ( val id : Int )
data class RadioTrackBody ( val session : Int )
data class RadioTrack ( val position : Int , val track : RadioTrackID )
@ -33,10 +33,12 @@ class RadioPlayer(val context: Context) {
@@ -33,10 +33,12 @@ class RadioPlayer(val context: Context) {
private val favoritedRepository = FavoritedRepository ( context )
init {
Cache . get ( context , " radio_id " ) ?. readLine ( ) ?. toInt ( ) ?. let { radio _id ->
Cache . get ( context , " radio_session " ) ?. readLine ( ) ?. toInt ( ) ?. let { radio _session ->
currentRadio = Radio ( radio _id , " " , " " )
session = radio _session
Cache . get ( context , " radio_type " ) ?. readLine ( ) ?. let { radio _type ->
Cache . get ( context , " radio_id " ) ?. readLine ( ) ?. toInt ( ) ?. let { radio _id ->
Cache . get ( context , " radio_session " ) ?. readLine ( ) ?. toInt ( ) ?. let { radio _session ->
currentRadio = Radio ( radio _id , radio _type , " " , " " )
session = radio _session
}
}
}
}
@ -54,6 +56,7 @@ class RadioPlayer(val context: Context) {
@@ -54,6 +56,7 @@ class RadioPlayer(val context: Context) {
currentRadio = null
session = null
Cache . delete ( context , " radio_type " )
Cache . delete ( context , " radio_id " )
Cache . delete ( context , " radio_session " )
}
@ -61,27 +64,34 @@ class RadioPlayer(val context: Context) {
@@ -61,27 +64,34 @@ class RadioPlayer(val context: Context) {
fun isActive ( ) = currentRadio != null && session != null
private suspend fun createSession ( ) {
currentRadio ?. let { radio ->
try {
val body = Gson ( ) . toJson ( RadioSessionBody ( " custom " , radio . id ) )
val result = Fuel . post ( mustNormalizeUrl ( " /api/v1/radios/sessions/ " ) )
. authorize ( )
. header ( " Content-Type " , " application/json " )
. body ( body )
. awaitObjectResult ( gsonDeserializerOf ( RadioSession :: class . java ) )
session = result . get ( ) . id
Cache . set ( context , " radio_id " , radio . id . toString ( ) . toByteArray ( ) )
Cache . set ( context , " radio_session " , session . toString ( ) . toByteArray ( ) )
prepareNextTrack ( true )
} catch ( e : Exception ) {
withContext ( Main ) {
context . toast ( context . getString ( R . string . radio _playback _error ) )
currentRadio ?. let { radio ->
try {
val request = RadioSessionBody ( radio . radio _type ) . apply {
if ( radio _type == " custom " ) {
custom _radio = radio . id
}
}
val body = Gson ( ) . toJson ( request )
val result = Fuel . post ( mustNormalizeUrl ( " /api/v1/radios/sessions/ " ) )
. authorize ( )
. header ( " Content-Type " , " application/json " )
. body ( body )
. awaitObjectResult ( gsonDeserializerOf ( RadioSession :: class . java ) )
session = result . get ( ) . id
Cache . set ( context , " radio_type " , radio . radio _type . toByteArray ( ) )
Cache . set ( context , " radio_id " , radio . id . toString ( ) . toByteArray ( ) )
Cache . set ( context , " radio_session " , session . toString ( ) . toByteArray ( ) )
prepareNextTrack ( true )
} catch ( e : Exception ) {
withContext ( Main ) {
context . toast ( context . getString ( R . string . radio _playback _error ) )
}
}
}
}
suspend fun prepareNextTrack ( first : Boolean = false ) {
@ -98,7 +108,7 @@ class RadioPlayer(val context: Context) {
@@ -98,7 +108,7 @@ class RadioPlayer(val context: Context) {
. authorize ( )
. awaitObjectResult ( gsonDeserializerOf ( Track :: class . java ) )
val favorites = FavoritedRepository ( context ) . fetch ( Repository . Origin . Network . origin )
val favorites = favoritedRepository . fetch ( Repository . Origin . Network . origin )
. map { it . data }
. toList ( )
. flatten ( )