From 765172d69fffccf6ddfb21966fe163cc17a955e3 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Fri, 18 Sep 2020 16:31:55 +0530 Subject: [PATCH] Code refactor --- .../retromusic/adapter/album/AlbumAdapter.kt | 5 +- .../adapter/base/MediaEntryViewHolder.java | 6 +- .../song/OrderablePlaylistSongAdapter.kt | 1 + .../monkey/retromusic/db/SongExtension.kt | 38 ++++++++++ .../dialogs/CreatePlaylistDialog.kt | 1 + .../retromusic/fragments/LibraryViewModel.kt | 1 + .../fragments/albums/AlbumDetailsFragment.kt | 17 +++-- .../fragments/base/AbsPlayerFragment.kt | 1 + .../playlists/ImportPlaylistFragment.kt | 72 ------------------- .../fragments/songs/SongsFragment.kt | 1 - .../code/name/monkey/retromusic/model/Song.kt | 36 ---------- .../retromusic/repository/RoomRepository.kt | 2 +- .../res/layout/fragment_album_details.xml | 4 +- app/src/main/res/navigation/library_graph.xml | 6 +- 14 files changed, 64 insertions(+), 127 deletions(-) delete mode 100644 app/src/main/java/code/name/monkey/retromusic/fragments/playlists/ImportPlaylistFragment.kt diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumAdapter.kt index 9e9fc505..73c07145 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/album/AlbumAdapter.kt @@ -136,7 +136,7 @@ open class AlbumAdapter( private fun getSongList(albums: List): List { val songs = ArrayList() for (album in albums) { - songs.addAll(album.songs!!) + songs.addAll(album.songs) } return songs } @@ -170,8 +170,7 @@ open class AlbumAdapter( if (isInQuickSelectMode) { toggleChecked(layoutPosition) } else { - println(dataSet[layoutPosition].id) - image?.let { albumClickListener?.onAlbumClick(dataSet[layoutPosition].id, it) } + albumClickListener?.onAlbumClick(dataSet[layoutPosition].id, itemView) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java b/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java index ddf99c21..87460143 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/base/MediaEntryViewHolder.java @@ -113,6 +113,7 @@ public class MediaEntryViewHolder extends AbstractDraggableSwipeableItemViewHold itemView.setOnLongClickListener(this); } + @Nullable @Override public View getSwipeableContainerView() { return null; @@ -129,11 +130,12 @@ public class MediaEntryViewHolder extends AbstractDraggableSwipeableItemViewHold } public void setImageTransitionName(@NonNull String transitionName) { - if (imageContainerCard != null) { + itemView.setTransitionName(transitionName); + /* if (imageContainerCard != null) { imageContainerCard.setTransitionName(transitionName); } if (image != null) { image.setTransitionName(transitionName); - } + }*/ } } diff --git a/app/src/main/java/code/name/monkey/retromusic/adapter/song/OrderablePlaylistSongAdapter.kt b/app/src/main/java/code/name/monkey/retromusic/adapter/song/OrderablePlaylistSongAdapter.kt index 6407a626..a806c72a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/adapter/song/OrderablePlaylistSongAdapter.kt +++ b/app/src/main/java/code/name/monkey/retromusic/adapter/song/OrderablePlaylistSongAdapter.kt @@ -6,6 +6,7 @@ import androidx.fragment.app.FragmentActivity import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R.menu import code.name.monkey.retromusic.db.PlaylistEntity +import code.name.monkey.retromusic.db.toSongEntity import code.name.monkey.retromusic.db.toSongs import code.name.monkey.retromusic.dialogs.RemoveSongFromPlaylistDialog import code.name.monkey.retromusic.interfaces.CabHolder diff --git a/app/src/main/java/code/name/monkey/retromusic/db/SongExtension.kt b/app/src/main/java/code/name/monkey/retromusic/db/SongExtension.kt index fd7c116a..03666f0a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/SongExtension.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/SongExtension.kt @@ -14,6 +14,44 @@ fun List.toSongs(playlistId: Long): List { } } +fun Song.toHistoryEntity(timePlayed: Long): HistoryEntity { + return HistoryEntity( + id = id, + title = title, + trackNumber = trackNumber, + year = year, + duration = duration, + data = data, + dateModified = dateModified, + albumId = albumId, + albumName = albumName, + artistId = artistId, + artistName = artistName, + composer = composer, + albumArtist = albumArtist, + timePlayed = timePlayed + ) +} + +fun Song.toSongEntity(playListId: Long): SongEntity { + return SongEntity( + playlistCreatorId = playListId, + id = id, + title = title, + trackNumber = trackNumber, + year = year, + duration = duration, + data = data, + dateModified = dateModified, + albumId = albumId, + albumName = albumName, + artistId = artistId, + artistName = artistName, + composer = composer, + albumArtist = albumArtist + ) +} + fun SongEntity.toSong(): Song { return Song( id = id, 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 b1ceb033..d1e86e12 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 @@ -11,6 +11,7 @@ import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.EXTRA_SONG import code.name.monkey.retromusic.R import code.name.monkey.retromusic.db.PlaylistEntity +import code.name.monkey.retromusic.db.toSongEntity import code.name.monkey.retromusic.extensions.colorButtons import code.name.monkey.retromusic.extensions.extra import code.name.monkey.retromusic.extensions.materialDialog 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 4a890613..94de6a91 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 @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope 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.toSongEntity import code.name.monkey.retromusic.fragments.ReloadType.* import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.interfaces.MusicServiceEventListener diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumDetailsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumDetailsFragment.kt index 0001ee8c..15667a40 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumDetailsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumDetailsFragment.kt @@ -46,6 +46,7 @@ import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.RetroUtil import code.name.monkey.retromusic.util.color.MediaNotificationProcessor import com.bumptech.glide.Glide +import com.google.android.material.transition.MaterialContainerTransform import kotlinx.android.synthetic.main.fragment_album_content.* import kotlinx.android.synthetic.main.fragment_album_details.* import kotlinx.coroutines.Dispatchers @@ -70,8 +71,14 @@ class AlbumDetailsFragment : AbsMainActivityFragment(R.layout.fragment_album_det private val savedSortOrder: String get() = PreferenceUtil.albumDetailSongSortOrder - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + sharedElementEnterTransition = MaterialContainerTransform().apply { + duration = 1000L + } + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) setHasOptionsMenu(true) mainActivity.hideBottomBarVisibility(false) mainActivity.addMusicServiceEventListener(detailsViewModel) @@ -91,11 +98,11 @@ class AlbumDetailsFragment : AbsMainActivityFragment(R.layout.fragment_album_det bundleOf(EXTRA_ARTIST_ID to album.artistId) ) } - playAction.setOnClickListener { MusicPlayerRemote.openQueue(album.songs!!, 0, true) } + playAction.setOnClickListener { MusicPlayerRemote.openQueue(album.songs, 0, true) } shuffleAction.setOnClickListener { MusicPlayerRemote.openAndShuffleQueue( - album.songs!!, + album.songs, true ) } @@ -134,7 +141,7 @@ class AlbumDetailsFragment : AbsMainActivityFragment(R.layout.fragment_album_det } private fun showAlbum(album: Album) { - if (album.songs!!.isEmpty()) { + if (album.songs.isEmpty()) { return } this.album = album diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsPlayerFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsPlayerFragment.kt index 59875b4f..e2b499a0 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsPlayerFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/base/AbsPlayerFragment.kt @@ -23,6 +23,7 @@ import code.name.monkey.retromusic.activities.tageditor.AbsTagEditorActivity import code.name.monkey.retromusic.activities.tageditor.SongTagEditorActivity import code.name.monkey.retromusic.db.PlaylistEntity import code.name.monkey.retromusic.db.SongEntity +import code.name.monkey.retromusic.db.toSongEntity import code.name.monkey.retromusic.dialogs.* import code.name.monkey.retromusic.extensions.hide import code.name.monkey.retromusic.extensions.whichFragment diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/ImportPlaylistFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/ImportPlaylistFragment.kt deleted file mode 100644 index f2dc0792..00000000 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/playlists/ImportPlaylistFragment.kt +++ /dev/null @@ -1,72 +0,0 @@ -package code.name.monkey.retromusic.fragments.playlists - -import android.os.Bundle -import android.view.Menu -import android.view.MenuInflater -import android.view.View -import android.widget.Toast -import androidx.lifecycle.Observer -import androidx.lifecycle.lifecycleScope -import androidx.recyclerview.widget.LinearLayoutManager -import code.name.monkey.retromusic.R -import code.name.monkey.retromusic.adapter.playlist.LegacyPlaylistAdapter -import code.name.monkey.retromusic.db.PlaylistEntity -import code.name.monkey.retromusic.fragments.ReloadType.Playlists -import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment -import code.name.monkey.retromusic.model.Playlist -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.launch - -class ImportPlaylistFragment : - AbsRecyclerViewFragment(), - LegacyPlaylistAdapter.PlaylistClickListener { - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - libraryViewModel.getLegacyPlaylist().observe(viewLifecycleOwner, Observer { - if (it.isNotEmpty()) - adapter?.swapData(it) - else - adapter?.swapData(listOf()) - }) - } - - override fun createLayoutManager(): LinearLayoutManager { - return LinearLayoutManager(requireContext()) - } - - override fun createAdapter(): LegacyPlaylistAdapter { - return LegacyPlaylistAdapter( - requireActivity(), - ArrayList(), - R.layout.item_list_no_image, - this - ) - } - - override fun onPlaylistClick(playlist: Playlist) { - Toast.makeText(requireContext(), "Importing ${playlist.name}", Toast.LENGTH_LONG).show() - lifecycleScope.launch(IO) { - if (playlist.name.isNotEmpty()) { - if (libraryViewModel.checkPlaylistExists(playlist.name).isEmpty()) { - val playlistId: Long = - libraryViewModel.createPlaylist(PlaylistEntity(playlistName = playlist.name)) - libraryViewModel.insertSongs(playlist.getSongs().map { - it.toSongEntity(playlistId) - }) - libraryViewModel.forceReload(Playlists) - } else { - Toast.makeText(requireContext(), "Playlist exists", Toast.LENGTH_SHORT) - .show() - } - } - } - } - - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - menu.removeItem(R.id.action_grid_size) - menu.removeItem(R.id.action_layout_type) - menu.removeItem(R.id.action_sort_order) - super.onCreateOptionsMenu(menu, inflater) - } -} \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt index 533aa0f7..33a899de 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/songs/SongsFragment.kt @@ -16,7 +16,6 @@ import code.name.monkey.retromusic.util.RetroUtil class SongsFragment : AbsRecyclerViewCustomGridSizeFragment() { - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) libraryViewModel.getSongs().observe(viewLifecycleOwner, Observer { diff --git a/app/src/main/java/code/name/monkey/retromusic/model/Song.kt b/app/src/main/java/code/name/monkey/retromusic/model/Song.kt index 35bf1bfa..8c1481e1 100644 --- a/app/src/main/java/code/name/monkey/retromusic/model/Song.kt +++ b/app/src/main/java/code/name/monkey/retromusic/model/Song.kt @@ -35,43 +35,7 @@ open class Song( open val composer: String?, open val albumArtist: String? ) : Parcelable { - fun toHistoryEntity(timePlayed: Long): HistoryEntity { - return HistoryEntity( - id = id, - title = title, - trackNumber = trackNumber, - year = year, - duration = duration, - data = data, - dateModified = dateModified, - albumId = albumId, - albumName = albumName, - artistId = artistId, - artistName = artistName, - composer = composer, - albumArtist = albumArtist, - timePlayed = timePlayed - ) - } - fun toSongEntity(playListId: Long): SongEntity { - return SongEntity( - playlistCreatorId = playListId, - id = id, - title = title, - trackNumber = trackNumber, - year = year, - duration = duration, - data = data, - dateModified = dateModified, - albumId = albumId, - albumName = albumName, - artistId = artistId, - artistName = artistName, - composer = composer, - albumArtist = albumArtist - ) - } // need to override manually because is open and cannot be a data class override fun equals(other: Any?): Boolean { diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt b/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt index f2e86636..4a6eb42d 100644 --- a/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt +++ b/app/src/main/java/code/name/monkey/retromusic/repository/RoomRepository.kt @@ -70,7 +70,7 @@ class RealRoomRepository( } - override fun getSongs(playlistEntity: PlaylistEntity): LiveData> = + override fun getSongs(playlistEntity: PlaylistEntity): LiveData> = playlistDao.songsFromPlaylist(playlistEntity.playListId) override suspend fun deletePlaylistEntities(playlistEntities: List) = diff --git a/app/src/main/res/layout/fragment_album_details.xml b/app/src/main/res/layout/fragment_album_details.xml index 9aadedfc..2ad7e45b 100644 --- a/app/src/main/res/layout/fragment_album_details.xml +++ b/app/src/main/res/layout/fragment_album_details.xml @@ -3,7 +3,8 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="match_parent" + android:transitionName="@string/transition_album_art"> - + \ No newline at end of file