Fix loading genres

main
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.MusicUtil
import code.name.monkey.retromusic.util.NavigationUtil import code.name.monkey.retromusic.util.NavigationUtil
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import java.util.*
import kotlin.collections.ArrayList
import android.util.Pair as UtilPair import android.util.Pair as UtilPair
class SearchAdapter( class SearchAdapter(
@ -70,7 +68,6 @@ class SearchAdapter(
GENRE -> { GENRE -> {
val genre = dataSet?.get(position) as Genre val genre = dataSet?.get(position) as Genre
holder.title?.text = genre.name 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 -> { else -> {
holder.title?.text = dataSet?.get(position).toString() 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.Genre
import code.name.monkey.retromusic.model.Song import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import java.util.*
object GenreLoader { object GenreLoader {
fun getAllGenres(context: Context): ArrayList<Genre> { fun getAllGenres(context: Context): ArrayList<Genre> {
return getGenresFromCursor(context, makeGenreCursor(context)) return getGenresFromCursor(context, makeGenreCursor(context))
} }
fun searchGenres(context: Context): ArrayList<Genre> {
return getGenresFromCursorForSearch(context, makeGenreCursor(context));
}
fun getSongs(context: Context, genreId: Int): ArrayList<Song> { fun getSongs(context: Context, genreId: Int): ArrayList<Song> {
// The genres table only stores songs that have a genre specified, // The genres table only stores songs that have a genre specified,
// so we need to get songs without a genre a different way. // 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> { private fun getSongsWithNoGenre(context: Context): ArrayList<Song> {
val selection = BaseColumns._ID + " NOT IN " + val selection = BaseColumns._ID + " NOT IN " +
"(SELECT " + Genres.Members.AUDIO_ID + " FROM audio_genres_map)" "(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) context.contentResolver.delete(Genres.EXTERNAL_CONTENT_URI, Genres._ID + " == " + genre.id, null)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
// nothing we can do then
} }
} }
@ -118,18 +125,24 @@ object GenreLoader {
return genres 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? { private fun makeGenreCursor(context: Context): Cursor? {
val projection = arrayOf(Genres._ID, Genres.NAME) val projection = arrayOf(Genres._ID, Genres.NAME)
try { try {
return context.contentResolver.query( return context.contentResolver.query(Genres.EXTERNAL_CONTENT_URI, projection, null, null, PreferenceUtil.getInstance(context).genreSortOrder)
Genres.EXTERNAL_CONTENT_URI,
projection, null, null, PreferenceUtil.getInstance(context).genreSortOrder)
} catch (e: SecurityException) { } catch (e: SecurityException) {
return null return null
} }
} }
} }

View File

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

View File

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