Fix bugs/code style
Fix a recreation bug where MainActivity wont hide LoadingFragment, Fix a bug where artist click listeners wouldnt work, slightly tweak code here and there.
This commit is contained in:
parent
4b50b80af4
commit
4e57a94d3e
13 changed files with 40 additions and 28 deletions
|
@ -76,7 +76,7 @@ dependencies {
|
||||||
// Lint
|
// Lint
|
||||||
ktlint "com.pinterest:ktlint:0.37.2"
|
ktlint "com.pinterest:ktlint:0.37.2"
|
||||||
|
|
||||||
// Memory Leak checking
|
// Memory Leak checkin
|
||||||
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
|
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@ import android.os.Bundle
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
@ -39,7 +38,6 @@ class MainActivity : AppCompatActivity() {
|
||||||
)
|
)
|
||||||
|
|
||||||
private lateinit var binding: ActivityMainBinding
|
private lateinit var binding: ActivityMainBinding
|
||||||
private lateinit var permLauncher: ActivityResultLauncher<String>
|
|
||||||
|
|
||||||
private val musicModel: MusicViewModel by lazy {
|
private val musicModel: MusicViewModel by lazy {
|
||||||
ViewModelProvider(
|
ViewModelProvider(
|
||||||
|
@ -105,13 +103,13 @@ class MainActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Log.d(this::class.simpleName, musicModel.done.toString())
|
||||||
|
|
||||||
musicModel.response.observe(
|
musicModel.response.observe(
|
||||||
this,
|
this,
|
||||||
{
|
{
|
||||||
// When the load is completed successfully, remove the loadingFragment view
|
|
||||||
// and replace it with the ViewPager, now that its loaded.
|
|
||||||
if (it == MusicLoaderResponse.DONE) {
|
if (it == MusicLoaderResponse.DONE) {
|
||||||
// binding.loadingFragment.visibility = View.GONE
|
binding.loadingFragment.visibility = View.GONE
|
||||||
binding.viewPager.visibility = View.VISIBLE
|
binding.viewPager.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,9 @@ import androidx.fragment.app.activityViewModels
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||||
import org.oxycblt.auxio.music.MusicViewModel
|
import org.oxycblt.auxio.music.MusicViewModel
|
||||||
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.adapters.ArtistAdapter
|
import org.oxycblt.auxio.recycler.adapters.ArtistAdapter
|
||||||
import org.oxycblt.auxio.recycler.applyDivider
|
import org.oxycblt.auxio.recycler.applyDivider
|
||||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
|
||||||
|
|
||||||
class LibraryFragment : Fragment() {
|
class LibraryFragment : Fragment() {
|
||||||
|
|
||||||
|
|
|
@ -117,8 +117,6 @@ class LoadingFragment : Fragment(R.layout.fragment_loading) {
|
||||||
binding.statusIcon.visibility = View.VISIBLE
|
binding.statusIcon.visibility = View.VISIBLE
|
||||||
binding.retryButton.visibility = View.VISIBLE
|
binding.retryButton.visibility = View.VISIBLE
|
||||||
}
|
}
|
||||||
|
|
||||||
musicModel.doneWithResponse()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,9 @@ import androidx.fragment.app.activityViewModels
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
||||||
import org.oxycblt.auxio.music.MusicViewModel
|
import org.oxycblt.auxio.music.MusicViewModel
|
||||||
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.adapters.SongAdapter
|
import org.oxycblt.auxio.recycler.adapters.SongAdapter
|
||||||
import org.oxycblt.auxio.recycler.applyDivider
|
import org.oxycblt.auxio.recycler.applyDivider
|
||||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
|
||||||
|
|
||||||
class SongsFragment : Fragment() {
|
class SongsFragment : Fragment() {
|
||||||
|
|
||||||
|
|
|
@ -42,9 +42,6 @@ class MusicViewModel(private val app: Application) : ViewModel() {
|
||||||
private val mSongs = MutableLiveData<List<Song>>()
|
private val mSongs = MutableLiveData<List<Song>>()
|
||||||
val songs: LiveData<List<Song>> get() = mSongs
|
val songs: LiveData<List<Song>> get() = mSongs
|
||||||
|
|
||||||
private val mResponse = MutableLiveData<MusicLoaderResponse>()
|
|
||||||
val response: LiveData<MusicLoaderResponse> get() = mResponse
|
|
||||||
|
|
||||||
// UI control
|
// UI control
|
||||||
private val mRedo = MutableLiveData<Boolean>()
|
private val mRedo = MutableLiveData<Boolean>()
|
||||||
val doReload: LiveData<Boolean> get() = mRedo
|
val doReload: LiveData<Boolean> get() = mRedo
|
||||||
|
@ -52,6 +49,18 @@ class MusicViewModel(private val app: Application) : ViewModel() {
|
||||||
private val mDoGrant = MutableLiveData<Boolean>()
|
private val mDoGrant = MutableLiveData<Boolean>()
|
||||||
val doGrant: LiveData<Boolean> get() = mDoGrant
|
val doGrant: LiveData<Boolean> get() = mDoGrant
|
||||||
|
|
||||||
|
// Response Management
|
||||||
|
|
||||||
|
// The actual response from MusicLoader. This is set to null so that LoadingFragment doesn't
|
||||||
|
//
|
||||||
|
private val mResponse = MutableLiveData<MusicLoaderResponse>()
|
||||||
|
val response: LiveData<MusicLoaderResponse> get() = mResponse
|
||||||
|
|
||||||
|
// Whether MusicViewModel has finished the load [Used to hide LoadingFragment in MainActivity]
|
||||||
|
private var mDone = false
|
||||||
|
val done: Boolean get() = mDone
|
||||||
|
|
||||||
|
// Whether go() has ran. Used to prevent multiple loads from the recreation of LoadingFragment.
|
||||||
private var started = false
|
private var started = false
|
||||||
|
|
||||||
// Start the music loading sequence.
|
// Start the music loading sequence.
|
||||||
|
@ -89,6 +98,8 @@ class MusicViewModel(private val app: Application) : ViewModel() {
|
||||||
mAlbums.value = sorter.albums.toList()
|
mAlbums.value = sorter.albums.toList()
|
||||||
mArtists.value = sorter.artists.toList()
|
mArtists.value = sorter.artists.toList()
|
||||||
mGenres.value = sorter.genres.toList()
|
mGenres.value = sorter.genres.toList()
|
||||||
|
|
||||||
|
mDone = true
|
||||||
}
|
}
|
||||||
|
|
||||||
mResponse.value = loader.response
|
mResponse.value = loader.response
|
||||||
|
@ -103,13 +114,9 @@ class MusicViewModel(private val app: Application) : ViewModel() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UI communication functions
|
|
||||||
fun doneWithResponse() {
|
|
||||||
mResponse.value = null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun reload() {
|
fun reload() {
|
||||||
mRedo.value = true
|
mRedo.value = true
|
||||||
|
mDone = false
|
||||||
|
|
||||||
doLoad()
|
doLoad()
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,3 +21,6 @@ fun RecyclerView.applyDivider() {
|
||||||
|
|
||||||
addItemDecoration(div)
|
addItemDecoration(div)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generic ClickListener
|
||||||
|
class ClickListener<T>(val onClick: (T) -> Unit)
|
||||||
|
|
|
@ -5,8 +5,8 @@ import android.view.ViewGroup
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||||
import org.oxycblt.auxio.music.models.Album
|
import org.oxycblt.auxio.music.models.Album
|
||||||
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.viewholders.AlbumViewHolder
|
import org.oxycblt.auxio.recycler.viewholders.AlbumViewHolder
|
||||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
|
||||||
|
|
||||||
class AlbumAdapter(
|
class AlbumAdapter(
|
||||||
private val data: List<Album>,
|
private val data: List<Album>,
|
||||||
|
@ -29,6 +29,10 @@ class AlbumAdapter(
|
||||||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||||
val album = data[position]
|
val album = data[position]
|
||||||
|
|
||||||
|
holder.itemView.setOnClickListener {
|
||||||
|
listener.onClick(album)
|
||||||
|
}
|
||||||
|
|
||||||
holder.bind(album)
|
holder.bind(album)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@ import android.view.ViewGroup
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import org.oxycblt.auxio.databinding.ArtistItemBinding
|
import org.oxycblt.auxio.databinding.ArtistItemBinding
|
||||||
import org.oxycblt.auxio.music.models.Artist
|
import org.oxycblt.auxio.music.models.Artist
|
||||||
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.viewholders.ArtistViewHolder
|
import org.oxycblt.auxio.recycler.viewholders.ArtistViewHolder
|
||||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
|
||||||
|
|
||||||
class ArtistAdapter(
|
class ArtistAdapter(
|
||||||
private val data: List<Artist>,
|
private val data: List<Artist>,
|
||||||
|
@ -27,8 +27,12 @@ class ArtistAdapter(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: ArtistViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: ArtistViewHolder, position: Int) {
|
||||||
val album = data[position]
|
val artist = data[position]
|
||||||
|
|
||||||
holder.bind(album)
|
holder.itemView.setOnClickListener {
|
||||||
|
listener.onClick(artist)
|
||||||
|
}
|
||||||
|
|
||||||
|
holder.bind(artist)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import android.view.ViewGroup
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import org.oxycblt.auxio.databinding.SongItemBinding
|
import org.oxycblt.auxio.databinding.SongItemBinding
|
||||||
import org.oxycblt.auxio.music.models.Song
|
import org.oxycblt.auxio.music.models.Song
|
||||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
import org.oxycblt.auxio.recycler.ClickListener
|
||||||
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
||||||
|
|
||||||
class SongAdapter(
|
class SongAdapter(
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
package org.oxycblt.auxio.recycler.viewholders
|
|
||||||
|
|
||||||
// Generic ClickListener
|
|
||||||
class ClickListener<T>(val onClick: (T) -> Unit)
|
|
|
@ -16,7 +16,7 @@
|
||||||
android:animateLayoutChanges="true">
|
android:animateLayoutChanges="true">
|
||||||
|
|
||||||
<androidx.viewpager2.widget.ViewPager2
|
<androidx.viewpager2.widget.ViewPager2
|
||||||
android:id="@+id/viewPager"
|
android:id="@+id/view_pager"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="@dimen/margin_medium"
|
android:layout_marginStart="@dimen/margin_medium"
|
||||||
|
android:layout_marginEnd="@dimen/margin_medium"
|
||||||
android:ellipsize="end"
|
android:ellipsize="end"
|
||||||
android:maxLines="1"
|
android:maxLines="1"
|
||||||
android:text="@{song.name}"
|
android:text="@{song.name}"
|
||||||
|
@ -51,6 +52,7 @@
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="@dimen/margin_medium"
|
android:layout_marginStart="@dimen/margin_medium"
|
||||||
|
android:layout_marginEnd="@dimen/margin_medium"
|
||||||
android:ellipsize="end"
|
android:ellipsize="end"
|
||||||
android:maxLines="1"
|
android:maxLines="1"
|
||||||
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
|
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
|
||||||
|
|
Loading…
Reference in a new issue