Fix search slow, last added slow
This commit is contained in:
parent
27190d5b74
commit
e75246ff46
36 changed files with 732 additions and 501 deletions
|
@ -14,13 +14,17 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp
|
||||
|
||||
import androidx.annotation.CallSuper
|
||||
|
||||
/**
|
||||
* Created by hemanths on 16/08/17.
|
||||
*/
|
||||
|
||||
|
||||
interface Presenter<T> {
|
||||
@CallSuper
|
||||
fun attachView(view: T)
|
||||
|
||||
@CallSuper
|
||||
fun detachView()
|
||||
}
|
||||
|
|
|
@ -14,14 +14,16 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Album
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
||||
/**
|
||||
|
@ -37,22 +39,30 @@ interface AlbumsPresenter : Presenter<AlbumsView> {
|
|||
|
||||
class AlbumsPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<AlbumsView>(), AlbumsPresenter {
|
||||
) : PresenterImpl<AlbumsView>(), AlbumsPresenter, CoroutineScope {
|
||||
private val job = Job()
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
private fun showList(albums: ArrayList<Album>) {
|
||||
view?.albums(albums)
|
||||
}
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
disposable?.dispose()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun loadAlbums() {
|
||||
disposable = repository.allAlbumsFlowable
|
||||
.subscribe({ view?.albums(it) }, { t -> println(t) })
|
||||
launch {
|
||||
when (val result = repository.allAlbums()) {
|
||||
is Result.Success -> {
|
||||
withContext(Dispatchers.Main) {
|
||||
view?.albums(result.data)
|
||||
}
|
||||
}
|
||||
is Result.Error -> {
|
||||
view?.showEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,13 +14,15 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Artist
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
interface ArtistsView : BaseView {
|
||||
fun artists(artists: ArrayList<Artist>)
|
||||
|
@ -32,18 +34,26 @@ interface ArtistsPresenter : Presenter<ArtistsView> {
|
|||
|
||||
class ArtistsPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<ArtistsView>(), ArtistsPresenter {
|
||||
) : PresenterImpl<ArtistsView>(), ArtistsPresenter, CoroutineScope {
|
||||
private val job = Job()
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
disposable?.dispose()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun loadArtists() {
|
||||
disposable = repository.allArtistsFlowable
|
||||
.subscribe({ view?.artists(it) }, { t -> println(t) })
|
||||
launch {
|
||||
when (val result = repository.allArtists()) {
|
||||
is Result.Success -> withContext(Dispatchers.Main) {
|
||||
view?.artists(result.data)
|
||||
}
|
||||
is Result.Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,14 +14,16 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
||||
/**
|
||||
|
@ -37,18 +39,29 @@ interface GenreDetailsPresenter : Presenter<GenreDetailsView> {
|
|||
|
||||
class GenreDetailsPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<GenreDetailsView>(), GenreDetailsPresenter {
|
||||
) : PresenterImpl<GenreDetailsView>(), GenreDetailsPresenter, CoroutineScope {
|
||||
private val job = Job()
|
||||
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
disposable?.dispose()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
|
||||
override fun loadGenreSongs(genreId: Int) {
|
||||
disposable = repository.getGenreFlowable(genreId)
|
||||
.subscribe({ view?.songs(it) }, { t -> println(t) })
|
||||
launch {
|
||||
when (val result = repository.getGenre(genreId)) {
|
||||
is Result.Success -> withContext(Dispatchers.Main) {
|
||||
view?.songs(result.data)
|
||||
}
|
||||
is Result.Error -> withContext(Dispatchers.Main) {
|
||||
view?.showEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,14 +14,16 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Genre
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* @author Hemanth S (h4h13).
|
||||
|
@ -35,13 +37,26 @@ interface GenresPresenter : Presenter<GenresView> {
|
|||
|
||||
class GenresPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<GenresView>(), GenresPresenter {
|
||||
) : PresenterImpl<GenresView>(), GenresPresenter, CoroutineScope {
|
||||
private val job = Job()
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun loadGenres() {
|
||||
disposable = repository.allGenresFlowable
|
||||
.subscribe({ view.genres(it) }, { t -> println(t) })
|
||||
launch {
|
||||
when (val result = repository.allGenres()) {
|
||||
is Result.Success -> withContext(Dispatchers.Main) {
|
||||
view?.genres(result.data)
|
||||
}
|
||||
is Result.Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,12 +14,7 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.R
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter.Companion.PLAYLISTS
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter.Companion.RECENT_ALBUMS
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter.Companion.RECENT_ARTISTS
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter.Companion.TOP_ALBUMS
|
||||
import code.name.monkey.retromusic.adapter.HomeAdapter.Companion.TOP_ARTISTS
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Home
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
import code.name.monkey.retromusic.mvp.Presenter
|
||||
|
@ -27,7 +22,9 @@ import code.name.monkey.retromusic.mvp.PresenterImpl
|
|||
import code.name.monkey.retromusic.providers.interfaces.Repository
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.disposables.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
|
||||
add(disposable)
|
||||
|
@ -40,238 +37,38 @@ interface HomeView : BaseView {
|
|||
interface HomePresenter : Presenter<HomeView> {
|
||||
fun loadSections()
|
||||
|
||||
|
||||
class HomePresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<HomeView>(), HomePresenter {
|
||||
) : PresenterImpl<HomeView>(), HomePresenter, CoroutineScope {
|
||||
private val job = Job()
|
||||
|
||||
private val hashSet: HashSet<Home> = HashSet()
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun loadSections() {
|
||||
loadRecentArtists()
|
||||
loadRecentAlbums()
|
||||
loadTopArtists()
|
||||
loadATopAlbums()
|
||||
loadFavorite()
|
||||
//loadHomeSection()
|
||||
}
|
||||
|
||||
private var disposable: CompositeDisposable = CompositeDisposable()
|
||||
|
||||
private fun showData(sections: ArrayList<Home>) {
|
||||
if (sections.isEmpty()) {
|
||||
view.showEmptyView()
|
||||
} else {
|
||||
view.sections(sections)
|
||||
launch {
|
||||
val list = ArrayList<Home>()
|
||||
val recentArtistResult = listOf(
|
||||
repository.topArtists(),
|
||||
repository.topAlbums(),
|
||||
repository.recentArtists(),
|
||||
repository.recentAlbums(),
|
||||
repository.favoritePlaylist()
|
||||
)
|
||||
for (r in recentArtistResult) {
|
||||
when (r) {
|
||||
is Result.Success -> list.add(r.data)
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
if (list.isNotEmpty()) view?.sections(list) else view?.showEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadRecentArtists() {
|
||||
disposable += repository.recentArtistsFlowable
|
||||
.subscribe {
|
||||
if (it.isNotEmpty()) hashSet.add(
|
||||
Home(0,
|
||||
R.string.recent_artists,
|
||||
R.string.recent_added_artists,
|
||||
it,
|
||||
RECENT_ARTISTS,
|
||||
R.drawable.ic_artist_white_24dp
|
||||
))
|
||||
showData(ArrayList(hashSet))
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadRecentAlbums() {
|
||||
disposable += repository.recentAlbumsFlowable
|
||||
.subscribe {
|
||||
if (it.isNotEmpty()) hashSet.add(
|
||||
Home(1,
|
||||
R.string.recent_albums,
|
||||
R.string.recent_added_albums,
|
||||
it,
|
||||
RECENT_ALBUMS,
|
||||
R.drawable.ic_album_white_24dp
|
||||
))
|
||||
showData(ArrayList(hashSet))
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadTopArtists() {
|
||||
disposable += repository.topArtistsFlowable
|
||||
.subscribe {
|
||||
if (it.isNotEmpty()) hashSet.add(
|
||||
Home(2,
|
||||
R.string.top_artists,
|
||||
R.string.most_played_artists,
|
||||
it,
|
||||
TOP_ARTISTS,
|
||||
R.drawable.ic_artist_white_24dp
|
||||
))
|
||||
showData(ArrayList(hashSet))
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadATopAlbums() {
|
||||
disposable += repository.topAlbumsFlowable
|
||||
.subscribe {
|
||||
if (it.isNotEmpty()) hashSet.add(
|
||||
Home(3,
|
||||
R.string.top_albums,
|
||||
R.string.most_played_albums,
|
||||
it,
|
||||
TOP_ALBUMS,
|
||||
R.drawable.ic_album_white_24dp
|
||||
))
|
||||
showData(ArrayList(hashSet))
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadFavorite() {
|
||||
disposable += repository.favoritePlaylistFlowable
|
||||
.subscribe {
|
||||
if (it.isNotEmpty()) hashSet.add(
|
||||
Home(4,
|
||||
R.string.favorites,
|
||||
R.string.favorites_songs,
|
||||
it,
|
||||
PLAYLISTS,
|
||||
R.drawable.ic_favorite_white_24dp
|
||||
))
|
||||
showData(ArrayList(hashSet))
|
||||
}
|
||||
}
|
||||
|
||||
/*private fun loadHomeSection() {
|
||||
val ob = listOf(repository.recentArtistsFlowable,
|
||||
repository.recentAlbumsFlowable,
|
||||
repository.topArtistsFlowable,
|
||||
repository.topAlbumsFlowable,
|
||||
repository.favoritePlaylistFlowable)
|
||||
|
||||
disposable += Observable.combineLatest(ob) {
|
||||
val hashSet: HashSet<Home> = HashSet()
|
||||
val recentArtist = it[0] as ArrayList<Artist>
|
||||
if (recentArtist.isNotEmpty()) hashSet.add(
|
||||
Home(0,
|
||||
R.string.recent_artists,
|
||||
0,
|
||||
recentArtist,
|
||||
RECENT_ARTISTS,
|
||||
R.drawable.ic_artist_white_24dp
|
||||
))
|
||||
val recentAlbums = it[1] as ArrayList<Album>
|
||||
if (recentAlbums.isNotEmpty()) hashSet.add(
|
||||
Home(1,
|
||||
R.string.recent_albums,
|
||||
0,
|
||||
recentAlbums,
|
||||
RECENT_ALBUMS,
|
||||
R.drawable.ic_album_white_24dp
|
||||
))
|
||||
val topArtists = it[2] as ArrayList<Artist>
|
||||
if (topArtists.isNotEmpty()) hashSet.add(
|
||||
Home(2,
|
||||
R.string.top_artists,
|
||||
0,
|
||||
topArtists,
|
||||
TOP_ARTISTS,
|
||||
R.drawable.ic_artist_white_24dp
|
||||
))
|
||||
val topAlbums = it[3] as ArrayList<Album>
|
||||
if (topAlbums.isNotEmpty()) hashSet.add(
|
||||
Home(3,
|
||||
R.string.top_albums,
|
||||
0,
|
||||
topAlbums,
|
||||
TOP_ALBUMS,
|
||||
R.drawable.ic_album_white_24dp
|
||||
))
|
||||
val playlists = it[4] as ArrayList<Playlist>
|
||||
if (playlists.isNotEmpty()) hashSet.add(
|
||||
Home(4,
|
||||
R.string.favorites,
|
||||
0,
|
||||
playlists,
|
||||
PLAYLISTS,
|
||||
R.drawable.ic_favorite_white_24dp
|
||||
))
|
||||
return@combineLatest hashSet
|
||||
}.subscribe {
|
||||
view.sections(ArrayList(it))
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
/*class HomePresenter(
|
||||
private val view: HomeContract.HomeView,
|
||||
private val repositoryImpl: RepositoryImpl
|
||||
) : Presenter(), HomeContract.HomePresenter {
|
||||
private val hashSet: HashSet<Home> = HashSet()
|
||||
|
||||
override fun homeSections() {
|
||||
loadRecentArtists()
|
||||
loadRecentAlbums()
|
||||
loadTopArtists()
|
||||
loadATopAlbums()
|
||||
loadFavorite()
|
||||
}
|
||||
|
||||
override fun subscribe() {
|
||||
homeSections()
|
||||
}
|
||||
|
||||
override fun unsubscribe() {
|
||||
disposable.dispose()
|
||||
}
|
||||
|
||||
private fun loadRecentArtists() {
|
||||
disposable += repositoryImpl.recentArtistsFlowable
|
||||
.subscribe({
|
||||
if (it.isNotEmpty()) hashSet.add(Home(0, R.string.recent_artists, 0, it, RECENT_ARTISTS, R.drawable.ic_artist_white_24dp))
|
||||
view.showData(ArrayList(hashSet))
|
||||
}, {
|
||||
view.showEmptyView()
|
||||
})
|
||||
}
|
||||
|
||||
private fun loadRecentAlbums() {
|
||||
disposable += repositoryImpl.recentAlbumsFlowable
|
||||
.subscribe({
|
||||
if (it.isNotEmpty()) hashSet.add(Home(1, R.string.recent_albums, 0, it, RECENT_ALBUMS, R.drawable.ic_album_white_24dp))
|
||||
view.showData(ArrayList(hashSet))
|
||||
}, {
|
||||
view.showEmptyView()
|
||||
})
|
||||
}
|
||||
|
||||
private fun loadATopAlbums() {
|
||||
disposable += repositoryImpl.topAlbumsFlowable
|
||||
.subscribe({
|
||||
if (it.isNotEmpty()) hashSet.add(Home(3, R.string.top_albums, 0, it, TOP_ALBUMS, R.drawable.ic_album_white_24dp))
|
||||
view.showData(ArrayList(hashSet))
|
||||
}, {
|
||||
view.showEmptyView()
|
||||
})
|
||||
}
|
||||
|
||||
private fun loadTopArtists() {
|
||||
disposable += repositoryImpl.topArtistsFlowable
|
||||
.subscribe({
|
||||
if (it.isNotEmpty()) hashSet.add(Home(2, R.string.top_artists, 0, it, TOP_ARTISTS, R.drawable.ic_artist_white_24dp))
|
||||
view.showData(ArrayList(hashSet))
|
||||
}, {
|
||||
view.showEmptyView()
|
||||
})
|
||||
}
|
||||
|
||||
private fun loadFavorite() {
|
||||
disposable += repositoryImpl.favoritePlaylistFlowable
|
||||
.subscribe({
|
||||
if (it.isNotEmpty()) hashSet.add(Home(4, R.string.favorites, 0, it, PLAYLISTS, R.drawable.ic_favorite_white_24dp))
|
||||
view.showData(ArrayList(hashSet))
|
||||
}, {
|
||||
view.showEmptyView()
|
||||
})
|
||||
}
|
||||
}*/
|
||||
}
|
|
@ -14,13 +14,15 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Playlist
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
||||
/**
|
||||
|
@ -37,13 +39,29 @@ interface PlaylistsPresenter : Presenter<PlaylistView> {
|
|||
|
||||
class PlaylistsPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<PlaylistView>(), PlaylistsPresenter {
|
||||
) : PresenterImpl<PlaylistView>(), PlaylistsPresenter, CoroutineScope {
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
private val job = Job()
|
||||
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun playlists() {
|
||||
disposable = repository.allPlaylistsFlowable
|
||||
.subscribe({ view?.playlists(it) }, { t -> println(t) })
|
||||
launch {
|
||||
when (val result = repository.allPlaylists()) {
|
||||
is Result.Success -> withContext(Dispatchers.Main) {
|
||||
view?.playlists(result.data)
|
||||
}
|
||||
is Result.Error -> withContext(Dispatchers.Main) {
|
||||
view?.showEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,14 +14,16 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Playlist
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* Created by hemanths on 20/08/17.
|
||||
|
@ -35,18 +37,29 @@ interface PlaylistSongsPresenter : Presenter<PlaylistSongsView> {
|
|||
|
||||
class PlaylistSongsPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<PlaylistSongsView>(), PlaylistSongsPresenter {
|
||||
) : PresenterImpl<PlaylistSongsView>(), PlaylistSongsPresenter, CoroutineScope {
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
private var job: Job = Job()
|
||||
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun loadPlaylistSongs(playlist: Playlist) {
|
||||
disposable = repository.getPlaylistSongsFlowable(playlist)
|
||||
.subscribe({ view?.songs(it) }, { t -> println(t) })
|
||||
launch {
|
||||
when (val songs = repository.getPlaylistSongs(playlist)) {
|
||||
is Result.Success -> withContext(Dispatchers.Main) {
|
||||
view?.songs(songs.data)
|
||||
}
|
||||
is Result.Error -> withContext(Dispatchers.Main) {
|
||||
view?.showEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
disposable?.dispose()
|
||||
job.cancel()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,19 +14,22 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result.Error
|
||||
import code.name.monkey.retromusic.Result.Success
|
||||
import code.name.monkey.retromusic.mvp.BaseView
|
||||
import code.name.monkey.retromusic.mvp.Presenter
|
||||
import code.name.monkey.retromusic.mvp.PresenterImpl
|
||||
import code.name.monkey.retromusic.providers.interfaces.Repository
|
||||
import kotlinx.coroutines.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* Created by hemanths on 20/08/17.
|
||||
*/
|
||||
|
||||
interface SearchView {
|
||||
interface SearchView : BaseView {
|
||||
fun showData(data: MutableList<Any>)
|
||||
|
||||
fun showEmptyView()
|
||||
}
|
||||
|
||||
interface SearchPresenter : Presenter<SearchView> {
|
||||
|
@ -35,18 +38,27 @@ interface SearchPresenter : Presenter<SearchView> {
|
|||
|
||||
class SearchPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<SearchView>(), SearchPresenter {
|
||||
) : PresenterImpl<SearchView>(), SearchPresenter, CoroutineScope {
|
||||
|
||||
override fun attachView(view: SearchView) {
|
||||
super.attachView(view)
|
||||
}
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
private var job: Job = Job()
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
job.cancel()
|
||||
}
|
||||
|
||||
override fun search(query: String?) {
|
||||
view?.showData(repository.search(query))
|
||||
launch {
|
||||
when (val result = repository.search(query)) {
|
||||
is Success -> withContext(Dispatchers.Main) {
|
||||
view?.showData(result.data)
|
||||
}
|
||||
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,13 +14,15 @@
|
|||
|
||||
package code.name.monkey.retromusic.mvp.presenter
|
||||
|
||||
import code.name.monkey.retromusic.Result
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
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.Disposable
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
/**
|
||||
* Created by hemanths on 10/08/17.
|
||||
|
@ -33,21 +35,27 @@ interface SongView {
|
|||
|
||||
interface SongPresenter : Presenter<SongView> {
|
||||
fun loadSongs()
|
||||
|
||||
class SongPresenterImpl @Inject constructor(
|
||||
private val repository: Repository
|
||||
) : PresenterImpl<SongView>(), SongPresenter {
|
||||
) : PresenterImpl<SongView>(), SongPresenter, CoroutineScope {
|
||||
|
||||
private var disposable: Disposable? = null
|
||||
private var job: Job = Job()
|
||||
|
||||
override val coroutineContext: CoroutineContext
|
||||
get() = Dispatchers.IO + job
|
||||
|
||||
override fun loadSongs() {
|
||||
disposable = repository.allSongsFlowable
|
||||
.subscribe({ view?.songs(it) }, { t -> print(t) })
|
||||
launch {
|
||||
when (val songs = repository.allSongs()) {
|
||||
is Result.Success -> withContext(Dispatchers.Main) { view?.songs(songs.data) }
|
||||
is Result.Error -> view?.showEmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun detachView() {
|
||||
super.detachView()
|
||||
disposable?.dispose()
|
||||
job.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue