diff --git a/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt b/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt index bbdac8e..cd6e776 100644 --- a/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt +++ b/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt @@ -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) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt index 1759f84..23f02b8 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/TracksAdapter.kt @@ -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) } } diff --git a/app/src/main/java/com/github/apognu/otter/fragments/QueueFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/QueueFragment.kt index 55eb780..cfab509 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/QueueFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/QueueFragment.kt @@ -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) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt b/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt index dd0c015..8194091 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt @@ -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() { @@ -28,7 +29,7 @@ class FavoritesRepository(override val context: Context?) : Repository