From 7f9fa542922842f4238e20e2df58d377309d2519 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Tue, 26 May 2020 04:10:40 +0530 Subject: [PATCH] Removed sealed class --- app/src/main/AndroidManifest.xml | 2 +- .../{ => albums}/AlbumDetailsActivity.kt | 48 +-- .../albums/AlbumDetailsViewModel.kt | 53 +++ .../albums/AlbumDetailsViewModelFactory.kt | 19 + .../retromusic/dagger/MusicComponent.kt | 7 +- .../retromusic/fragments/LibraryViewModel.kt | 32 +- .../mainactivity/FoldersFragment.java | 4 +- .../mvp/presenter/AlbumDetailsPresenter.kt | 29 +- .../mvp/presenter/AlbumPresenter.kt | 13 +- .../mvp/presenter/ArtistDetailsPresenter.kt | 19 +- .../mvp/presenter/ArtistPresenter.kt | 13 +- .../mvp/presenter/GenreDetailsPresenter.kt | 13 +- .../mvp/presenter/GenrePresenter.kt | 13 +- .../retromusic/mvp/presenter/HomePresenter.kt | 5 +- .../mvp/presenter/PlaylistPresenter.kt | 12 +- .../mvp/presenter/PlaylistSongsPresenter.kt | 13 +- .../mvp/presenter/SearchPresenter.kt | 13 +- .../retromusic/mvp/presenter/SongPresenter.kt | 12 +- .../retromusic/providers/RepositoryImpl.kt | 325 +++++------------- .../providers/interfaces/Repository.kt | 35 +- .../retromusic/util/NavigationUtil.java | 2 +- 21 files changed, 277 insertions(+), 405 deletions(-) rename app/src/main/java/code/name/monkey/retromusic/activities/{ => albums}/AlbumDetailsActivity.kt (93%) create mode 100644 app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt create mode 100644 app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModelFactory.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1ff05316..42364983 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -105,7 +105,7 @@ - + diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/AlbumDetailsActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt similarity index 93% rename from app/src/main/java/code/name/monkey/retromusic/activities/AlbumDetailsActivity.kt rename to app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt index 8f2340c1..98b30d23 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/AlbumDetailsActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt @@ -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(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) { @@ -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(EXTRA_ALBUM_ID).value - albumDetailsPresenter.loadAlbum(albumId) - } override fun onBackPressed() { if (cab != null && cab!!.isActive) { diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt new file mode 100644 index 00000000..5aa311e1 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModel.kt @@ -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() + private val _artist = MutableLiveData() + private val _lastFmAlbum = MutableLiveData() + + fun getAlbum(): LiveData = _album + fun getArtist(): LiveData = _artist + fun getAlbumInfo(): LiveData = _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 + get() = viewModelScope.async(Dispatchers.IO) { + _repository.albumById(albumId).apply { + _artist.value = loadArtistAsync(artistId).await() + _lastFmAlbum.value = loadAlbumInfoAsync(this).await() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModelFactory.kt b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModelFactory.kt new file mode 100644 index 00000000..7f5aa847 --- /dev/null +++ b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsViewModelFactory.kt @@ -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 create(modelClass: Class): T { + return if (modelClass.isAssignableFrom(AlbumDetailsViewModel::class.java)) { + AlbumDetailsViewModel(application, albumId) as T + } else { + throw IllegalArgumentException("ViewModel Not Found") + } + } +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt index 86703f4b..7cd5ef8d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dagger/MusicComponent.kt @@ -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) diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt index 754182fa..57604d50 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/LibraryViewModel.kt @@ -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> get() = viewModelScope.async(IO) { - when (val result = _repository.allSongs()) { - is Success -> result.data - is Error -> arrayListOf() - } + _repository.allSongs() } private val loadAlbums: Deferred> get() = viewModelScope.async(IO) { - when (val result = _repository.allAlbums()) { - is Success -> result.data - is Error -> arrayListOf() - } + _repository.allAlbums() } + private val loadArtists: Deferred> get() = viewModelScope.async(IO) { - when (val result = _repository.allArtists()) { - is Success -> result.data - is Error -> arrayListOf() - } + _repository.allArtists() } private val loadPlaylists: Deferred> get() = viewModelScope.async(IO) { - when (val result = _repository.allPlaylists()) { - is Success -> result.data - is Error -> arrayListOf() - } + _repository.allPlaylists() } private val loadGenres: Deferred> get() = viewModelScope.async(IO) { - when (val result = _repository.allGenres()) { - is Success -> result.data - is Error -> arrayListOf() - } + _repository.allGenres() } fun forceReload(reloadType: ReloadType) = viewModelScope.launch { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/FoldersFragment.java b/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/FoldersFragment.java index 679e5d91..5016ba7a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/FoldersFragment.java +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/mainactivity/FoldersFragment.java @@ -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, 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 8b53f370..be7e64e4 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,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 { 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 { 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) + } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumPresenter.kt index 936fd44b..0f97fde0 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/AlbumPresenter.kt @@ -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 { 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) } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistDetailsPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistDetailsPresenter.kt index 649df094..4aa9d1d1 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistDetailsPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistDetailsPresenter.kt @@ -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 { 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) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistPresenter.kt index 42a941da..54a9481b 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/ArtistPresenter.kt @@ -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 { 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) } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenreDetailsPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenreDetailsPresenter.kt index ed664155..3db4517e 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenreDetailsPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenreDetailsPresenter.kt @@ -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 { 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) } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenrePresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenrePresenter.kt index 50f0722c..f1908e43 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenrePresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/GenrePresenter.kt @@ -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 { 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) } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/HomePresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/HomePresenter.kt index 1bdf66e9..8ea312d8 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/HomePresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/HomePresenter.kt @@ -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 { 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() diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistPresenter.kt index dcd8d921..ce1ab456 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistPresenter.kt @@ -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 { 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) } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistSongsPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistSongsPresenter.kt index d02bc52a..494e0d66 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistSongsPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/PlaylistSongsPresenter.kt @@ -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 { 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) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SearchPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SearchPresenter.kt index b371cb7b..3c6441a3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SearchPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SearchPresenter.kt @@ -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 { 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) } } } diff --git a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SongPresenter.kt b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SongPresenter.kt index ceb6b45c..c837ca8d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SongPresenter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/mvp/presenter/SongPresenter.kt @@ -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 { 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) } } 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 e54cd5d0..9f60056f 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 @@ -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,268 +23,114 @@ 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> { - 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 = AlbumLoader.getAllAlbums(context) + + override suspend fun allArtists(): List = ArtistLoader.getAllArtists(context) + + override suspend fun allPlaylists(): List = PlaylistLoader.getAllPlaylists(context) + + override suspend fun allGenres(): List = GenreLoader.getAllGenres(context) + + override suspend fun allSongs(): List = SongLoader.getAllSongs(context) + + override suspend fun albumById(albumId: Int): Album = AlbumLoader.getAlbum(context, albumId) + + override suspend fun artistById(artistId: Int): Artist = + ArtistLoader.getArtist(context, artistId) + + override suspend fun search(query: String?): MutableList = + SearchLoader.searchAll(context, query) + + override suspend fun getPlaylistSongs(playlist: Playlist): ArrayList { + return if (playlist is AbsCustomPlaylist) { + playlist.getSongs(context) + } else { + PlaylistSongsLoader.getPlaylistSongList(context, playlist.id) } } - 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 getGenre(genreId: Int): ArrayList = + GenreLoader.getSongs(context, genreId) + + override suspend fun recentArtists(): Home? { + val artists = LastAddedSongsLoader.getLastAddedArtists(context) + return if (artists.isNotEmpty()) Home( + 0, + R.string.recent_artists, + artists, + HomeAdapter.RECENT_ARTISTS, + R.drawable.ic_artist_white_24dp + ) else null } - override suspend fun allArtists(): Result> { - 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 recentAlbums(): Home? { + val albums = LastAddedSongsLoader.getLastAddedAlbums(context) + return if (albums.isNotEmpty()) { + Home( + 1, + R.string.recent_albums, + albums, + HomeAdapter.RECENT_ALBUMS, + R.drawable.ic_album_white_24dp + ) + } else null } - override suspend fun allPlaylists(): Result> { - 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 topAlbums(): Home? { + val albums = TopAndRecentlyPlayedTracksLoader.getTopAlbums(context) + return if (albums.isNotEmpty()) { + Home( + 3, + R.string.top_albums, + albums, + HomeAdapter.TOP_ALBUMS, + R.drawable.ic_album_white_24dp + ) + } else null } - override suspend fun allGenres(): Result> { - 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 topArtists(): Home? { + + val artists = TopAndRecentlyPlayedTracksLoader.getTopArtists(context) + return if (artists.isNotEmpty()) { + Home( + 2, + R.string.top_artists, + artists, + HomeAdapter.TOP_ARTISTS, + R.drawable.ic_artist_white_24dp + ) + } else null + } - override suspend fun search(query: String?): Result> { - 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 allSongs(): Result> { - 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 getPlaylistSongs(playlist: Playlist): Result> { - return try { - val songs: ArrayList = 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> { - 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 recentArtists(): Result { - return try { - val artists = LastAddedSongsLoader.getLastAddedArtists(context) - if (artists.isEmpty()) { - Error(Throwable("No items found")) - } else { - Success( - Home( - 0, - R.string.recent_artists, - artists, - HomeAdapter.RECENT_ARTISTS, - R.drawable.ic_artist_white_24dp - ) - ) - } - } catch (e: Exception) { - Error(e) - } - } - - override suspend fun recentAlbums(): Result { - return try { - val albums = LastAddedSongsLoader.getLastAddedAlbums(context) - if (albums.isEmpty()) { - Error(Throwable("No items found")) - } else { - Success( - Home( - 1, - R.string.recent_albums, - albums, - HomeAdapter.RECENT_ALBUMS, - R.drawable.ic_album_white_24dp - ) - ) - } - } catch (e: Exception) { - Error(e) - } - } - - override suspend fun topAlbums(): Result { - return try { - val albums = TopAndRecentlyPlayedTracksLoader.getTopAlbums(context) - if (albums.isEmpty()) { - Error(Throwable("No items found")) - } else { - Success( - Home( - 3, - R.string.top_albums, - albums, - HomeAdapter.TOP_ALBUMS, - R.drawable.ic_album_white_24dp - ) - ) - } - } catch (e: Exception) { - Error(e) - } - } - - override suspend fun topArtists(): Result { - return try { - val artists = TopAndRecentlyPlayedTracksLoader.getTopArtists(context) - if (artists.isEmpty()) { - Error(Throwable("No items found")) - } else { - Success( - Home( - 2, - R.string.top_artists, - artists, - HomeAdapter.TOP_ARTISTS, - R.drawable.ic_artist_white_24dp - ) - ) - } - } catch (e: Exception) { - Error(e) - } - } - - override suspend fun favoritePlaylist(): Result { - return try { - val playlists = PlaylistLoader.getFavoritePlaylist(context) - if (playlists.isEmpty()) { - Error(Throwable("No items found")) - } else { - Success( - Home( - 4, - R.string.favorites, - playlists, - HomeAdapter.PLAYLISTS, - R.drawable.ic_favorite_white_24dp - ) - ) - } - } catch (e: Exception) { - Error(e) - } + override suspend fun favoritePlaylist(): Home? { + val playlists = PlaylistLoader.getFavoritePlaylist(context) + return if (playlists.isNotEmpty()) { + Home( + 4, + R.string.favorites, + playlists, + HomeAdapter.PLAYLISTS, + R.drawable.ic_favorite_white_24dp + ) + } else null } override suspend fun artistInfo( name: String, lang: String?, cache: String? - ): Result = 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 = safeApiCall( - call = { - Success(LastFmClient.getApiService().albumInfo(artist, album)) - }, - errorMessage = "Error" - ) + ): LastFmAlbum = LastFmClient.getApiService().albumInfo(artist, album) - override suspend fun artistById(artistId: Int): Result { - return try { - val artist = ArtistLoader.getArtist(context, artistId) - return Success(artist) - } catch (e: Exception) { - Error(Throwable("Error loading artist")) - } - } -} - -suspend fun safeApiCall(call: suspend () -> Result, errorMessage: String): Result = - try { - call.invoke() - } catch (e: Exception) { - Error(IOException(errorMessage, e)) - } \ No newline at end of file +} \ No newline at end of file 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 a3e9f4d8..a3b61341 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 @@ -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> + suspend fun allAlbums(): List - suspend fun albumById(albumId: Int): Result + suspend fun albumById(albumId: Int): Album - suspend fun allSongs(): Result> + suspend fun allSongs(): List - suspend fun allArtists(): Result> + suspend fun allArtists(): List - suspend fun allPlaylists(): Result> + suspend fun allPlaylists(): List - suspend fun allGenres(): Result> + suspend fun allGenres(): List - suspend fun search(query: String?): Result> + suspend fun search(query: String?): MutableList - suspend fun getPlaylistSongs(playlist: Playlist): Result> + suspend fun getPlaylistSongs(playlist: Playlist): ArrayList - suspend fun getGenre(genreId: Int): Result> + suspend fun getGenre(genreId: Int): ArrayList - suspend fun recentArtists(): Result + suspend fun recentArtists(): Home? - suspend fun topArtists(): Result + suspend fun topArtists(): Home? - suspend fun topAlbums(): Result + suspend fun topAlbums(): Home? - suspend fun recentAlbums(): Result + suspend fun recentAlbums(): Home? - suspend fun favoritePlaylist(): Result + suspend fun favoritePlaylist(): Home? - suspend fun artistInfo(name: String, lang: String?, cache: String?): Result + suspend fun artistInfo(name: String, lang: String?, cache: String?): LastFmArtist - suspend fun albumInfo(artist: String, album: String): Result + suspend fun albumInfo(artist: String, album: String): LastFmAlbum - suspend fun artistById(artistId: Int): Result + suspend fun artistById(artistId: Int): Artist } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java b/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java index 416d194f..eb32f35b 100755 --- a/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/NavigationUtil.java @@ -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;