Removed Rx java

This commit is contained in:
h4h13 2019-12-27 00:53:11 +05:30
parent ccad96dd41
commit 62726de918
12 changed files with 189 additions and 386 deletions

View file

@ -1,165 +1,171 @@
package code.name.monkey.retromusic.adapter.album package code.name.monkey.retromusic.adapter.album
import android.os.Bundle import android.os.Bundle
import android.view.* import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import androidx.fragment.app.* import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.fragments.AlbumCoverStyle import code.name.monkey.retromusic.fragments.AlbumCoverStyle
import code.name.monkey.retromusic.glide.* import code.name.monkey.retromusic.glide.RetroMusicColoredTarget
import code.name.monkey.retromusic.glide.SongGlideRequest
import code.name.monkey.retromusic.misc.CustomFragmentStatePagerAdapter import code.name.monkey.retromusic.misc.CustomFragmentStatePagerAdapter
import code.name.monkey.retromusic.model.Song import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.* import code.name.monkey.retromusic.util.NavigationUtil
import code.name.monkey.retromusic.util.PreferenceUtil
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import java.util.* import java.util.*
class AlbumCoverPagerAdapter( class AlbumCoverPagerAdapter(
fm: FragmentManager, fm: FragmentManager,
private val dataSet: ArrayList<Song> private val dataSet: ArrayList<Song>
) : CustomFragmentStatePagerAdapter(fm) { ) : CustomFragmentStatePagerAdapter(fm) {
private var currentColorReceiver: AlbumCoverFragment.ColorReceiver? = null private var currentColorReceiver: AlbumCoverFragment.ColorReceiver? = null
private var currentColorReceiverPosition = -1 private var currentColorReceiverPosition = -1
override fun getItem(position: Int): Fragment { override fun getItem(position: Int): Fragment {
return AlbumCoverFragment.newInstance(dataSet[position]) return AlbumCoverFragment.newInstance(dataSet[position])
} }
override fun getCount(): Int { override fun getCount(): Int {
return dataSet.size return dataSet.size
} }
override fun instantiateItem(container: ViewGroup, position: Int): Any { override fun instantiateItem(container: ViewGroup, position: Int): Any {
val o = super.instantiateItem(container, position) val o = super.instantiateItem(container, position)
if (currentColorReceiver != null && currentColorReceiverPosition == position) { if (currentColorReceiver != null && currentColorReceiverPosition == position) {
receiveColor(currentColorReceiver!!, currentColorReceiverPosition) receiveColor(currentColorReceiver!!, currentColorReceiverPosition)
} }
return o return o
} }
/** /**
* Only the latest passed [AlbumCoverFragment.ColorReceiver] is guaranteed to receive a * Only the latest passed [AlbumCoverFragment.ColorReceiver] is guaranteed to receive a
* response * response
*/ */
fun receiveColor(colorReceiver: AlbumCoverFragment.ColorReceiver, position: Int) { fun receiveColor(colorReceiver: AlbumCoverFragment.ColorReceiver, position: Int) {
if (getFragment(position) is AlbumCoverFragment) { if (getFragment(position) is AlbumCoverFragment) {
val fragment = getFragment(position) as AlbumCoverFragment val fragment = getFragment(position) as AlbumCoverFragment
currentColorReceiver = null currentColorReceiver = null
currentColorReceiverPosition = -1 currentColorReceiverPosition = -1
fragment.receiveColor(colorReceiver, position) fragment.receiveColor(colorReceiver, position)
} else { } else {
currentColorReceiver = colorReceiver currentColorReceiver = colorReceiver
currentColorReceiverPosition = position currentColorReceiverPosition = position
} }
} }
class AlbumCoverFragment : Fragment() { class AlbumCoverFragment : Fragment() {
lateinit var albumCover: ImageView lateinit var albumCover: ImageView
private var isColorReady: Boolean = false private var isColorReady: Boolean = false
private var color: Int = 0 private var color: Int = 0
private lateinit var song: Song private lateinit var song: Song
private var colorReceiver: ColorReceiver? = null private var colorReceiver: ColorReceiver? = null
private var request: Int = 0 private var request: Int = 0
private val layout: Int private val layout: Int
get() { get() {
return when (PreferenceUtil.getInstance(activity).albumCoverStyle) { return when (PreferenceUtil.getInstance(activity).albumCoverStyle) {
AlbumCoverStyle.NORMAL -> R.layout.fragment_album_cover AlbumCoverStyle.NORMAL -> R.layout.fragment_album_cover
AlbumCoverStyle.FLAT -> R.layout.fragment_album_flat_cover AlbumCoverStyle.FLAT -> R.layout.fragment_album_flat_cover
AlbumCoverStyle.CIRCLE -> R.layout.fragment_album_circle_cover AlbumCoverStyle.CIRCLE -> R.layout.fragment_album_circle_cover
AlbumCoverStyle.CARD -> R.layout.fragment_album_card_cover AlbumCoverStyle.CARD -> R.layout.fragment_album_card_cover
AlbumCoverStyle.MATERIAL -> R.layout.fragment_album_material_cover AlbumCoverStyle.MATERIAL -> R.layout.fragment_album_material_cover
AlbumCoverStyle.FULL -> R.layout.fragment_album_full_cover AlbumCoverStyle.FULL -> R.layout.fragment_album_full_cover
AlbumCoverStyle.FULL_CARD -> R.layout.fragment_album_full_card_cover AlbumCoverStyle.FULL_CARD -> R.layout.fragment_album_full_card_cover
else -> R.layout.fragment_album_cover else -> R.layout.fragment_album_cover
} }
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (arguments != null) { if (arguments != null) {
song = arguments!!.getParcelable(SONG_ARG)!! song = arguments!!.getParcelable(SONG_ARG)!!
} }
} }
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
val finalLayout = when { val finalLayout = when {
PreferenceUtil.getInstance(activity).carouselEffect() -> R.layout.fragment_album_carousel_cover PreferenceUtil.getInstance(activity).carouselEffect() -> R.layout.fragment_album_carousel_cover
else -> layout else -> layout
} }
val view = inflater.inflate(finalLayout, container, false) val view = inflater.inflate(finalLayout, container, false)
albumCover = view.findViewById(R.id.player_image) albumCover = view.findViewById(R.id.player_image)
albumCover.setOnClickListener { albumCover.setOnClickListener {
NavigationUtil.goToLyrics(requireActivity()) NavigationUtil.goToLyrics(requireActivity())
} }
return view return view
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
loadAlbumCover() loadAlbumCover()
} }
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
colorReceiver = null colorReceiver = null
} }
private fun loadAlbumCover() { private fun loadAlbumCover() {
SongGlideRequest.Builder.from(Glide.with(requireContext()), song) SongGlideRequest.Builder.from(Glide.with(requireContext()), song)
.checkIgnoreMediaStore(activity).generatePalette(activity).build() .checkIgnoreMediaStore(requireContext())
.into(object : RetroMusicColoredTarget(albumCover) { .generatePalette(requireContext()).build()
override fun onColorReady(color: Int) { .into(object : RetroMusicColoredTarget(albumCover) {
setColor(color) override fun onColorReady(color: Int) {
} setColor(color)
}) }
} })
}
private fun setColor(color: Int) { private fun setColor(color: Int) {
this.color = color this.color = color
isColorReady = true isColorReady = true
if (colorReceiver != null) { if (colorReceiver != null) {
colorReceiver!!.onColorReady(color, request) colorReceiver!!.onColorReady(color, request)
colorReceiver = null colorReceiver = null
} }
} }
internal fun receiveColor(colorReceiver: ColorReceiver, request: Int) { internal fun receiveColor(colorReceiver: ColorReceiver, request: Int) {
if (isColorReady) { if (isColorReady) {
colorReceiver.onColorReady(color, request) colorReceiver.onColorReady(color, request)
} else { } else {
this.colorReceiver = colorReceiver this.colorReceiver = colorReceiver
this.request = request this.request = request
} }
} }
interface ColorReceiver { interface ColorReceiver {
fun onColorReady(color: Int, request: Int) fun onColorReady(color: Int, request: Int)
} }
companion object { companion object {
private const val SONG_ARG = "song" private const val SONG_ARG = "song"
fun newInstance(song: Song): AlbumCoverFragment { fun newInstance(song: Song): AlbumCoverFragment {
val frag = AlbumCoverFragment() val frag = AlbumCoverFragment()
val args = Bundle() val args = Bundle()
args.putParcelable(SONG_ARG, song) args.putParcelable(SONG_ARG, song)
frag.arguments = args frag.arguments = args
return frag return frag
} }
} }
} }
companion object { companion object {
val TAG: String = AlbumCoverPagerAdapter::class.java.simpleName val TAG: String = AlbumCoverPagerAdapter::class.java.simpleName
} }
} }

View file

@ -19,7 +19,6 @@ import android.provider.MediaStore.Audio.AudioColumns
import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Album
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 io.reactivex.Observable
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -29,31 +28,6 @@ import kotlin.collections.ArrayList
*/ */
object AlbumLoader { object AlbumLoader {
fun getAllAlbumsFlowable(
context: Context
): Observable<ArrayList<Album>> {
val songs = SongLoader.getSongsFlowable(
SongLoader.makeSongCursor(
context, null, null,
getSongLoaderSortOrder(context))
)
return splitIntoAlbumsFlowable(songs)
}
fun getAlbumsFlowable(
context: Context,
query: String
): Observable<ArrayList<Album>> {
val songs = SongLoader.getSongsFlowable(
SongLoader.makeSongCursor(
context,
AudioColumns.ALBUM + " LIKE ?",
arrayOf("%$query%"),
getSongLoaderSortOrder(context))
)
return splitIntoAlbumsFlowable(songs)
}
fun getAlbums( fun getAlbums(
context: Context, context: Context,
@ -68,26 +42,6 @@ object AlbumLoader {
return splitIntoAlbums(songs) return splitIntoAlbums(songs)
} }
fun getAlbumFlowable(
context: Context,
albumId: Int
): Observable<Album> {
return Observable.create { e ->
val songs = SongLoader.getSongsFlowable(
SongLoader.makeSongCursor(
context,
AudioColumns.ALBUM_ID + "=?",
arrayOf(albumId.toString()),
getSongLoaderSortOrder(context)
)
)
songs.subscribe { songs1 ->
e.onNext(Album(songs1))
e.onComplete()
}
}
}
fun getAlbum( fun getAlbum(
context: Context, context: Context,
albumId: Int albumId: Int
@ -103,35 +57,10 @@ object AlbumLoader {
return album return album
} }
fun splitIntoAlbumsFlowable(
songs: Observable<ArrayList<Song>>?
): Observable<ArrayList<Album>> {
return Observable.create { e ->
val albums = ArrayList<Album>()
songs?.subscribe { songs1 ->
for (song in songs1) {
getOrCreateAlbumFlowable(albums, song.albumId).subscribe { album ->
album.songs!!.add(song)
}
}
}
for (album in albums) {
sortSongsByTrackNumber(album)
}
e.onNext(albums)
e.onComplete()
}
}
fun getAllAlbums( fun getAllAlbums(
context: Context context: Context
): ArrayList<Album> { ): ArrayList<Album> {
val songs = SongLoader.getSongs( val songs = SongLoader.getSongs(SongLoader.makeSongCursor(context, null, null, getSongLoaderSortOrder(context)))
SongLoader.makeSongCursor(
context, null, null,
getSongLoaderSortOrder(context))
)
return splitIntoAlbums(songs) return splitIntoAlbums(songs)
} }
@ -150,25 +79,6 @@ object AlbumLoader {
return albums return albums
} }
private fun getOrCreateAlbumFlowable(
albums: ArrayList<Album>,
albumId: Int
): Observable<Album> {
return Observable.create { e ->
for (album in albums) {
if (!album.songs!!.isEmpty() && album.songs[0].albumId == albumId) {
e.onNext(album)
e.onComplete()
return@create
}
}
val album = Album()
albums.add(album)
e.onNext(album)
e.onComplete()
}
}
private fun getOrCreateAlbum( private fun getOrCreateAlbum(
albums: ArrayList<Album>, albums: ArrayList<Album>,
albumId: Int albumId: Int
@ -183,14 +93,11 @@ object AlbumLoader {
return album return album
} }
private fun sortSongsByTrackNumber( private fun sortSongsByTrackNumber(album: Album) {
album: Album album.songs?.sortWith(Comparator { o1, o2 -> o1.trackNumber.compareTo(o2.trackNumber) })
) {
album.songs?.sortWith(Comparator { o1, o2 -> o1.trackNumber - o2.trackNumber })
} }
private fun getSongLoaderSortOrder(context: Context): String { private fun getSongLoaderSortOrder(context: Context): String {
return PreferenceUtil.getInstance(context).albumSortOrder + ", " + return PreferenceUtil.getInstance(context).albumSortOrder + ", " + PreferenceUtil.getInstance(context).albumSongSortOrder
PreferenceUtil.getInstance(context).albumDetailSongSortOrder
} }
} }

View file

@ -19,15 +19,11 @@ import android.provider.MediaStore.Audio.AudioColumns
import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import io.reactivex.Observable
object ArtistLoader { object ArtistLoader {
private fun getSongLoaderSortOrder(context: Context): String { private fun getSongLoaderSortOrder(context: Context): String {
return PreferenceUtil.getInstance(context).artistSortOrder + ", " + return PreferenceUtil.getInstance(context).artistSortOrder + ", " + PreferenceUtil.getInstance(context).artistAlbumSortOrder + ", " + PreferenceUtil.getInstance(context).albumSongSortOrder
PreferenceUtil.getInstance(context).artistAlbumSortOrder + ", " +
PreferenceUtil.getInstance(context).albumDetailSongSortOrder + ", " +
PreferenceUtil.getInstance(context).artistDetailSongSortOrder
} }
fun getAllArtists(context: Context): ArrayList<Artist> { fun getAllArtists(context: Context): ArrayList<Artist> {
@ -70,34 +66,6 @@ object ArtistLoader {
return album return album
} }
fun splitIntoArtists(albums: Observable<ArrayList<Album>>): Observable<ArrayList<Artist>> {
return Observable.create { e ->
val artists = ArrayList<Artist>()
albums.subscribe { localAlbums ->
if (localAlbums != null) {
for (album in localAlbums) {
getOrCreateArtist(artists, album.artistId).albums!!.add(album)
}
}
e.onNext(artists)
e.onComplete()
}
}
}
fun getArtistFlowable(context: Context, artistId: Int): Observable<Artist> {
return Observable.create { e ->
SongLoader.getSongsFlowable(SongLoader.makeSongCursor(context, AudioColumns.ARTIST_ID + "=?",
arrayOf(artistId.toString()),
getSongLoaderSortOrder(context)))
.subscribe { songs ->
val artist = Artist(AlbumLoader.splitIntoAlbums(songs))
e.onNext(artist)
e.onComplete()
}
}
}
fun getArtist(context: Context, artistId: Int): Artist { fun getArtist(context: Context, artistId: Int): Artist {
val songs = SongLoader.getSongs(SongLoader.makeSongCursor( val songs = SongLoader.getSongs(SongLoader.makeSongCursor(
context, context,

View file

@ -21,9 +21,6 @@ import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
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 io.reactivex.Observable
import java.util.*
import kotlin.collections.ArrayList
/** /**
* Created by hemanths on 16/08/17. * Created by hemanths on 16/08/17.
@ -31,11 +28,6 @@ import kotlin.collections.ArrayList
object LastAddedSongsLoader { object LastAddedSongsLoader {
fun getLastAddedSongsFlowable(context: Context): Observable<ArrayList<Song>> {
return SongLoader.getSongsFlowable(makeLastAddedCursor(context))
}
fun getLastAddedSongs(context: Context): ArrayList<Song> { fun getLastAddedSongs(context: Context): ArrayList<Song> {
return SongLoader.getSongs(makeLastAddedCursor(context)) return SongLoader.getSongs(makeLastAddedCursor(context))
} }
@ -50,21 +42,10 @@ object LastAddedSongsLoader {
MediaStore.Audio.Media.DATE_ADDED + " DESC") MediaStore.Audio.Media.DATE_ADDED + " DESC")
} }
fun getLastAddedAlbumsFlowable(context: Context): Observable<ArrayList<Album>> {
return AlbumLoader.splitIntoAlbumsFlowable(getLastAddedSongsFlowable(context))
}
fun getLastAddedAlbums(context: Context): ArrayList<Album> { fun getLastAddedAlbums(context: Context): ArrayList<Album> {
return AlbumLoader.splitIntoAlbums(getLastAddedSongs(context)) return AlbumLoader.splitIntoAlbums(getLastAddedSongs(context))
} }
fun getLastAddedArtistsFlowable(context: Context): Observable<ArrayList<Artist>> {
return ArtistLoader.splitIntoArtists(getLastAddedAlbumsFlowable(context))
}
fun getLastAddedArtists(context: Context): ArrayList<Artist> { fun getLastAddedArtists(context: Context): ArrayList<Artist> {
return ArtistLoader.splitIntoArtists(getLastAddedAlbums(context)) return ArtistLoader.splitIntoArtists(getLastAddedAlbums(context))
} }

View file

@ -23,8 +23,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import io.reactivex.Observable;
/** /**
* @author Karim Abou Zeid (kabouzeid) * @author Karim Abou Zeid (kabouzeid)
*/ */
@ -41,9 +39,6 @@ public abstract class AbsCustomPlaylist extends Playlist {
super(in); super(in);
} }
@NonNull
public abstract Observable<ArrayList<Song>> getSongsFlowable(@NotNull Context context);
@NonNull @NonNull
public abstract ArrayList<Song> getSongs(@NotNull Context context); public abstract ArrayList<Song> getSongs(@NotNull Context context);
} }

View file

@ -27,7 +27,6 @@ import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.loaders.TopAndRecentlyPlayedTracksLoader; import code.name.monkey.retromusic.loaders.TopAndRecentlyPlayedTracksLoader;
import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.providers.HistoryStore; import code.name.monkey.retromusic.providers.HistoryStore;
import io.reactivex.Observable;
/** /**
* @author Karim Abou Zeid (kabouzeid) * @author Karim Abou Zeid (kabouzeid)
@ -52,12 +51,6 @@ public class HistoryPlaylist extends AbsSmartPlaylist {
super(in); super(in);
} }
@NonNull
@Override
public Observable<ArrayList<Song>> getSongsFlowable(@NotNull @NonNull Context context) {
return TopAndRecentlyPlayedTracksLoader.INSTANCE.getRecentlyPlayedTracksFlowable(context);
}
@NonNull @NonNull
@Override @Override
public ArrayList<Song> getSongs(@NotNull @NonNull Context context) { public ArrayList<Song> getSongs(@NotNull @NonNull Context context) {

View file

@ -26,7 +26,6 @@ import java.util.ArrayList;
import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.loaders.LastAddedSongsLoader; import code.name.monkey.retromusic.loaders.LastAddedSongsLoader;
import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.model.Song;
import io.reactivex.Observable;
public class LastAddedPlaylist extends AbsSmartPlaylist { public class LastAddedPlaylist extends AbsSmartPlaylist {
@ -49,12 +48,6 @@ public class LastAddedPlaylist extends AbsSmartPlaylist {
super(in); super(in);
} }
@NonNull
@Override
public Observable<ArrayList<Song>> getSongsFlowable(@NotNull @NonNull Context context) {
return LastAddedSongsLoader.INSTANCE.getLastAddedSongsFlowable(context);
}
@NonNull @NonNull
@Override @Override
public ArrayList<Song> getSongs(@NotNull @NonNull Context context) { public ArrayList<Song> getSongs(@NotNull @NonNull Context context) {

View file

@ -27,7 +27,6 @@ import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.loaders.TopAndRecentlyPlayedTracksLoader; import code.name.monkey.retromusic.loaders.TopAndRecentlyPlayedTracksLoader;
import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.providers.SongPlayCountStore; import code.name.monkey.retromusic.providers.SongPlayCountStore;
import io.reactivex.Observable;
/** /**
* @author Karim Abou Zeid (kabouzeid) * @author Karim Abou Zeid (kabouzeid)
@ -52,11 +51,6 @@ public class MyTopTracksPlaylist extends AbsSmartPlaylist {
super(in); super(in);
} }
@NonNull
@Override
public Observable<ArrayList<Song>> getSongsFlowable(@NotNull @NonNull Context context) {
return TopAndRecentlyPlayedTracksLoader.INSTANCE.getTopTracksFlowable(context);
}
@NonNull @NonNull
@Override @Override

View file

@ -14,13 +14,16 @@
package code.name.monkey.retromusic.mvp.presenter package code.name.monkey.retromusic.mvp.presenter
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.Result.Success
import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.mvp.Presenter import code.name.monkey.retromusic.mvp.Presenter
import code.name.monkey.retromusic.mvp.PresenterImpl import code.name.monkey.retromusic.mvp.PresenterImpl
import code.name.monkey.retromusic.providers.interfaces.Repository import code.name.monkey.retromusic.providers.interfaces.Repository
import io.reactivex.disposables.CompositeDisposable import kotlinx.coroutines.*
import javax.inject.Inject import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
/** /**
@ -43,44 +46,47 @@ interface AlbumDetailsPresenter : Presenter<AlbumDetailsView> {
class AlbumDetailsPresenterImpl @Inject constructor( class AlbumDetailsPresenterImpl @Inject constructor(
private val repository: Repository private val repository: Repository
) : PresenterImpl<AlbumDetailsView>(), AlbumDetailsPresenter { ) : PresenterImpl<AlbumDetailsView>(), AlbumDetailsPresenter, CoroutineScope {
private val job = Job()
private lateinit var album: Album private lateinit var album: Album
private var disposable: CompositeDisposable = CompositeDisposable()
override fun loadMore(artistId: Int) { override fun loadMore(artistId: Int) {
disposable += repository.getArtistByIdFlowable(artistId) launch {
.map { when (val result = repository.artistById(artistId)) {
view?.loadArtistImage(it) is Success -> withContext(Dispatchers.Main) { showArtistImage(result.data) }
return@map it.albums is Result.Error -> withContext(Dispatchers.Main) {}
} }
.map { }
it.filter { filterAlbum -> album.id != filterAlbum.id } }
}
.subscribe({ private fun showArtistImage(artist: Artist) {
if (it.isEmpty()) { view?.loadArtistImage(artist)
return@subscribe
} artist.albums?.filter { it.id != album.id }?.let {
view?.moreAlbums(ArrayList(it)) view?.moreAlbums(ArrayList(it))
}, { t -> println(t) }) }
} }
override fun loadAlbum(albumId: Int) { override fun loadAlbum(albumId: Int) {
disposable += repository.getAlbumFlowable(albumId) launch {
.doOnComplete { when (val result = repository.albumById(albumId)) {
view?.complete() is Success -> withContext(Dispatchers.Main) {
album = result.data
view?.album(result.data)
} }
.subscribe({ is Error -> withContext(Dispatchers.Main) { view?.complete() }
album = it }
view?.album(it) view?.complete()
}, { t -> println(t) }) }
} }
override fun detachView() { override fun detachView() {
super.detachView() super.detachView()
disposable.dispose() job.cancel()
} }
override val coroutineContext: CoroutineContext
get() = Dispatchers.IO + job
} }
} }

View file

@ -25,9 +25,6 @@ import code.name.monkey.retromusic.model.*
import code.name.monkey.retromusic.providers.interfaces.Repository import code.name.monkey.retromusic.providers.interfaces.Repository
import code.name.monkey.retromusic.rest.LastFMRestClient import code.name.monkey.retromusic.rest.LastFMRestClient
import code.name.monkey.retromusic.rest.model.LastFmArtist import code.name.monkey.retromusic.rest.model.LastFmArtist
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.io.IOException import java.io.IOException
class RepositoryImpl(private val context: Context) : Repository { class RepositoryImpl(private val context: Context) : Repository {
@ -45,6 +42,19 @@ class RepositoryImpl(private val context: Context) : Repository {
} }
} }
override suspend fun albumById(albumId: Int): Result<Album> {
return try {
val album = AlbumLoader.getAlbum(context, albumId)
if (album != null) {
Success(album)
} else {
Error(Throwable("No album"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun allArtists(): Result<ArrayList<Artist>> { override suspend fun allArtists(): Result<ArrayList<Artist>> {
return try { return try {
val artists = ArtistLoader.getAllArtists(context) val artists = ArtistLoader.getAllArtists(context)
@ -246,43 +256,6 @@ class RepositoryImpl(private val context: Context) : Repository {
Error(Throwable("Error loading artist")) Error(Throwable("Error loading artist"))
} }
} }
override fun getAlbumFlowable(albumId: Int): Observable<Album> {
return AlbumLoader.getAlbumFlowable(context, albumId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
override fun getArtistByIdFlowable(artistId: Int): Observable<Artist> {
return ArtistLoader.getArtistFlowable(context, artistId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
override fun getPlaylistSongsFlowable(playlist: Playlist): Observable<ArrayList<Song>> {
return PlaylistSongsLoader.getPlaylistSongListFlowable(context, playlist)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
}
override val favoritePlaylistFlowable: Observable<ArrayList<Playlist>>
get() = PlaylistLoader.getFavoritePlaylistFlowable(context)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
override fun getSong(id: Int): Song {
return SongLoader.getSong(context, id)
}
override fun getAlbum(albumId: Int): Album {
return AlbumLoader.getAlbum(context, albumId)
}
override fun getArtistById(artistId: Long): Artist {
return ArtistLoader.getArtist(context, artistId.toInt())
}
} }
suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> = try { suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> = try {

View file

@ -17,7 +17,6 @@ package code.name.monkey.retromusic.providers.interfaces
import code.name.monkey.retromusic.Result import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.model.*
import code.name.monkey.retromusic.rest.model.LastFmArtist import code.name.monkey.retromusic.rest.model.LastFmArtist
import io.reactivex.Observable
/** /**
* Created by hemanths on 11/08/17. * Created by hemanths on 11/08/17.
@ -27,6 +26,8 @@ interface Repository {
suspend fun allAlbums(): Result<ArrayList<Album>> suspend fun allAlbums(): Result<ArrayList<Album>>
suspend fun albumById(albumId: Int): Result<Album>
suspend fun allSongs(): Result<ArrayList<Song>> suspend fun allSongs(): Result<ArrayList<Song>>
suspend fun allArtists(): Result<ArrayList<Artist>> suspend fun allArtists(): Result<ArrayList<Artist>>
@ -54,19 +55,4 @@ interface Repository {
suspend fun artistInfo(name: String, lang: String?, cache: String?): Result<LastFmArtist> suspend fun artistInfo(name: String, lang: String?, cache: String?): Result<LastFmArtist>
suspend fun artistById(artistId: Int): Result<Artist> suspend fun artistById(artistId: Int): Result<Artist>
fun getSong(id: Int): Song
fun getAlbumFlowable(albumId: Int): Observable<Album>
fun getAlbum(albumId: Int): Album
fun getArtistByIdFlowable(artistId: Int): Observable<Artist>
fun getArtistById(artistId: Long): Artist
fun getPlaylistSongsFlowable(playlist: Playlist): Observable<ArrayList<Song>>
val favoritePlaylistFlowable: Observable<ArrayList<Playlist>>
} }

View file

@ -42,6 +42,7 @@ import code.name.monkey.retromusic.model.Album;
import code.name.monkey.retromusic.model.Artist; import code.name.monkey.retromusic.model.Artist;
import code.name.monkey.retromusic.model.Playlist; import code.name.monkey.retromusic.model.Playlist;
import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.model.Song;
import code.name.monkey.retromusic.util.MusicUtil;
import code.name.monkey.retromusic.util.RetroUtil; import code.name.monkey.retromusic.util.RetroUtil;
/** /**
@ -234,7 +235,7 @@ public class WearBrowserService extends MediaBrowserService {
Integer.toString(TYPE_ALBUM_SONGS) + Long.toString(album.getId()), Integer.toString(TYPE_ALBUM_SONGS) + Long.toString(album.getId()),
album.getTitle(), album.getTitle(),
album.getArtistName(), album.getArtistName(),
RetroUtil.getAlbumArtUri(album.getId()), MusicUtil.getMediaStoreAlbumCoverUri(album.getId()),
MediaBrowser.MediaItem.FLAG_BROWSABLE); MediaBrowser.MediaItem.FLAG_BROWSABLE);
} }
break; break;