Added playlist search
This commit is contained in:
parent
7850e0be46
commit
f5f789bfb1
6 changed files with 91 additions and 99 deletions
|
@ -33,7 +33,7 @@ object GenreLoader {
|
|||
}
|
||||
|
||||
fun searchGenres(context: Context): ArrayList<Genre> {
|
||||
return getGenresFromCursorForSearch(context, makeGenreCursor(context));
|
||||
return getGenresFromCursorForSearch(context, makeGenreCursor(context))
|
||||
}
|
||||
|
||||
fun getSongs(context: Context, genreId: Int): ArrayList<Song> {
|
||||
|
|
|
@ -20,9 +20,7 @@ import android.provider.BaseColumns
|
|||
import android.provider.MediaStore
|
||||
import android.provider.MediaStore.Audio.PlaylistsColumns
|
||||
import code.name.monkey.retromusic.model.Playlist
|
||||
import io.reactivex.Observable
|
||||
import java.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
/**
|
||||
* Created by hemanths on 16/08/17.
|
||||
|
@ -30,24 +28,8 @@ import java.util.*
|
|||
|
||||
object PlaylistLoader {
|
||||
|
||||
private fun getPlaylistFlowable(
|
||||
cursor: Cursor?
|
||||
): Observable<Playlist> {
|
||||
return Observable.create { e ->
|
||||
var playlist = Playlist()
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
playlist = getPlaylistFromCursorImpl(cursor)
|
||||
}
|
||||
cursor?.close()
|
||||
|
||||
e.onNext(playlist)
|
||||
e.onComplete()
|
||||
}
|
||||
}
|
||||
|
||||
fun getPlaylist(
|
||||
cursor: Cursor?
|
||||
private fun getPlaylist(
|
||||
cursor: Cursor?
|
||||
): Playlist {
|
||||
var playlist = Playlist()
|
||||
|
||||
|
@ -58,66 +40,25 @@ object PlaylistLoader {
|
|||
return playlist
|
||||
}
|
||||
|
||||
fun getPlaylistFlowable(
|
||||
context: Context,
|
||||
playlistName: String
|
||||
): Observable<Playlist> {
|
||||
return getPlaylistFlowable(makePlaylistCursor(
|
||||
context,
|
||||
PlaylistsColumns.NAME + "=?",
|
||||
arrayOf(playlistName)
|
||||
))
|
||||
fun searchPlaylist(context: Context, searchString: String): List<Playlist> {
|
||||
return getAllPlaylists(
|
||||
makePlaylistCursor(
|
||||
context, PlaylistsColumns.NAME + "=?", arrayOf(searchString)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun getPlaylist(
|
||||
context: Context,
|
||||
playlistName: String
|
||||
context: Context,
|
||||
playlistName: String
|
||||
): Playlist {
|
||||
return getPlaylist(makePlaylistCursor(
|
||||
return getPlaylist(
|
||||
makePlaylistCursor(
|
||||
context,
|
||||
PlaylistsColumns.NAME + "=?",
|
||||
arrayOf(playlistName)
|
||||
))
|
||||
}
|
||||
|
||||
fun getPlaylistFlowable(
|
||||
context: Context,
|
||||
playlistId: Int
|
||||
): Observable<Playlist> {
|
||||
return getPlaylistFlowable(makePlaylistCursor(
|
||||
context,
|
||||
BaseColumns._ID + "=?",
|
||||
arrayOf(playlistId.toString())
|
||||
))
|
||||
}
|
||||
|
||||
fun getAllPlaylistsFlowoable(
|
||||
context: Context
|
||||
): Observable<ArrayList<Playlist>> {
|
||||
return getAllPlaylistsFlowable(makePlaylistCursor(context, null, null))
|
||||
}
|
||||
|
||||
fun getFavoritePlaylistFlowable(context: Context): Observable<ArrayList<Playlist>> {
|
||||
return getAllPlaylistsFlowable(makePlaylistCursor(
|
||||
context,
|
||||
PlaylistsColumns.NAME + "=?",
|
||||
arrayOf(context.getString(code.name.monkey.retromusic.R.string.favorites))))
|
||||
}
|
||||
|
||||
private fun getAllPlaylistsFlowable(cursor: Cursor?): Observable<ArrayList<Playlist>> {
|
||||
return Observable.create { e ->
|
||||
val playlists = ArrayList<Playlist>()
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
playlists.add(getPlaylistFromCursorImpl(cursor))
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
cursor?.close()
|
||||
|
||||
e.onNext(playlists)
|
||||
e.onComplete()
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun getAllPlaylists(context: Context): ArrayList<Playlist> {
|
||||
|
@ -125,10 +66,13 @@ object PlaylistLoader {
|
|||
}
|
||||
|
||||
fun getFavoritePlaylist(context: Context): ArrayList<Playlist> {
|
||||
return getAllPlaylists(makePlaylistCursor(
|
||||
return getAllPlaylists(
|
||||
makePlaylistCursor(
|
||||
context,
|
||||
PlaylistsColumns.NAME + "=?",
|
||||
arrayOf(context.getString(code.name.monkey.retromusic.R.string.favorites))))
|
||||
arrayOf(context.getString(code.name.monkey.retromusic.R.string.favorites))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun getAllPlaylists(cursor: Cursor?): ArrayList<Playlist> {
|
||||
|
@ -153,40 +97,44 @@ object PlaylistLoader {
|
|||
}
|
||||
|
||||
private fun makePlaylistCursor(
|
||||
context: Context,
|
||||
selection: String?,
|
||||
values: Array<String>?
|
||||
context: Context,
|
||||
selection: String?,
|
||||
values: Array<String>?
|
||||
): Cursor? {
|
||||
try {
|
||||
return context.contentResolver.query(
|
||||
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
arrayOf(BaseColumns._ID, /* 0 */
|
||||
PlaylistsColumns.NAME), /* 1 */
|
||||
selection,
|
||||
values,
|
||||
MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER)
|
||||
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
|
||||
arrayOf(
|
||||
BaseColumns._ID, /* 0 */
|
||||
PlaylistsColumns.NAME
|
||||
), /* 1 */
|
||||
selection,
|
||||
values,
|
||||
MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER
|
||||
)
|
||||
} catch (e: SecurityException) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun getPlaylist(
|
||||
context: Context,
|
||||
playlistId: Int
|
||||
context: Context,
|
||||
playlistId: Int
|
||||
): Playlist {
|
||||
return getPlaylist(makePlaylistCursor(
|
||||
return getPlaylist(
|
||||
makePlaylistCursor(
|
||||
context,
|
||||
BaseColumns._ID + "=?",
|
||||
arrayOf(playlistId.toString())
|
||||
))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun getPlaylistFromCursorImpl(
|
||||
cursor: Cursor
|
||||
cursor: Cursor
|
||||
): Playlist {
|
||||
val id = cursor.getInt(0)
|
||||
val name = cursor.getString(1)
|
||||
return Playlist(id, name)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,8 +17,7 @@ package code.name.monkey.retromusic.loaders
|
|||
import android.content.Context
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.model.Genre
|
||||
import java.util.*
|
||||
|
||||
import java.util.Locale
|
||||
|
||||
object SearchLoader {
|
||||
fun searchAll(context: Context, query: String?): MutableList<Any> {
|
||||
|
@ -41,11 +40,24 @@ object SearchLoader {
|
|||
results.add(context.resources.getString(R.string.albums))
|
||||
results.addAll(albums)
|
||||
}
|
||||
val genres: List<Genre> = GenreLoader.searchGenres(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()))
|
||||
}
|
||||
if (genres.isNotEmpty()) {
|
||||
results.add(context.resources.getString(R.string.genres))
|
||||
results.addAll(genres)
|
||||
}
|
||||
val playlist = PlaylistLoader.getAllPlaylists(context)
|
||||
.filter { playlist ->
|
||||
playlist.name.toLowerCase(Locale.getDefault())
|
||||
.contains(searchString.toLowerCase(Locale.getDefault()))
|
||||
}
|
||||
if (playlist.isNotEmpty()) {
|
||||
results.add(context.getString(R.string.playlists))
|
||||
results.addAll(playlist)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue