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" /> <data android:mimeType="vnd.android.cursor.dir/audio" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".activities.AlbumDetailsActivity" /> <activity android:name=".activities.albums.AlbumDetailsActivity" />
<activity android:name=".activities.ArtistDetailActivity" /> <activity android:name=".activities.ArtistDetailActivity" />
<activity android:name=".activities.PlaylistDetailActivity" /> <activity android:name=".activities.PlaylistDetailActivity" />
<activity android:name=".activities.PlayingQueueActivity" /> <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.app.ActivityOptions
import android.content.Intent import android.content.Intent
@ -10,13 +10,13 @@ import android.view.SubMenu
import android.view.View import android.view.View
import android.widget.ImageView import android.widget.ImageView
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import code.name.monkey.appthemehelper.util.ATHUtil import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.MaterialUtil import code.name.monkey.appthemehelper.util.MaterialUtil
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity
import code.name.monkey.retromusic.activities.tageditor.AbsTagEditorActivity 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.interfaces.CabHolder
import code.name.monkey.retromusic.model.Album import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.mvp.presenter.AlbumDetailsPresenter
import code.name.monkey.retromusic.mvp.presenter.AlbumDetailsView import code.name.monkey.retromusic.mvp.presenter.AlbumDetailsView
import code.name.monkey.retromusic.rest.model.LastFmAlbum import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.util.* 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.*
import kotlinx.android.synthetic.main.activity_album_content.* import kotlinx.android.synthetic.main.activity_album_content.*
import java.util.* import java.util.*
import javax.inject.Inject
import android.util.Pair as UtilPair import android.util.Pair as UtilPair
class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, CabHolder { class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, CabHolder {
@ -70,6 +68,7 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
return cab as MaterialCab return cab as MaterialCab
} }
private lateinit var viewModel: AlbumDetailsViewModel
private lateinit var simpleSongAdapter: SimpleSongAdapter private lateinit var simpleSongAdapter: SimpleSongAdapter
private lateinit var album: Album private lateinit var album: Album
private lateinit var artistImage: ImageView private lateinit var artistImage: ImageView
@ -81,9 +80,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
return wrapSlidingMusicPanel(R.layout.activity_album) return wrapSlidingMusicPanel(R.layout.activity_album)
} }
@Inject
lateinit var albumDetailsPresenter: AlbumDetailsPresenter
private fun windowEnterTransition() { private fun windowEnterTransition() {
val slide = Slide() val slide = Slide()
slide.excludeTarget(R.id.appBarLayout, true) slide.excludeTarget(R.id.appBarLayout, true)
@ -104,14 +100,21 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
window.sharedElementsUseOverlay = true window.sharedElementsUseOverlay = true
windowEnterTransition() windowEnterTransition()
App.musicComponent.inject(this)
albumDetailsPresenter.attachView(this)
val albumId = extraNotNull<Int>(EXTRA_ALBUM_ID).value val albumId = extraNotNull<Int>(EXTRA_ALBUM_ID).value
albumDetailsPresenter.loadAlbum(albumId)
ActivityCompat.postponeEnterTransition(this) 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() setupRecyclerView()
artistImage = findViewById(R.id.artistImage) artistImage = findViewById(R.id.artistImage)
artistImage.setOnClickListener { artistImage.setOnClickListener {
val artistPairs = ActivityOptions.makeSceneTransitionAnimation( val artistPairs = ActivityOptions.makeSceneTransitionAnimation(
@ -149,11 +152,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
} }
} }
override fun onDestroy() {
super.onDestroy()
albumDetailsPresenter.detachView()
}
override fun complete() { override fun complete() {
ActivityCompat.startPostponedEnterTransition(this) ActivityCompat.startPostponedEnterTransition(this)
} }
@ -191,8 +189,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
} }
loadAlbumCover() loadAlbumCover()
simpleSongAdapter.swapDataSet(album.songs) simpleSongAdapter.swapDataSet(album.songs)
albumDetailsPresenter.loadMore(album.artistId)
albumDetailsPresenter.aboutAlbum(album.artistName ?: "-", album.title ?: "-")
} }
override fun moreAlbums(albums: List<Album>) { override fun moreAlbums(albums: List<Album>) {
@ -321,7 +317,10 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
albumCoverContainer, albumCoverContainer,
"${getString(R.string.transition_album_art)}_${album.id}" "${getString(R.string.transition_album_art)}_${album.id}"
) )
startActivityForResult(intent, TAG_EDITOR_REQUEST, options.toBundle()) startActivityForResult(
intent,
TAG_EDITOR_REQUEST, options.toBundle()
)
return true return true
} }
/*Sort*/ /*Sort*/
@ -378,15 +377,6 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
album.songs?.let { simpleSongAdapter.swapDataSet(it) } 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() { override fun onBackPressed() {
if (cab != null && cab!!.isActive) { 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 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.AppModule
import code.name.monkey.retromusic.dagger.module.PresenterModule import code.name.monkey.retromusic.dagger.module.PresenterModule
import code.name.monkey.retromusic.fragments.albums.AlbumsFragment import code.name.monkey.retromusic.fragments.albums.AlbumsFragment
@ -50,8 +53,6 @@ interface MusicComponent {
fun inject(artistDetailActivity: ArtistDetailActivity) fun inject(artistDetailActivity: ArtistDetailActivity)
fun inject(albumDetailsActivity: AlbumDetailsActivity)
fun inject(playlistDetailActivity: PlaylistDetailActivity) fun inject(playlistDetailActivity: PlaylistDetailActivity)
fun inject(genreDetailsActivity: GenreDetailsActivity) fun inject(genreDetailsActivity: GenreDetailsActivity)

View File

@ -5,8 +5,6 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope 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.fragments.ReloadType.*
import code.name.monkey.retromusic.interfaces.MusicServiceEventListener import code.name.monkey.retromusic.interfaces.MusicServiceEventListener
import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.model.*
@ -60,8 +58,8 @@ class LibraryViewModel(application: Application) :
_repository.favoritePlaylist() _repository.favoritePlaylist()
) )
for (r in result) { for (r in result) {
if (r is Success) { if (r != null) {
list.add(r.data) list.add(r)
} }
} }
_homeSections.value = list _homeSections.value = list
@ -69,41 +67,27 @@ class LibraryViewModel(application: Application) :
private val loadSongs: Deferred<List<Song>> private val loadSongs: Deferred<List<Song>>
get() = viewModelScope.async(IO) { get() = viewModelScope.async(IO) {
when (val result = _repository.allSongs()) { _repository.allSongs()
is Success -> result.data
is Error -> arrayListOf()
}
} }
private val loadAlbums: Deferred<List<Album>> private val loadAlbums: Deferred<List<Album>>
get() = viewModelScope.async(IO) { get() = viewModelScope.async(IO) {
when (val result = _repository.allAlbums()) { _repository.allAlbums()
is Success -> result.data
is Error -> arrayListOf()
}
} }
private val loadArtists: Deferred<List<Artist>> private val loadArtists: Deferred<List<Artist>>
get() = viewModelScope.async(IO) { get() = viewModelScope.async(IO) {
when (val result = _repository.allArtists()) { _repository.allArtists()
is Success -> result.data
is Error -> arrayListOf()
}
} }
private val loadPlaylists: Deferred<List<Playlist>> private val loadPlaylists: Deferred<List<Playlist>>
get() = viewModelScope.async(IO) { get() = viewModelScope.async(IO) {
when (val result = _repository.allPlaylists()) { _repository.allPlaylists()
is Success -> result.data
is Error -> arrayListOf()
}
} }
private val loadGenres: Deferred<List<Genre>> private val loadGenres: Deferred<List<Genre>>
get() = viewModelScope.async(IO) { get() = viewModelScope.async(IO) {
when (val result = _repository.allGenres()) { _repository.allGenres()
is Success -> result.data
is Error -> arrayListOf()
}
} }
fun forceReload(reloadType: ReloadType) = viewModelScope.launch { 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())), String.format(getString(R.string.not_listed_in_media_store), file1.getName())),
Snackbar.LENGTH_LONG) Snackbar.LENGTH_LONG)
.setAction(R.string.action_scan, .setAction(R.string.action_scan,
v -> new ListPathsAsyncTask(getActivity(), this::scanPaths) v -> new ListPathsAsyncTask(requireActivity(), this::scanPaths)
.execute( .execute(
new ListPathsAsyncTask.LoadingInfo(finalFile, AUDIO_FILE_FILTER))) new ListPathsAsyncTask.LoadingInfo(finalFile, AUDIO_FILE_FILTER)))
.setActionTextColor(ThemeStore.Companion.accentColor(getActivity())) .setActionTextColor(ThemeStore.Companion.accentColor(requireActivity()))
.show(); .show();
} }
}).execute(new ListSongsAsyncTask.LoadingInfo(toList(file.getParentFile()), fileFilter, }).execute(new ListSongsAsyncTask.LoadingInfo(toList(file.getParentFile()), fileFilter,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,13 +14,15 @@
package code.name.monkey.retromusic.mvp.presenter package code.name.monkey.retromusic.mvp.presenter
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.model.Playlist import code.name.monkey.retromusic.model.Playlist
import code.name.monkey.retromusic.mvp.BaseView import code.name.monkey.retromusic.mvp.BaseView
import code.name.monkey.retromusic.mvp.Presenter import code.name.monkey.retromusic.mvp.Presenter
import code.name.monkey.retromusic.mvp.PresenterImpl import code.name.monkey.retromusic.mvp.PresenterImpl
import code.name.monkey.retromusic.providers.interfaces.Repository import code.name.monkey.retromusic.providers.interfaces.Repository
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -53,10 +55,8 @@ interface PlaylistsPresenter : Presenter<PlaylistView> {
override fun playlists() { override fun playlists() {
launch { launch {
when (val result = repository.allPlaylists()) { val result = repository.allPlaylists()
is Result.Success -> withContext(Dispatchers.Main) { view?.playlists(result.data) } view?.playlists(result)
is Result.Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
} }
} }
} }

View File

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

View File

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

View File

@ -14,12 +14,14 @@
package code.name.monkey.retromusic.mvp.presenter package code.name.monkey.retromusic.mvp.presenter
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.model.Song import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.mvp.Presenter import code.name.monkey.retromusic.mvp.Presenter
import code.name.monkey.retromusic.mvp.PresenterImpl import code.name.monkey.retromusic.mvp.PresenterImpl
import code.name.monkey.retromusic.providers.interfaces.Repository import code.name.monkey.retromusic.providers.interfaces.Repository
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -48,10 +50,8 @@ interface SongPresenter : Presenter<SongView> {
override fun loadSongs() { override fun loadSongs() {
launch { launch {
when (val songs = repository.allSongs()) { val songs = repository.allSongs()
is Result.Success -> withContext(Dispatchers.Main) { view?.songs(songs.data) } view?.songs(songs)
is Result.Error -> withContext(Dispatchers.Main) { view?.showEmptyView() }
}
} }
} }

View File

@ -16,9 +16,6 @@ package code.name.monkey.retromusic.providers
import android.content.Context import android.content.Context
import code.name.monkey.retromusic.R 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.adapter.HomeAdapter
import code.name.monkey.retromusic.loaders.* import code.name.monkey.retromusic.loaders.*
import code.name.monkey.retromusic.model.* 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.LastFmClient
import code.name.monkey.retromusic.rest.model.LastFmAlbum import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.rest.model.LastFmArtist import code.name.monkey.retromusic.rest.model.LastFmArtist
import java.io.IOException
import javax.inject.Inject import javax.inject.Inject
class RepositoryImpl @Inject constructor(private val context: Context) : Repository { class RepositoryImpl @Inject constructor(private val context: Context) : Repository {
override suspend fun allAlbums(): Result<ArrayList<Album>> { override suspend fun allAlbums(): List<Album> = AlbumLoader.getAllAlbums(context)
return try {
val albums = AlbumLoader.getAllAlbums(context) override suspend fun allArtists(): List<Artist> = ArtistLoader.getAllArtists(context)
if (albums.isNotEmpty()) {
Success(albums) override suspend fun allPlaylists(): List<Playlist> = PlaylistLoader.getAllPlaylists(context)
} else {
Error(Throwable("No items found")) override suspend fun allGenres(): List<Genre> = GenreLoader.getAllGenres(context)
}
} catch (e: Exception) { override suspend fun allSongs(): List<Song> = SongLoader.getAllSongs(context)
Error(e)
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<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)
} }
} }
override suspend fun albumById(albumId: Int): Result<Album> { override suspend fun getGenre(genreId: Int): ArrayList<Song> =
return try { GenreLoader.getSongs(context, genreId)
val album = AlbumLoader.getAlbum(context, albumId)
if (album != null) { override suspend fun recentArtists(): Home? {
Success(album) val artists = LastAddedSongsLoader.getLastAddedArtists(context)
} else { return if (artists.isNotEmpty()) Home(
Error(Throwable("No album")) 0,
} R.string.recent_artists,
} catch (e: Exception) { artists,
Error(e) HomeAdapter.RECENT_ARTISTS,
} R.drawable.ic_artist_white_24dp
) else null
} }
override suspend fun allArtists(): Result<ArrayList<Artist>> { override suspend fun recentAlbums(): Home? {
return try { val albums = LastAddedSongsLoader.getLastAddedAlbums(context)
val artists = ArtistLoader.getAllArtists(context) return if (albums.isNotEmpty()) {
if (artists.isNotEmpty()) { Home(
Success(artists) 1,
} else { R.string.recent_albums,
Error(Throwable("No items found")) albums,
} HomeAdapter.RECENT_ALBUMS,
} catch (e: Exception) { R.drawable.ic_album_white_24dp
Error(e) )
} } else null
} }
override suspend fun allPlaylists(): Result<ArrayList<Playlist>> { override suspend fun topAlbums(): Home? {
return try { val albums = TopAndRecentlyPlayedTracksLoader.getTopAlbums(context)
val playlists = PlaylistLoader.getAllPlaylists(context) return if (albums.isNotEmpty()) {
if (playlists.isNotEmpty()) { Home(
Success(playlists) 3,
} else { R.string.top_albums,
Error(Throwable("No items found")) albums,
} HomeAdapter.TOP_ALBUMS,
} catch (e: Exception) { R.drawable.ic_album_white_24dp
Error(e) )
} } else null
} }
override suspend fun allGenres(): Result<ArrayList<Genre>> { override suspend fun topArtists(): Home? {
return try {
val genres = GenreLoader.getAllGenres(context) val artists = TopAndRecentlyPlayedTracksLoader.getTopArtists(context)
if (genres.isNotEmpty()) { return if (artists.isNotEmpty()) {
Success(genres) Home(
} else { 2,
Error(Throwable("No items found")) R.string.top_artists,
} artists,
} catch (e: Exception) { HomeAdapter.TOP_ARTISTS,
Error(e) R.drawable.ic_artist_white_24dp
} )
} else null
} }
override suspend fun search(query: String?): Result<MutableList<Any>> { override suspend fun favoritePlaylist(): Home? {
return try { val playlists = PlaylistLoader.getFavoritePlaylist(context)
val result = SearchLoader.searchAll(context, query) return if (playlists.isNotEmpty()) {
if (result.isNotEmpty()) { Home(
Success(result) 4,
} else { R.string.favorites,
Error(Throwable("No items found")) playlists,
} HomeAdapter.PLAYLISTS,
} catch (e: Exception) { R.drawable.ic_favorite_white_24dp
Error(e) )
} } else null
}
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 getPlaylistSongs(playlist: Playlist): Result<ArrayList<Song>> {
return try {
val songs: ArrayList<Song> = 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 recentArtists(): Result<Home> {
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<Home> {
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<Home> {
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<Home> {
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<Home> {
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 artistInfo( override suspend fun artistInfo(
name: String, name: String,
lang: String?, lang: String?,
cache: String? cache: String?
): Result<LastFmArtist> = safeApiCall( ): LastFmArtist = LastFmClient.getApiService().artistInfo(name, lang, cache)
call = {
Success(LastFmClient.getApiService().artistInfo(name, lang, cache))
},
errorMessage = "Error"
)
override suspend fun albumInfo( override suspend fun albumInfo(
artist: String, artist: String,
album: String album: String
): Result<LastFmAlbum> = safeApiCall( ): LastFmAlbum = LastFmClient.getApiService().albumInfo(artist, album)
call = {
Success(LastFmClient.getApiService().albumInfo(artist, album))
},
errorMessage = "Error"
)
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 package code.name.monkey.retromusic.providers.interfaces
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.model.*
import code.name.monkey.retromusic.rest.model.LastFmAlbum import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.rest.model.LastFmArtist import code.name.monkey.retromusic.rest.model.LastFmArtist
@ -25,37 +24,37 @@ import code.name.monkey.retromusic.rest.model.LastFmArtist
interface Repository { 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.R;
import code.name.monkey.retromusic.activities.AboutActivity; 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.ArtistDetailActivity;
import code.name.monkey.retromusic.activities.DriveModeActivity; import code.name.monkey.retromusic.activities.DriveModeActivity;
import code.name.monkey.retromusic.activities.GenreDetailsActivity; import code.name.monkey.retromusic.activities.GenreDetailsActivity;