Browse Source

Fixed an issue where favorited tracks would not show up erroneously in track lists.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
61fdb116ad
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 7
      app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt
  2. 9
      app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt
  3. 12
      app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt
  4. 2
      app/src/main/java/com/github/apognu/otter/repositories/TracksRepository.kt

7
app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt

@ -43,7 +43,6 @@ import kotlinx.coroutines.Dispatchers.Default @@ -43,7 +43,6 @@ import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -53,7 +52,7 @@ class MainActivity : AppCompatActivity() { @@ -53,7 +52,7 @@ class MainActivity : AppCompatActivity() {
}
private val favoriteRepository = FavoritesRepository(this)
private val favoriteCheckRepository = FavoritedRepository(this)
private val favoritedRepository = FavoritedRepository(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -78,7 +77,7 @@ class MainActivity : AppCompatActivity() { @@ -78,7 +77,7 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
favoriteCheckRepository.fetch().map { Cache.set(this, favoriteCheckRepository.cacheId, Gson().toJson(favoriteCheckRepository.cache(it.data)).toByteArray()) }
favoritedRepository.update(this, lifecycleScope)
startService(Intent(this, PlayerService::class.java))
DownloadService.start(this, PinService::class.java)
@ -427,7 +426,7 @@ class MainActivity : AppCompatActivity() { @@ -427,7 +426,7 @@ class MainActivity : AppCompatActivity() {
}
now_playing_details_favorite?.let { now_playing_details_favorite ->
favoriteCheckRepository.fetch().untilNetwork(lifecycleScope, IO) { favorites, _, _, _ ->
favoritedRepository.fetch().untilNetwork(lifecycleScope, IO) { favorites, _, _, _ ->
lifecycleScope.launch(Main) {
track.favorite = favorites.contains(track.id)

9
app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt

@ -9,6 +9,7 @@ import androidx.lifecycle.lifecycleScope @@ -9,6 +9,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import com.github.apognu.otter.R
import com.github.apognu.otter.adapters.TracksAdapter
import com.github.apognu.otter.repositories.FavoritedRepository
import com.github.apognu.otter.repositories.FavoritesRepository
import com.github.apognu.otter.repositories.TracksRepository
import com.github.apognu.otter.utils.*
@ -27,6 +28,7 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() { @@ -27,6 +28,7 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() {
override val recycler: RecyclerView get() = tracks
lateinit var favoritesRepository: FavoritesRepository
lateinit var favoritedRepository: FavoritedRepository
private var albumId = 0
private var albumArtist = ""
@ -59,6 +61,7 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() { @@ -59,6 +61,7 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() {
adapter = TracksAdapter(context, FavoriteListener())
repository = TracksRepository(context, albumId)
favoritesRepository = FavoritesRepository(context)
favoritedRepository = FavoritedRepository(context)
watchEventBus()
}
@ -147,7 +150,10 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() { @@ -147,7 +150,10 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() {
lifecycleScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
is Command.RefreshTrack -> {
log("${command.track?.title} -> ${command.track?.favorite}")
refreshCurrentTrack(command.track)
}
}
}
}
@ -185,6 +191,7 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() { @@ -185,6 +191,7 @@ class TracksFragment : OtterFragment<Track, TracksAdapter>() {
adapter.currentTrack = track.apply {
current = true
}
adapter.notifyDataSetChanged()
}
}

12
app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
package com.github.apognu.otter.repositories
import android.content.Context
import androidx.lifecycle.lifecycleScope
import com.github.apognu.otter.Otter
import com.github.apognu.otter.utils.*
import com.github.kittinunf.fuel.Fuel
@ -8,6 +9,7 @@ import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult @@ -8,6 +9,7 @@ import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@ -55,7 +57,7 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr @@ -55,7 +57,7 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
favoritedRepository.fetch().map { Cache.set(context, favoritedRepository.cacheId, Gson().toJson(favoritedRepository.cache(it.data)).toByteArray()) }
favoritedRepository.update(context, scope)
}
}
@ -74,7 +76,7 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr @@ -74,7 +76,7 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
favoritedRepository.fetch().map { Cache.set(context, favoritedRepository.cacheId, Gson().toJson(favoritedRepository.cache(it.data)).toByteArray()) }
favoritedRepository.update(context, scope)
}
}
}
@ -85,4 +87,10 @@ class FavoritedRepository(override val context: Context?) : Repository<Int, Favo @@ -85,4 +87,10 @@ class FavoritedRepository(override val context: Context?) : Repository<Int, Favo
override fun cache(data: List<Int>) = FavoritedCache(data)
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(FavoritedCache::class.java).deserialize(reader)
fun update(context: Context?, scope: CoroutineScope) {
fetch(Origin.Network.origin).untilNetwork(scope, IO) { favorites, _, _, _ ->
Cache.set(context, cacheId, Gson().toJson(cache(favorites)).toByteArray())
}
}
}

2
app/src/main/java/com/github/apognu/otter/repositories/TracksRepository.kt

@ -38,7 +38,7 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor @@ -38,7 +38,7 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor
}
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
val favorites = FavoritedRepository(context).fetch(Origin.Network.origin)
val favorites = FavoritedRepository(context).fetch(Origin.Cache.origin)
.map { it.data }
.toList()
.flatten()

Loading…
Cancel
Save