From adfb68342c27312755b06a03a3974a963f448054 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 30 Nov 2017 13:33:03 +0100 Subject: [PATCH] rewrite the playlists adapter too --- app/build.gradle | 2 +- .../musicplayer/activities/MainActivity.kt | 15 +- .../activities/PlaylistsActivity.kt | 19 +- .../musicplayer/adapters/PlaylistsAdapter.kt | 164 +++++------------- .../main/res/layout/activity_playlists.xml | 2 +- app/src/main/res/layout/item_playlist.xml | 2 + app/src/main/res/layout/item_song.xml | 1 + 7 files changed, 64 insertions(+), 141 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d1b85aa1..4f7aedcf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,7 +46,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.0.4' + implementation 'com.simplemobiletools:commons:3.0.5' implementation 'com.squareup:otto:1.3.8' implementation 'com.facebook.stetho:stetho:1.5.0' diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt index e7f8ea21..afaa99e6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/MainActivity.kt @@ -6,7 +6,6 @@ import android.graphics.PorterDuffColorFilter import android.media.AudioManager import android.os.Bundle import android.os.Environment -import android.support.v7.widget.DividerItemDecoration import android.support.v7.widget.LinearLayoutManager import android.view.Menu import android.view.MenuItem @@ -274,16 +273,14 @@ class MainActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListener { val currAdapter = songs_list.adapter songs_fastscroller.setViews(songs_list) if (currAdapter == null) { - val adapter = SongAdapter(this@MainActivity, songs, songs_list) { + SongAdapter(this@MainActivity, songs, songs_list) { songPicked(getSongIndex(it as Song)) + }.apply { + setupDragListener(true) + this.isThirdPartyIntent = isThirdPartyIntent + addVerticalDividers(true) + songs_list.adapter = this } - adapter.setupDragListener(true) - adapter.isThirdPartyIntent = isThirdPartyIntent - DividerItemDecoration(this, DividerItemDecoration.VERTICAL).apply { - setDrawable(resources.getDrawable(R.drawable.divider)) - songs_list.addItemDecoration(this) - } - songs_list.adapter = adapter } else { val state = (songs_list.layoutManager as LinearLayoutManager).onSaveInstanceState() (currAdapter as SongAdapter).apply { diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/PlaylistsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/PlaylistsActivity.kt index baa8dd40..62e3e29d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/PlaylistsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/activities/PlaylistsActivity.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.musicplayer.activities import android.os.Bundle -import android.support.v7.widget.DividerItemDecoration import android.view.Menu import android.view.MenuItem import com.simplemobiletools.musicplayer.R @@ -10,6 +9,7 @@ import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog import com.simplemobiletools.musicplayer.extensions.dbHelper import com.simplemobiletools.musicplayer.extensions.playlistChanged import com.simplemobiletools.musicplayer.interfaces.RefreshItemsListener +import com.simplemobiletools.musicplayer.models.Playlist import kotlinx.android.synthetic.main.activity_playlists.* class PlaylistsActivity : SimpleActivity(), RefreshItemsListener { @@ -22,16 +22,13 @@ class PlaylistsActivity : SimpleActivity(), RefreshItemsListener { private fun getPlaylists() { dbHelper.getPlaylists { runOnUiThread { - playlists_list.apply { - adapter = PlaylistsAdapter(this@PlaylistsActivity, it, this@PlaylistsActivity) { - getPlaylists() - playlistChanged(it.id) - } - - DividerItemDecoration(context, DividerItemDecoration.VERTICAL).apply { - setDrawable(context.resources.getDrawable(R.drawable.divider)) - addItemDecoration(this) - } + PlaylistsAdapter(this@PlaylistsActivity, it, this@PlaylistsActivity, playlists_list) { + getPlaylists() + playlistChanged((it as Playlist).id) + }.apply { + setupDragListener(true) + addVerticalDividers(true) + playlists_list.adapter = this } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt index c52d8e95..cb603422 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/adapters/PlaylistsAdapter.kt @@ -1,15 +1,14 @@ package com.simplemobiletools.musicplayer.adapters import android.graphics.PorterDuff -import android.support.v7.view.ActionMode -import android.support.v7.widget.RecyclerView -import android.view.* -import com.bignerdranch.android.multiselector.ModalMultiSelectorCallback -import com.bignerdranch.android.multiselector.MultiSelector -import com.bignerdranch.android.multiselector.SwappingHolder +import android.view.Menu +import android.view.View +import android.view.ViewGroup +import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter import com.simplemobiletools.commons.extensions.beInvisibleIf import com.simplemobiletools.commons.extensions.deleteFiles import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.activities.SimpleActivity import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog @@ -23,73 +22,49 @@ import kotlinx.android.synthetic.main.item_playlist.view.* import java.io.File import java.util.* -class PlaylistsAdapter(val activity: SimpleActivity, val mItems: List, val listener: RefreshItemsListener?, val itemClick: (Playlist) -> Unit) : - RecyclerView.Adapter() { - val multiSelector = MultiSelector() - val views = ArrayList() - - companion object { - var actMode: ActionMode? = null - val markedItems = HashSet() - var textColor = 0 - var itemCnt = 0 - - fun toggleItemSelection(itemView: View, select: Boolean, pos: Int = -1) { - itemView.playlist_frame.isSelected = select - if (pos == -1) - return - - if (select) - markedItems.add(pos) - else - markedItems.remove(pos) - } - - fun updateTitle(cnt: Int) { - actMode?.title = "$cnt / $itemCnt" - actMode?.invalidate() - } - } +class PlaylistsAdapter(activity: SimpleActivity, val playlists: ArrayList, val listener: RefreshItemsListener?, recyclerView: MyRecyclerView, + itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { init { - textColor = activity.config.textColor - itemCnt = mItems.size + selectableItemCount = playlists.count() } - private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { - override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { - when (item.itemId) { - R.id.cab_delete -> askConfirmDelete() - R.id.cab_rename -> showRenameDialog() - else -> return false - } - return true - } + override fun getActionMenuId() = R.menu.cab_playlists - override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean { - super.onCreateActionMode(actionMode, menu) - actMode = actionMode - activity.menuInflater.inflate(R.menu.cab_playlists, menu) - return true - } + override fun prepareItemSelection(view: View) {} - override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean { - menu.findItem(R.id.cab_rename).isVisible = multiSelector.selectedPositions.size <= 1 - return true - } + override fun markItemSelection(select: Boolean, view: View?) { + view?.playlist_frame?.isSelected = select + } - override fun onDestroyActionMode(actionMode: ActionMode?) { - super.onDestroyActionMode(actionMode) - views.forEach { toggleItemSelection(it, false) } - markedItems.clear() + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = createViewHolder(R.layout.item_playlist, parent) + + override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { + val playlist = playlists[position] + val view = holder.bindView(playlist) { itemView, layoutPosition -> + setupView(itemView, playlist, layoutPosition) + } + bindViewHolder(holder, position, view) + } + + override fun getItemCount() = playlists.size + + override fun prepareActionMode(menu: Menu) { + menu.apply { + findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1 + } + } + + override fun actionItemPressed(id: Int) { + when (id) { + R.id.cab_delete -> askConfirmDelete() + R.id.cab_rename -> showRenameDialog() } } private fun askConfirmDelete() { RemovePlaylistDialog(activity) { - actMode?.finish() - val selections = multiSelector.selectedPositions - val ids = selections.map { mItems[it].id } as ArrayList + val ids = selectedPositions.map { playlists[it].id } as ArrayList if (it) { deletePlaylistSongs(ids) { removePlaylists(ids) @@ -97,6 +72,7 @@ class PlaylistsAdapter(val activity: SimpleActivity, val mItems: List, } else { removePlaylists(ids) } + finishActMode() } } @@ -121,68 +97,18 @@ class PlaylistsAdapter(val activity: SimpleActivity, val mItems: List, } private fun showRenameDialog() { - val selections = multiSelector.selectedPositions - NewPlaylistDialog(activity, mItems[selections[0]]) { - actMode?.finish() + NewPlaylistDialog(activity, playlists[selectedPositions.first()]) { + finishActMode() listener?.refreshItems() } } - override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent?.context).inflate(R.layout.item_playlist, parent, false) - return ViewHolder(view, activity, multiSelectorMode, multiSelector, itemClick) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - views.add(holder.bindView(mItems[position])) - } - - override fun getItemCount() = mItems.size - - class ViewHolder(view: View, val activity: SimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback, val multiSelector: MultiSelector, - val itemClick: (Playlist) -> (Unit)) : SwappingHolder(view, MultiSelector()) { - fun bindView(playlist: Playlist): View { - - itemView.apply { - playlist_title.text = playlist.title - toggleItemSelection(this, markedItems.contains(layoutPosition), layoutPosition) - - playlist_title.setTextColor(textColor) - playlist_icon.setColorFilter(textColor, PorterDuff.Mode.SRC_IN) - playlist_icon.beInvisibleIf(playlist.id != context.config.currentPlaylist) - - setOnClickListener { viewClicked(playlist) } - setOnLongClickListener { viewLongClicked(); true } - } - - return itemView - } - - private fun viewClicked(playlist: Playlist) { - if (multiSelector.isSelectable) { - val isSelected = multiSelector.selectedPositions.contains(layoutPosition) - multiSelector.setSelected(this, !isSelected) - toggleItemSelection(itemView, !isSelected, layoutPosition) - - val selectedCnt = multiSelector.selectedPositions.size - if (selectedCnt == 0) { - actMode?.finish() - } else { - updateTitle(selectedCnt) - } - actMode?.invalidate() - } else { - itemClick(playlist) - } - } - - private fun viewLongClicked() { - if (!multiSelector.isSelectable) { - activity.startSupportActionMode(multiSelectorCallback) - multiSelector.setSelected(this@ViewHolder, true) - updateTitle(multiSelector.selectedPositions.size) - toggleItemSelection(itemView, true, layoutPosition) - } + private fun setupView(view: View, playlist: Playlist, layoutPosition: Int) { + view.apply { + playlist_title.text = playlist.title + playlist_title.setTextColor(textColor) + playlist_icon.setColorFilter(textColor, PorterDuff.Mode.SRC_IN) + playlist_icon.beInvisibleIf(playlist.id != context.config.currentPlaylist) } } } diff --git a/app/src/main/res/layout/activity_playlists.xml b/app/src/main/res/layout/activity_playlists.xml index 4fa15fd4..04bad351 100644 --- a/app/src/main/res/layout/activity_playlists.xml +++ b/app/src/main/res/layout/activity_playlists.xml @@ -1,5 +1,5 @@ -