From b5e07a31d860e9d4d3332e8c83fba750ad2211ee Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Thu, 20 Aug 2020 12:19:08 +0530 Subject: [PATCH] Add Room for playlist Added DAO Queries and Able insert songs to playlist --- .../code/name/monkey/retromusic/MainModule.kt | 18 ++++-- .../retromusic/activities/MainActivity.kt | 7 +-- .../adapter/playlist/PlaylistAdapter.kt | 60 +++++++++---------- .../name/monkey/retromusic/db/PlaylistDao.kt | 7 +++ .../monkey/retromusic/db/PlaylistDatabase.kt | 2 +- .../monkey/retromusic/db/PlaylistEntity.kt | 7 ++- .../monkey/retromusic/db/PlaylistWithSongs.kt | 4 +- .../retromusic/db/RoomPlaylistRepository.kt | 35 ++++++++++- .../name/monkey/retromusic/db/SongEntity.kt | 4 +- .../retromusic/dialogs/AddToRetroPlaylist.kt | 10 ++-- .../retromusic/dialogs/CreateRetroPlaylist.kt | 10 +++- .../retromusic/fragments/LibraryViewModel.kt | 12 +++- .../fragments/library/LibraryFragment.kt | 6 +- .../fragments/playlists/PlaylistsFragment.kt | 3 +- .../retromusic/repository/Repository.kt | 13 +++- .../name/monkey/retromusic/util/MusicUtil.kt | 8 +++ 16 files changed, 140 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/MainModule.kt b/app/src/main/java/code/name/monkey/retromusic/MainModule.kt index f884f1fa..675f5abd 100644 --- a/app/src/main/java/code/name/monkey/retromusic/MainModule.kt +++ b/app/src/main/java/code/name/monkey/retromusic/MainModule.kt @@ -1,6 +1,8 @@ package code.name.monkey.retromusic import code.name.monkey.retromusic.db.PlaylistDatabase +import code.name.monkey.retromusic.db.RealRoomPlaylistRepository +import code.name.monkey.retromusic.db.RoomPlaylistRepository import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.albums.AlbumDetailsViewModel import code.name.monkey.retromusic.fragments.artists.ArtistDetailsViewModel @@ -16,9 +18,15 @@ import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.dsl.bind import org.koin.dsl.module +private val mainModule = module { + single { + androidContext().contentResolver + } + +} private val dataModule = module { single { - RealRepository(get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) + RealRepository(get(), get(), get(), get(), get(), get(), get(), get(), get(), get(), get()) } bind Repository::class single { @@ -64,12 +72,12 @@ private val dataModule = module { } single { - androidContext().contentResolver + PlaylistDatabase.getDatabase(get()).playlistDao() } single { - PlaylistDatabase.getDatabase(get()) - } + RealRoomPlaylistRepository(get()) + } bind RoomPlaylistRepository::class } private val viewModules = module { @@ -111,4 +119,4 @@ private val viewModules = module { } } -val appModules = listOf(dataModule, viewModules, networkModule) \ No newline at end of file +val appModules = listOf(mainModule, dataModule, viewModules, networkModule) \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt index 87791f4a..45d94b07 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt @@ -10,7 +10,7 @@ import android.view.View import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.* import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity -import code.name.monkey.retromusic.db.PlaylistDatabase +import code.name.monkey.retromusic.db.RoomPlaylistRepository import code.name.monkey.retromusic.extensions.findNavController import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.helper.MusicPlayerRemote.openAndShuffleQueue @@ -58,11 +58,10 @@ class MainActivity : AbsSlidingMusicPanelActivity(), OnSharedPreferenceChangeLis addMusicServiceEventListener(libraryViewModel) updateTabs() - val database: PlaylistDatabase = get() + val playlistRepository = get() lifecycleScope.launch { - println("Size:${database.playlistDao().playlistsWithSong()}") + println("Size:${playlistRepository.playlistWithSongs()}") } - } override fun onSupportNavigateUp(): Boolean = diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt index 39d4f8e1..a4fa0914 100755 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/playlist/PlaylistAdapter.kt @@ -13,22 +13,21 @@ import androidx.appcompat.widget.PopupMenu import androidx.core.os.bundleOf import androidx.fragment.app.FragmentActivity import androidx.navigation.findNavController -import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.util.ATHUtil import code.name.monkey.appthemehelper.util.TintHelper import code.name.monkey.retromusic.EXTRA_PLAYLIST import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.base.AbsMultiSelectAdapter import code.name.monkey.retromusic.adapter.base.MediaEntryViewHolder +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.extensions.hide import code.name.monkey.retromusic.extensions.show -import code.name.monkey.retromusic.helper.menu.PlaylistMenuHelper import code.name.monkey.retromusic.helper.menu.SongsMenuHelper import code.name.monkey.retromusic.interfaces.CabHolder -import code.name.monkey.retromusic.model.AbsCustomPlaylist import code.name.monkey.retromusic.model.Playlist import code.name.monkey.retromusic.model.Song -import code.name.monkey.retromusic.model.smartplaylist.AbsSmartPlaylist import code.name.monkey.retromusic.repository.PlaylistSongsLoader import code.name.monkey.retromusic.util.AutoGeneratedPlaylistBitmap import code.name.monkey.retromusic.util.MusicUtil @@ -37,10 +36,10 @@ import java.util.* class PlaylistAdapter( private val activity: FragmentActivity, - var dataSet: List, + var dataSet: List, private var itemLayoutRes: Int, cabHolder: CabHolder? -) : AbsMultiSelectAdapter( +) : AbsMultiSelectAdapter( activity, cabHolder, R.menu.menu_playlists_selection @@ -51,13 +50,13 @@ class PlaylistAdapter( setHasStableIds(true) } - fun swapDataSet(dataSet: List) { + fun swapDataSet(dataSet: List) { this.dataSet = dataSet notifyDataSetChanged() } override fun getItemId(position: Int): Long { - return dataSet[position].id.toLong() + return dataSet[position].playlistEntity.playListId.toLong() } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { @@ -69,18 +68,18 @@ class PlaylistAdapter( return ViewHolder(view) } - private fun getPlaylistTitle(playlist: Playlist): String { - return if (TextUtils.isEmpty(playlist.name)) "-" else playlist.name + private fun getPlaylistTitle(playlist: PlaylistEntity): String { + return if (TextUtils.isEmpty(playlist.playlistName)) "-" else playlist.playlistName } - private fun getPlaylistText(playlist: Playlist): String { - return MusicUtil.getPlaylistInfoString(activity, getSongs(playlist)) + private fun getPlaylistText(playlist: PlaylistWithSongs): String { + return MusicUtil.playlistInfoString(activity, getSongs(playlist)) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val playlist = dataSet[position] holder.itemView.isActivated = isChecked(playlist) - holder.title?.text = getPlaylistTitle(playlist) + holder.title?.text = getPlaylistTitle(playlist.playlistEntity) holder.text?.text = getPlaylistText(playlist) holder.image?.setImageDrawable(getIconRes(playlist)) val isChecked = isChecked(playlist) @@ -92,37 +91,34 @@ class PlaylistAdapter( //PlaylistBitmapLoader(this, holder, playlist).execute() } - private fun getIconRes(playlist: Playlist): Drawable { - return if (MusicUtil.isFavoritePlaylist(activity, playlist)) + private fun getIconRes(playlist: PlaylistWithSongs): Drawable { + return/* if (MusicUtil.isFavoritePlaylist(activity, playlist)) TintHelper.createTintedDrawable( activity, R.drawable.ic_favorite, ThemeStore.accentColor(activity) ) - else TintHelper.createTintedDrawable( + else*/ TintHelper.createTintedDrawable( activity, R.drawable.ic_playlist_play, ATHUtil.resolveColor(activity, R.attr.colorControlNormal) ) } - override fun getItemViewType(position: Int): Int { - return if (dataSet[position] is AbsSmartPlaylist) SMART_PLAYLIST else DEFAULT_PLAYLIST - } override fun getItemCount(): Int { return dataSet.size } - override fun getIdentifier(position: Int): Playlist? { + override fun getIdentifier(position: Int): PlaylistWithSongs? { return dataSet[position] } - override fun getName(playlist: Playlist): String { - return playlist.name + override fun getName(playlist: PlaylistWithSongs): String { + return playlist.playlistEntity.playlistName } - override fun onMultipleItemAction(menuItem: MenuItem, selection: List) { + override fun onMultipleItemAction(menuItem: MenuItem, selection: List) { when (menuItem.itemId) { else -> SongsMenuHelper.handleMenuClick( activity, @@ -132,25 +128,27 @@ class PlaylistAdapter( } } - private fun getSongList(playlists: List): List { + private fun getSongList(playlists: List): List { val songs = ArrayList() - for (playlist in playlists) { + /* for (playlist in playlists) { + songs.addAll(playlist.songs) if (playlist is AbsCustomPlaylist) { songs.addAll(playlist.songs()) } else { songs.addAll(PlaylistSongsLoader.getPlaylistSongList(activity, playlist.id)) } - } + }*/ return songs } - private fun getSongs(playlist: Playlist): List { - val songs = ArrayList() - if (playlist is AbsSmartPlaylist) { + private fun getSongs(playlist: PlaylistWithSongs): List { + val songs = ArrayList() + songs.addAll(playlist.songs) + /*if (playlist is AbsSmartPlaylist) { songs.addAll(playlist.songs()) } else { songs.addAll(playlist.getSongs()) - } + }*/ return songs } @@ -167,7 +165,7 @@ class PlaylistAdapter( val popupMenu = PopupMenu(activity, view) popupMenu.inflate(R.menu.menu_item_playlist) popupMenu.setOnMenuItemClickListener { item -> - PlaylistMenuHelper.handleMenuClick(activity, dataSet[layoutPosition], item) + return@setOnMenuItemClickListener true //PlaylistMenuHelper.handleMenuClick(activity, dataSet[layoutPosition], item) } popupMenu.show() } diff --git a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDao.kt b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDao.kt index 7c595b3c..efe2e437 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDao.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDao.kt @@ -4,6 +4,10 @@ import androidx.room.* @Dao interface PlaylistDao { + + @Query("SELECT * FROM PlaylistEntity WHERE playlist_name = :name") + suspend fun checkPlaylistExists(name: String): List + @Insert suspend fun createPlaylist(playlistEntity: PlaylistEntity) @@ -16,4 +20,7 @@ interface PlaylistDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertSongs(songEntities: List) + + @Query("SELECT * FROM SongEntity WHERE playlist_creator_id = :playlistName AND song_id = :songId") + suspend fun checkSongExistsWithPlaylistName(playlistName: String, songId: Int): List } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDatabase.kt b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDatabase.kt index 582c705a..3f8ec314 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDatabase.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistDatabase.kt @@ -7,7 +7,7 @@ import androidx.room.RoomDatabase @Database( entities = [PlaylistEntity::class, SongEntity::class], - version = 4, + version = 7, exportSchema = false ) abstract class PlaylistDatabase : RoomDatabase() { diff --git a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistEntity.kt b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistEntity.kt index 7a490564..3bb1681f 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistEntity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistEntity.kt @@ -9,7 +9,10 @@ import kotlinx.android.parcel.Parcelize @Entity @Parcelize class PlaylistEntity( - @PrimaryKey @ColumnInfo(name = "playlist_name") val playlistName: String -) : Parcelable \ No newline at end of file +) : Parcelable { + @PrimaryKey(autoGenerate = true) + @ColumnInfo(name = "playlist_id") + var playListId: Int = 0 +} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistWithSongs.kt b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistWithSongs.kt index c47fac08..6645d6cd 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/PlaylistWithSongs.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/PlaylistWithSongs.kt @@ -6,7 +6,7 @@ import androidx.room.Relation data class PlaylistWithSongs( @Embedded val playlistEntity: PlaylistEntity, @Relation( - parentColumn = "playlist_name", - entityColumn = "playlist_creator_name" + parentColumn = "playlist_id", + entityColumn = "playlist_creator_id" ) val songs: List ) \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/db/RoomPlaylistRepository.kt b/app/src/main/java/code/name/monkey/retromusic/db/RoomPlaylistRepository.kt index 94477b4d..7ab60e80 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/RoomPlaylistRepository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/RoomPlaylistRepository.kt @@ -2,8 +2,39 @@ package code.name.monkey.retromusic.db import androidx.annotation.WorkerThread -class RoomPlaylistRepository(private val playlistDao: PlaylistDao) { + +interface RoomPlaylistRepository { + suspend fun createPlaylist(playlistEntity: PlaylistEntity) + suspend fun checkPlaylistExists(playlistName: String): List + suspend fun playlists(): List + suspend fun playlistWithSongs(): List + suspend fun insertSongs(songs: List) +} + +class RealRoomPlaylistRepository(private val playlistDao: PlaylistDao) : RoomPlaylistRepository { + @WorkerThread + override suspend fun createPlaylist(playlistEntity: PlaylistEntity) = + playlistDao.createPlaylist(playlistEntity) @WorkerThread - suspend fun getPlaylistWithSongs(): List = playlistDao.playlistsWithSong() + override suspend fun checkPlaylistExists(playlistName: String): List = + playlistDao.checkPlaylistExists(playlistName) + + @WorkerThread + override suspend fun playlists(): List = playlistDao.playlists() + + @WorkerThread + override suspend fun playlistWithSongs(): List = + playlistDao.playlistsWithSong() + + @WorkerThread + override suspend fun insertSongs(songs: List) { + /* val tempList = ArrayList(songs) + val existingSongs = songs.map { + playlistDao.checkSongExistsWithPlaylistName(it.playlistCreatorName, it.songId) + }.first() + println("Existing ${existingSongs.size}") + tempList.removeAll(existingSongs)*/ + playlistDao.insertSongs(songs) + } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/db/SongEntity.kt b/app/src/main/java/code/name/monkey/retromusic/db/SongEntity.kt index 3f492fc4..75c096ac 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/SongEntity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/SongEntity.kt @@ -8,8 +8,8 @@ import androidx.room.PrimaryKey class SongEntity( @ColumnInfo(name = "song_id") val songId: Int, - @ColumnInfo(name = "playlist_creator_name") - val playlistCreatorName: String + @ColumnInfo(name = "playlist_creator_id") + val playlistCreatorId: Int ) { @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "song_key") diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/AddToRetroPlaylist.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/AddToRetroPlaylist.kt index b731c198..b3c29b02 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/AddToRetroPlaylist.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/AddToRetroPlaylist.kt @@ -6,8 +6,8 @@ import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.R -import code.name.monkey.retromusic.db.PlaylistDatabase import code.name.monkey.retromusic.db.PlaylistEntity +import code.name.monkey.retromusic.db.RoomPlaylistRepository import code.name.monkey.retromusic.db.SongEntity import code.name.monkey.retromusic.extensions.colorButtons import code.name.monkey.retromusic.extensions.extraNotNull @@ -37,15 +37,13 @@ class AddToRetroPlaylist : DialogFragment() { return materialDialog(R.string.add_playlist_title) .setItems(playlistNames.toTypedArray()) { _, which -> val songs = RealSongRepository(requireContext()).songs() - println(songs.size) if (which == 0) { CreateRetroPlaylist().show(requireActivity().supportFragmentManager, "Dialog") } else { lifecycleScope.launch(Dispatchers.IO) { - println(Thread.currentThread().name) - val database: PlaylistDatabase = get() + val playlistRepository = get() val songEntities = songs.withPlaylistIds(playlistEntities[which - 1]) - database.playlistDao().insertSongs(songEntities) + playlistRepository.insertSongs(songEntities) } } dismiss() @@ -56,7 +54,7 @@ class AddToRetroPlaylist : DialogFragment() { private fun List.withPlaylistIds(playlistEntity: PlaylistEntity): List { val songEntities = map { - SongEntity(it.id, playlistEntity.playlistName) + SongEntity(it.id, playlistEntity.playListId) } println(songEntities.size) return songEntities diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/CreateRetroPlaylist.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/CreateRetroPlaylist.kt index c7d9fad0..9f0b6ea3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/CreateRetroPlaylist.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/CreateRetroPlaylist.kt @@ -7,8 +7,8 @@ import android.view.LayoutInflater import androidx.fragment.app.DialogFragment import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.R -import code.name.monkey.retromusic.db.PlaylistDatabase import code.name.monkey.retromusic.db.PlaylistEntity +import code.name.monkey.retromusic.db.RoomPlaylistRepository import code.name.monkey.retromusic.extensions.colorButtons import code.name.monkey.retromusic.extensions.materialDialog import com.google.android.material.textfield.TextInputEditText @@ -29,9 +29,13 @@ class CreateRetroPlaylist : DialogFragment() { ) { _, _ -> val playlistName = playlistView.text.toString() if (!TextUtils.isEmpty(playlistName)) { - val database: PlaylistDatabase = get() + val playlistRepository: RoomPlaylistRepository = get() lifecycleScope.launch { - database.playlistDao().createPlaylist(PlaylistEntity(playlistName)) + if (playlistRepository.checkPlaylistExists(playlistName).isEmpty()) { + playlistRepository.createPlaylist(PlaylistEntity(playlistName)) + } else { + println("Playlist exists") + } } } 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 dd4727a7..baa117a6 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 @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import code.name.monkey.retromusic.db.PlaylistWithSongs import code.name.monkey.retromusic.fragments.ReloadType.* import code.name.monkey.retromusic.interfaces.MusicServiceEventListener import code.name.monkey.retromusic.model.* @@ -22,6 +23,7 @@ class LibraryViewModel( private val songs = MutableLiveData>() private val artists = MutableLiveData>() private val playlists = MutableLiveData>() + private val roomPlaylists = MutableLiveData>() private val genres = MutableLiveData>() private val home = MutableLiveData>() @@ -31,6 +33,7 @@ class LibraryViewModel( val songsLiveData: LiveData> = songs val artistsLiveData: LiveData> = artists val playlisitsLiveData: LiveData> = playlists + val roomPlaylisitsLiveData: LiveData> = roomPlaylists val genresLiveData: LiveData> = genres init { @@ -44,7 +47,8 @@ class LibraryViewModel( albums.value = loadAlbums.await() artists.value = loadArtists.await() playlists.value = loadPlaylists.await() - genres.value = loadGenres.await() + roomPlaylists.value = loadPlaylistsWithSongs.await() + //genres.value = loadGenres.await() home.value = loadHome.await() } @@ -68,6 +72,10 @@ class LibraryViewModel( get() = viewModelScope.async(IO) { realRepository.allPlaylists() } + private val loadPlaylistsWithSongs: Deferred> + get() = viewModelScope.async(IO) { + realRepository.playlistWithSongs() + } private val loadGenres: Deferred> get() = viewModelScope.async(IO) { @@ -80,7 +88,7 @@ class LibraryViewModel( Songs -> songs.value = loadSongs.await() Albums -> albums.value = loadAlbums.await() Artists -> artists.value = loadArtists.await() - HomeSections -> songs.value = loadSongs.await() + HomeSections -> home.value = loadHome.await() } } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/library/LibraryFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/library/LibraryFragment.kt index 162133d1..ad50073c 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/library/LibraryFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/library/LibraryFragment.kt @@ -10,7 +10,7 @@ import androidx.navigation.ui.NavigationUI import code.name.monkey.appthemehelper.common.ATHToolbarActivity.getToolbarBackgroundColor import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper import code.name.monkey.retromusic.R -import code.name.monkey.retromusic.db.PlaylistDatabase +import code.name.monkey.retromusic.db.RoomPlaylistRepository import code.name.monkey.retromusic.dialogs.AddToRetroPlaylist import code.name.monkey.retromusic.extensions.findNavController import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment @@ -63,8 +63,8 @@ class LibraryFragment : AbsMainActivityFragment(R.layout.fragment_library) { R.id.action_settings -> //CreateRetroPlaylist().show(childFragmentManager, "Dialog") lifecycleScope.launch { - val database = get() - AddToRetroPlaylist.getInstance(database.playlistDao().playlists()) + val playlistRepository = get() + AddToRetroPlaylist.getInstance(playlistRepository.playlists()) .show(childFragmentManager, "PlaylistDialog") } diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt index 22681715..6659fca1 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/PlaylistsFragment.kt @@ -19,7 +19,7 @@ class PlaylistsFragment : override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - libraryViewModel.playlisitsLiveData.observe(viewLifecycleOwner, Observer { + libraryViewModel.roomPlaylisitsLiveData.observe(viewLifecycleOwner, Observer { if (it.isNotEmpty()) adapter?.swapDataSet(it) else @@ -43,6 +43,7 @@ class PlaylistsFragment : ) } + companion object { fun newInstance(): PlaylistsFragment { return PlaylistsFragment() diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt b/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt index d035a72d..5ebdc374 100644 --- a/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/repository/Repository.kt @@ -16,6 +16,8 @@ package code.name.monkey.retromusic.repository import android.content.Context import code.name.monkey.retromusic.* +import code.name.monkey.retromusic.db.PlaylistWithSongs +import code.name.monkey.retromusic.db.RoomPlaylistRepository import code.name.monkey.retromusic.model.* import code.name.monkey.retromusic.model.smartplaylist.NotPlayedPlaylist import code.name.monkey.retromusic.network.LastFMService @@ -82,6 +84,10 @@ interface Repository { suspend fun homeSectionsFlow(): Flow>> + suspend fun playlist(playlistId: Int): Playlist + + suspend fun playlistWithSongs(): List + fun songsFlow(): Flow>> fun albumsFlow(): Flow>> @@ -92,7 +98,6 @@ interface Repository { fun genresFlow(): Flow>> - suspend fun playlist(playlistId: Int): Playlist } class RealRepository( @@ -105,7 +110,8 @@ class RealRepository( private val lastAddedRepository: LastAddedRepository, private val playlistRepository: PlaylistRepository, private val searchRepository: RealSearchRepository, - private val playedTracksRepository: TopPlayedRepository + private val playedTracksRepository: TopPlayedRepository, + private val roomPlaylistRepository: RoomPlaylistRepository ) : Repository { override suspend fun allAlbums(): List = albumRepository.albums() @@ -214,6 +220,9 @@ class RealRepository( override suspend fun playlist(playlistId: Int) = playlistRepository.playlist(playlistId) + override suspend fun playlistWithSongs(): List = + roomPlaylistRepository.playlistWithSongs() + override suspend fun suggestionsHome(): Home { val songs = NotPlayedPlaylist().songs().shuffled().takeIf { diff --git a/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.kt b/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.kt index 130cee69..e1dfd027 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.kt +++ b/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.kt @@ -13,6 +13,7 @@ import android.widget.Toast import androidx.core.content.FileProvider import androidx.fragment.app.FragmentActivity import code.name.monkey.retromusic.R +import code.name.monkey.retromusic.db.SongEntity import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue import code.name.monkey.retromusic.model.Artist import code.name.monkey.retromusic.model.Playlist @@ -189,6 +190,13 @@ object MusicUtil : KoinComponent { ) } + fun playlistInfoString( + context: Context, + songs: List + ): String { + return getSongCountString(context, songs.size) + } + fun getReadableDurationString(songDurationMillis: Long): String? { var minutes = songDurationMillis / 1000 / 60 val seconds = songDurationMillis / 1000 % 60