diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 423649830..5453babf2 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -106,7 +106,7 @@
-
+
diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt
index 65d6a1cc2..956f7788f 100644
--- a/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt
+++ b/app/src/main/java/code/name/monkey/retromusic/activities/albums/AlbumDetailsActivity.kt
@@ -189,8 +189,8 @@ class AlbumDetailsActivity : AbsSlidingMusicPanelActivity(), AlbumDetailsView, C
}
loadAlbumCover()
simpleSongAdapter.swapDataSet(album.songs)
- viewModel.loadArtistAsync(album.artistId)
- viewModel.loadAlbumInfoAsync(album)
+ viewModel.loadArtist(album.artistId)
+ viewModel.loadAlbumInfo(album)
}
override fun moreAlbums(albums: List) {
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
index 4debc3da7..29dd6c327 100644
--- 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
@@ -32,17 +32,16 @@ class AlbumDetailsViewModel(
}
private fun loadDetails() = viewModelScope.launch {
- _album.postValue(
- loadAlbumAsync.await() ?: throw NullPointerException("Album couldn't found")
- )
+ val album = 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 ?: "-")
_lastFmAlbum.postValue(lastFmAlbum)
}
- fun loadArtistAsync(artistId: Int) = viewModelScope.launch(Dispatchers.IO) {
+ fun loadArtist(artistId: Int) = viewModelScope.launch(Dispatchers.IO) {
val artist = _repository.artistById(artistId)
_artist.postValue(artist)
}
diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/ArtistDetailActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt
similarity index 92%
rename from app/src/main/java/code/name/monkey/retromusic/activities/ArtistDetailActivity.kt
rename to app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt
index f16f28714..b5b91d561 100755
--- a/app/src/main/java/code/name/monkey/retromusic/activities/ArtistDetailActivity.kt
+++ b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailActivity.kt
@@ -1,4 +1,4 @@
-package code.name.monkey.retromusic.activities
+package code.name.monkey.retromusic.activities.artists
import android.app.Activity
import android.content.Intent
@@ -11,13 +11,14 @@ import android.view.View
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.text.HtmlCompat
+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.retromusic.App
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.adapter.album.HorizontalAlbumAdapter
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.interfaces.CabHolder
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.rest.model.LastFmArtist
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_details.*
import java.util.*
-import javax.inject.Inject
import kotlin.collections.ArrayList
class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView, CabHolder {
@@ -70,14 +69,12 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
private lateinit var songAdapter: SimpleSongAdapter
private lateinit var albumAdapter: HorizontalAlbumAdapter
private var forceDownload: Boolean = false
+ private lateinit var viewModel: ArtistDetailsViewModel
override fun createContentView(): View {
return wrapSlidingMusicPanel(R.layout.activity_artist_details)
}
- @Inject
- lateinit var artistDetailsPresenter: ArtistDetailsPresenter
-
private fun windowEnterTransition() {
val slide = Slide()
slide.excludeTarget(R.id.appBarLayout, true)
@@ -98,11 +95,17 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
window.sharedElementsUseOverlay = true
windowEnterTransition()
- App.musicComponent.inject(this)
- artistDetailsPresenter.attachView(this)
val artistId = extraNotNull(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)
setupRecyclerView()
@@ -123,11 +126,6 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
}
}
- override fun onDestroy() {
- super.onDestroy()
- artistDetailsPresenter.detachView()
- }
-
private fun setupRecyclerView() {
albumAdapter = HorizontalAlbumAdapter(this, ArrayList(), null)
albumRecyclerView.apply {
@@ -152,7 +150,7 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
}
}
else -> if (resultCode == Activity.RESULT_OK) {
- reload()
+ //reload()
}
}
}
@@ -205,7 +203,7 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
) {
biography = null
this.lang = lang
- artistDetailsPresenter.loadBiography(name, lang, null)
+ viewModel.loadBiography(name, lang, null)
}
override fun artistInfo(lastFmArtist: LastFmArtist?) {
@@ -315,19 +313,6 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
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() {
if (cab != null && cab!!.isActive) {
cab?.finish()
@@ -337,7 +322,6 @@ class ArtistDetailActivity : AbsSlidingMusicPanelActivity(), ArtistDetailsView,
}
companion object {
-
const val EXTRA_ARTIST_ID = "extra_artist_id"
const val REQUEST_CODE_SELECT_IMAGE = 9003
}
diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt
new file mode 100644
index 000000000..fc535b99f
--- /dev/null
+++ b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModel.kt
@@ -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
+ get() = viewModelScope.async(Dispatchers.IO) {
+ _repository.artistById(artistId)
+ }
+ private val _repository = RepositoryImpl(application.applicationContext)
+ private val _artist = MutableLiveData()
+ private val _lastFmArtist = MutableLiveData()
+
+ fun getArtist(): LiveData = _artist
+ fun getArtistInfo(): LiveData = _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)
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModelFactory.kt b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModelFactory.kt
new file mode 100644
index 000000000..e3ab552bb
--- /dev/null
+++ b/app/src/main/java/code/name/monkey/retromusic/activities/artists/ArtistDetailsViewModelFactory.kt
@@ -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 create(modelClass: Class): T {
+ return if (modelClass.isAssignableFrom(ArtistDetailsViewModel::class.java)) {
+ ArtistDetailsViewModel(application, artistId) 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 7cd5ef8d0..42a6584fd 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,7 @@
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.PlaylistDetailActivity
import code.name.monkey.retromusic.activities.SearchActivity
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 eb32f35b5..3c8341725 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
@@ -30,7 +30,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.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.GenreDetailsActivity;
import code.name.monkey.retromusic.activities.LicenseActivity;