Removed sealed class

main
Hemanth S 2020-05-26 04:10:40 +05:30
parent 530b42608b
commit 7f9fa54292
21 changed files with 277 additions and 405 deletions

View File

@ -105,7 +105,7 @@
<data android:mimeType="vnd.android.cursor.dir/audio" />
</intent-filter>
</activity>
<activity android:name=".activities.AlbumDetailsActivity" />
<activity android:name=".activities.albums.AlbumDetailsActivity" />
<activity android:name=".activities.ArtistDetailActivity" />
<activity android:name=".activities.PlaylistDetailActivity" />
<activity android:name=".activities.PlayingQueueActivity" />

View File

@ -1,4 +1,4 @@
package code.name.monkey.retromusic.activities
package code.name.monkey.retromusic.activities.albums
import android.app.ActivityOptions
import android.content.Intent
@ -10,13 +10,13 @@ import android.view.SubMenu
import android.view.View
import android.widget.ImageView
import androidx.core.app.ActivityCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.MaterialUtil
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity
import code.name.monkey.retromusic.activities.tageditor.AbsTagEditorActivity
@ -37,7 +37,6 @@ import code.name.monkey.retromusic.helper.SortOrder.AlbumSongSortOrder
import code.name.monkey.retromusic.interfaces.CabHolder
import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.mvp.presenter.AlbumDetailsPresenter
import code.name.monkey.retromusic.mvp.presenter.AlbumDetailsView
import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.util.*
@ -47,7 +46,6 @@ import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_album.*
import kotlinx.android.synthetic.main.activity_album_content.*
import java.util.*
import javax.inject.Inject
import android.util.Pair as UtilPair
class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, CabHolder {
@ -70,6 +68,7 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
return cab as MaterialCab
}
private lateinit var viewModel: AlbumDetailsViewModel
private lateinit var simpleSongAdapter: SimpleSongAdapter
private lateinit var album: Album
private lateinit var artistImage: ImageView
@ -81,9 +80,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
return wrapSlidingMusicPanel(R.layout.activity_album)
}
@Inject
lateinit var albumDetailsPresenter: AlbumDetailsPresenter
private fun windowEnterTransition() {
val slide = Slide()
slide.excludeTarget(R.id.appBarLayout, true)
@ -104,14 +100,21 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
window.sharedElementsUseOverlay = true
windowEnterTransition()
App.musicComponent.inject(this)
albumDetailsPresenter.attachView(this)
val albumId = extraNotNull<Int>(EXTRA_ALBUM_ID).value
albumDetailsPresenter.loadAlbum(albumId)
ActivityCompat.postponeEnterTransition(this)
val viewModelFactory = AlbumDetailsViewModelFactory(application, albumId)
viewModel = ViewModelProvider(this, viewModelFactory).get(AlbumDetailsViewModel::class.java)
viewModel.getAlbum().observe(this, androidx.lifecycle.Observer {
ActivityCompat.startPostponedEnterTransition(this@AlbumDetailsActivity)
album(it)
})
viewModel.getArtist().observe(this, androidx.lifecycle.Observer {
loadArtistImage(it)
})
viewModel.getAlbumInfo().observe(this, androidx.lifecycle.Observer {
aboutAlbum(it)
})
setupRecyclerView()
artistImage = findViewById(R.id.artistImage)
artistImage.setOnClickListener {
val artistPairs = ActivityOptions.makeSceneTransitionAnimation(
@ -149,11 +152,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
}
}
override fun onDestroy() {
super.onDestroy()
albumDetailsPresenter.detachView()
}
override fun complete() {
ActivityCompat.startPostponedEnterTransition(this)
}
@ -191,8 +189,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
}
loadAlbumCover()
simpleSongAdapter.swapDataSet(album.songs)
albumDetailsPresenter.loadMore(album.artistId)
albumDetailsPresenter.aboutAlbum(album.artistName ?: "-", album.title ?: "-")
}
override fun moreAlbums(albums: List<Album>) {
@ -321,7 +317,10 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
albumCoverContainer,
"${getString(R.string.transition_album_art)}_${album.id}"
)
startActivityForResult(intent, TAG_EDITOR_REQUEST, options.toBundle())
startActivityForResult(
intent,
TAG_EDITOR_REQUEST, options.toBundle()
)
return true
}
/*Sort*/
@ -378,15 +377,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
album.songs?.let { simpleSongAdapter.swapDataSet(it) }
}
override fun onMediaStoreChanged() {
super.onMediaStoreChanged()
reload()
}
private fun reload() {
val albumId = extraNotNull<Int>(EXTRA_ALBUM_ID).value
albumDetailsPresenter.loadAlbum(albumId)
}
override fun onBackPressed() {
if (cab != null && cab!!.isActive) {

View File

@ -0,0 +1,53 @@
package code.name.monkey.retromusic.activities.albums
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.providers.RepositoryImpl
import code.name.monkey.retromusic.rest.model.LastFmAlbum
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
class AlbumDetailsViewModel(
application: Application,
private val albumId: Int
) : AndroidViewModel(application) {
private val _repository = RepositoryImpl(application.applicationContext)
private val _album = MutableLiveData<Album>()
private val _artist = MutableLiveData<Artist>()
private val _lastFmAlbum = MutableLiveData<LastFmAlbum>()
fun getAlbum(): LiveData<Album> = _album
fun getArtist(): LiveData<Artist> = _artist
fun getAlbumInfo(): LiveData<LastFmAlbum> = _lastFmAlbum
init {
loadDetails()
}
private fun loadDetails() = viewModelScope.launch {
_album.value = loadAlbumAsync.await() ?: throw NullPointerException("Album couldn't found")
}
private fun loadAlbumInfoAsync(album: Album) = viewModelScope.async(Dispatchers.IO) {
_repository.albumInfo(album.artistName ?: "-", album.title ?: "-")
}
private fun loadArtistAsync(artistId: Int) = viewModelScope.async(Dispatchers.IO) {
_repository.artistById(artistId)
}
private val loadAlbumAsync: Deferred<Album?>
get() = viewModelScope.async(Dispatchers.IO) {
_repository.albumById(albumId).apply {
_artist.value = loadArtistAsync(artistId).await()
_lastFmAlbum.value = loadAlbumInfoAsync(this).await()
}
}
}

View File

@ -0,0 +1,19 @@
package code.name.monkey.retromusic.activities.albums
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
class AlbumDetailsViewModelFactory(
private val application: Application,
private val albumId: Int
) :
ViewModelProvider.AndroidViewModelFactory(application) {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return if (modelClass.isAssignableFrom(AlbumDetailsViewModel::class.java)) {
AlbumDetailsViewModel(application, albumId) as T
} else {
throw IllegalArgumentException("ViewModel Not Found")
}
}
}

View File

@ -14,7 +14,10 @@
package code.name.monkey.retromusic.dagger
import code.name.monkey.retromusic.activities.*
import code.name.monkey.retromusic.activities.ArtistDetailActivity
import code.name.monkey.retromusic.activities.GenreDetailsActivity
import code.name.monkey.retromusic.activities.PlaylistDetailActivity
import code.name.monkey.retromusic.activities.SearchActivity
import code.name.monkey.retromusic.dagger.module.AppModule
import code.name.monkey.retromusic.dagger.module.PresenterModule
import code.name.monkey.retromusic.fragments.albums.AlbumsFragment
@ -50,8 +53,6 @@ interface MusicComponent {
fun inject(artistDetailActivity: ArtistDetailActivity)
fun inject(albumDetailsActivity: AlbumDetailsActivity)
fun inject(playlistDetailActivity: PlaylistDetailActivity)
fun inject(genreDetailsActivity: GenreDetailsActivity)

View File

@ -5,8 +5,6 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import code.name.monkey.retromusic.Result.Error
import code.name.monkey.retromusic.Result.Success
import code.name.monkey.retromusic.fragments.ReloadType.*
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
import code.name.monkey.retromusic.model.*
@ -60,8 +58,8 @@ class LibraryViewModel(application: Application) :
_repository.favoritePlaylist()
)
for (r in result) {
if (r is Success) {
list.add(r.data)
if (r != null) {
list.add(r)
}
}
_homeSections.value = list
@ -69,41 +67,27 @@ class LibraryViewModel(application: Application) :
private val loadSongs: Deferred<List<Song>>
get() = viewModelScope.async(IO) {
when (val result = _repository.allSongs()) {
is Success -> result.data
is Error -> arrayListOf()
}
_repository.allSongs()
}
private val loadAlbums: Deferred<List<Album>>
get() = viewModelScope.async(IO) {
when (val result = _repository.allAlbums()) {
is Success -> result.data
is Error -> arrayListOf()
}
_repository.allAlbums()
}
private val loadArtists: Deferred<List<Artist>>
get() = viewModelScope.async(IO) {
when (val result = _repository.allArtists()) {
is Success -> result.data
is Error -> arrayListOf()
}
_repository.allArtists()
}
private val loadPlaylists: Deferred<List<Playlist>>
get() = viewModelScope.async(IO) {
when (val result = _repository.allPlaylists()) {
is Success -> result.data
is Error -> arrayListOf()
}
_repository.allPlaylists()
}
private val loadGenres: Deferred<List<Genre>>
get() = viewModelScope.async(IO) {
when (val result = _repository.allGenres()) {
is Success -> result.data
is Error -> arrayListOf()
}
_repository.allGenres()
}
fun forceReload(reloadType: ReloadType) = viewModelScope.launch {

View File

@ -307,10 +307,10 @@ public class FoldersFragment extends AbsMainActivityFragment implements
String.format(getString(R.string.not_listed_in_media_store), file1.getName())),
Snackbar.LENGTH_LONG)
.setAction(R.string.action_scan,
v -> new ListPathsAsyncTask(getActivity(), this::scanPaths)
v -> new ListPathsAsyncTask(requireActivity(), this::scanPaths)
.execute(
new ListPathsAsyncTask.LoadingInfo(finalFile, AUDIO_FILE_FILTER)))
.setActionTextColor(ThemeStore.Companion.accentColor(getActivity()))
.setActionTextColor(ThemeStore.Companion.accentColor(requireActivity()))
.show();
}
}).execute(new ListSongsAsyncTask.LoadingInfo(toList(file.getParentFile()), fileFilter,

View File

@ -14,14 +14,16 @@
package code.name.monkey.retromusic.mvp.presenter
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 code.name.monkey.retromusic.rest.model.LastFmAlbum
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -57,19 +59,15 @@ interface AlbumDetailsPresenter : Presenter<AlbumDetailsView> {
override fun loadMore(artistId: Int) {
launch {
when (val result = repository.artistById(artistId)) {
is Success -> withContext(Dispatchers.Main) { showArtistImage(result.data) }
is Error -> withContext(Dispatchers.Main) {}
}
val result = repository.artistById(artistId)
showArtistImage(result)
}
}
override fun aboutAlbum(artist: String, album: String) {
launch {
when (val result = repository.albumInfo(artist, album)) {
is Success -> withContext(Dispatchers.Main) { view.aboutAlbum(result.data) }
is Error -> withContext(Dispatchers.Main) {}
}
val result = repository.albumInfo(artist, album)
view.aboutAlbum(result)
}
}
@ -83,13 +81,10 @@ interface AlbumDetailsPresenter : Presenter<AlbumDetailsView> {
override fun loadAlbum(albumId: Int) {
launch {
when (val result = repository.albumById(albumId)) {
is Success -> withContext(Dispatchers.Main) {
album = result.data
view?.album(result.data)
}
is Error -> withContext(Dispatchers.Main) { view?.complete() }
}
val result = repository.albumById(albumId)
album = result
view?.album(result)
}
}

View File

@ -14,14 +14,15 @@
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.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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -53,10 +54,8 @@ interface AlbumsPresenter : Presenter<AlbumsView> {
override fun loadAlbums() {
launch {
when (val result = repository.allAlbums()) {
is Success -> withContext(Dispatchers.Main) { view?.albums(result.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.allAlbums()
view?.albums(result)
}
}
}

View File

@ -14,15 +14,16 @@
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.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 code.name.monkey.retromusic.rest.model.LastFmArtist
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.util.*
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -60,19 +61,15 @@ interface ArtistDetailsPresenter : Presenter<ArtistDetailsView> {
override fun loadBiography(name: String, lang: String?, cache: String?) {
launch {
when (val result = repository.artistInfo(name, lang, cache)) {
is Success -> withContext(Dispatchers.Main) { view?.artistInfo(result.data) }
is Error -> withContext(Dispatchers.Main) {}
}
val result = repository.artistInfo(name, lang, cache)
view?.artistInfo(result)
}
}
override fun loadArtist(artistId: Int) {
launch {
when (val result = repository.artistById(artistId)) {
is Success -> withContext(Dispatchers.Main) { view?.artist(result.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.artistById(artistId)
view?.artist(result)
}
}

View File

@ -14,14 +14,15 @@
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.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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -49,10 +50,8 @@ interface ArtistsPresenter : Presenter<ArtistsView> {
override fun loadArtists() {
launch {
when (val result = repository.allArtists()) {
is Success -> withContext(Dispatchers.Main) { view?.artists(result.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.allArtists()
view?.artists(result)
}
}
}

View File

@ -14,14 +14,15 @@
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.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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -53,10 +54,8 @@ interface GenreDetailsPresenter : Presenter<GenreDetailsView> {
override fun loadGenreSongs(genreId: Int) {
launch {
when (val result = repository.getGenre(genreId)) {
is Success -> withContext(Dispatchers.Main) { view?.songs(result.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.getGenre(genreId)
view?.songs(result)
}
}
}

View File

@ -14,14 +14,15 @@
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.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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -52,10 +53,8 @@ interface GenresPresenter : Presenter<GenresView> {
override fun loadGenres() {
launch {
when (val result = repository.allGenres()) {
is Success -> withContext(Dispatchers.Main) { view?.genres(result.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.allGenres()
view?.genres(result)
}
}
}

View File

@ -14,7 +14,6 @@
package code.name.monkey.retromusic.mvp.presenter
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
@ -56,9 +55,7 @@ interface HomePresenter : Presenter<HomeView> {
repository.favoritePlaylist()
)
for (r in recentArtistResult) {
when (r) {
is Result.Success -> list.add(r.data)
}
r?.let { list.add(it) }
}
withContext(Dispatchers.Main) {
if (list.isNotEmpty()) view?.sections(list) else view?.showEmptyView()

View File

@ -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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -53,10 +55,8 @@ interface PlaylistsPresenter : Presenter<PlaylistView> {
override fun playlists() {
launch {
when (val result = repository.allPlaylists()) {
is Result.Success -> withContext(Dispatchers.Main) { view?.playlists(result.data) }
is Result.Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.allPlaylists()
view?.playlists(result)
}
}
}

View File

@ -14,15 +14,16 @@
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.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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -48,10 +49,8 @@ interface PlaylistSongsPresenter : Presenter<PlaylistSongsView> {
override fun loadPlaylistSongs(playlist: Playlist) {
launch {
when (val songs = repository.getPlaylistSongs(playlist)) {
is Success -> withContext(Dispatchers.Main) { view?.songs(songs.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val songs = repository.getPlaylistSongs(playlist)
view?.songs(songs)
}
}

View File

@ -14,13 +14,14 @@
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 kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -53,10 +54,8 @@ interface SearchPresenter : Presenter<SearchView> {
override fun search(query: String?) {
launch {
when (val result = repository.search(query)) {
is Success -> withContext(Dispatchers.Main) { view?.showData(result.data) }
is Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val result = repository.search(query)
view?.showData(result)
}
}
}

View File

@ -14,12 +14,14 @@
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 kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext
@ -48,10 +50,8 @@ interface SongPresenter : Presenter<SongView> {
override fun loadSongs() {
launch {
when (val songs = repository.allSongs()) {
is Result.Success -> withContext(Dispatchers.Main) { view?.songs(songs.data) }
is Result.Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
val songs = repository.allSongs()
view?.songs(songs)
}
}

View File

@ -16,9 +16,6 @@ package code.name.monkey.retromusic.providers
import android.content.Context
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.Result.Error
import code.name.monkey.retromusic.Result.Success
import code.name.monkey.retromusic.adapter.HomeAdapter
import code.name.monkey.retromusic.loaders.*
import code.name.monkey.retromusic.model.*
@ -26,156 +23,53 @@ import code.name.monkey.retromusic.providers.interfaces.Repository
import code.name.monkey.retromusic.rest.LastFmClient
import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.rest.model.LastFmArtist
import java.io.IOException
import javax.inject.Inject
class RepositoryImpl @Inject constructor(private val context: Context) : Repository {
override suspend fun allAlbums(): Result<ArrayList<Album>> {
return try {
val albums = AlbumLoader.getAllAlbums(context)
if (albums.isNotEmpty()) {
Success(albums)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun allAlbums(): List<Album> = AlbumLoader.getAllAlbums(context)
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(): List<Artist> = ArtistLoader.getAllArtists(context)
override suspend fun allArtists(): Result<ArrayList<Artist>> {
return try {
val artists = ArtistLoader.getAllArtists(context)
if (artists.isNotEmpty()) {
Success(artists)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun allPlaylists(): List<Playlist> = PlaylistLoader.getAllPlaylists(context)
override suspend fun allPlaylists(): Result<ArrayList<Playlist>> {
return try {
val playlists = PlaylistLoader.getAllPlaylists(context)
if (playlists.isNotEmpty()) {
Success(playlists)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun allGenres(): List<Genre> = GenreLoader.getAllGenres(context)
override suspend fun allGenres(): Result<ArrayList<Genre>> {
return try {
val genres = GenreLoader.getAllGenres(context)
if (genres.isNotEmpty()) {
Success(genres)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun allSongs(): List<Song> = SongLoader.getAllSongs(context)
override suspend fun search(query: String?): Result<MutableList<Any>> {
return try {
val result = SearchLoader.searchAll(context, query)
if (result.isNotEmpty()) {
Success(result)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun albumById(albumId: Int): Album = AlbumLoader.getAlbum(context, albumId)
override suspend fun allSongs(): Result<ArrayList<Song>> {
return try {
val songs = SongLoader.getAllSongs(context)
if (songs.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(songs)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun artistById(artistId: Int): Artist =
ArtistLoader.getArtist(context, artistId)
override suspend fun getPlaylistSongs(playlist: Playlist): Result<ArrayList<Song>> {
return try {
val songs: ArrayList<Song> = if (playlist is AbsCustomPlaylist) {
override suspend fun search(query: String?): MutableList<Any> =
SearchLoader.searchAll(context, query)
override suspend fun getPlaylistSongs(playlist: Playlist): ArrayList<Song> {
return if (playlist is AbsCustomPlaylist) {
playlist.getSongs(context)
} else {
PlaylistSongsLoader.getPlaylistSongList(context, playlist.id)
}
Success(songs)
} catch (e: Exception) {
Error(e)
}
}
override suspend fun getGenre(genreId: Int): Result<ArrayList<Song>> {
return try {
val songs = GenreLoader.getSongs(context, genreId)
if (songs.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(songs)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun getGenre(genreId: Int): ArrayList<Song> =
GenreLoader.getSongs(context, genreId)
override suspend fun recentArtists(): Result<Home> {
return try {
override suspend fun recentArtists(): Home? {
val artists = LastAddedSongsLoader.getLastAddedArtists(context)
if (artists.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
Home(
return if (artists.isNotEmpty()) Home(
0,
R.string.recent_artists,
artists,
HomeAdapter.RECENT_ARTISTS,
R.drawable.ic_artist_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
) else null
}
override suspend fun recentAlbums(): Result<Home> {
return try {
override suspend fun recentAlbums(): Home? {
val albums = LastAddedSongsLoader.getLastAddedAlbums(context)
if (albums.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
return if (albums.isNotEmpty()) {
Home(
1,
R.string.recent_albums,
@ -183,20 +77,12 @@ class RepositoryImpl @Inject constructor(private val context: Context) : Reposit
HomeAdapter.RECENT_ALBUMS,
R.drawable.ic_album_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
} else null
}
override suspend fun topAlbums(): Result<Home> {
return try {
override suspend fun topAlbums(): Home? {
val albums = TopAndRecentlyPlayedTracksLoader.getTopAlbums(context)
if (albums.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
return if (albums.isNotEmpty()) {
Home(
3,
R.string.top_albums,
@ -204,20 +90,13 @@ class RepositoryImpl @Inject constructor(private val context: Context) : Reposit
HomeAdapter.TOP_ALBUMS,
R.drawable.ic_album_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
} else null
}
override suspend fun topArtists(): Result<Home> {
return try {
override suspend fun topArtists(): Home? {
val artists = TopAndRecentlyPlayedTracksLoader.getTopArtists(context)
if (artists.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
return if (artists.isNotEmpty()) {
Home(
2,
R.string.top_artists,
@ -225,20 +104,13 @@ class RepositoryImpl @Inject constructor(private val context: Context) : Reposit
HomeAdapter.TOP_ARTISTS,
R.drawable.ic_artist_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
} else null
}
override suspend fun favoritePlaylist(): Result<Home> {
return try {
override suspend fun favoritePlaylist(): Home? {
val playlists = PlaylistLoader.getFavoritePlaylist(context)
if (playlists.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
return if (playlists.isNotEmpty()) {
Home(
4,
R.string.favorites,
@ -246,48 +118,19 @@ class RepositoryImpl @Inject constructor(private val context: Context) : Reposit
HomeAdapter.PLAYLISTS,
R.drawable.ic_favorite_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
} else null
}
override suspend fun artistInfo(
name: String,
lang: String?,
cache: String?
): Result<LastFmArtist> = safeApiCall(
call = {
Success(LastFmClient.getApiService().artistInfo(name, lang, cache))
},
errorMessage = "Error"
): LastFmArtist = LastFmClient.getApiService().artistInfo(name, lang, cache)
)
override suspend fun albumInfo(
artist: String,
album: String
): Result<LastFmAlbum> = safeApiCall(
call = {
Success(LastFmClient.getApiService().albumInfo(artist, album))
},
errorMessage = "Error"
)
): LastFmAlbum = LastFmClient.getApiService().albumInfo(artist, album)
override suspend fun artistById(artistId: Int): Result<Artist> {
return try {
val artist = ArtistLoader.getArtist(context, artistId)
return Success(artist)
} catch (e: Exception) {
Error(Throwable("Error loading artist"))
}
}
}
suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> =
try {
call.invoke()
} catch (e: Exception) {
Error(IOException(errorMessage, e))
}

View File

@ -14,7 +14,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.LastFmAlbum
import code.name.monkey.retromusic.rest.model.LastFmArtist
@ -25,37 +24,37 @@ import code.name.monkey.retromusic.rest.model.LastFmArtist
interface Repository {
suspend fun allAlbums(): Result<ArrayList<Album>>
suspend fun allAlbums(): List<Album>
suspend fun albumById(albumId: Int): Result<Album>
suspend fun albumById(albumId: Int): Album
suspend fun allSongs(): Result<ArrayList<Song>>
suspend fun allSongs(): List<Song>
suspend fun allArtists(): Result<ArrayList<Artist>>
suspend fun allArtists(): List<Artist>
suspend fun allPlaylists(): Result<ArrayList<Playlist>>
suspend fun allPlaylists(): List<Playlist>
suspend fun allGenres(): Result<ArrayList<Genre>>
suspend fun allGenres(): List<Genre>
suspend fun search(query: String?): Result<MutableList<Any>>
suspend fun search(query: String?): MutableList<Any>
suspend fun getPlaylistSongs(playlist: Playlist): Result<ArrayList<Song>>
suspend fun getPlaylistSongs(playlist: Playlist): ArrayList<Song>
suspend fun getGenre(genreId: Int): Result<ArrayList<Song>>
suspend fun getGenre(genreId: Int): ArrayList<Song>
suspend fun recentArtists(): Result<Home>
suspend fun recentArtists(): Home?
suspend fun topArtists(): Result<Home>
suspend fun topArtists(): Home?
suspend fun topAlbums(): Result<Home>
suspend fun topAlbums(): Home?
suspend fun recentAlbums(): Result<Home>
suspend fun recentAlbums(): Home?
suspend fun favoritePlaylist(): Result<Home>
suspend fun favoritePlaylist(): Home?
suspend fun artistInfo(name: String, lang: String?, cache: String?): Result<LastFmArtist>
suspend fun artistInfo(name: String, lang: String?, cache: String?): LastFmArtist
suspend fun albumInfo(artist: String, album: String): Result<LastFmAlbum>
suspend fun albumInfo(artist: String, album: String): LastFmAlbum
suspend fun artistById(artistId: Int): Result<Artist>
suspend fun artistById(artistId: Int): Artist
}

View File

@ -29,7 +29,7 @@ import org.jetbrains.annotations.NotNull;
import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.activities.AboutActivity;
import code.name.monkey.retromusic.activities.AlbumDetailsActivity;
import code.name.monkey.retromusic.activities.albums.AlbumDetailsActivity;
import code.name.monkey.retromusic.activities.ArtistDetailActivity;
import code.name.monkey.retromusic.activities.DriveModeActivity;
import code.name.monkey.retromusic.activities.GenreDetailsActivity;