From 6c4ec53cc1d1e26e633c99a4e0d8993056cbd62e Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 16 Nov 2016 23:50:46 +0100 Subject: [PATCH] replace directory baseadapter with recyclerview adapter --- .../gallery/activities/MainActivity.kt | 41 ++++----- .../gallery/adapters/DirectoryAdapter.kt | 83 +++++++------------ .../gallery/dialogs/PickAlbumDialog.kt | 32 ++----- app/src/main/res/layout/activity_main.xml | 10 +-- .../main/res/layout/dialog_album_picker.xml | 11 +-- app/src/main/res/layout/directory_item.xml | 3 +- app/src/main/res/values-land/integers.xml | 3 + app/src/main/res/values/integers.xml | 3 + 8 files changed, 72 insertions(+), 114 deletions(-) create mode 100644 app/src/main/res/values-land/integers.xml create mode 100644 app/src/main/res/values/integers.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index a0cf72ba1..ded305ff9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -17,17 +17,13 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.AdapterView -import android.widget.GridView -import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask import com.simplemobiletools.filepicker.extensions.* -import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.Utils import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog -import com.simplemobiletools.gallery.dialogs.CopyDialog import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.activity_main.* @@ -109,14 +105,14 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef override fun onResume() { super.onResume() tryloadGallery() - if (mState != null) - directories_grid.onRestoreInstanceState(mState) + /*if (mState != null) + directories_grid.onRestoreInstanceState(mState)*/ } override fun onPause() { super.onPause() deleteDirs() - mState = directories_grid.onSaveInstanceState() + //mState = directories_grid.onSaveInstanceState() } override fun onDestroy() { @@ -164,7 +160,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef private fun prepareForDeleting() { toast(R.string.deleting) - val items = directories_grid.checkedItemPositions + /*val items = directories_grid.checkedItemPositions val cnt = items.size() var deletedCnt = 0 for (i in 0..cnt - 1) { @@ -182,7 +178,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef } } - notifyDeletion(deletedCnt) + notifyDeletion(deletedCnt)*/ } private fun notifyDeletion(cnt: Int) { @@ -245,7 +241,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef } private fun showProperties() { - val items = directories_grid.checkedItemPositions + /*val items = directories_grid.checkedItemPositions if (items.size() == 1) { PropertiesDialog(this, selectedPaths.toTypedArray()[0], false) } else { @@ -259,11 +255,11 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef } PropertiesDialog(this, paths, false) - } + }*/ } private fun editDirectory() { - val items = directories_grid.checkedItemPositions + /*val items = directories_grid.checkedItemPositions val cnt = items.size() for (i in 0..cnt - 1) { if (items.valueAt(i)) { @@ -272,7 +268,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef renameDir(path) break } - } + }*/ } private fun renameDir(path: String) { @@ -292,7 +288,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef private fun displayCopyDialog() { val files = ArrayList() - val items = directories_grid.checkedItemPositions + /*val items = directories_grid.checkedItemPositions val cnt = items.size() for (i in 0..cnt - 1) { if (items.valueAt(i)) { @@ -315,7 +311,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef override fun copyFailed() { toast(R.string.copy_move_failed) } - }) + })*/ } private fun isPickImageIntent(intent: Intent) = isPickIntent(intent) && (hasImageContentData(intent) || isImageType(intent)) @@ -487,7 +483,8 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef private val selectedPaths: Set get() { - val items = directories_grid.checkedItemPositions + return HashSet() + /*val items = directories_grid.checkedItemPositions val selectedPaths = HashSet() val cnt = items.size() for (i in 0..cnt - 1) { @@ -496,7 +493,7 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef selectedPaths.add(mDirs[id].path) } } - return selectedPaths + return selectedPaths*/ } private fun scanCompleted(path: String) { @@ -520,13 +517,9 @@ class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRef } mDirs = dirs - val adapter = DirectoryAdapter(this, mDirs) - directories_grid.apply { - this@apply.adapter = adapter - onItemClickListener = this@MainActivity - //setMultiChoiceModeListener(this) - //setOnTouchListener(this) - choiceMode = GridView.CHOICE_MODE_MULTIPLE_MODAL + val adapter = DirectoryAdapter(this, mDirs) { + } + directories_grid.adapter = adapter } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 1891e28e5..9c2236863 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -1,12 +1,12 @@ package com.simplemobiletools.gallery.adapters import android.content.Context +import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.BaseAdapter -import android.widget.ImageView -import android.widget.TextView +import com.bignerdranch.android.multiselector.MultiSelector +import com.bignerdranch.android.multiselector.SwappingHolder import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.signature.StringSignature @@ -15,59 +15,36 @@ import com.simplemobiletools.gallery.models.Directory import kotlinx.android.synthetic.main.directory_item.view.* import kotlinx.android.synthetic.main.directory_tmb.view.* -class DirectoryAdapter(private val mContext: Context, private val mDirs: MutableList) : BaseAdapter() { - private val mInflater: LayoutInflater +class DirectoryAdapter(val mContext: Context, val mDirs: MutableList, val itemClick: (Directory) -> Unit) : + RecyclerView.Adapter() { - init { - mInflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + override fun getItemCount() = mDirs.size + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.bindView(mContext, mDirs[position]) } - override fun getView(position: Int, view: View?, parent: ViewGroup): View { - var convertView = view - val viewHolder: ViewHolder - if (convertView == null) { - convertView = mInflater.inflate(R.layout.directory_item, parent, false) - viewHolder = ViewHolder(convertView) - convertView!!.tag = viewHolder - } else { - viewHolder = convertView.tag as ViewHolder + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { + val view = LayoutInflater.from(parent?.context).inflate(R.layout.directory_item, parent, false) + return ViewHolder(view, itemClick) + } + + class ViewHolder(view: View, val itemClick: (Directory) -> (Unit)) : SwappingHolder(view, MultiSelector()) { + fun bindView(context: Context, directory: Directory) { + itemView.dir_name.text = directory.name + itemView.photo_cnt.text = directory.mediaCnt.toString() + + val tmb = directory.thumbnail + val timestampSignature = StringSignature(directory.timestamp.toString()) + if (tmb.endsWith(".gif")) { + Glide.with(context).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) + .placeholder(R.color.tmb_background).centerCrop().crossFade().into(itemView.dir_thumbnail) + } else { + Glide.with(context).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) + .placeholder(R.color.tmb_background).centerCrop().crossFade().into(itemView.dir_thumbnail) + } + + itemView.setOnClickListener { itemClick(directory) } } - - val dir = mDirs[position] - viewHolder.dirName.text = formatDirectoryName(dir) - viewHolder.photoCnt.text = dir.mediaCnt.toString() - val tmb = dir.thumbnail - val timestampSignature = StringSignature(dir.timestamp.toString()) - if (tmb.endsWith(".gif")) { - Glide.with(mContext).load(tmb).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) - .placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.dirThumbnail) - } else { - Glide.with(mContext).load(tmb).diskCacheStrategy(DiskCacheStrategy.RESULT).signature(timestampSignature) - .placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.dirThumbnail) - } - - return convertView - } - - private fun formatDirectoryName(dir: Directory): String { - return dir.name - } - - override fun getCount(): Int { - return mDirs.size - } - - override fun getItem(position: Int): Any { - return mDirs[position] - } - - override fun getItemId(position: Int): Long { - return 0 - } - - internal class ViewHolder(view: View) { - val dirName: TextView = view.dir_name - val photoCnt: TextView = view.photo_cnt - val dirThumbnail: ImageView = view.dir_thumbnail } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt index b78139533..132112229 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickAlbumDialog.kt @@ -3,50 +3,36 @@ package com.simplemobiletools.gallery.dialogs import android.app.Activity import android.support.v7.app.AlertDialog import android.view.LayoutInflater -import android.view.View -import android.widget.AdapterView -import android.widget.GridView import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.models.Directory -import kotlinx.android.synthetic.main.activity_main.view.* import java.util.* -class PickAlbumDialog(val activity: Activity, val listener: OnPickAlbumListener) : AdapterView.OnItemClickListener, GetDirectoriesAsynctask.GetDirectoriesListener { +class PickAlbumDialog(val activity: Activity, val listener: OnPickAlbumListener) : GetDirectoriesAsynctask.GetDirectoriesListener { val context = activity.applicationContext - var grid: GridView? = null - var dirs = ArrayList() - var dialog: AlertDialog? = null + var dialog: AlertDialog init { val view = LayoutInflater.from(context).inflate(R.layout.dialog_album_picker, null) - grid = view.directories_grid dialog = AlertDialog.Builder(activity) .setTitle(context.resources.getString(R.string.select_destination)) .setView(view) .setPositiveButton(R.string.ok, null) .setNegativeButton(R.string.cancel, null) - .create().apply { - show() - } + .create() + + dialog.show() GetDirectoriesAsynctask(context, false, false, ArrayList(), this).execute() } - override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { - listener.onSuccess(dirs[position].path) - dialog?.dismiss() - } - override fun gotDirectories(dirs: ArrayList) { - this.dirs = dirs - - val adapter = DirectoryAdapter(context, dirs) - - grid?.adapter = adapter - grid?.onItemClickListener = this + DirectoryAdapter(context, dirs) { + listener.onSuccess(it.path) + dialog.dismiss() + } } interface OnPickAlbumListener { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3fb3717e8..2310cd4ea 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,7 @@ @@ -10,15 +11,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - + app:layoutManager="android.support.v7.widget.GridLayoutManager" + app:spanCount="@integer/columns"/> diff --git a/app/src/main/res/layout/dialog_album_picker.xml b/app/src/main/res/layout/dialog_album_picker.xml index 774a991a9..419ee1c42 100644 --- a/app/src/main/res/layout/dialog_album_picker.xml +++ b/app/src/main/res/layout/dialog_album_picker.xml @@ -1,12 +1,9 @@ - + app:layoutManager="android.support.v7.widget.GridLayoutManager" + app:spanCount="@integer/columns"/> diff --git a/app/src/main/res/layout/directory_item.xml b/app/src/main/res/layout/directory_item.xml index 696c8ebbd..be4d6acf9 100644 --- a/app/src/main/res/layout/directory_item.xml +++ b/app/src/main/res/layout/directory_item.xml @@ -3,7 +3,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dir_holder" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content" + android:padding="1px"> diff --git a/app/src/main/res/values-land/integers.xml b/app/src/main/res/values-land/integers.xml new file mode 100644 index 000000000..64ff1f622 --- /dev/null +++ b/app/src/main/res/values-land/integers.xml @@ -0,0 +1,3 @@ + + 4 + diff --git a/app/src/main/res/values/integers.xml b/app/src/main/res/values/integers.xml new file mode 100644 index 000000000..c1976443b --- /dev/null +++ b/app/src/main/res/values/integers.xml @@ -0,0 +1,3 @@ + + 2 +