Add playback fragment
Add a fragment to show songs that are currently being played.
This commit is contained in:
@@ -41,7 +41,7 @@ class MainFragment : Fragment() {
|
||||
val binding = FragmentMainBinding.inflate(inflater)
|
||||
|
||||
// If musicModel was cleared while the app was closed [Likely due to Auxio being suspended
|
||||
// in the background], then navigate back to loading to reload the music.
|
||||
// in the background], then navigate back to LoadingFragment to reload the music.
|
||||
if (musicModel.response.value == null) {
|
||||
findNavController().navigate(MainFragmentDirections.actionReturnToLoading())
|
||||
|
||||
@@ -57,7 +57,7 @@ class MainFragment : Fragment() {
|
||||
|
||||
// --- UI SETUP ---
|
||||
|
||||
binding.lifecycleOwner = viewLifecycleOwner
|
||||
binding.lifecycleOwner = this
|
||||
binding.mainViewPager.adapter = PagerAdapter()
|
||||
|
||||
// Link the ViewPager & Tab View
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentAlbumDetailBinding
|
||||
import org.oxycblt.auxio.detail.adapters.DetailSongAdapter
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
import org.oxycblt.auxio.theme.disable
|
||||
@@ -21,6 +22,7 @@ class AlbumDetailFragment : Fragment() {
|
||||
|
||||
private val args: AlbumDetailFragmentArgs by navArgs()
|
||||
private val detailModel: DetailViewModel by activityViewModels()
|
||||
private val playbackModel: PlaybackViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@@ -45,7 +47,7 @@ class AlbumDetailFragment : Fragment() {
|
||||
|
||||
val songAdapter = DetailSongAdapter(
|
||||
ClickListener {
|
||||
Log.d(this::class.simpleName, it.name)
|
||||
playbackModel.updateSong(it)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -17,13 +17,15 @@ import androidx.transition.TransitionManager
|
||||
import org.oxycblt.auxio.MainFragmentDirections
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.library.recycler.LibraryAdapter
|
||||
import org.oxycblt.auxio.library.recycler.SearchAdapter
|
||||
import org.oxycblt.auxio.library.adapters.LibraryAdapter
|
||||
import org.oxycblt.auxio.library.adapters.SearchAdapter
|
||||
import org.oxycblt.auxio.music.Album
|
||||
import org.oxycblt.auxio.music.Artist
|
||||
import org.oxycblt.auxio.music.BaseModel
|
||||
import org.oxycblt.auxio.music.Genre
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||
import org.oxycblt.auxio.recycler.ShowMode
|
||||
import org.oxycblt.auxio.theme.applyColor
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
@@ -36,6 +38,7 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
||||
}
|
||||
|
||||
private val libraryModel: LibraryViewModel by activityViewModels()
|
||||
private val playbackModel: PlaybackViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@@ -173,6 +176,13 @@ class LibraryFragment : Fragment(), SearchView.OnQueryTextListener {
|
||||
}
|
||||
|
||||
private fun navToItem(baseModel: BaseModel) {
|
||||
// If the item is a song [That was selected through search], then update the playback
|
||||
// to that song instead of doing any naviagation
|
||||
if (baseModel is Song) {
|
||||
playbackModel.updateSong(baseModel)
|
||||
return
|
||||
}
|
||||
|
||||
if (!libraryModel.isNavigating) {
|
||||
libraryModel.updateNavigationStatus(true)
|
||||
|
||||
|
||||
+3
-2
@@ -1,4 +1,4 @@
|
||||
package org.oxycblt.auxio.library.recycler
|
||||
package org.oxycblt.auxio.library.adapters
|
||||
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
@@ -13,7 +13,8 @@ import org.oxycblt.auxio.recycler.viewholders.ArtistViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.GenreViewHolder
|
||||
|
||||
// A ListAdapter that can contain three different types of ViewHolders depending
|
||||
// the showmode given. It cannot display multiple types of viewholders *at once*.
|
||||
// the ShowMode given.
|
||||
// It cannot display multiple ViewHolders *at once* however. That's what SearchAdapter is for.
|
||||
class LibraryAdapter(
|
||||
private val showMode: ShowMode,
|
||||
private val doOnClick: (BaseModel) -> Unit
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package org.oxycblt.auxio.library.recycler
|
||||
package org.oxycblt.auxio.library.adapters
|
||||
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
@@ -20,62 +20,44 @@ import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||
|
||||
class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
|
||||
// LoadingFragment is [hopefully] going to be the first one to have to create musicModel,
|
||||
// so pass a factory instance so that the model has access to the application resources.
|
||||
private val musicModel: MusicViewModel by activityViewModels {
|
||||
MusicViewModel.Factory(requireActivity().application)
|
||||
}
|
||||
|
||||
private lateinit var binding: FragmentLoadingBinding
|
||||
private lateinit var permLauncher: ActivityResultLauncher<String>
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
binding = FragmentLoadingBinding.inflate(inflater)
|
||||
|
||||
binding.lifecycleOwner = this
|
||||
binding.musicModel = musicModel
|
||||
|
||||
musicModel.response.observe(
|
||||
viewLifecycleOwner,
|
||||
{ response ->
|
||||
onMusicLoadResponse(response)
|
||||
}
|
||||
)
|
||||
|
||||
musicModel.doReload.observe(
|
||||
viewLifecycleOwner,
|
||||
{ retry ->
|
||||
onRetry(retry)
|
||||
}
|
||||
)
|
||||
|
||||
musicModel.doGrant.observe(
|
||||
viewLifecycleOwner,
|
||||
{ grant ->
|
||||
onGrant(grant)
|
||||
}
|
||||
)
|
||||
val binding = FragmentLoadingBinding.inflate(inflater)
|
||||
|
||||
// Set up the permission launcher, as its disallowed outside of onCreate.
|
||||
permLauncher =
|
||||
val permLauncher =
|
||||
registerForActivityResult(
|
||||
ActivityResultContracts.RequestPermission()
|
||||
) { granted: Boolean ->
|
||||
// If its actually granted, restart the loading process again.
|
||||
if (granted) {
|
||||
wipeViews()
|
||||
wipeViews(binding)
|
||||
|
||||
musicModel.reload()
|
||||
}
|
||||
}
|
||||
|
||||
// --- UI SETUP ---
|
||||
|
||||
binding.lifecycleOwner = this
|
||||
binding.musicModel = musicModel
|
||||
|
||||
// --- VIEWMODEL SETUP ---
|
||||
|
||||
musicModel.response.observe(viewLifecycleOwner) { onMusicLoadResponse(it, binding) }
|
||||
musicModel.doReload.observe(viewLifecycleOwner) { onRetry(it, binding) }
|
||||
musicModel.doGrant.observe(viewLifecycleOwner) { onGrant(it, permLauncher) }
|
||||
|
||||
// Force an error screen if the permissions are denied or the prompt needs to be shown.
|
||||
if (checkPerms()) {
|
||||
onNoPerms()
|
||||
onNoPerms(binding)
|
||||
} else {
|
||||
musicModel.go()
|
||||
}
|
||||
@@ -96,31 +78,31 @@ class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
) == PackageManager.PERMISSION_DENIED
|
||||
}
|
||||
|
||||
private fun onMusicLoadResponse(response: MusicLoaderResponse?) {
|
||||
private fun onMusicLoadResponse(
|
||||
response: MusicLoaderResponse?,
|
||||
binding: FragmentLoadingBinding
|
||||
) {
|
||||
if (response == MusicLoaderResponse.DONE) {
|
||||
findNavController().navigate(
|
||||
LoadingFragmentDirections.actionToMain()
|
||||
)
|
||||
} else {
|
||||
binding.let { binding ->
|
||||
binding.loadingErrorText.text =
|
||||
if (response == MusicLoaderResponse.NO_MUSIC)
|
||||
getString(R.string.error_no_music)
|
||||
else
|
||||
getString(R.string.error_music_load_failed)
|
||||
binding.loadingErrorText.text =
|
||||
if (response == MusicLoaderResponse.NO_MUSIC)
|
||||
getString(R.string.error_no_music)
|
||||
else
|
||||
getString(R.string.error_music_load_failed)
|
||||
|
||||
// If the response wasn't a success, then show the specific error message
|
||||
// depending on which error response was given, along with a retry button
|
||||
binding.loadingBar.visibility = View.GONE
|
||||
|
||||
binding.loadingErrorText.visibility = View.VISIBLE
|
||||
binding.loadingErrorIcon.visibility = View.VISIBLE
|
||||
binding.loadingRetryButton.visibility = View.VISIBLE
|
||||
}
|
||||
// If the response wasn't a success, then show the specific error message
|
||||
// depending on which error response was given, along with a retry button
|
||||
binding.loadingBar.visibility = View.GONE
|
||||
binding.loadingErrorText.visibility = View.VISIBLE
|
||||
binding.loadingErrorIcon.visibility = View.VISIBLE
|
||||
binding.loadingRetryButton.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
private fun onNoPerms() {
|
||||
private fun onNoPerms(binding: FragmentLoadingBinding) {
|
||||
// If there are no perms, switch out the view elements as if an error screen was being
|
||||
// shown, but show the label that Auxio needs to read external storage to function,
|
||||
// along with a GRANT button
|
||||
@@ -133,15 +115,15 @@ class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
binding.loadingErrorText.text = getString(R.string.error_no_perms)
|
||||
}
|
||||
|
||||
private fun onRetry(retry: Boolean) {
|
||||
private fun onRetry(retry: Boolean, binding: FragmentLoadingBinding) {
|
||||
if (retry) {
|
||||
wipeViews()
|
||||
wipeViews(binding)
|
||||
|
||||
musicModel.doneWithReload()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onGrant(grant: Boolean) {
|
||||
private fun onGrant(grant: Boolean, permLauncher: ActivityResultLauncher<String>) {
|
||||
if (grant) {
|
||||
permLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
|
||||
@@ -150,7 +132,7 @@ class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||
}
|
||||
|
||||
// Wipe views and switch back to the plain ProgressBar
|
||||
private fun wipeViews() {
|
||||
private fun wipeViews(binding: FragmentLoadingBinding) {
|
||||
binding.loadingBar.visibility = View.VISIBLE
|
||||
binding.loadingErrorText.visibility = View.GONE
|
||||
binding.loadingErrorIcon.visibility = View.GONE
|
||||
|
||||
@@ -13,9 +13,9 @@ sealed class BaseModel {
|
||||
data class Song(
|
||||
override val id: Long = -1,
|
||||
override var name: String,
|
||||
val albumId: Long,
|
||||
val track: Int,
|
||||
val duration: Long,
|
||||
val albumId: Long = -1,
|
||||
val track: Int = -1,
|
||||
val duration: Long = 0,
|
||||
) : BaseModel() {
|
||||
lateinit var album: Album
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.oxycblt.auxio.playback
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import org.oxycblt.auxio.databinding.FragmentCompactPlaybackBinding
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
|
||||
class CompactPlaybackFragment : Fragment() {
|
||||
private val musicModel: MusicViewModel by activityViewModels {
|
||||
MusicViewModel.Factory(requireActivity().application)
|
||||
}
|
||||
|
||||
private val playbackModel: PlaybackViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
val binding = FragmentCompactPlaybackBinding.inflate(inflater)
|
||||
|
||||
binding.lifecycleOwner = this
|
||||
|
||||
// Put a placeholder song in the binding & hide the playback fragment initially,
|
||||
// as for some reason the attach event doesn't register anymore w/LiveData
|
||||
binding.song = musicModel.songs.value!![0]
|
||||
binding.root.visibility = View.GONE
|
||||
|
||||
playbackModel.currentSong.observe(viewLifecycleOwner) {
|
||||
if (it == null) {
|
||||
Log.d(this::class.simpleName, "Hiding playback bar due to no song being played.")
|
||||
|
||||
binding.root.visibility = View.GONE
|
||||
} else {
|
||||
Log.d(this::class.simpleName, "Updating song display to ${it.name}")
|
||||
|
||||
binding.song = it
|
||||
|
||||
binding.root.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment Created")
|
||||
|
||||
return binding.root
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.oxycblt.auxio.playback
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import org.oxycblt.auxio.music.Song
|
||||
|
||||
class PlaybackViewModel : ViewModel() {
|
||||
private val mCurrentSong = MutableLiveData<Song>()
|
||||
val currentSong: LiveData<Song> get() = mCurrentSong
|
||||
|
||||
fun updateSong(song: Song) {
|
||||
mCurrentSong.value = song
|
||||
}
|
||||
}
|
||||
@@ -9,15 +9,17 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.playback.PlaybackViewModel
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.theme.applyDivider
|
||||
|
||||
class SongsFragment : Fragment() {
|
||||
|
||||
private val musicModel: MusicViewModel by activityViewModels {
|
||||
MusicViewModel.Factory(requireActivity().application)
|
||||
}
|
||||
|
||||
private val playbackModel: PlaybackViewModel by activityViewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
@@ -31,7 +33,7 @@ class SongsFragment : Fragment() {
|
||||
adapter = SongAdapter(
|
||||
musicModel.songs.value!!,
|
||||
ClickListener { song ->
|
||||
Log.d(this::class.simpleName, song.name)
|
||||
playbackModel.updateSong(song)
|
||||
}
|
||||
)
|
||||
applyDivider()
|
||||
|
||||
@@ -87,7 +87,7 @@ fun resolveAttr(context: Context, @AttrRes attr: Int): Int {
|
||||
}
|
||||
|
||||
// Apply a color to a Menu Item
|
||||
fun MenuItem.applyColor(@ColorRes color: Int) {
|
||||
fun MenuItem.applyColor(@ColorInt color: Int) {
|
||||
SpannableString(title).apply {
|
||||
setSpan(ForegroundColorSpan(color), 0, length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
title = this
|
||||
|
||||
Reference in New Issue
Block a user