Add ArtistViewModel

This commit is contained in:
h4h13 2020-06-06 01:31:37 +05:30
parent acc77478b7
commit 45671fe593
8 changed files with 90 additions and 42 deletions

View file

@ -106,7 +106,7 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".activities.albums.AlbumDetailsActivity" /> <activity android:name=".activities.albums.AlbumDetailsActivity" />
<activity android:name=".activities.ArtistDetailActivity" /> <activity android:name=".activities.artists.ArtistDetailActivity" />
<activity android:name=".activities.PlaylistDetailActivity" /> <activity android:name=".activities.PlaylistDetailActivity" />
<activity android:name=".activities.PlayingQueueActivity" /> <activity android:name=".activities.PlayingQueueActivity" />
<activity android:name=".activities.AboutActivity" /> <activity android:name=".activities.AboutActivity" />

View file

@ -189,8 +189,8 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
} }
loadAlbumCover() loadAlbumCover()
simpleSongAdapter.swapDataSet(album.songs) simpleSongAdapter.swapDataSet(album.songs)
viewModel.loadArtistAsync(album.artistId) viewModel.loadArtist(album.artistId)
viewModel.loadAlbumInfoAsync(album) viewModel.loadAlbumInfo(album)
} }
override fun moreAlbums(albums: List<Album>) { override fun moreAlbums(albums: List<Album>) {

View file

@ -32,17 +32,16 @@ class AlbumDetailsViewModel(
} }
private fun loadDetails() = viewModelScope.launch { private fun loadDetails() = viewModelScope.launch {
_album.postValue( val album = loadAlbumAsync.await() ?: throw NullPointerException("Album couldn't found")
loadAlbumAsync.await() ?: throw NullPointerException("Album couldn't found") _album.postValue(album)
)
} }
fun loadAlbumInfoAsync(album: Album) = viewModelScope.launch(Dispatchers.IO) { fun loadAlbumInfo(album: Album) = viewModelScope.launch(Dispatchers.IO) {
val lastFmAlbum = _repository.albumInfo(album.artistName ?: "-", album.title ?: "-") val lastFmAlbum = _repository.albumInfo(album.artistName ?: "-", album.title ?: "-")
_lastFmAlbum.postValue(lastFmAlbum) _lastFmAlbum.postValue(lastFmAlbum)
} }
fun loadArtistAsync(artistId: Int) = viewModelScope.launch(Dispatchers.IO) { fun loadArtist(artistId: Int) = viewModelScope.launch(Dispatchers.IO) {
val artist = _repository.artistById(artistId) val artist = _repository.artistById(artistId)
_artist.postValue(artist) _artist.postValue(artist)
} }

View file

@ -1,4 +1,4 @@
package code.name.monkey.retromusic.activities package code.name.monkey.retromusic.activities.artists
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
@ -11,13 +11,14 @@ import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
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.retromusic.App
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.albums.AlbumDetailsViewModelFactory
import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity
import code.name.monkey.retromusic.adapter.album.HorizontalAlbumAdapter import code.name.monkey.retromusic.adapter.album.HorizontalAlbumAdapter
import code.name.monkey.retromusic.adapter.song.SimpleSongAdapter import code.name.monkey.retromusic.adapter.song.SimpleSongAdapter
@ -31,7 +32,6 @@ import code.name.monkey.retromusic.glide.RetroMusicColoredTarget
import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.interfaces.CabHolder import code.name.monkey.retromusic.interfaces.CabHolder
import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.mvp.presenter.ArtistDetailsPresenter
import code.name.monkey.retromusic.mvp.presenter.ArtistDetailsView import code.name.monkey.retromusic.mvp.presenter.ArtistDetailsView
import code.name.monkey.retromusic.rest.model.LastFmArtist import code.name.monkey.retromusic.rest.model.LastFmArtist
import code.name.monkey.retromusic.util.* import code.name.monkey.retromusic.util.*
@ -41,7 +41,6 @@ import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_artist_content.* import kotlinx.android.synthetic.main.activity_artist_content.*
import kotlinx.android.synthetic.main.activity_artist_details.* import kotlinx.android.synthetic.main.activity_artist_details.*
import java.util.* import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView, CabHolder { class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView, CabHolder {
@ -70,14 +69,12 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
private lateinit var songAdapter: SimpleSongAdapter private lateinit var songAdapter: SimpleSongAdapter
private lateinit var albumAdapter: HorizontalAlbumAdapter private lateinit var albumAdapter: HorizontalAlbumAdapter
private var forceDownload: Boolean = false private var forceDownload: Boolean = false
private lateinit var viewModel: ArtistDetailsViewModel
override fun createContentView(): View { override fun createContentView(): View {
return wrapSlidingMusicPanel(R.layout.activity_artist_details) return wrapSlidingMusicPanel(R.layout.activity_artist_details)
} }
@Inject
lateinit var artistDetailsPresenter: ArtistDetailsPresenter
private fun windowEnterTransition() { private fun windowEnterTransition() {
val slide = Slide() val slide = Slide()
slide.excludeTarget(R.id.appBarLayout, true) slide.excludeTarget(R.id.appBarLayout, true)
@ -98,11 +95,17 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
window.sharedElementsUseOverlay = true window.sharedElementsUseOverlay = true
windowEnterTransition() windowEnterTransition()
App.musicComponent.inject(this)
artistDetailsPresenter.attachView(this)
val artistId = extraNotNull<Int>(EXTRA_ARTIST_ID).value val artistId = extraNotNull<Int>(EXTRA_ARTIST_ID).value
artistDetailsPresenter.loadArtist(artistId) val viewModelFactory = ArtistDetailsViewModelFactory(application, artistId)
viewModel =
ViewModelProvider(this, viewModelFactory).get(ArtistDetailsViewModel::class.java)
viewModel.getArtist().observe(this, androidx.lifecycle.Observer {
ActivityCompat.startPostponedEnterTransition(this@ArtistDetailActivity)
artist(it)
})
viewModel.getArtistInfo().observe(this, androidx.lifecycle.Observer {
artistInfo(it)
})
ActivityCompat.postponeEnterTransition(this) ActivityCompat.postponeEnterTransition(this)
setupRecyclerView() setupRecyclerView()
@ -123,11 +126,6 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
} }
} }
override fun onDestroy() {
super.onDestroy()
artistDetailsPresenter.detachView()
}
private fun setupRecyclerView() { private fun setupRecyclerView() {
albumAdapter = HorizontalAlbumAdapter(this, ArrayList(), null) albumAdapter = HorizontalAlbumAdapter(this, ArrayList(), null)
albumRecyclerView.apply { albumRecyclerView.apply {
@ -152,7 +150,7 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
} }
} }
else -> if (resultCode == Activity.RESULT_OK) { else -> if (resultCode == Activity.RESULT_OK) {
reload() //reload()
} }
} }
} }
@ -205,7 +203,7 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
) { ) {
biography = null biography = null
this.lang = lang this.lang = lang
artistDetailsPresenter.loadBiography(name, lang, null) viewModel.loadBiography(name, lang, null)
} }
override fun artistInfo(lastFmArtist: LastFmArtist?) { override fun artistInfo(lastFmArtist: LastFmArtist?) {
@ -315,19 +313,6 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
return super.onCreateOptionsMenu(menu) return super.onCreateOptionsMenu(menu)
} }
override fun onMediaStoreChanged() {
super.onMediaStoreChanged()
reload()
}
private fun reload() {
if (intent.extras!!.containsKey(EXTRA_ARTIST_ID)) {
intent.extras?.getInt(EXTRA_ARTIST_ID)?.let { artistDetailsPresenter.loadArtist(it) }
} else {
finish()
}
}
override fun onBackPressed() { override fun onBackPressed() {
if (cab != null && cab!!.isActive) { if (cab != null && cab!!.isActive) {
cab?.finish() cab?.finish()
@ -337,7 +322,6 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
} }
companion object { companion object {
const val EXTRA_ARTIST_ID = "extra_artist_id" const val EXTRA_ARTIST_ID = "extra_artist_id"
const val REQUEST_CODE_SELECT_IMAGE = 9003 const val REQUEST_CODE_SELECT_IMAGE = 9003
} }

View file

@ -0,0 +1,46 @@
package code.name.monkey.retromusic.activities.artists
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.Artist
import code.name.monkey.retromusic.providers.RepositoryImpl
import code.name.monkey.retromusic.rest.model.LastFmArtist
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
class ArtistDetailsViewModel(
application: Application,
private val artistId: Int
) : AndroidViewModel(application) {
private val loadArtistDetailsAsync: Deferred<Artist?>
get() = viewModelScope.async(Dispatchers.IO) {
_repository.artistById(artistId)
}
private val _repository = RepositoryImpl(application.applicationContext)
private val _artist = MutableLiveData<Artist>()
private val _lastFmArtist = MutableLiveData<LastFmArtist>()
fun getArtist(): LiveData<Artist> = _artist
fun getArtistInfo(): LiveData<LastFmArtist> = _lastFmArtist
init {
loadArtistDetails()
}
private fun loadArtistDetails() = viewModelScope.launch {
val artist =
loadArtistDetailsAsync.await() ?: throw NullPointerException("Album couldn't found")
_artist.postValue(artist)
}
fun loadBiography(name: String, lang: String?, cache: String?) = viewModelScope.launch {
val info = _repository.artistInfo(name, lang, cache)
_lastFmArtist.postValue(info)
}
}

View file

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

View file

@ -14,7 +14,7 @@
package code.name.monkey.retromusic.dagger package code.name.monkey.retromusic.dagger
import code.name.monkey.retromusic.activities.ArtistDetailActivity import code.name.monkey.retromusic.activities.artists.ArtistDetailActivity
import code.name.monkey.retromusic.activities.GenreDetailsActivity import code.name.monkey.retromusic.activities.GenreDetailsActivity
import code.name.monkey.retromusic.activities.PlaylistDetailActivity import code.name.monkey.retromusic.activities.PlaylistDetailActivity
import code.name.monkey.retromusic.activities.SearchActivity import code.name.monkey.retromusic.activities.SearchActivity

View file

@ -30,7 +30,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.albums.AlbumDetailsActivity; import code.name.monkey.retromusic.activities.albums.AlbumDetailsActivity;
import code.name.monkey.retromusic.activities.ArtistDetailActivity; import code.name.monkey.retromusic.activities.artists.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;
import code.name.monkey.retromusic.activities.LicenseActivity; import code.name.monkey.retromusic.activities.LicenseActivity;