Merge pull request #1018 from SimonMTaye/dev

Fixed search bug
This commit is contained in:
Hemanth S 2020-12-05 02:51:17 +05:30 committed by GitHub
commit 5dd7ec15e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View file

@ -32,6 +32,8 @@ import code.name.monkey.retromusic.util.PreferenceUtil
interface GenreRepository {
fun genres(): List<Genre>
fun genres(query: String): List<Genre>
fun songs(genreId: Long): List<Song>
}
@ -44,6 +46,9 @@ class RealGenreRepository(
return getGenresFromCursor(makeGenreCursor())
}
override fun genres(query: String): List<Genre> {
return getGenresFromCursor(makeGenreCursor(arrayOf("%$query%")))
}
override fun songs(genreId: Long): List<Song> {
// The genres table only stores songs that have a genre specified,
// so we need to get songs without a genre a different way.
@ -161,4 +166,19 @@ class RealGenreRepository(
return null
}
}
private fun makeGenreCursor(selectionValues: Array<String>?): Cursor? {
val selection = MediaStore.Audio.GenresColumns.NAME + " LIKE ?"
val projection = arrayOf(Genres._ID, Genres.NAME)
return try {
contentResolver.query(
Genres.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionValues,
PreferenceUtil.genreSortOrder
)
} catch (e: SecurityException) {
return null
}
}
}

View file

@ -45,10 +45,8 @@ class RealSearchRepository(
results.add(context.resources.getString(R.string.albums))
results.addAll(albums)
}
val genres: List<Genre> = genreRepository.genres().filter { genre ->
genre.name.toLowerCase(Locale.getDefault())
.contains(searchString.toLowerCase(Locale.getDefault()))
}
val genres = genreRepository.genres(searchString)
if (genres.isNotEmpty()) {
results.add(context.resources.getString(R.string.genres))
results.addAll(genres)