Code refactor
This commit is contained in:
parent
3f27463281
commit
1096cea0b4
26 changed files with 353 additions and 486 deletions
|
@ -23,9 +23,7 @@ import code.name.monkey.retromusic.model.AbsCustomPlaylist
|
|||
import code.name.monkey.retromusic.model.Playlist
|
||||
import code.name.monkey.retromusic.model.PlaylistSong
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import io.reactivex.Observable
|
||||
import java.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
/**
|
||||
* Created by hemanths on 16/08/17.
|
||||
|
@ -33,37 +31,12 @@ import java.util.*
|
|||
|
||||
object PlaylistSongsLoader {
|
||||
|
||||
fun getPlaylistSongListFlowable(
|
||||
context: Context,
|
||||
playlist: Playlist
|
||||
): Observable<ArrayList<Song>> {
|
||||
return (playlist as? AbsCustomPlaylist)?.getSongsFlowable(context)
|
||||
?: getPlaylistSongListFlowable(context, playlist.id)
|
||||
}
|
||||
|
||||
fun getPlaylistSongList(
|
||||
context: Context,
|
||||
playlist: Playlist
|
||||
context: Context,
|
||||
playlist: Playlist
|
||||
): ArrayList<Song> {
|
||||
return (playlist as? AbsCustomPlaylist)?.getSongs(context)
|
||||
?: getPlaylistSongList(context, playlist.id)
|
||||
}
|
||||
|
||||
|
||||
fun getPlaylistSongListFlowable(context: Context, playlistId: Int): Observable<ArrayList<Song>> {
|
||||
return Observable.create { e ->
|
||||
val songs = ArrayList<Song>()
|
||||
val cursor = makePlaylistSongCursor(context, playlistId)
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
songs.add(getPlaylistSongFromCursorImpl(cursor, playlistId))
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
cursor?.close()
|
||||
e.onNext(songs)
|
||||
e.onComplete()
|
||||
}
|
||||
?: getPlaylistSongList(context, playlist.id)
|
||||
}
|
||||
|
||||
fun getPlaylistSongList(context: Context, playlistId: Int): ArrayList<Song> {
|
||||
|
@ -79,7 +52,6 @@ object PlaylistSongsLoader {
|
|||
return songs
|
||||
}
|
||||
|
||||
|
||||
private fun getPlaylistSongFromCursorImpl(cursor: Cursor, playlistId: Int): PlaylistSong {
|
||||
val id = cursor.getInt(0)
|
||||
val title = cursor.getString(1)
|
||||
|
@ -95,28 +67,46 @@ object PlaylistSongsLoader {
|
|||
val idInPlaylist = cursor.getInt(11)
|
||||
val composer = cursor.getString(12)
|
||||
|
||||
return PlaylistSong(id, title, trackNumber, year, duration, data, dateModified, albumId, albumName, artistId, artistName, playlistId, idInPlaylist, composer)
|
||||
return PlaylistSong(
|
||||
id,
|
||||
title,
|
||||
trackNumber,
|
||||
year,
|
||||
duration,
|
||||
data,
|
||||
dateModified,
|
||||
albumId,
|
||||
albumName,
|
||||
artistId,
|
||||
artistName,
|
||||
playlistId,
|
||||
idInPlaylist,
|
||||
composer
|
||||
)
|
||||
}
|
||||
|
||||
private fun makePlaylistSongCursor(context: Context, playlistId: Int): Cursor? {
|
||||
try {
|
||||
return context.contentResolver.query(
|
||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId.toLong()),
|
||||
arrayOf(MediaStore.Audio.Playlists.Members.AUDIO_ID, // 0
|
||||
AudioColumns.TITLE, // 1
|
||||
AudioColumns.TRACK, // 2
|
||||
AudioColumns.YEAR, // 3
|
||||
AudioColumns.DURATION, // 4
|
||||
AudioColumns.DATA, // 5
|
||||
AudioColumns.DATE_MODIFIED, // 6
|
||||
AudioColumns.ALBUM_ID, // 7
|
||||
AudioColumns.ALBUM, // 8
|
||||
AudioColumns.ARTIST_ID, // 9
|
||||
AudioColumns.ARTIST, // 10
|
||||
MediaStore.Audio.Playlists.Members._ID,//11
|
||||
AudioColumns.COMPOSER)// 12
|
||||
, BASE_SELECTION, null,
|
||||
MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER)
|
||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId.toLong()),
|
||||
arrayOf(
|
||||
MediaStore.Audio.Playlists.Members.AUDIO_ID, // 0
|
||||
AudioColumns.TITLE, // 1
|
||||
AudioColumns.TRACK, // 2
|
||||
AudioColumns.YEAR, // 3
|
||||
AudioColumns.DURATION, // 4
|
||||
AudioColumns.DATA, // 5
|
||||
AudioColumns.DATE_MODIFIED, // 6
|
||||
AudioColumns.ALBUM_ID, // 7
|
||||
AudioColumns.ALBUM, // 8
|
||||
AudioColumns.ARTIST_ID, // 9
|
||||
AudioColumns.ARTIST, // 10
|
||||
MediaStore.Audio.Playlists.Members._ID,//11
|
||||
AudioColumns.COMPOSER
|
||||
)// 12
|
||||
, BASE_SELECTION, null,
|
||||
MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER
|
||||
)
|
||||
} catch (e: SecurityException) {
|
||||
return null
|
||||
}
|
||||
|
|
|
@ -20,52 +20,27 @@ import android.provider.MediaStore
|
|||
import android.provider.MediaStore.Audio.AudioColumns
|
||||
import code.name.monkey.retromusic.Constants.BASE_SELECTION
|
||||
import code.name.monkey.retromusic.Constants.baseProjection
|
||||
import code.name.monkey.retromusic.helper.ShuffleHelper
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.providers.BlacklistStore
|
||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||
import io.reactivex.Observable
|
||||
import java.util.*
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
/**
|
||||
* Created by hemanths on 10/08/17.
|
||||
*/
|
||||
|
||||
object SongLoader {
|
||||
fun getAllSongsFlowable(
|
||||
context: Context
|
||||
): Observable<ArrayList<Song>> {
|
||||
val cursor = makeSongCursor(context, null, null)
|
||||
return getSongsFlowable(cursor)
|
||||
}
|
||||
|
||||
|
||||
fun getAllSongs(
|
||||
context: Context
|
||||
context: Context
|
||||
): ArrayList<Song> {
|
||||
val cursor = makeSongCursor(context, null, null)
|
||||
return getSongs(cursor)
|
||||
}
|
||||
|
||||
fun getSongsFlowable(
|
||||
cursor: Cursor?
|
||||
): Observable<ArrayList<Song>> {
|
||||
return Observable.create { e ->
|
||||
val songs = ArrayList<Song>()
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
songs.add(getSongFromCursorImpl(cursor))
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
|
||||
cursor?.close()
|
||||
e.onNext(songs)
|
||||
e.onComplete()
|
||||
}
|
||||
}
|
||||
|
||||
fun getSongs(
|
||||
cursor: Cursor?
|
||||
cursor: Cursor?
|
||||
): ArrayList<Song> {
|
||||
val songs = arrayListOf<Song>()
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
|
@ -78,40 +53,16 @@ object SongLoader {
|
|||
return songs
|
||||
}
|
||||
|
||||
fun getSongsFlowable(
|
||||
context: Context,
|
||||
query: String
|
||||
): Observable<ArrayList<Song>> {
|
||||
val cursor = makeSongCursor(context, AudioColumns.TITLE + " LIKE ?", arrayOf("%$query%"))
|
||||
return getSongsFlowable(cursor)
|
||||
}
|
||||
|
||||
fun getSongs(
|
||||
context: Context,
|
||||
query: String
|
||||
context: Context,
|
||||
query: String
|
||||
): ArrayList<Song> {
|
||||
val cursor = makeSongCursor(context, AudioColumns.TITLE + " LIKE ?", arrayOf("%$query%"))
|
||||
return getSongs(cursor)
|
||||
}
|
||||
|
||||
|
||||
private fun getSongFlowable(
|
||||
cursor: Cursor?
|
||||
): Observable<Song> {
|
||||
return Observable.create { e ->
|
||||
val song: Song = if (cursor != null && cursor.moveToFirst()) {
|
||||
getSongFromCursorImpl(cursor)
|
||||
} else {
|
||||
Song.emptySong
|
||||
}
|
||||
cursor?.close()
|
||||
e.onNext(song)
|
||||
e.onComplete()
|
||||
}
|
||||
}
|
||||
|
||||
fun getSong(
|
||||
cursor: Cursor?
|
||||
cursor: Cursor?
|
||||
): Song {
|
||||
val song: Song
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
|
@ -123,36 +74,13 @@ object SongLoader {
|
|||
return song
|
||||
}
|
||||
|
||||
fun getSongFlowable(
|
||||
context: Context,
|
||||
queryId: Int
|
||||
): Observable<Song> {
|
||||
val cursor = makeSongCursor(context, AudioColumns._ID + "=?",
|
||||
arrayOf(queryId.toString()))
|
||||
return getSongFlowable(cursor)
|
||||
}
|
||||
|
||||
fun getSong(context: Context, queryId: Int): Song {
|
||||
val cursor = makeSongCursor(context, AudioColumns._ID + "=?", arrayOf(queryId.toString()))
|
||||
return getSong(cursor)
|
||||
}
|
||||
|
||||
fun suggestSongs(
|
||||
context: Context
|
||||
): Observable<ArrayList<Song>> {
|
||||
return SongLoader.getAllSongsFlowable(context)
|
||||
.flatMap {
|
||||
val list = ArrayList<Song>()
|
||||
ShuffleHelper.makeShuffleList(it, -1)
|
||||
if (it.size >= 7) {
|
||||
list.addAll(it.subList(0, 7))
|
||||
}
|
||||
return@flatMap Observable.just(list)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSongFromCursorImpl(
|
||||
cursor: Cursor
|
||||
cursor: Cursor
|
||||
): Song {
|
||||
val id = cursor.getInt(0)
|
||||
val title = cursor.getString(1)
|
||||
|
@ -167,17 +95,18 @@ object SongLoader {
|
|||
val artistName = cursor.getString(10)
|
||||
val composer = cursor.getString(11)
|
||||
|
||||
return Song(id, title, trackNumber, year, duration, data, dateModified, albumId,
|
||||
albumName ?: "", artistId, artistName, composer ?: "")
|
||||
return Song(
|
||||
id, title, trackNumber, year, duration, data, dateModified, albumId,
|
||||
albumName ?: "", artistId, artistName, composer ?: ""
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@JvmOverloads
|
||||
fun makeSongCursor(
|
||||
context: Context,
|
||||
selection: String?,
|
||||
selectionValues: Array<String>?,
|
||||
sortOrder: String = PreferenceUtil.getInstance(context).songSortOrder
|
||||
context: Context,
|
||||
selection: String?,
|
||||
selectionValues: Array<String>?,
|
||||
sortOrder: String = PreferenceUtil.getInstance(context).songSortOrder
|
||||
): Cursor? {
|
||||
var selectionFinal = selection
|
||||
var selectionValuesFinal = selectionValues
|
||||
|
@ -195,20 +124,26 @@ object SongLoader {
|
|||
}
|
||||
|
||||
try {
|
||||
return context.contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
baseProjection, selectionFinal + " AND " + MediaStore.Audio.Media.DURATION + ">= " + (PreferenceUtil.getInstance(context).filterLength * 1000), selectionValuesFinal, sortOrder)
|
||||
return context.contentResolver.query(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
baseProjection,
|
||||
selectionFinal + " AND " + MediaStore.Audio.Media.DURATION + ">= " + (PreferenceUtil.getInstance(
|
||||
context
|
||||
).filterLength * 1000),
|
||||
selectionValuesFinal,
|
||||
sortOrder
|
||||
)
|
||||
} catch (e: SecurityException) {
|
||||
return null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun generateBlacklistSelection(
|
||||
selection: String?,
|
||||
pathCount: Int
|
||||
selection: String?,
|
||||
pathCount: Int
|
||||
): String {
|
||||
val newSelection = StringBuilder(
|
||||
if (selection != null && selection.trim { it <= ' ' } != "") "$selection AND " else "")
|
||||
if (selection != null && selection.trim { it <= ' ' } != "") "$selection AND " else "")
|
||||
newSelection.append(AudioColumns.DATA + " NOT LIKE ?")
|
||||
for (i in 0 until pathCount - 1) {
|
||||
newSelection.append(" AND " + AudioColumns.DATA + " NOT LIKE ?")
|
||||
|
@ -217,8 +152,8 @@ object SongLoader {
|
|||
}
|
||||
|
||||
private fun addBlacklistSelectionValues(
|
||||
selectionValues: Array<String>?,
|
||||
paths: ArrayList<String>
|
||||
selectionValues: Array<String>?,
|
||||
paths: ArrayList<String>
|
||||
): Array<String>? {
|
||||
var selectionValuesFinal = selectionValues
|
||||
if (selectionValuesFinal == null) {
|
||||
|
|
|
@ -23,8 +23,7 @@ import code.name.monkey.retromusic.model.Artist
|
|||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.providers.HistoryStore
|
||||
import code.name.monkey.retromusic.providers.SongPlayCountStore
|
||||
import io.reactivex.Observable
|
||||
import java.util.*
|
||||
import java.util.ArrayList
|
||||
|
||||
/**
|
||||
* Created by hemanths on 16/08/17.
|
||||
|
@ -32,18 +31,10 @@ import java.util.*
|
|||
|
||||
object TopAndRecentlyPlayedTracksLoader {
|
||||
|
||||
fun getRecentlyPlayedTracksFlowable(context: Context): Observable<ArrayList<Song>> {
|
||||
return SongLoader.getSongsFlowable(makeRecentTracksCursorAndClearUpDatabase(context))
|
||||
}
|
||||
|
||||
fun getRecentlyPlayedTracks(context: Context): ArrayList<Song> {
|
||||
return SongLoader.getSongs(makeRecentTracksCursorAndClearUpDatabase(context))
|
||||
}
|
||||
|
||||
fun getTopTracksFlowable(context: Context): Observable<ArrayList<Song>> {
|
||||
return SongLoader.getSongsFlowable(makeTopTracksCursorAndClearUpDatabase(context))
|
||||
}
|
||||
|
||||
fun getTopTracks(context: Context): ArrayList<Song> {
|
||||
return SongLoader.getSongs(makeTopTracksCursorAndClearUpDatabase(context))
|
||||
}
|
||||
|
@ -83,8 +74,10 @@ object TopAndRecentlyPlayedTracksLoader {
|
|||
val songs = HistoryStore.getInstance(context).queryRecentIds()
|
||||
|
||||
try {
|
||||
return makeSortedCursor(context, songs,
|
||||
songs!!.getColumnIndex(HistoryStore.RecentStoreColumns.ID))
|
||||
return makeSortedCursor(
|
||||
context, songs,
|
||||
songs!!.getColumnIndex(HistoryStore.RecentStoreColumns.ID)
|
||||
)
|
||||
} finally {
|
||||
songs?.close()
|
||||
}
|
||||
|
@ -93,16 +86,20 @@ object TopAndRecentlyPlayedTracksLoader {
|
|||
private fun makeTopTracksCursorImpl(context: Context): SortedLongCursor? {
|
||||
// first get the top results ids from the internal database
|
||||
val songs = SongPlayCountStore.getInstance(context)
|
||||
.getTopPlayedResults(NUMBER_OF_TOP_TRACKS)
|
||||
.getTopPlayedResults(NUMBER_OF_TOP_TRACKS)
|
||||
|
||||
songs.use { localSongs ->
|
||||
return makeSortedCursor(context, localSongs,
|
||||
localSongs.getColumnIndex(SongPlayCountStore.SongPlayCountColumns.ID))
|
||||
return makeSortedCursor(
|
||||
context, localSongs,
|
||||
localSongs.getColumnIndex(SongPlayCountStore.SongPlayCountColumns.ID)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeSortedCursor(context: Context,
|
||||
cursor: Cursor?, idColumn: Int): SortedLongCursor? {
|
||||
private fun makeSortedCursor(
|
||||
context: Context,
|
||||
cursor: Cursor?, idColumn: Int
|
||||
): SortedLongCursor? {
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
// create the list of ids to select against
|
||||
|
@ -138,37 +135,13 @@ object TopAndRecentlyPlayedTracksLoader {
|
|||
return null
|
||||
}
|
||||
|
||||
fun getTopAlbumsFlowable(
|
||||
context: Context
|
||||
): Observable<ArrayList<Album>> {
|
||||
return Observable.create { e ->
|
||||
getTopTracksFlowable(context).subscribe { songs ->
|
||||
if (songs.size > 0) {
|
||||
e.onNext(AlbumLoader.splitIntoAlbums(songs))
|
||||
}
|
||||
e.onComplete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getTopAlbums(
|
||||
context: Context
|
||||
context: Context
|
||||
): ArrayList<Album> {
|
||||
arrayListOf<Album>()
|
||||
return AlbumLoader.splitIntoAlbums(getTopTracks(context))
|
||||
}
|
||||
|
||||
fun getTopArtistsFlowable(context: Context): Observable<ArrayList<Artist>> {
|
||||
return Observable.create { e ->
|
||||
getTopAlbumsFlowable(context).subscribe { albums ->
|
||||
if (albums.size > 0) {
|
||||
e.onNext(ArtistLoader.splitIntoArtists(albums))
|
||||
}
|
||||
e.onComplete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getTopArtists(context: Context): ArrayList<Artist> {
|
||||
return ArtistLoader.splitIntoArtists(getTopAlbums(context))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue