From 2854b33d56e73599fed9f254b2a1176274af72b1 Mon Sep 17 00:00:00 2001 From: Hemanth S Date: Sun, 30 Aug 2020 00:23:15 +0530 Subject: [PATCH 1/2] Refactor code Moving business logic from UI to ViewModel class --- .../retromusic/activities/MainActivity.kt | 109 +++++++++--------- .../base/AbsSlidingMusicPanelActivity.kt | 3 +- .../retromusic/dialogs/AddToRetroPlaylist.kt | 5 +- .../retromusic/dialogs/DeleteRetroPlaylist.kt | 15 +-- .../dialogs/RemoveSongFromPlaylistDialog.kt | 13 +-- .../dialogs/RenameRetroPlaylistDialog.kt | 13 +-- .../monkey/retromusic/extensions/ColorExt.kt | 12 ++ .../retromusic/fragments/LibraryViewModel.kt | 34 ++++++ .../fragments/MiniPlayerFragment.kt | 12 +- .../fragments/base/AbsPlayerFragment.kt | 61 +++++----- .../retromusic/fragments/home/HomeFragment.kt | 22 +--- .../player/gradient/GradientPlayerFragment.kt | 41 +------ .../repository/PlaylistSongsLoader.kt | 5 +- .../res/layout/fragment_gradient_player.xml | 8 +- 14 files changed, 155 insertions(+), 198 deletions(-) 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 2f0df045..c772c320 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,21 +10,15 @@ import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.* import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity import code.name.monkey.retromusic.extensions.findNavController -import code.name.monkey.retromusic.helper.MusicPlayerRemote.openAndShuffleQueue -import code.name.monkey.retromusic.helper.MusicPlayerRemote.openQueue -import code.name.monkey.retromusic.helper.MusicPlayerRemote.playFromUri -import code.name.monkey.retromusic.helper.MusicPlayerRemote.shuffleMode +import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.SearchQueryHelper.getSongs import code.name.monkey.retromusic.model.Song -import code.name.monkey.retromusic.repository.PlaylistSongsLoader.getPlaylistSongList -import code.name.monkey.retromusic.repository.Repository +import code.name.monkey.retromusic.repository.PlaylistSongsLoader import code.name.monkey.retromusic.service.MusicService import code.name.monkey.retromusic.util.AppRater.appLaunched import code.name.monkey.retromusic.util.PreferenceUtil -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject -import java.util.* class MainActivity : AbsSlidingMusicPanelActivity(), OnSharedPreferenceChangeListener { companion object { @@ -33,8 +27,6 @@ class MainActivity : AbsSlidingMusicPanelActivity(), OnSharedPreferenceChangeLis const val APP_UPDATE_REQUEST_CODE = 9002 } - private val repository by inject() - private var blockRequestPermissions = false override fun createContentView(): View { @@ -95,61 +87,68 @@ class MainActivity : AbsSlidingMusicPanelActivity(), OnSharedPreferenceChangeLis override fun onServiceConnected() { super.onServiceConnected() - handlePlaybackIntent(intent) - } - - private fun handlePlaybackIntent(intent: Intent?) { if (intent == null) { return } - val uri = intent.data - val mimeType = intent.type - var handled = false - if (intent.action != null && (intent.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH) - ) { - val songs: List = - getSongs(intent.extras!!) - if (shuffleMode == MusicService.SHUFFLE_MODE_SHUFFLE) { - openAndShuffleQueue(songs, true) - } else { - openQueue(songs, 0, true) - } - handled = true - } - if (uri != null && uri.toString().isNotEmpty()) { - playFromUri(uri) - handled = true - } else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) { - val id = parseIdFromIntent(intent, "playlistId", "playlist").toInt() - if (id >= 0) { - val position = intent.getIntExtra("position", 0) - val songs: List = - ArrayList(getPlaylistSongList(this, id)) - openQueue(songs, position, true) + handlePlaybackIntent(intent) + } + + private fun handlePlaybackIntent(intent: Intent) { + lifecycleScope.launch(IO) { + val uri = intent.data + val mimeType = intent.type + var handled = false + if (intent.action != null && + intent.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH + ) { + val songs: List = getSongs(intent.extras!!) + if (MusicPlayerRemote.shuffleMode == MusicService.SHUFFLE_MODE_SHUFFLE) { + MusicPlayerRemote.openAndShuffleQueue(songs, true) + } else { + MusicPlayerRemote.openQueue(songs, 0, true) + } handled = true } - } else if (MediaStore.Audio.Albums.CONTENT_TYPE == mimeType) { - val id = parseIdFromIntent(intent, "albumId", "album").toInt() - if (id >= 0) { - lifecycleScope.launch(Dispatchers.Main) { + if (uri != null && uri.toString().isNotEmpty()) { + MusicPlayerRemote.playFromUri(uri) + handled = true + } else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) { + val id = parseIdFromIntent(intent, "playlistId", "playlist").toInt() + if (id >= 0) { val position = intent.getIntExtra("position", 0) - openQueue(repository.albumById(id).songs!!, position, true) + val songs: List = + PlaylistSongsLoader.getPlaylistSongList(this@MainActivity, id) + MusicPlayerRemote.openQueue(songs, position, true) + handled = true + } + } else if (MediaStore.Audio.Albums.CONTENT_TYPE == mimeType) { + val id = parseIdFromIntent(intent, "albumId", "album").toInt() + if (id >= 0) { + val position = intent.getIntExtra("position", 0) + MusicPlayerRemote.openQueue( + libraryViewModel.albumById(id).songs!!, + position, + true + ) + handled = true + } + } else if (MediaStore.Audio.Artists.CONTENT_TYPE == mimeType) { + val id = parseIdFromIntent(intent, "artistId", "artist").toInt() + if (id >= 0) { + val position = intent.getIntExtra("position", 0) + MusicPlayerRemote.openQueue( + libraryViewModel.artistById(id).songs, + position, + true + ) handled = true } } - } else if (MediaStore.Audio.Artists.CONTENT_TYPE == mimeType) { - val id = parseIdFromIntent(intent, "artistId", "artist").toInt() - if (id >= 0) { - lifecycleScope.launch { - val position = intent.getIntExtra("position", 0) - openQueue(repository.artistById(id).songs, position, true) - handled = true - } + if (handled) { + setIntent(Intent()) } } - if (handled) { - setIntent(Intent()) - } + } private fun parseIdFromIntent( diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsSlidingMusicPanelActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsSlidingMusicPanelActivity.kt index 57c27f92..d81485a0 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsSlidingMusicPanelActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsSlidingMusicPanelActivity.kt @@ -33,7 +33,7 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() { val TAG: String = AbsSlidingMusicPanelActivity::class.java.simpleName } - private val libraryViewModel by viewModel() + protected val libraryViewModel by viewModel() private lateinit var behavior: RetroBottomSheetBehavior private var miniPlayerFragment: MiniPlayerFragment? = null private var cps: NowPlayingScreen? = null @@ -79,6 +79,7 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() { libraryViewModel.paletteColorLiveData.observe(this, Observer { this.paletteColor = it + miniPlayerFragment?.updateProgressBar(it) onPaletteColorChanged() }) } 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 a4275ba1..e00ec73b 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 @@ -15,15 +15,12 @@ import code.name.monkey.retromusic.extensions.materialDialog import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.ReloadType.Playlists import code.name.monkey.retromusic.model.Song -import code.name.monkey.retromusic.repository.RealRepository import com.google.android.material.bottomsheet.BottomSheetDialogFragment import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.sharedViewModel class AddToRetroPlaylist : BottomSheetDialogFragment() { - private val repository by inject() private val libraryViewModel by sharedViewModel() companion object { @@ -58,7 +55,7 @@ class AddToRetroPlaylist : BottomSheetDialogFragment() { } else { lifecycleScope.launch(Dispatchers.IO) { val songEntities = songs.toSongEntity(playlistEntities[which - 1]) - repository.insertSongs(songEntities) + libraryViewModel.insertSongs(songEntities) libraryViewModel.forceReload(Playlists) } } diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/DeleteRetroPlaylist.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/DeleteRetroPlaylist.kt index 3b024044..1888d287 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/DeleteRetroPlaylist.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/DeleteRetroPlaylist.kt @@ -5,7 +5,6 @@ import android.os.Bundle import androidx.core.os.bundleOf import androidx.core.text.HtmlCompat import androidx.fragment.app.DialogFragment -import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.EXTRA_PLAYLIST import code.name.monkey.retromusic.R import code.name.monkey.retromusic.db.PlaylistEntity @@ -14,14 +13,10 @@ import code.name.monkey.retromusic.extensions.extraNotNull import code.name.monkey.retromusic.extensions.materialDialog import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.ReloadType -import code.name.monkey.retromusic.repository.Repository -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.sharedViewModel class DeleteRetroPlaylist : DialogFragment() { - private val repository by inject() + private val libraryViewModel by sharedViewModel() companion object { @@ -63,11 +58,9 @@ class DeleteRetroPlaylist : DialogFragment() { .setMessage(message) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(R.string.action_delete) { _, _ -> - lifecycleScope.launch(Dispatchers.IO) { - repository.deleteSongsFromPlaylist(playlists) - repository.deleteRoomPlaylist(playlists) - libraryViewModel.forceReload(ReloadType.Playlists) - } + libraryViewModel.deleteSongsFromPlaylist(playlists) + libraryViewModel.deleteRoomPlaylist(playlists) + libraryViewModel.forceReload(ReloadType.Playlists) } .create() .colorButtons() diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/RemoveSongFromPlaylistDialog.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/RemoveSongFromPlaylistDialog.kt index 9bf9cfdf..b52e9fef 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/RemoveSongFromPlaylistDialog.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/RemoveSongFromPlaylistDialog.kt @@ -5,7 +5,6 @@ import android.os.Bundle import androidx.core.os.bundleOf import androidx.core.text.HtmlCompat import androidx.fragment.app.DialogFragment -import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.EXTRA_SONG import code.name.monkey.retromusic.R import code.name.monkey.retromusic.db.SongEntity @@ -14,14 +13,9 @@ import code.name.monkey.retromusic.extensions.extraNotNull import code.name.monkey.retromusic.extensions.materialDialog import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.ReloadType.Playlists -import code.name.monkey.retromusic.repository.Repository -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.sharedViewModel class RemoveSongFromPlaylistDialog : DialogFragment() { - private val repository by inject() private val libraryViewModel by sharedViewModel() companion object { @@ -65,11 +59,8 @@ class RemoveSongFromPlaylistDialog : DialogFragment() { return materialDialog(pair.first) .setMessage(pair.second) .setPositiveButton(R.string.remove_action) { _, _ -> - lifecycleScope.launch(Dispatchers.IO) { - //repository.removeSongFromPlaylist(songs) - repository.deleteSongsInPlaylist(songs) - libraryViewModel.forceReload(Playlists) - } + libraryViewModel.deleteSongsInPlaylist(songs) + libraryViewModel.forceReload(Playlists) } .setNegativeButton(android.R.string.cancel, null) .create() diff --git a/app/src/main/java/code/name/monkey/retromusic/dialogs/RenameRetroPlaylistDialog.kt b/app/src/main/java/code/name/monkey/retromusic/dialogs/RenameRetroPlaylistDialog.kt index a12da522..9c2601ad 100644 --- a/app/src/main/java/code/name/monkey/retromusic/dialogs/RenameRetroPlaylistDialog.kt +++ b/app/src/main/java/code/name/monkey/retromusic/dialogs/RenameRetroPlaylistDialog.kt @@ -5,7 +5,6 @@ import android.os.Bundle import android.view.LayoutInflater import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment -import androidx.lifecycle.lifecycleScope import code.name.monkey.retromusic.EXTRA_PLAYLIST_ID import code.name.monkey.retromusic.R import code.name.monkey.retromusic.db.PlaylistEntity @@ -15,16 +14,12 @@ import code.name.monkey.retromusic.extensions.extraNotNull import code.name.monkey.retromusic.extensions.materialDialog import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.ReloadType -import code.name.monkey.retromusic.repository.Repository import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.sharedViewModel class RenameRetroPlaylistDialog : DialogFragment() { - private val repository by inject() + private val libraryViewModel by sharedViewModel() companion object { @@ -50,10 +45,8 @@ class RenameRetroPlaylistDialog : DialogFragment() { .setPositiveButton(R.string.action_rename) { _, _ -> val name = inputEditText.text.toString() if (name.isNotEmpty()) { - lifecycleScope.launch(Dispatchers.IO) { - repository.renameRoomPlaylist(playlistEntity.playListId, name) - libraryViewModel.forceReload(ReloadType.Playlists) - } + libraryViewModel.renameRoomPlaylist(playlistEntity.playListId, name) + libraryViewModel.forceReload(ReloadType.Playlists) } else { nameContainer.error = "Playlist name should'nt be empty" } diff --git a/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt b/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt index 2059d929..fc08d6e7 100644 --- a/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt +++ b/app/src/main/java/code/name/monkey/retromusic/extensions/ColorExt.kt @@ -35,6 +35,7 @@ import code.name.monkey.retromusic.R import com.google.android.material.button.MaterialButton import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton +import com.google.android.material.progressindicator.ProgressIndicator import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout @@ -145,6 +146,17 @@ fun TextInputLayout.accentColor() { isHintAnimationEnabled = true } +fun ProgressIndicator.accentColor() { + val accentColor = ThemeStore.accentColor(context) + indicatorColors = intArrayOf(accentColor) + trackColor = ColorUtil.withAlpha(accentColor, 0.2f) +} + +fun ProgressIndicator.applyColor(color: Int) { + indicatorColors = intArrayOf(color) + trackColor = ColorUtil.withAlpha(color, 0.2f) +} + fun TextInputEditText.accentColor() { } 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 cefe5c70..e531b510 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,7 +4,9 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel 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.toPlayCount import code.name.monkey.retromusic.fragments.ReloadType.* import code.name.monkey.retromusic.helper.MusicPlayerRemote @@ -151,6 +153,38 @@ class LibraryViewModel( println("onShuffleModeChanged") } + fun shuffleSongs() = viewModelScope.launch(IO) { + val songs = repository.allSongs() + MusicPlayerRemote.openAndShuffleQueue( + songs, + true + ) + } + + fun renameRoomPlaylist(playListId: Int, name: String) = viewModelScope.launch(IO) { + repository.renameRoomPlaylist(playListId, name) + } + + fun deleteSongsInPlaylist(songs: List) = viewModelScope.launch(IO) { + repository.deleteSongsInPlaylist(songs) + } + + fun deleteSongsFromPlaylist(playlists: List) = viewModelScope.launch(IO) { + repository.deleteSongsFromPlaylist(playlists) + } + + fun deleteRoomPlaylist(playlists: List) = viewModelScope.launch(IO) { + repository.deleteRoomPlaylist(playlists) + } + + suspend fun albumById(id: Int) = repository.albumById(id) + suspend fun artistById(id: Int) = repository.artistById(id) + suspend fun favoritePlaylist() = repository.favoritePlaylist() + suspend fun isFavoriteSong(song: SongEntity) = repository.isFavoriteSong(song) + suspend fun insertSongs(songs: List) = repository.insertSongs(songs) + suspend fun removeSongFromPlaylist(songEntity: SongEntity) = + repository.removeSongFromPlaylist(songEntity) + } enum class ReloadType { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/MiniPlayerFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/MiniPlayerFragment.kt index 9f05dc43..9a16d656 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/MiniPlayerFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/MiniPlayerFragment.kt @@ -11,18 +11,14 @@ import android.view.GestureDetector import android.view.MotionEvent import android.view.View import android.view.animation.DecelerateInterpolator -import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.retromusic.R -import code.name.monkey.retromusic.extensions.show -import code.name.monkey.retromusic.extensions.textColorPrimary -import code.name.monkey.retromusic.extensions.textColorSecondary +import code.name.monkey.retromusic.extensions.* import code.name.monkey.retromusic.fragments.base.AbsMusicServiceFragment import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicProgressViewUpdateHelper import code.name.monkey.retromusic.helper.PlayPauseButtonOnClickHandler import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.RetroUtil -import code.name.monkey.retromusic.util.ViewUtil import kotlinx.android.synthetic.main.fragment_mini_player.* import kotlin.math.abs @@ -67,7 +63,7 @@ open class MiniPlayerFragment : AbsMusicServiceFragment(R.layout.fragment_mini_p private fun setUpMiniPlayer() { setUpPlayPauseButton() - ViewUtil.setProgressDrawable(progressBar, ThemeStore.accentColor(requireContext())) + progressBar.accentColor() } private fun setUpPlayPauseButton() { @@ -129,6 +125,10 @@ open class MiniPlayerFragment : AbsMusicServiceFragment(R.layout.fragment_mini_p } } + fun updateProgressBar(paletteColor: Int) { + progressBar.applyColor(paletteColor) + } + class FlingPlayBackController(context: Context) : View.OnTouchListener { private var flingPlayBackController: GestureDetector 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 975443f7..1aa7dc14 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 @@ -25,7 +25,7 @@ import code.name.monkey.retromusic.dialogs.* import code.name.monkey.retromusic.extensions.hide import code.name.monkey.retromusic.extensions.whichFragment import code.name.monkey.retromusic.fragments.LibraryViewModel -import code.name.monkey.retromusic.fragments.ReloadType.Playlists +import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.player.PlayerAlbumCoverFragment import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.interfaces.PaletteColorHolder @@ -35,13 +35,11 @@ import code.name.monkey.retromusic.repository.RealRepository import code.name.monkey.retromusic.service.MusicService import code.name.monkey.retromusic.util.* import kotlinx.android.synthetic.main.shadow_statusbar_toolbar.* -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.koin.android.ext.android.get -import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.sharedViewModel import java.io.FileNotFoundException @@ -75,7 +73,7 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme R.id.action_add_to_playlist -> { lifecycleScope.launch(IO) { val playlists = get().roomPlaylists() - withContext(Dispatchers.Main) { + withContext(Main) { AddToRetroPlaylist.create(playlists, song) .show(childFragmentManager, "ADD_PLAYLIST") } @@ -161,20 +159,6 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme return false } - protected open fun toggleFavorite(song: Song) { - lifecycleScope.launch(IO) { - val playlist: PlaylistEntity = repository.favoritePlaylist().first() - val songEntity = song.toSongEntity(playlist.playListId) - val isFavorite = repository.isFavoriteSong(songEntity).isNotEmpty() - if (isFavorite) { - repository.removeSongFromPlaylist(songEntity) - } else { - repository.insertSongs(listOf(song.toSongEntity(playlist.playListId))) - libraryViewModel.forceReload(Playlists) - } - requireContext().sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED)) - } - } abstract fun playerToolbar(): Toolbar? @@ -196,15 +180,35 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme updateLyrics() } + protected open fun toggleFavorite(song: Song) { + lifecycleScope.launch(IO) { + val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist().first() + val songEntity = song.toSongEntity(playlist.playListId) + val isFavorite = libraryViewModel.isFavoriteSong(songEntity).isNotEmpty() + if (isFavorite) { + libraryViewModel.removeSongFromPlaylist(songEntity) + } else { + libraryViewModel.insertSongs(listOf(song.toSongEntity(playlist.playListId))) + libraryViewModel.forceReload(ReloadType.Playlists) + } + requireContext().sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED)) + } + } + fun updateIsFavorite() { lifecycleScope.launch(IO) { - val playlist: PlaylistEntity = repository.favoritePlaylist().first() + val playlist: PlaylistEntity = libraryViewModel.favoritePlaylist().first() val song = MusicPlayerRemote.currentSong.toSongEntity(playlist.playListId) - val isFavorite = repository.isFavoriteSong(song).isNotEmpty() - withContext(Dispatchers.Main) { - val icon = if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border + val isFavorite = libraryViewModel.isFavoriteSong(song).isNotEmpty() + withContext(Main) { + val icon = + if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border val drawable = - RetroUtil.getTintedVectorDrawable(requireContext(), icon, toolbarIconColor()) + RetroUtil.getTintedVectorDrawable( + requireContext(), + icon, + toolbarIconColor() + ) if (playerToolbar() != null) { playerToolbar()?.menu?.findItem(R.id.action_toggle_favorite) ?.setIcon(drawable)?.title = @@ -241,17 +245,6 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMainActivityFragme open fun setLyrics(l: Lyrics?) { } - private val repository by inject() - - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) - lifecycleScope.launch(IO) { - if (repository.checkPlaylistExists(getString(R.string.favorites)).isEmpty()) { - repository.createPlaylist(PlaylistEntity(getString(R.string.favorites))) - } - } - } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) if (PreferenceUtil.isFullScreenMode && diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt index 47d0918f..20932004 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/home/HomeFragment.kt @@ -16,11 +16,9 @@ package code.name.monkey.retromusic.fragments.home import android.app.ActivityOptions import android.os.Bundle -import android.util.DisplayMetrics import android.view.View import androidx.core.os.bundleOf import androidx.lifecycle.Observer -import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import code.name.monkey.retromusic.HISTORY_PLAYLIST import code.name.monkey.retromusic.LAST_ADDED_PLAYLIST @@ -32,32 +30,19 @@ import code.name.monkey.retromusic.fragments.LibraryViewModel import code.name.monkey.retromusic.fragments.base.AbsMainActivityFragment import code.name.monkey.retromusic.glide.ProfileBannerGlideRequest import code.name.monkey.retromusic.glide.UserProfileGlideRequest -import code.name.monkey.retromusic.helper.MusicPlayerRemote -import code.name.monkey.retromusic.repository.Repository import code.name.monkey.retromusic.util.NavigationUtil import code.name.monkey.retromusic.util.PreferenceUtil import com.bumptech.glide.Glide import kotlinx.android.synthetic.main.abs_playlists.* import kotlinx.android.synthetic.main.fragment_banner_home.* import kotlinx.android.synthetic.main.home_content.* -import kotlinx.coroutines.launch -import org.koin.android.ext.android.inject import org.koin.androidx.viewmodel.ext.android.sharedViewModel class HomeFragment : AbsMainActivityFragment(if (PreferenceUtil.isHomeBanner) R.layout.fragment_banner_home else R.layout.fragment_home) { - private val repository by inject() private val libraryViewModel: LibraryViewModel by sharedViewModel() - private val displayMetrics: DisplayMetrics - get() { - val display = mainActivity.windowManager.defaultDisplay - val metrics = DisplayMetrics() - display.getMetrics(metrics) - return metrics - } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setStatusBarColorAuto(view) @@ -85,12 +70,7 @@ class HomeFragment : } actionShuffle.setOnClickListener { - lifecycleScope.launch { - MusicPlayerRemote.openAndShuffleQueue( - repository.allSongs(), - true - ) - } + libraryViewModel.shuffleSongs() } history.setOnClickListener { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/player/gradient/GradientPlayerFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/player/gradient/GradientPlayerFragment.kt index 41d52255..0eacbb9a 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/player/gradient/GradientPlayerFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/player/gradient/GradientPlayerFragment.kt @@ -5,7 +5,6 @@ import android.annotation.SuppressLint import android.content.res.ColorStateList import android.graphics.Color import android.graphics.PorterDuff -import android.os.AsyncTask import android.os.Bundle import android.view.View import android.view.animation.LinearInterpolator @@ -18,7 +17,6 @@ import androidx.core.view.ViewCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import code.name.monkey.appthemehelper.util.ColorUtil -import code.name.monkey.appthemehelper.util.TintHelper import code.name.monkey.retromusic.R import code.name.monkey.retromusic.RetroBottomSheetBehavior import code.name.monkey.retromusic.adapter.song.PlayingQueueAdapter @@ -61,7 +59,6 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play private var recyclerViewSwipeManager: RecyclerViewSwipeManager? = null private var recyclerViewTouchActionGuardManager: RecyclerViewTouchActionGuardManager? = null private var playingQueueAdapter: PlayingQueueAdapter? = null - private var updateIsFavoriteTask: AsyncTask<*, *, *>? = null private lateinit var linearLayoutManager: LinearLayoutManager private val bottomSheetCallbackList = object : BottomSheetBehavior.BottomSheetCallback() { @@ -218,9 +215,8 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play override fun toggleFavorite(song: Song) { super.toggleFavorite(song) - MusicUtil.toggleFavorite(requireContext(), song) if (song.id == MusicPlayerRemote.currentSong.id) { - updateFavorite() + updateIsFavorite() } } @@ -268,6 +264,7 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play override fun onQueueChanged() { super.onQueueChanged() updateLabel() + playingQueueAdapter?.swapDataSet(MusicPlayerRemote.playingQueue) } private fun updateSong() { @@ -366,7 +363,7 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play } else { val title = MusicPlayerRemote.playingQueue[MusicPlayerRemote.position + 1].title nextSong.apply { - text = "Next: $title" + text = title show() } } @@ -472,36 +469,4 @@ class GradientPlayerFragment : AbsPlayerFragment(R.layout.fragment_gradient_play songTotalTime.text = MusicUtil.getReadableDurationString(total.toLong()) songCurrentProgress.text = MusicUtil.getReadableDurationString(progress.toLong()) } - - @SuppressLint("StaticFieldLeak") - private fun updateFavorite() { - if (updateIsFavoriteTask != null) { - updateIsFavoriteTask?.cancel(false) - } - updateIsFavoriteTask = - object : AsyncTask() { - override fun doInBackground(vararg params: Song): Boolean? { - val activity = activity - return if (activity != null) { - MusicUtil.isFavorite(requireActivity(), params[0]) - } else { - cancel(false) - null - } - } - - override fun onPostExecute(isFavorite: Boolean?) { - val activity = activity - if (activity != null) { - val res = if (isFavorite!!) - R.drawable.ic_favorite - else - R.drawable.ic_favorite_border - - val drawable = TintHelper.createTintedDrawable(activity, res, Color.WHITE) - songFavourite?.setImageDrawable(drawable) - } - } - }.execute(MusicPlayerRemote.currentSong) - } } \ No newline at end of file diff --git a/app/src/main/java/code/name/monkey/retromusic/repository/PlaylistSongsLoader.kt b/app/src/main/java/code/name/monkey/retromusic/repository/PlaylistSongsLoader.kt index 383785b8..96f86d37 100644 --- a/app/src/main/java/code/name/monkey/retromusic/repository/PlaylistSongsLoader.kt +++ b/app/src/main/java/code/name/monkey/retromusic/repository/PlaylistSongsLoader.kt @@ -23,7 +23,6 @@ import code.name.monkey.retromusic.model.AbsCustomPlaylist import code.name.monkey.retromusic.model.Playlist import code.name.monkey.retromusic.model.PlaylistSong import code.name.monkey.retromusic.model.Song -import java.util.* /** * Created by hemanths on 16/08/17. @@ -43,8 +42,8 @@ object PlaylistSongsLoader { } @JvmStatic - fun getPlaylistSongList(context: Context, playlistId: Int): ArrayList { - val songs = arrayListOf() + fun getPlaylistSongList(context: Context, playlistId: Int): List { + val songs = mutableListOf() val cursor = makePlaylistSongCursor( context, diff --git a/app/src/main/res/layout/fragment_gradient_player.xml b/app/src/main/res/layout/fragment_gradient_player.xml index 2aa519ef..6c6a3bfc 100644 --- a/app/src/main/res/layout/fragment_gradient_player.xml +++ b/app/src/main/res/layout/fragment_gradient_player.xml @@ -12,7 +12,7 @@ android:layout_height="match_parent" android:paddingBottom="48dp"> - + app:layout_constraintWidth_default="spread" + tools:background="@color/md_red_500"> + app:layout_constraintTop_toTopOf="@id/queueIcon" /> Date: Sun, 30 Aug 2020 22:23:09 +0530 Subject: [PATCH 2/2] Remove MainActivityFragmentCallbacks --- .../code/name/monkey/retromusic/db/RetroDatabase.kt | 2 +- .../retromusic/fragments/albums/AlbumsFragment.kt | 3 +-- .../retromusic/fragments/artists/ArtistsFragment.kt | 11 ++--------- .../retromusic/fragments/genres/GenresFragment.kt | 8 +------- .../fragments/playlists/PlaylistsFragment.kt | 9 +-------- .../fragments/queue/PlayingQueueFragment.kt | 9 +-------- .../retromusic/fragments/songs/SongsFragment.kt | 10 +--------- 7 files changed, 8 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/code/name/monkey/retromusic/db/RetroDatabase.kt b/app/src/main/java/code/name/monkey/retromusic/db/RetroDatabase.kt index 713fe635..8ef2f0d2 100644 --- a/app/src/main/java/code/name/monkey/retromusic/db/RetroDatabase.kt +++ b/app/src/main/java/code/name/monkey/retromusic/db/RetroDatabase.kt @@ -5,7 +5,7 @@ import androidx.room.RoomDatabase @Database( entities = [PlaylistEntity::class, SongEntity::class, HistoryEntity::class, PlayCountEntity::class, BlackListStoreEntity::class], - version = 18, + version = 20, exportSchema = false ) abstract class RetroDatabase : RoomDatabase() { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt index d9f383ec..5b4a30b6 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/albums/AlbumsFragment.kt @@ -14,8 +14,7 @@ import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment import code.name.monkey.retromusic.util.PreferenceUtil -class AlbumsFragment : - AbsRecyclerViewCustomGridSizeFragment(), +class AlbumsFragment : AbsRecyclerViewCustomGridSizeFragment(), AlbumClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt index 6f2d91a1..30e188e6 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/artists/ArtistsFragment.kt @@ -12,17 +12,10 @@ import code.name.monkey.retromusic.adapter.artist.ArtistAdapter import code.name.monkey.retromusic.extensions.findActivityNavController import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment -import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import code.name.monkey.retromusic.util.PreferenceUtil -class ArtistsFragment : - AbsRecyclerViewCustomGridSizeFragment(), - MainActivityFragmentCallbacks, ArtistClickListener { - - override fun handleBackPress(): Boolean { - return false - } - +class ArtistsFragment : AbsRecyclerViewCustomGridSizeFragment(), + ArtistClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt index e4017e57..93af4203 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/genres/GenresFragment.kt @@ -21,14 +21,8 @@ import androidx.recyclerview.widget.LinearLayoutManager import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.GenreAdapter import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment -import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks -class GenresFragment : AbsRecyclerViewFragment(), - MainActivityFragmentCallbacks { - - override fun handleBackPress(): Boolean { - return false - } +class GenresFragment : AbsRecyclerViewFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) 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 cd3a2f60..6c094048 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 @@ -7,15 +7,8 @@ import androidx.recyclerview.widget.GridLayoutManager import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.playlist.PlaylistAdapter import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment -import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks -class PlaylistsFragment : - AbsRecyclerViewFragment(), - MainActivityFragmentCallbacks { - - override fun handleBackPress(): Boolean { - return false - } +class PlaylistsFragment : AbsRecyclerViewFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt index f829f985..6376420c 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/queue/PlayingQueueFragment.kt @@ -22,7 +22,6 @@ import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.song.PlayingQueueAdapter import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment import code.name.monkey.retromusic.helper.MusicPlayerRemote -import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import com.h6ah4i.android.widget.advrecyclerview.animator.DraggableItemAnimator import com.h6ah4i.android.widget.advrecyclerview.draggable.RecyclerViewDragDropManager import com.h6ah4i.android.widget.advrecyclerview.swipeable.RecyclerViewSwipeManager @@ -33,13 +32,7 @@ import kotlinx.android.synthetic.main.activity_playing_queue.* /** * Created by hemanths on 2019-12-08. */ -class PlayingQueueFragment : - AbsRecyclerViewFragment(), - MainActivityFragmentCallbacks { - - override fun handleBackPress(): Boolean { - return false - } +class PlayingQueueFragment : AbsRecyclerViewFragment() { private lateinit var wrappedAdapter: RecyclerView.Adapter<*> private var recyclerViewDragDropManager: RecyclerViewDragDropManager? = null 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 3db01eb7..51dd9d8c 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 @@ -9,17 +9,9 @@ import code.name.monkey.retromusic.R import code.name.monkey.retromusic.adapter.song.SongAdapter import code.name.monkey.retromusic.fragments.ReloadType import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewCustomGridSizeFragment -import code.name.monkey.retromusic.interfaces.MainActivityFragmentCallbacks import code.name.monkey.retromusic.util.PreferenceUtil -class SongsFragment : - AbsRecyclerViewCustomGridSizeFragment(), - MainActivityFragmentCallbacks { - - override fun handleBackPress(): Boolean { - return false - } - +class SongsFragment : AbsRecyclerViewCustomGridSizeFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) libraryViewModel.songsLiveData.observe(viewLifecycleOwner, Observer {