Fix loading genres

This commit is contained in:
h4h13 2019-12-08 23:57:07 +05:30
parent fe603fb11c
commit a3323aeb7f
4 changed files with 26 additions and 17 deletions

View file

@ -20,8 +20,6 @@ import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
import code.name.monkey.retromusic.util.NavigationUtil
import com.bumptech.glide.Glide
import java.util.*
import kotlin.collections.ArrayList
import android.util.Pair as UtilPair
class SearchAdapter(
@ -70,7 +68,6 @@ class SearchAdapter(
GENRE -> {
val genre = dataSet?.get(position) as Genre
holder.title?.text = genre.name
holder.text?.text = String.format(Locale.getDefault(), "%d %s", genre.songCount, if (genre.songCount > 1) activity.getString(R.string.songs) else activity.getString(R.string.song))
}
else -> {
holder.title?.text = dataSet?.get(position).toString()

View file

@ -24,16 +24,18 @@ import code.name.monkey.retromusic.Constants.baseProjection
import code.name.monkey.retromusic.model.Genre
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.PreferenceUtil
import java.util.*
object GenreLoader {
fun getAllGenres(context: Context): ArrayList<Genre> {
return getGenresFromCursor(context, makeGenreCursor(context))
}
fun searchGenres(context: Context): ArrayList<Genre> {
return getGenresFromCursorForSearch(context, makeGenreCursor(context));
}
fun getSongs(context: Context, genreId: Int): ArrayList<Song> {
// The genres table only stores songs that have a genre specified,
// so we need to get songs without a genre a different way.
@ -51,6 +53,12 @@ object GenreLoader {
}
private fun getGenreFromCursorWithOutSongs(context: Context, cursor: Cursor): Genre {
val id = cursor.getInt(0)
val name = cursor.getString(1)
return Genre(id, name, -1)
}
private fun getSongsWithNoGenre(context: Context): ArrayList<Song> {
val selection = BaseColumns._ID + " NOT IN " +
"(SELECT " + Genres.Members.AUDIO_ID + " FROM audio_genres_map)"
@ -107,7 +115,6 @@ object GenreLoader {
context.contentResolver.delete(Genres.EXTERNAL_CONTENT_URI, Genres._ID + " == " + genre.id, null)
} catch (e: Exception) {
e.printStackTrace()
// nothing we can do then
}
}
@ -118,18 +125,24 @@ object GenreLoader {
return genres
}
private fun getGenresFromCursorForSearch(context: Context, cursor: Cursor?): ArrayList<Genre> {
val genres = arrayListOf<Genre>()
if (cursor != null && cursor.moveToFirst()) {
do {
genres.add(getGenreFromCursorWithOutSongs(context, cursor))
} while (cursor.moveToNext())
}
cursor?.close()
return genres
}
private fun makeGenreCursor(context: Context): Cursor? {
val projection = arrayOf(Genres._ID, Genres.NAME)
try {
return context.contentResolver.query(
Genres.EXTERNAL_CONTENT_URI,
projection, null, null, PreferenceUtil.getInstance(context).genreSortOrder)
return context.contentResolver.query(Genres.EXTERNAL_CONTENT_URI, projection, null, null, PreferenceUtil.getInstance(context).genreSortOrder)
} catch (e: SecurityException) {
return null
}
}
}

View file

@ -41,10 +41,7 @@ object SearchLoader {
results.add(context.resources.getString(R.string.albums))
results.addAll(albums)
}
val genres: List<Genre> = GenreLoader.getAllGenres(context).filter { genre -> genre.name.toLowerCase(Locale.getDefault()).contains(searchString.toLowerCase(Locale.getDefault())) }
genres.forEach {
println(it.name)
}
val genres: List<Genre> = GenreLoader.searchGenres(context).filter { genre -> genre.name.toLowerCase(Locale.getDefault()).contains(searchString.toLowerCase(Locale.getDefault())) }
if (genres.isNotEmpty()) {
results.add(context.resources.getString(R.string.genres))
results.addAll(genres)

View file

@ -4,8 +4,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="?attr/colorSurface"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
@ -13,7 +13,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:elevation="0dp"
android:fitsSystemWindows="true"
app:elevation="0dp"
app:liftOnScroll="true">
<com.google.android.material.card.MaterialCardView