Favorites button is now async. Added favorite button management in queue and search.
This commit is contained in:
parent
09ada772e6
commit
cac32332e0
|
@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity
|
|||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.github.apognu.otter.R
|
||||
import com.github.apognu.otter.adapters.TracksAdapter
|
||||
import com.github.apognu.otter.repositories.FavoritesRepository
|
||||
import com.github.apognu.otter.repositories.Repository
|
||||
import com.github.apognu.otter.repositories.SearchRepository
|
||||
import com.github.apognu.otter.utils.untilNetwork
|
||||
|
@ -17,13 +18,14 @@ class SearchActivity : AppCompatActivity() {
|
|||
private lateinit var adapter: TracksAdapter
|
||||
|
||||
lateinit var repository: SearchRepository
|
||||
lateinit var favoritesRepository: FavoritesRepository
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
setContentView(R.layout.activity_search)
|
||||
|
||||
adapter = TracksAdapter(this).also {
|
||||
adapter = TracksAdapter(this, FavoriteListener()).also {
|
||||
results.layoutManager = LinearLayoutManager(this)
|
||||
results.adapter = it
|
||||
}
|
||||
|
@ -40,6 +42,7 @@ class SearchActivity : AppCompatActivity() {
|
|||
val query = URLEncoder.encode(it, "UTF-8")
|
||||
|
||||
repository = SearchRepository(this@SearchActivity, query.toLowerCase(Locale.ROOT))
|
||||
favoritesRepository = FavoritesRepository(this@SearchActivity)
|
||||
|
||||
search_spinner.visibility = View.VISIBLE
|
||||
search_no_results.visibility = View.GONE
|
||||
|
@ -66,4 +69,13 @@ class SearchActivity : AppCompatActivity() {
|
|||
override fun onQueryTextChange(newText: String?) = true
|
||||
})
|
||||
}
|
||||
|
||||
inner class FavoriteListener : TracksAdapter.OnFavoriteListener {
|
||||
override fun onToggleFavorite(id: Int, state: Boolean) {
|
||||
when (state) {
|
||||
true -> favoritesRepository.addFavorite(id)
|
||||
false -> favoritesRepository.deleteFavorite(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -55,6 +55,8 @@ class TracksAdapter(private val context: Context?, private val favoriteListener:
|
|||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val track = data[position]
|
||||
|
||||
log("Bind ${track.favorite}")
|
||||
|
||||
Picasso.get()
|
||||
.maybeLoad(maybeNormalizeUrl(track.album.cover.original))
|
||||
.fit()
|
||||
|
@ -90,7 +92,8 @@ class TracksAdapter(private val context: Context?, private val favoriteListener:
|
|||
favoriteListener?.let {
|
||||
favoriteListener.onToggleFavorite(track.id, !track.favorite)
|
||||
|
||||
track.favorite = !track.favorite
|
||||
data[position].favorite = !track.favorite
|
||||
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import androidx.fragment.app.DialogFragment
|
|||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.github.apognu.otter.R
|
||||
import com.github.apognu.otter.adapters.TracksAdapter
|
||||
import com.github.apognu.otter.repositories.FavoritesRepository
|
||||
import com.github.apognu.otter.utils.*
|
||||
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||
|
@ -22,9 +23,13 @@ import kotlinx.coroutines.launch
|
|||
class QueueFragment : BottomSheetDialogFragment() {
|
||||
private var adapter: TracksAdapter? = null
|
||||
|
||||
lateinit var favoritesRepository: FavoritesRepository
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
favoritesRepository = FavoritesRepository(context)
|
||||
|
||||
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_FloatingBottomSheet)
|
||||
|
||||
watchEventBus()
|
||||
|
@ -42,7 +47,7 @@ class QueueFragment : BottomSheetDialogFragment() {
|
|||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_queue, container, false).apply {
|
||||
adapter = TracksAdapter(context, fromQueue = true).also {
|
||||
adapter = TracksAdapter(context, FavoriteListener(), fromQueue = true).also {
|
||||
queue.layoutManager = LinearLayoutManager(context)
|
||||
queue.adapter = it
|
||||
}
|
||||
|
@ -87,4 +92,13 @@ class QueueFragment : BottomSheetDialogFragment() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
inner class FavoriteListener : TracksAdapter.OnFavoriteListener {
|
||||
override fun onToggleFavorite(id: Int, state: Boolean) {
|
||||
when (state) {
|
||||
true -> favoritesRepository.addFavorite(id)
|
||||
false -> favoritesRepository.deleteFavorite(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,7 +9,8 @@ import com.google.gson.Gson
|
|||
import com.google.gson.reflect.TypeToken
|
||||
import com.preference.PowerPreference
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import java.io.BufferedReader
|
||||
|
||||
class FavoritesRepository(override val context: Context?) : Repository<Track, TracksCache>() {
|
||||
|
@ -28,7 +29,7 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
|
|||
val token = PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).getString("access_token")
|
||||
val body = mapOf("track" to id)
|
||||
|
||||
runBlocking(IO) {
|
||||
GlobalScope.launch(IO) {
|
||||
Fuel
|
||||
.post(mustNormalizeUrl("/api/v1/favorites/tracks/"))
|
||||
.header("Authorization", "Bearer $token")
|
||||
|
@ -42,7 +43,7 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
|
|||
val token = PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).getString("access_token")
|
||||
val body = mapOf("track" to id)
|
||||
|
||||
runBlocking(IO) {
|
||||
GlobalScope.launch(IO) {
|
||||
Fuel
|
||||
.post(mustNormalizeUrl("/api/v1/favorites/tracks/remove/"))
|
||||
.header("Authorization", "Bearer $token")
|
||||
|
|
Loading…
Reference in New Issue