2020-10-06 08:46:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
2019-04-20 05:29:45 +00:00
|
|
|
package code.name.monkey.retromusic.fragments.base
|
2018-11-30 01:06:16 +00:00
|
|
|
|
|
|
|
import android.os.Bundle
|
2021-09-08 18:30:20 +00:00
|
|
|
import android.view.*
|
2019-12-01 11:28:57 +00:00
|
|
|
import androidx.annotation.NonNull
|
|
|
|
import androidx.annotation.StringRes
|
2021-09-11 06:11:16 +00:00
|
|
|
import androidx.appcompat.widget.Toolbar
|
2020-12-03 15:01:27 +00:00
|
|
|
import androidx.core.view.doOnPreDraw
|
2021-09-23 15:26:30 +00:00
|
|
|
import androidx.core.view.isVisible
|
2021-09-24 07:31:53 +00:00
|
|
|
import androidx.core.view.updateLayoutParams
|
2020-09-25 17:38:59 +00:00
|
|
|
import androidx.navigation.fragment.findNavController
|
2018-11-30 01:06:16 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2020-09-25 17:38:59 +00:00
|
|
|
import code.name.monkey.appthemehelper.common.ATHToolbarActivity
|
|
|
|
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
|
2018-11-30 01:06:16 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2021-09-08 18:30:20 +00:00
|
|
|
import code.name.monkey.retromusic.databinding.FragmentMainRecyclerBinding
|
2020-09-25 17:38:59 +00:00
|
|
|
import code.name.monkey.retromusic.dialogs.CreatePlaylistDialog
|
|
|
|
import code.name.monkey.retromusic.dialogs.ImportPlaylistDialog
|
2021-09-23 15:26:30 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.accentColor
|
2021-10-03 13:58:05 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.dip
|
2021-10-22 14:53:23 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.drawNextToNavbar
|
2019-11-09 17:33:28 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
2020-01-17 17:19:06 +00:00
|
|
|
import code.name.monkey.retromusic.util.ThemedFastScroller.create
|
2021-09-12 14:20:33 +00:00
|
|
|
import com.google.android.material.shape.MaterialShapeDrawable
|
2021-11-11 15:29:28 +00:00
|
|
|
import com.google.android.material.transition.MaterialFadeThrough
|
2021-09-08 18:30:20 +00:00
|
|
|
import com.google.android.material.transition.MaterialSharedAxis
|
2020-01-17 17:19:06 +00:00
|
|
|
import me.zhanghai.android.fastscroll.FastScroller
|
|
|
|
import me.zhanghai.android.fastscroll.FastScrollerBuilder
|
2018-11-30 01:06:16 +00:00
|
|
|
|
2020-07-30 14:11:19 +00:00
|
|
|
abstract class AbsRecyclerViewFragment<A : RecyclerView.Adapter<*>, LM : RecyclerView.LayoutManager> :
|
2021-09-08 18:30:20 +00:00
|
|
|
AbsMainActivityFragment(R.layout.fragment_main_recycler) {
|
2020-05-23 15:09:07 +00:00
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
private var _binding: FragmentMainRecyclerBinding? = null
|
|
|
|
private val binding get() = _binding!!
|
2019-12-01 11:28:57 +00:00
|
|
|
protected var adapter: A? = null
|
|
|
|
protected var layoutManager: LM? = null
|
2021-09-23 15:26:30 +00:00
|
|
|
val shuffleButton get() = binding.shuffleButton
|
|
|
|
abstract val isShuffleVisible: Boolean
|
2019-12-01 11:28:57 +00:00
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2021-09-08 18:30:20 +00:00
|
|
|
_binding = FragmentMainRecyclerBinding.bind(view)
|
2020-12-03 15:01:27 +00:00
|
|
|
postponeEnterTransition()
|
|
|
|
view.doOnPreDraw { startPostponedEnterTransition() }
|
2021-11-11 15:29:28 +00:00
|
|
|
enterTransition = MaterialFadeThrough().apply {
|
|
|
|
addTarget(binding.recyclerView)
|
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
mainActivity.setSupportActionBar(binding.toolbar)
|
2020-09-25 17:38:59 +00:00
|
|
|
mainActivity.supportActionBar?.title = null
|
2019-12-01 11:28:57 +00:00
|
|
|
initLayoutManager()
|
|
|
|
initAdapter()
|
|
|
|
setUpRecyclerView()
|
2021-09-08 18:30:20 +00:00
|
|
|
setupToolbar()
|
2021-09-23 15:26:30 +00:00
|
|
|
// Add listeners when shuffle is visible
|
|
|
|
if (isShuffleVisible) {
|
|
|
|
binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
|
|
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
|
|
|
super.onScrolled(recyclerView, dx, dy)
|
|
|
|
if (dy > 0) {
|
|
|
|
binding.shuffleButton.hide()
|
|
|
|
} else if (dy < 0) {
|
|
|
|
binding.shuffleButton.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
2021-09-25 13:19:21 +00:00
|
|
|
binding.shuffleButton.apply {
|
|
|
|
setOnClickListener {
|
|
|
|
onShuffleClicked()
|
|
|
|
}
|
|
|
|
accentColor()
|
2021-09-23 15:26:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
binding.shuffleButton.isVisible = false
|
|
|
|
}
|
2021-09-24 07:31:53 +00:00
|
|
|
libraryViewModel.getFabMargin().observe(viewLifecycleOwner, {
|
|
|
|
binding.shuffleButton.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
|
|
|
bottomMargin = it
|
|
|
|
}
|
|
|
|
})
|
2021-09-23 15:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open fun onShuffleClicked() {
|
2020-09-25 17:38:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 06:11:16 +00:00
|
|
|
fun toolbar(): Toolbar {
|
|
|
|
return binding.toolbar
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
private fun setupToolbar() {
|
|
|
|
binding.toolbar.setNavigationOnClickListener {
|
|
|
|
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, true).addTarget(requireView())
|
|
|
|
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, false)
|
2020-09-25 17:38:59 +00:00
|
|
|
findNavController().navigate(
|
|
|
|
R.id.searchFragment,
|
|
|
|
null,
|
|
|
|
navOptions
|
|
|
|
)
|
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
val appName = resources.getString(titleRes)
|
|
|
|
binding.appNameText.text = appName
|
2021-10-23 11:55:48 +00:00
|
|
|
binding.toolbarContainer.drawNextToNavbar()
|
|
|
|
binding.appBarLayout.statusBarForeground =
|
|
|
|
MaterialShapeDrawable.createWithElevationOverlay(requireContext())
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
abstract val titleRes: Int
|
|
|
|
|
2019-12-01 11:28:57 +00:00
|
|
|
private fun setUpRecyclerView() {
|
2021-09-08 18:30:20 +00:00
|
|
|
binding.recyclerView.apply {
|
2020-08-13 08:24:36 +00:00
|
|
|
layoutManager = this@AbsRecyclerViewFragment.layoutManager
|
|
|
|
adapter = this@AbsRecyclerViewFragment.adapter
|
2021-09-08 18:30:20 +00:00
|
|
|
create(this)
|
2020-08-13 08:24:36 +00:00
|
|
|
}
|
2020-02-25 06:44:46 +00:00
|
|
|
checkForPadding()
|
2020-01-17 17:19:06 +00:00
|
|
|
}
|
2020-01-05 18:22:21 +00:00
|
|
|
|
2020-01-17 17:19:06 +00:00
|
|
|
protected open fun createFastScroller(recyclerView: RecyclerView): FastScroller {
|
|
|
|
return FastScrollerBuilder(recyclerView).useMd2Style().build()
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun initAdapter() {
|
|
|
|
adapter = createAdapter()
|
|
|
|
adapter?.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
|
|
|
override fun onChanged() {
|
|
|
|
super.onChanged()
|
|
|
|
checkIsEmpty()
|
|
|
|
checkForPadding()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected open val emptyMessage: Int
|
|
|
|
@StringRes get() = R.string.empty
|
|
|
|
|
|
|
|
private fun getEmojiByUnicode(unicode: Int): String {
|
|
|
|
return String(Character.toChars(unicode))
|
|
|
|
}
|
|
|
|
|
2020-09-25 17:38:59 +00:00
|
|
|
private fun checkIsEmpty() {
|
2021-09-08 18:30:20 +00:00
|
|
|
binding.emptyText.setText(emptyMessage)
|
|
|
|
binding.empty.visibility = if (adapter!!.itemCount == 0) View.VISIBLE else View.GONE
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun checkForPadding() {
|
|
|
|
val itemCount: Int = adapter?.itemCount ?: 0
|
2020-10-17 12:15:52 +00:00
|
|
|
|
2019-12-01 11:28:57 +00:00
|
|
|
if (itemCount > 0 && MusicPlayerRemote.playingQueue.isNotEmpty()) {
|
2021-11-23 09:22:46 +00:00
|
|
|
binding.recyclerView.updatePadding(bottom = dip(R.dimen.mini_player_height_expanded))
|
2019-12-01 11:28:57 +00:00
|
|
|
} else {
|
2021-11-23 09:22:46 +00:00
|
|
|
binding.recyclerView.updatePadding(bottom = dip(R.dimen.mini_player_height))
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initLayoutManager() {
|
|
|
|
layoutManager = createLayoutManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract fun createLayoutManager(): LM
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
protected abstract fun createAdapter(): A
|
|
|
|
|
|
|
|
override fun onQueueChanged() {
|
|
|
|
super.onQueueChanged()
|
|
|
|
checkForPadding()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceConnected() {
|
|
|
|
super.onServiceConnected()
|
|
|
|
checkForPadding()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected fun invalidateLayoutManager() {
|
|
|
|
initLayoutManager()
|
2021-09-08 18:30:20 +00:00
|
|
|
binding.recyclerView.layoutManager = layoutManager
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected fun invalidateAdapter() {
|
|
|
|
initAdapter()
|
|
|
|
checkIsEmpty()
|
2021-09-08 18:30:20 +00:00
|
|
|
binding.recyclerView.adapter = adapter
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 14:51:57 +00:00
|
|
|
val recyclerView get() = binding.recyclerView
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2021-11-11 14:51:57 +00:00
|
|
|
val container get() = binding.root
|
2021-09-08 18:30:20 +00:00
|
|
|
|
|
|
|
fun scrollToTop() {
|
2021-11-11 14:51:57 +00:00
|
|
|
recyclerView.scrollToPosition(0)
|
2021-09-08 18:30:20 +00:00
|
|
|
binding.appBarLayout.setExpanded(true, true)
|
2019-12-01 11:28:57 +00:00
|
|
|
}
|
2020-09-25 17:38:59 +00:00
|
|
|
|
|
|
|
override fun onPrepareOptionsMenu(menu: Menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu)
|
2021-09-08 18:30:20 +00:00
|
|
|
ToolbarContentTintHelper.handleOnPrepareOptionsMenu(requireActivity(), binding.toolbar)
|
2020-09-25 17:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
|
|
|
super.onCreateOptionsMenu(menu, inflater)
|
|
|
|
inflater.inflate(R.menu.menu_main, menu)
|
|
|
|
ToolbarContentTintHelper.handleOnCreateOptionsMenu(
|
|
|
|
requireContext(),
|
2021-09-08 18:30:20 +00:00
|
|
|
binding.toolbar,
|
2020-09-25 17:38:59 +00:00
|
|
|
menu,
|
2021-09-08 18:30:20 +00:00
|
|
|
ATHToolbarActivity.getToolbarBackgroundColor(binding.toolbar)
|
2020-09-25 17:38:59 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
when (item.itemId) {
|
|
|
|
R.id.action_settings -> findNavController().navigate(
|
|
|
|
R.id.settingsActivity,
|
|
|
|
null,
|
|
|
|
navOptions
|
|
|
|
)
|
|
|
|
R.id.action_import_playlist -> ImportPlaylistDialog().show(
|
|
|
|
childFragmentManager,
|
|
|
|
"ImportPlaylist"
|
|
|
|
)
|
|
|
|
R.id.action_add_to_playlist -> CreatePlaylistDialog.create(emptyList()).show(
|
|
|
|
childFragmentManager,
|
|
|
|
"ShowCreatePlaylistDialog"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
2021-09-08 18:30:20 +00:00
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_binding = null
|
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
}
|