Fix playlist reload

This commit is contained in:
Hemanth S 2020-10-09 01:14:10 +05:30
parent 54f95dbd92
commit e5f6e83dd4
5 changed files with 90 additions and 52 deletions

View file

@ -68,17 +68,8 @@ class CreatePlaylistDialog : DialogFragment() {
) { _, _ -> ) { _, _ ->
val playlistName = playlistView.text.toString() val playlistName = playlistView.text.toString()
if (!TextUtils.isEmpty(playlistName)) { if (!TextUtils.isEmpty(playlistName)) {
lifecycleScope.launch(Dispatchers.IO) { libraryViewModel.addToPlaylist(playlistName, songs)
if (libraryViewModel.checkPlaylistExists(playlistName).isEmpty()) {
val playlistId: Long =
libraryViewModel.createPlaylist(PlaylistEntity(playlistName = playlistName))
libraryViewModel.insertSongs(songs.map { it.toSongEntity(playlistId) })
libraryViewModel.forceReload(Playlists)
} else {
Toast.makeText(requireContext(), "Playlist exists", Toast.LENGTH_SHORT)
.show()
}
}
} else { } else {
playlistContainer.error = "Playlist is can't be empty" playlistContainer.error = "Playlist is can't be empty"
} }

View file

@ -14,16 +14,32 @@
*/ */
package code.name.monkey.retromusic.fragments package code.name.monkey.retromusic.fragments
import androidx.lifecycle.* import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import code.name.monkey.retromusic.App
import code.name.monkey.retromusic.RECENT_ALBUMS import code.name.monkey.retromusic.RECENT_ALBUMS
import code.name.monkey.retromusic.RECENT_ARTISTS import code.name.monkey.retromusic.RECENT_ARTISTS
import code.name.monkey.retromusic.TOP_ALBUMS import code.name.monkey.retromusic.TOP_ALBUMS
import code.name.monkey.retromusic.TOP_ARTISTS import code.name.monkey.retromusic.TOP_ARTISTS
import code.name.monkey.retromusic.db.* import code.name.monkey.retromusic.db.PlaylistEntity
import code.name.monkey.retromusic.db.PlaylistWithSongs
import code.name.monkey.retromusic.db.SongEntity
import code.name.monkey.retromusic.db.toSong
import code.name.monkey.retromusic.db.toSongEntity
import code.name.monkey.retromusic.fragments.ReloadType.* import code.name.monkey.retromusic.fragments.ReloadType.*
import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener
import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.model.Album
import code.name.monkey.retromusic.model.Artist
import code.name.monkey.retromusic.model.Contributor
import code.name.monkey.retromusic.model.Genre
import code.name.monkey.retromusic.model.Home
import code.name.monkey.retromusic.model.Playlist
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.repository.RealRepository import code.name.monkey.retromusic.repository.RealRepository
import code.name.monkey.retromusic.state.NowPlayingPanelState import code.name.monkey.retromusic.state.NowPlayingPanelState
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
@ -226,10 +242,10 @@ class LibraryViewModel(
suspend fun removeSongFromPlaylist(songEntity: SongEntity) = suspend fun removeSongFromPlaylist(songEntity: SongEntity) =
repository.removeSongFromPlaylist(songEntity) repository.removeSongFromPlaylist(songEntity)
suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity> = private suspend fun checkPlaylistExists(playlistName: String): List<PlaylistEntity> =
repository.checkPlaylistExists(playlistName) repository.checkPlaylistExists(playlistName)
suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long = private suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long =
repository.createPlaylist(playlistEntity) repository.createPlaylist(playlistEntity)
fun importPlaylists() = viewModelScope.launch(IO) { fun importPlaylists() = viewModelScope.launch(IO) {
@ -303,6 +319,29 @@ class LibraryViewModel(
searchResults.postValue(emptyList()) searchResults.postValue(emptyList())
} }
} }
fun addToPlaylist(playlistName: String, songs: List<Song>) {
viewModelScope.launch(IO) {
val playlists = checkPlaylistExists(playlistName)
if (playlists.isEmpty()) {
val playlistId: Long = createPlaylist(PlaylistEntity(playlistName = playlistName))
insertSongs(songs.map { it.toSongEntity(playlistId) })
forceReload(Playlists)
} else {
val playlist = playlists.firstOrNull()
if (playlist != null) {
insertSongs(songs.map {
it.toSongEntity(playListId = playlist.playListId)
})
}
Toast.makeText(
App.getContext(),
"Adding songs to $playlistName",
Toast.LENGTH_SHORT
).show()
}
}
}
} }
enum class ReloadType { enum class ReloadType {

View file

@ -16,32 +16,32 @@
android:scaleType="centerCrop" android:scaleType="centerCrop"
app:srcCompat="@color/black_color" /> app:srcCompat="@color/black_color" />
<View <View
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@drawable/shadow_up" /> android:background="@drawable/shadow_up" />
<LinearLayout <FrameLayout
android:id="@+id/playerContent" android:id="@+id/statusBarContainer"
android:layout_width="400dp" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="wrap_content"
android:layout_above="@id/playerToolbar"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/playerToolbar"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<code.name.monkey.retromusic.views.WidthFitSquareLayout <include layout="@layout/status_bar" />
android:id="@+id/album_cover_container" </FrameLayout>
android:layout_width="match_parent"
android:layout_height="wrap_content" <FrameLayout
android:id="@+id/albumCoverContainer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_weight="0"> android:layout_weight="0"
app:layout_constraintBottom_toTopOf="@id/playerContent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/statusBarContainer">
<fragment <fragment
android:id="@+id/playerAlbumCoverFragment" android:id="@+id/playerAlbumCoverFragment"
@ -49,13 +49,17 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:layout="@layout/fragment_album_cover" /> tools:layout="@layout/fragment_album_cover" />
</code.name.monkey.retromusic.views.WidthFitSquareLayout> </FrameLayout>
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:id="@+id/playerContent"
android:layout_height="match_parent" android:layout_width="400dp"
android:layout_weight="1" android:layout_height="0dp"
tools:background="@color/md_white_1000"> android:layout_above="@id/playerToolbar"
app:layout_constraintBottom_toTopOf="@+id/playerToolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/albumCoverContainer">
<fragment <fragment
android:id="@+id/playbackControlsFragment" android:id="@+id/playbackControlsFragment"
@ -63,7 +67,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />
</FrameLayout> </FrameLayout>
</LinearLayout>
<com.google.android.material.appbar.MaterialToolbar <com.google.android.material.appbar.MaterialToolbar

View file

@ -253,9 +253,15 @@
<item name="strokeColor">?android:attr/colorButtonNormal</item> <item name="strokeColor">?android:attr/colorButtonNormal</item>
<item name="cardElevation">0dp</item> <item name="cardElevation">0dp</item>
</style> </style>
<!-- This will set the fade in animation on all your activities by default --> <!-- This will set the fade in animation on all your activities by default -->
<style name="WindowAnimationTransition"> <style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">@android:anim/fade_in</item> <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
<item name="android:windowExitAnimation">@android:anim/fade_out</item> <item name="android:windowExitAnimation">@android:anim/fade_out</item>
</style> </style>
<style name="circleImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">5%</item>
</style>
</resources> </resources>

View file

@ -7,11 +7,10 @@ buildscript {
google() google()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:4.0.1' classpath 'com.android.tools.build:gradle:4.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
def nav_version = "2.3.0" def nav_version = "2.3.0"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
//noinspection GradleDependency
classpath "com.diffplug.spotless:spotless-plugin-gradle:4.5.1" classpath "com.diffplug.spotless:spotless-plugin-gradle:4.5.1"
} }
} }