diff --git a/app/src/main/java/com/github/apognu/otter/adapters/RadiosAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/RadiosAdapter.kt index 9943550..a596ec2 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/RadiosAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/RadiosAdapter.kt @@ -32,9 +32,25 @@ class RadiosAdapter(val context: Context?, private val listener: OnRadioClickLis holder.name.text = radio.name holder.description.text = radio.description + + context?.let { context -> + when (radio.radio_type) { + "random" -> { + holder.art.setImageDrawable(context.getDrawable(R.drawable.shuffle)) + holder.art.alpha = 0.7f + holder.art.setColorFilter(context.getColor(R.color.controlForeground)) + } + "less-listened" -> { + holder.art.setImageDrawable(context.getDrawable(R.drawable.sad)) + holder.art.alpha = 0.7f + holder.art.setColorFilter(context.getColor(R.color.controlForeground)) + } + } + } } - inner class ViewHolder(view: View, val listener: OnRadioClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { + inner class ViewHolder(view: View, private val listener: OnRadioClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener { + val art = view.art val name = view.name val description = view.description diff --git a/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt b/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt index 8bb6ae8..e5637fc 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt @@ -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) { 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) { 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) { 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) { .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() diff --git a/app/src/main/java/com/github/apognu/otter/repositories/RadiosRepository.kt b/app/src/main/java/com/github/apognu/otter/repositories/RadiosRepository.kt index 0c03bec..2ed9dff 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/RadiosRepository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/RadiosRepository.kt @@ -1,6 +1,7 @@ package com.github.apognu.otter.repositories import android.content.Context +import com.github.apognu.otter.R import com.github.apognu.otter.utils.FunkwhaleResponse import com.github.apognu.otter.utils.Radio import com.github.apognu.otter.utils.RadiosCache @@ -15,4 +16,18 @@ class RadiosRepository(override val context: Context?) : Repository) = RadiosCache(data) override fun uncache(reader: BufferedReader) = gsonDeserializerOf(RadiosCache::class.java).deserialize(reader) + + override fun onDataFetched(data: List): List { + return data + .map { radio -> + radio.apply { radio_type = "custom" } + } + .toMutableList() + .apply { + context?.let { context -> + add(0, Radio(0, "random", context.getString(R.string.radio_random_title), context.getString(R.string.radio_random_description))) + add(1, Radio(0, "less-listened", context.getString(R.string.radio_less_listened_title), context.getString(R.string.radio_less_listened_description))) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/utils/Models.kt b/app/src/main/java/com/github/apognu/otter/utils/Models.kt index 82c2990..06e3518 100644 --- a/app/src/main/java/com/github/apognu/otter/utils/Models.kt +++ b/app/src/main/java/com/github/apognu/otter/utils/Models.kt @@ -142,6 +142,7 @@ data class PlaylistTrack(val track: Track) data class Radio( val id: Int, + var radio_type: String, val name: String, val description: String ) \ No newline at end of file diff --git a/app/src/main/res/drawable/sad.xml b/app/src/main/res/drawable/sad.xml new file mode 100644 index 0000000..a04b621 --- /dev/null +++ b/app/src/main/res/drawable/sad.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 7ec9708..8ef8640 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -101,6 +101,10 @@ Instance Funkwhale Une erreur s\'est produite lors de la lecture de cette radio + Aléatoire + Choix de pistes totalement aléatoires, vous découvrirez peut-être quelque chose ? + Moins écoutées + Découvrez les morceaux que vous n\'écoutez généralement pas. Il est temps de rétablir un équilibre. Déconnexion Etes-vous certains de vouloir vous déconnecter de votre instance Funkwhale ? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 865378d..b018a6b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,6 +101,10 @@ Funkwhale instance There was an error while trying to play this radio + Random + Totally random picks, maybe you\'ll discover new things? + Less listened + Listen to tracks you usually don\'t. It\'s time to restore some balance. Sign out Are you sure you want to sign out of your Funkwhale instance?