diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/CreatePlaylistDialog.kt index 052e3a1d..b4bcf846 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/CreatePlaylistDialog.kt @@ -68,17 +68,8 @@ class CreatePlaylistDialog : DialogFragment() { ) { _, _ -> val playlistName = playlistView.text.toString() if (!TextUtils.isEmpty(playlistName)) { - lifecycleScope.launch(Dispatchers.IO) { - 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() - } - } + libraryViewModel.addToPlaylist(playlistName, songs) + } else { playlistContainer.error = "Playlist is can't be empty" } 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 6a554854..5854a750 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 @@ -14,16 +14,32 @@ */ 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_ARTISTS import code.name.monkey.retromusic.TOP_ALBUMS 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.helper.MusicPlayerRemote 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.state.NowPlayingPanelState import code.name.monkey.retromusic.util.PreferenceUtil @@ -226,10 +242,10 @@ class LibraryViewModel( suspend fun removeSongFromPlaylist(songEntity: SongEntity) = repository.removeSongFromPlaylist(songEntity) - suspend fun checkPlaylistExists(playlistName: String): List = + private suspend fun checkPlaylistExists(playlistName: String): List = repository.checkPlaylistExists(playlistName) - suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long = + private suspend fun createPlaylist(playlistEntity: PlaylistEntity): Long = repository.createPlaylist(playlistEntity) fun importPlaylists() = viewModelScope.launch(IO) { @@ -303,6 +319,29 @@ class LibraryViewModel( searchResults.postValue(emptyList()) } } + + fun addToPlaylist(playlistName: String, songs: List) { + 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 { diff --git a/app/src/main/res/layout-xlarge-land/fragment_blur.xml b/app/src/main/res/layout-xlarge-land/fragment_blur.xml index a2185426..c918471c 100644 --- a/app/src/main/res/layout-xlarge-land/fragment_blur.xml +++ b/app/src/main/res/layout-xlarge-land/fragment_blur.xml @@ -16,54 +16,57 @@ android:scaleType="centerCrop" app:srcCompat="@color/black_color" /> - - - + + - - + - + tools:layout="@layout/fragment_album_cover" /> + - - - + + + + ?android:attr/colorButtonNormal 0dp + + + diff --git a/build.gradle b/build.gradle index 23455001..c6c31362 100644 --- a/build.gradle +++ b/build.gradle @@ -7,11 +7,10 @@ buildscript { google() } 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" def nav_version = "2.3.0" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" - //noinspection GradleDependency classpath "com.diffplug.spotless:spotless-plugin-gradle:4.5.1" } }