Code refactor
This commit is contained in:
parent
e8549513bd
commit
8cebb0c603
2 changed files with 58 additions and 50 deletions
|
@ -0,0 +1,52 @@
|
||||||
|
package code.name.monkey.retromusic.adapter.playlist
|
||||||
|
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.fragment.app.FragmentActivity
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import code.name.monkey.retromusic.adapter.base.MediaEntryViewHolder
|
||||||
|
import code.name.monkey.retromusic.model.Playlist
|
||||||
|
import code.name.monkey.retromusic.util.MusicUtil
|
||||||
|
|
||||||
|
class LegacyPlaylistAdapter(
|
||||||
|
private val activity: FragmentActivity,
|
||||||
|
private var list: List<Playlist>,
|
||||||
|
private val layoutRes: Int,
|
||||||
|
private val playlistClickListener: PlaylistClickListener
|
||||||
|
) :
|
||||||
|
RecyclerView.Adapter<LegacyPlaylistAdapter.ViewHolder>() {
|
||||||
|
|
||||||
|
fun swapData(list: List<Playlist>) {
|
||||||
|
this.list = list
|
||||||
|
notifyDataSetChanged()
|
||||||
|
}
|
||||||
|
|
||||||
|
class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView)
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(
|
||||||
|
parent: ViewGroup,
|
||||||
|
viewType: Int
|
||||||
|
): ViewHolder {
|
||||||
|
return ViewHolder(
|
||||||
|
LayoutInflater.from(parent.context).inflate(layoutRes, parent, false)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
val playlist: Playlist = list[position]
|
||||||
|
holder.title?.text = playlist.name
|
||||||
|
holder.text?.text = MusicUtil.getPlaylistInfoString(activity, playlist.getSongs())
|
||||||
|
holder.itemView.setOnClickListener {
|
||||||
|
playlistClickListener.onPlaylistClick(playlist)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getItemCount(): Int {
|
||||||
|
return list.size
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PlaylistClickListener {
|
||||||
|
fun onPlaylistClick(playlist: Playlist)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,25 +1,25 @@
|
||||||
package code.name.monkey.retromusic.fragments.playlists
|
package code.name.monkey.retromusic.fragments.playlists
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.*
|
import android.view.Menu
|
||||||
|
import android.view.MenuInflater
|
||||||
|
import android.view.View
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.fragment.app.FragmentActivity
|
|
||||||
import androidx.lifecycle.Observer
|
import androidx.lifecycle.Observer
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
|
||||||
import code.name.monkey.retromusic.R
|
import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.adapter.base.MediaEntryViewHolder
|
import code.name.monkey.retromusic.adapter.playlist.LegacyPlaylistAdapter
|
||||||
import code.name.monkey.retromusic.db.PlaylistEntity
|
import code.name.monkey.retromusic.db.PlaylistEntity
|
||||||
import code.name.monkey.retromusic.fragments.ReloadType.Playlists
|
import code.name.monkey.retromusic.fragments.ReloadType.Playlists
|
||||||
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment
|
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment
|
||||||
import code.name.monkey.retromusic.model.Playlist
|
import code.name.monkey.retromusic.model.Playlist
|
||||||
import code.name.monkey.retromusic.util.MusicUtil
|
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class ImportPlaylistFragment :
|
class ImportPlaylistFragment :
|
||||||
AbsRecyclerViewFragment<LegacyPlaylistAdapter, LinearLayoutManager>(), PlaylistClickListener {
|
AbsRecyclerViewFragment<LegacyPlaylistAdapter, LinearLayoutManager>(),
|
||||||
|
LegacyPlaylistAdapter.PlaylistClickListener {
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
@ -70,47 +70,3 @@ class ImportPlaylistFragment :
|
||||||
super.onCreateOptionsMenu(menu, inflater)
|
super.onCreateOptionsMenu(menu, inflater)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LegacyPlaylistAdapter(
|
|
||||||
private val activity: FragmentActivity,
|
|
||||||
private var list: List<Playlist>,
|
|
||||||
private val layoutRes: Int,
|
|
||||||
private val playlistClickListener: PlaylistClickListener
|
|
||||||
) :
|
|
||||||
RecyclerView.Adapter<LegacyPlaylistAdapter.ViewHolder>() {
|
|
||||||
|
|
||||||
fun swapData(list: List<Playlist>) {
|
|
||||||
this.list = list
|
|
||||||
notifyDataSetChanged()
|
|
||||||
}
|
|
||||||
|
|
||||||
class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreateViewHolder(
|
|
||||||
parent: ViewGroup,
|
|
||||||
viewType: Int
|
|
||||||
): ViewHolder {
|
|
||||||
return ViewHolder(
|
|
||||||
LayoutInflater.from(parent.context).inflate(layoutRes, parent, false)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
||||||
val playlist: Playlist = list[position]
|
|
||||||
holder.title?.text = playlist.name
|
|
||||||
holder.text?.text = MusicUtil.getPlaylistInfoString(activity, playlist.getSongs())
|
|
||||||
holder.itemView.setOnClickListener {
|
|
||||||
playlistClickListener.onPlaylistClick(playlist)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItemCount(): Int {
|
|
||||||
return list.size
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
interface PlaylistClickListener {
|
|
||||||
fun onPlaylistClick(playlist: Playlist)
|
|
||||||
}
|
|
Loading…
Reference in a new issue