diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumCoverPagerAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumCoverPagerAdapter.kt index 3560ba70..d968a650 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumCoverPagerAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumCoverPagerAdapter.kt @@ -1,165 +1,171 @@ package code.name.monkey.retromusic.adapter.album import android.os.Bundle -import android.view.* +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup 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.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.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 java.util.* class AlbumCoverPagerAdapter( - fm: FragmentManager, - private val dataSet: ArrayList + fm: FragmentManager, + private val dataSet: ArrayList ) : CustomFragmentStatePagerAdapter(fm) { - private var currentColorReceiver: AlbumCoverFragment.ColorReceiver? = null - private var currentColorReceiverPosition = -1 + private var currentColorReceiver: AlbumCoverFragment.ColorReceiver? = null + private var currentColorReceiverPosition = -1 - override fun getItem(position: Int): Fragment { - return AlbumCoverFragment.newInstance(dataSet[position]) - } + override fun getItem(position: Int): Fragment { + return AlbumCoverFragment.newInstance(dataSet[position]) + } - override fun getCount(): Int { - return dataSet.size - } + override fun getCount(): Int { + return dataSet.size + } - override fun instantiateItem(container: ViewGroup, position: Int): Any { - val o = super.instantiateItem(container, position) - if (currentColorReceiver != null && currentColorReceiverPosition == position) { - receiveColor(currentColorReceiver!!, currentColorReceiverPosition) - } - return o - } + override fun instantiateItem(container: ViewGroup, position: Int): Any { + val o = super.instantiateItem(container, position) + if (currentColorReceiver != null && currentColorReceiverPosition == position) { + receiveColor(currentColorReceiver!!, currentColorReceiverPosition) + } + return o + } - /** - * Only the latest passed [AlbumCoverFragment.ColorReceiver] is guaranteed to receive a - * response - */ - fun receiveColor(colorReceiver: AlbumCoverFragment.ColorReceiver, position: Int) { + /** + * Only the latest passed [AlbumCoverFragment.ColorReceiver] is guaranteed to receive a + * response + */ + fun receiveColor(colorReceiver: AlbumCoverFragment.ColorReceiver, position: Int) { - if (getFragment(position) is AlbumCoverFragment) { - val fragment = getFragment(position) as AlbumCoverFragment - currentColorReceiver = null - currentColorReceiverPosition = -1 - fragment.receiveColor(colorReceiver, position) - } else { - currentColorReceiver = colorReceiver - currentColorReceiverPosition = position - } - } + if (getFragment(position) is AlbumCoverFragment) { + val fragment = getFragment(position) as AlbumCoverFragment + currentColorReceiver = null + currentColorReceiverPosition = -1 + fragment.receiveColor(colorReceiver, position) + } else { + currentColorReceiver = colorReceiver + currentColorReceiverPosition = position + } + } - class AlbumCoverFragment : Fragment() { + class AlbumCoverFragment : Fragment() { - lateinit var albumCover: ImageView - private var isColorReady: Boolean = false - private var color: Int = 0 - private lateinit var song: Song - private var colorReceiver: ColorReceiver? = null - private var request: Int = 0 + lateinit var albumCover: ImageView + private var isColorReady: Boolean = false + private var color: Int = 0 + private lateinit var song: Song + private var colorReceiver: ColorReceiver? = null + private var request: Int = 0 - private val layout: Int - get() { - return when (PreferenceUtil.getInstance(activity).albumCoverStyle) { - AlbumCoverStyle.NORMAL -> R.layout.fragment_album_cover - AlbumCoverStyle.FLAT -> R.layout.fragment_album_flat_cover - AlbumCoverStyle.CIRCLE -> R.layout.fragment_album_circle_cover - AlbumCoverStyle.CARD -> R.layout.fragment_album_card_cover - AlbumCoverStyle.MATERIAL -> R.layout.fragment_album_material_cover - AlbumCoverStyle.FULL -> R.layout.fragment_album_full_cover - AlbumCoverStyle.FULL_CARD -> R.layout.fragment_album_full_card_cover - else -> R.layout.fragment_album_cover - } - } + private val layout: Int + get() { + return when (PreferenceUtil.getInstance(activity).albumCoverStyle) { + AlbumCoverStyle.NORMAL -> R.layout.fragment_album_cover + AlbumCoverStyle.FLAT -> R.layout.fragment_album_flat_cover + AlbumCoverStyle.CIRCLE -> R.layout.fragment_album_circle_cover + AlbumCoverStyle.CARD -> R.layout.fragment_album_card_cover + AlbumCoverStyle.MATERIAL -> R.layout.fragment_album_material_cover + AlbumCoverStyle.FULL -> R.layout.fragment_album_full_cover + AlbumCoverStyle.FULL_CARD -> R.layout.fragment_album_full_card_cover + else -> R.layout.fragment_album_cover + } + } - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - if (arguments != null) { - song = arguments!!.getParcelable(SONG_ARG)!! - } - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + if (arguments != null) { + song = arguments!!.getParcelable(SONG_ARG)!! + } + } - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - val finalLayout = when { - PreferenceUtil.getInstance(activity).carouselEffect() -> R.layout.fragment_album_carousel_cover - else -> layout - } - val view = inflater.inflate(finalLayout, container, false) - albumCover = view.findViewById(R.id.player_image) - albumCover.setOnClickListener { + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val finalLayout = when { + PreferenceUtil.getInstance(activity).carouselEffect() -> R.layout.fragment_album_carousel_cover + else -> layout + } + val view = inflater.inflate(finalLayout, container, false) + albumCover = view.findViewById(R.id.player_image) + albumCover.setOnClickListener { - NavigationUtil.goToLyrics(requireActivity()) - } - return view - } + NavigationUtil.goToLyrics(requireActivity()) + } + return view + } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - loadAlbumCover() - } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + loadAlbumCover() + } - override fun onDestroyView() { - super.onDestroyView() - colorReceiver = null - } + override fun onDestroyView() { + super.onDestroyView() + colorReceiver = null + } - private fun loadAlbumCover() { - SongGlideRequest.Builder.from(Glide.with(requireContext()), song) - .checkIgnoreMediaStore(activity).generatePalette(activity).build() - .into(object : RetroMusicColoredTarget(albumCover) { - override fun onColorReady(color: Int) { - setColor(color) - } - }) - } + private fun loadAlbumCover() { + SongGlideRequest.Builder.from(Glide.with(requireContext()), song) + .checkIgnoreMediaStore(requireContext()) + .generatePalette(requireContext()).build() + .into(object : RetroMusicColoredTarget(albumCover) { + override fun onColorReady(color: Int) { + setColor(color) + } + }) + } - private fun setColor(color: Int) { - this.color = color - isColorReady = true - if (colorReceiver != null) { - colorReceiver!!.onColorReady(color, request) - colorReceiver = null - } - } + private fun setColor(color: Int) { + this.color = color + isColorReady = true + if (colorReceiver != null) { + colorReceiver!!.onColorReady(color, request) + colorReceiver = null + } + } - internal fun receiveColor(colorReceiver: ColorReceiver, request: Int) { - if (isColorReady) { - colorReceiver.onColorReady(color, request) - } else { - this.colorReceiver = colorReceiver - this.request = request - } - } + internal fun receiveColor(colorReceiver: ColorReceiver, request: Int) { + if (isColorReady) { + colorReceiver.onColorReady(color, request) + } else { + this.colorReceiver = colorReceiver + this.request = request + } + } - interface ColorReceiver { - fun onColorReady(color: Int, request: Int) - } + interface ColorReceiver { + 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 { - val frag = AlbumCoverFragment() - val args = Bundle() - args.putParcelable(SONG_ARG, song) - frag.arguments = args - return frag - } - } - } + fun newInstance(song: Song): AlbumCoverFragment { + val frag = AlbumCoverFragment() + val args = Bundle() + args.putParcelable(SONG_ARG, song) + frag.arguments = args + return frag + } + } + } - companion object { - val TAG: String = AlbumCoverPagerAdapter::class.java.simpleName - } + companion object { + val TAG: String = AlbumCoverPagerAdapter::class.java.simpleName + } } diff --git a/app/src/main/java/code/name/monkey/retromusic/loaders/AlbumLoader.kt b/app/src/main/java/code/name/monkey/retromusic/loaders/AlbumLoader.kt index 68396d54..2865b620 100644 --- a/app/src/main/java/code/name/monkey/retromusic/loaders/AlbumLoader.kt +++ b/app/src/main/java/code/name/monkey/retromusic/loaders/AlbumLoader.kt @@ -19,7 +19,6 @@ import android.provider.MediaStore.Audio.AudioColumns import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Song import code.name.monkey.retromusic.util.PreferenceUtil -import io.reactivex.Observable import java.util.* import kotlin.collections.ArrayList @@ -29,31 +28,6 @@ import kotlin.collections.ArrayList */ object AlbumLoader { - fun getAllAlbumsFlowable( - context: Context - ): Observable> { - val songs = SongLoader.getSongsFlowable( - SongLoader.makeSongCursor( - context, null, null, - getSongLoaderSortOrder(context)) - ) - - return splitIntoAlbumsFlowable(songs) - } - - fun getAlbumsFlowable( - context: Context, - query: String - ): Observable> { - val songs = SongLoader.getSongsFlowable( - SongLoader.makeSongCursor( - context, - AudioColumns.ALBUM + " LIKE ?", - arrayOf("%$query%"), - getSongLoaderSortOrder(context)) - ) - return splitIntoAlbumsFlowable(songs) - } fun getAlbums( context: Context, @@ -68,26 +42,6 @@ object AlbumLoader { return splitIntoAlbums(songs) } - fun getAlbumFlowable( - context: Context, - albumId: Int - ): Observable { - 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( context: Context, albumId: Int @@ -103,35 +57,10 @@ object AlbumLoader { return album } - fun splitIntoAlbumsFlowable( - songs: Observable>? - ): Observable> { - return Observable.create { e -> - val albums = ArrayList() - 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( context: Context ): ArrayList { - val songs = SongLoader.getSongs( - SongLoader.makeSongCursor( - context, null, null, - getSongLoaderSortOrder(context)) - ) - + val songs = SongLoader.getSongs(SongLoader.makeSongCursor(context, null, null, getSongLoaderSortOrder(context))) return splitIntoAlbums(songs) } @@ -150,25 +79,6 @@ object AlbumLoader { return albums } - private fun getOrCreateAlbumFlowable( - albums: ArrayList, - albumId: Int - ): Observable { - 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( albums: ArrayList, albumId: Int @@ -183,14 +93,11 @@ object AlbumLoader { return album } - private fun sortSongsByTrackNumber( - album: Album - ) { - album.songs?.sortWith(Comparator { o1, o2 -> o1.trackNumber - o2.trackNumber }) + private fun sortSongsByTrackNumber(album: Album) { + album.songs?.sortWith(Comparator { o1, o2 -> o1.trackNumber.compareTo(o2.trackNumber) }) } private fun getSongLoaderSortOrder(context: Context): String { - return PreferenceUtil.getInstance(context).albumSortOrder + ", " + - PreferenceUtil.getInstance(context).albumDetailSongSortOrder + return PreferenceUtil.getInstance(context).albumSortOrder + ", " + PreferenceUtil.getInstance(context).albumSongSortOrder } } diff --git a/app/src/main/java/code/name/monkey/retromusic/loaders/ArtistLoader.kt b/app/src/main/java/code/name/monkey/retromusic/loaders/ArtistLoader.kt index 3eae37f3..e4ce09c8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/loaders/ArtistLoader.kt +++ b/app/src/main/java/code/name/monkey/retromusic/loaders/ArtistLoader.kt @@ -19,15 +19,11 @@ import android.provider.MediaStore.Audio.AudioColumns import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.util.PreferenceUtil -import io.reactivex.Observable object ArtistLoader { private fun getSongLoaderSortOrder(context: Context): String { - return PreferenceUtil.getInstance(context).artistSortOrder + ", " + - PreferenceUtil.getInstance(context).artistAlbumSortOrder + ", " + - PreferenceUtil.getInstance(context).albumDetailSongSortOrder + ", " + - PreferenceUtil.getInstance(context).artistDetailSongSortOrder + return PreferenceUtil.getInstance(context).artistSortOrder + ", " + PreferenceUtil.getInstance(context).artistAlbumSortOrder + ", " + PreferenceUtil.getInstance(context).albumSongSortOrder } fun getAllArtists(context: Context): ArrayList { @@ -70,34 +66,6 @@ object ArtistLoader { return album } - fun splitIntoArtists(albums: Observable>): Observable> { - return Observable.create { e -> - val artists = ArrayList() - 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 { - 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 { val songs = SongLoader.getSongs(SongLoader.makeSongCursor( context, diff --git a/app/src/main/java/code/name/monkey/retromusic/loaders/LastAddedSongsLoader.kt b/app/src/main/java/code/name/monkey/retromusic/loaders/LastAddedSongsLoader.kt index 998ab703..358bd595 100644 --- a/app/src/main/java/code/name/monkey/retromusic/loaders/LastAddedSongsLoader.kt +++ b/app/src/main/java/code/name/monkey/retromusic/loaders/LastAddedSongsLoader.kt @@ -21,9 +21,6 @@ import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Song 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. @@ -31,11 +28,6 @@ import kotlin.collections.ArrayList object LastAddedSongsLoader { - - fun getLastAddedSongsFlowable(context: Context): Observable> { - return SongLoader.getSongsFlowable(makeLastAddedCursor(context)) - } - fun getLastAddedSongs(context: Context): ArrayList { return SongLoader.getSongs(makeLastAddedCursor(context)) } @@ -50,21 +42,10 @@ object LastAddedSongsLoader { MediaStore.Audio.Media.DATE_ADDED + " DESC") } - - fun getLastAddedAlbumsFlowable(context: Context): Observable> { - return AlbumLoader.splitIntoAlbumsFlowable(getLastAddedSongsFlowable(context)) - } - - fun getLastAddedAlbums(context: Context): ArrayList { return AlbumLoader.splitIntoAlbums(getLastAddedSongs(context)) } - - fun getLastAddedArtistsFlowable(context: Context): Observable> { - return ArtistLoader.splitIntoArtists(getLastAddedAlbumsFlowable(context)) - } - fun getLastAddedArtists(context: Context): ArrayList { return ArtistLoader.splitIntoArtists(getLastAddedAlbums(context)) } diff --git a/app/src/main/java/code/name/monkey/retromusic/model/AbsCustomPlaylist.java b/app/src/main/java/code/name/monkey/retromusic/model/AbsCustomPlaylist.java index cf3dc05d..cfe85c8d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/model/AbsCustomPlaylist.java +++ b/app/src/main/java/code/name/monkey/retromusic/model/AbsCustomPlaylist.java @@ -23,8 +23,6 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; -import io.reactivex.Observable; - /** * @author Karim Abou Zeid (kabouzeid) */ @@ -41,9 +39,6 @@ public abstract class AbsCustomPlaylist extends Playlist { super(in); } - @NonNull - public abstract Observable> getSongsFlowable(@NotNull Context context); - @NonNull public abstract ArrayList getSongs(@NotNull Context context); } diff --git a/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/HistoryPlaylist.java b/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/HistoryPlaylist.java index 11fae6a6..df63268d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/HistoryPlaylist.java +++ b/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/HistoryPlaylist.java @@ -27,7 +27,6 @@ import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.loaders.TopAndRecentlyPlayedTracksLoader; import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.providers.HistoryStore; -import io.reactivex.Observable; /** * @author Karim Abou Zeid (kabouzeid) @@ -52,12 +51,6 @@ public class HistoryPlaylist extends AbsSmartPlaylist { super(in); } - @NonNull - @Override - public Observable> getSongsFlowable(@NotNull @NonNull Context context) { - return TopAndRecentlyPlayedTracksLoader.INSTANCE.getRecentlyPlayedTracksFlowable(context); - } - @NonNull @Override public ArrayList getSongs(@NotNull @NonNull Context context) { diff --git a/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/LastAddedPlaylist.java b/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/LastAddedPlaylist.java index dc781a77..5e212b6f 100644 --- a/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/LastAddedPlaylist.java +++ b/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/LastAddedPlaylist.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.loaders.LastAddedSongsLoader; import code.name.monkey.retromusic.model.Song; -import io.reactivex.Observable; public class LastAddedPlaylist extends AbsSmartPlaylist { @@ -49,12 +48,6 @@ public class LastAddedPlaylist extends AbsSmartPlaylist { super(in); } - @NonNull - @Override - public Observable> getSongsFlowable(@NotNull @NonNull Context context) { - return LastAddedSongsLoader.INSTANCE.getLastAddedSongsFlowable(context); - } - @NonNull @Override public ArrayList getSongs(@NotNull @NonNull Context context) { diff --git a/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/MyTopTracksPlaylist.java b/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/MyTopTracksPlaylist.java index d5445937..7e33b806 100644 --- a/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/MyTopTracksPlaylist.java +++ b/app/src/main/java/code/name/monkey/retromusic/model/smartplaylist/MyTopTracksPlaylist.java @@ -27,7 +27,6 @@ import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.loaders.TopAndRecentlyPlayedTracksLoader; import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.providers.SongPlayCountStore; -import io.reactivex.Observable; /** * @author Karim Abou Zeid (kabouzeid) @@ -52,11 +51,6 @@ public class MyTopTracksPlaylist extends AbsSmartPlaylist { super(in); } - @NonNull - @Override - public Observable> getSongsFlowable(@NotNull @NonNull Context context) { - return TopAndRecentlyPlayedTracksLoader.INSTANCE.getTopTracksFlowable(context); - } @NonNull @Override diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumDetailsPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumDetailsPresenter.kt index ed242c56..93bb0f14 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumDetailsPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumDetailsPresenter.kt @@ -14,13 +14,16 @@ 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.Artist import code.name.monkey.retromusic.mvp.Presenter import code.name.monkey.retromusic.mvp.PresenterImpl import code.name.monkey.retromusic.providers.interfaces.Repository -import io.reactivex.disposables.CompositeDisposable +import kotlinx.coroutines.* import javax.inject.Inject +import kotlin.coroutines.CoroutineContext /** @@ -43,44 +46,47 @@ interface AlbumDetailsPresenter : Presenter { class AlbumDetailsPresenterImpl @Inject constructor( private val repository: Repository - ) : PresenterImpl(), AlbumDetailsPresenter { - + ) : PresenterImpl(), AlbumDetailsPresenter, CoroutineScope { + private val job = Job() private lateinit var album: Album - private var disposable: CompositeDisposable = CompositeDisposable() - override fun loadMore(artistId: Int) { - disposable += repository.getArtistByIdFlowable(artistId) - .map { - view?.loadArtistImage(it) - return@map it.albums - } - .map { - it.filter { filterAlbum -> album.id != filterAlbum.id } - } - .subscribe({ - if (it.isEmpty()) { - return@subscribe - } - view?.moreAlbums(ArrayList(it)) - }, { t -> println(t) }) + launch { + when (val result = repository.artistById(artistId)) { + is Success -> withContext(Dispatchers.Main) { showArtistImage(result.data) } + is Result.Error -> withContext(Dispatchers.Main) {} + } + } + } + + private fun showArtistImage(artist: Artist) { + view?.loadArtistImage(artist) + + artist.albums?.filter { it.id != album.id }?.let { + view?.moreAlbums(ArrayList(it)) + } } override fun loadAlbum(albumId: Int) { - disposable += repository.getAlbumFlowable(albumId) - .doOnComplete { - view?.complete() + launch { + when (val result = repository.albumById(albumId)) { + is Success -> withContext(Dispatchers.Main) { + album = result.data + view?.album(result.data) } - .subscribe({ - album = it - view?.album(it) - }, { t -> println(t) }) + is Error -> withContext(Dispatchers.Main) { view?.complete() } + } + view?.complete() + } } override fun detachView() { super.detachView() - disposable.dispose() + job.cancel() } + + override val coroutineContext: CoroutineContext + get() = Dispatchers.IO + job } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt b/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt index 2b3757a4..8da40b9f 100644 --- a/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt +++ b/app/src/main/java/code/name/monkey/retromusic/providers/RepositoryImpl.kt @@ -25,9 +25,6 @@ import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.providers.interfaces.Repository import code.name.monkey.retromusic.rest.LastFMRestClient 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 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 { + 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> { return try { val artists = ArtistLoader.getAllArtists(context) @@ -246,43 +256,6 @@ class RepositoryImpl(private val context: Context) : Repository { Error(Throwable("Error loading artist")) } } - - override fun getAlbumFlowable(albumId: Int): Observable { - return AlbumLoader.getAlbumFlowable(context, albumId) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - } - - override fun getArtistByIdFlowable(artistId: Int): Observable { - return ArtistLoader.getArtistFlowable(context, artistId) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - } - - override fun getPlaylistSongsFlowable(playlist: Playlist): Observable> { - return PlaylistSongsLoader.getPlaylistSongListFlowable(context, playlist) - .subscribeOn(Schedulers.computation()) - .observeOn(AndroidSchedulers.mainThread()) - } - - override val favoritePlaylistFlowable: Observable> - 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 safeApiCall(call: suspend () -> Result, errorMessage: String): Result = try { diff --git a/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt b/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt index d8ce2363..106fe24a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/providers/interfaces/Repository.kt @@ -17,7 +17,6 @@ package code.name.monkey.retromusic.providers.interfaces import code.name.monkey.retromusic.Result import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.rest.model.LastFmArtist -import io.reactivex.Observable /** * Created by hemanths on 11/08/17. @@ -27,6 +26,8 @@ interface Repository { suspend fun allAlbums(): Result> + suspend fun albumById(albumId: Int): Result + suspend fun allSongs(): Result> suspend fun allArtists(): Result> @@ -54,19 +55,4 @@ interface Repository { suspend fun artistInfo(name: String, lang: String?, cache: String?): Result suspend fun artistById(artistId: Int): Result - - fun getSong(id: Int): Song - - fun getAlbumFlowable(albumId: Int): Observable - - fun getAlbum(albumId: Int): Album - - fun getArtistByIdFlowable(artistId: Int): Observable - - fun getArtistById(artistId: Long): Artist - - fun getPlaylistSongsFlowable(playlist: Playlist): Observable> - - val favoritePlaylistFlowable: Observable> - } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/service/WearBrowserService.java b/app/src/main/java/code/name/monkey/retromusic/service/WearBrowserService.java index 56c47640..6f61630c 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/WearBrowserService.java +++ b/app/src/main/java/code/name/monkey/retromusic/service/WearBrowserService.java @@ -42,6 +42,7 @@ import code.name.monkey.retromusic.model.Album; import code.name.monkey.retromusic.model.Artist; import code.name.monkey.retromusic.model.Playlist; import code.name.monkey.retromusic.model.Song; +import code.name.monkey.retromusic.util.MusicUtil; 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()), album.getTitle(), album.getArtistName(), - RetroUtil.getAlbumArtUri(album.getId()), + MusicUtil.getMediaStoreAlbumCoverUri(album.getId()), MediaBrowser.MediaItem.FLAG_BROWSABLE); } break;