From 0824c66784bdf4e6028a9424a068784839452809 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 19 Nov 2016 19:20:50 +0100 Subject: [PATCH] change the media gridview to recyclerview --- .../gallery/activities/MediaActivity.kt | 40 +++--- .../gallery/adapters/MediaAdapter.kt | 126 +++++++++++------- app/src/main/res/layout/activity_media.xml | 11 +- app/src/main/res/layout/photo_video_item.xml | 5 +- app/src/main/res/values-w480dp/integers.xml | 1 + app/src/main/res/values-w600dp/integers.xml | 1 + app/src/main/res/values/integers.xml | 1 + 7 files changed, 109 insertions(+), 76 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index de4d13662..787dce047 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -5,7 +5,6 @@ import android.app.WallpaperManager import android.content.Intent import android.database.Cursor import android.graphics.Bitmap -import android.graphics.Color import android.net.Uri import android.os.Bundle import android.provider.MediaStore @@ -20,16 +19,15 @@ import android.widget.AdapterView import com.bumptech.glide.Glide import com.bumptech.glide.request.animation.GlideAnimation import com.bumptech.glide.request.target.SimpleTarget -import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask -import com.simplemobiletools.filepicker.extensions.* -import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog +import com.simplemobiletools.filepicker.extensions.hasStoragePermission +import com.simplemobiletools.filepicker.extensions.scanFiles +import com.simplemobiletools.filepicker.extensions.scanPath +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog -import com.simplemobiletools.gallery.dialogs.CopyDialog import com.simplemobiletools.gallery.extensions.getHumanizedFilename -import com.simplemobiletools.gallery.extensions.shareMedium import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.activity_media.* import java.io.File @@ -37,7 +35,7 @@ import java.io.IOException import java.util.* import java.util.regex.Pattern -class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.OnTouchListener, SwipeRefreshLayout.OnRefreshListener { +class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.OnTouchListener, SwipeRefreshLayout.OnRefreshListener, MediaAdapter.MediaOperationsListener { companion object { private val TAG = MediaActivity::class.java.simpleName @@ -75,7 +73,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On override fun onPause() { super.onPause() - deleteFiles() + //deleteFiles() } private fun tryloadGallery() { @@ -96,9 +94,11 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On if (isDirEmpty()) return - val adapter = MediaAdapter(this, mMedia) + val adapter = MediaAdapter(this, mMedia, this) { + + } + media_grid.adapter = adapter - media_grid.onItemClickListener = this media_grid.setOnTouchListener(this) mIsSnackbarShown = false @@ -232,12 +232,12 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On } private fun shareMedia() { - val selectedMedia = getSelectedMedia() + /*val selectedMedia = getSelectedMedia() if (selectedMedia.size <= 1) { shareMedium(selectedMedia[0]) } else { shareMedia(selectedMedia) - } + }*/ } private fun shareMedia(media: List) { @@ -254,7 +254,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On } } - private fun getSelectedMedia(): List { + /*private fun getSelectedMedia(): List { val items = media_grid.checkedItemPositions val cnt = items.size() val media = (0..cnt - 1) @@ -347,30 +347,30 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On mToBeDeleted.clear() mMedia = getMedia() updateGridView() - } + }*/ private fun updateGridView() { if (!isDirEmpty()) { - (media_grid.adapter as MediaAdapter).updateItems(mMedia) + //(media_grid.adapter as MediaAdapter).updateItems(mMedia) } } private fun showProperties() { - val selectedMedia = getSelectedMedia() + /*val selectedMedia = getSelectedMedia() if (selectedMedia.size == 1) { PropertiesDialog(this, selectedMedia[0].path, false) } else { val paths = ArrayList(selectedMedia.size) selectedMedia.mapTo(paths) { it.path } PropertiesDialog(this, paths, false) - } + }*/ } private fun isSetWallpaperIntent() = intent.getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false) private fun displayCopyDialog() { - val items = media_grid.checkedItemPositions + /*val items = media_grid.checkedItemPositions val cnt = items.size() val files = (0..cnt - 1) .filter { items.valueAt(it) } @@ -390,7 +390,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On override fun copyFailed() { toast(R.string.copy_move_failed) } - }) + })*/ } override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { @@ -484,7 +484,7 @@ class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener, View.On override fun onTouch(v: View, event: MotionEvent): Boolean { if (mIsSnackbarShown) { - deleteFiles() + //deleteFiles() } return false diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt index 09d85beec..06e59dbd0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -1,58 +1,73 @@ package com.simplemobiletools.gallery.adapters -import android.content.Context -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 android.os.Build +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 com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.signature.StringSignature import com.simplemobiletools.gallery.Config import com.simplemobiletools.gallery.R +import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.extensions.beVisibleIf import com.simplemobiletools.gallery.models.Medium import kotlinx.android.synthetic.main.photo_video_item.view.* import kotlinx.android.synthetic.main.photo_video_tmb.view.* +import java.util.* -class MediaAdapter(private val context: Context, private val media: MutableList) : BaseAdapter() { - private val mInflater: LayoutInflater - var displayFilenames = false +class MediaAdapter(val activity: SimpleActivity, val media: MutableList, val listener: MediaAdapter.MediaOperationsListener?, val itemClick: (Medium) -> Unit) : + RecyclerView.Adapter() { + val multiSelector = MultiSelector() + val views = ArrayList() + val config = Config.newInstance(activity) - init { - mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater - displayFilenames = Config.newInstance(context).displayFileNames + companion object { + var actMode: ActionMode? = null + var displayFilenames = false + + fun toggleItemSelection(itemView: View, select: Boolean) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) + itemView.medium_thumbnail_holder.isActivated = select + else + itemView.medium_thumbnail.isActivated = select + } } - override fun getView(position: Int, view: View?, parent: ViewGroup): View { - var convertView = view - val medium = media[position] - val viewHolder: ViewHolder - if (convertView == null) { - convertView = mInflater.inflate(R.layout.photo_video_item, parent, false) - viewHolder = ViewHolder(convertView) - convertView!!.tag = viewHolder - } else { - viewHolder = convertView.tag as ViewHolder + val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { + override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { + return false } - viewHolder.playOutline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE - - viewHolder.fileName.beVisibleIf(displayFilenames) - if (displayFilenames) - viewHolder.fileName.text = medium.name - val path = medium.path - val timestampSignature = StringSignature(medium.timestamp.toString()) - if (medium.isGif) { - Glide.with(context).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature).into(viewHolder.photoThumbnail) - } else { - Glide.with(context).load(path).diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) - .placeholder(R.color.tmb_background).centerCrop().crossFade().into(viewHolder.photoThumbnail) + override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean { + super.onCreateActionMode(actionMode, menu) + DirectoryAdapter.actMode = actionMode + activity.menuInflater.inflate(R.menu.cab_media, menu) + return true } - return convertView + override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean { + val menuItem = menu.findItem(R.id.cab_edit) + menuItem.isVisible = multiSelector.selectedPositions.size <= 1 + return true + } + + override fun onDestroyActionMode(actionMode: ActionMode?) { + super.onDestroyActionMode(actionMode) + views.forEach { MediaAdapter.toggleItemSelection(it, false) } + } + } + + override fun onBindViewHolder(holder: MediaAdapter.ViewHolder, position: Int) { + views.add(holder.bindView(activity, multiSelectorMode, multiSelector, media[position])) + } + + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MediaAdapter.ViewHolder { + val view = LayoutInflater.from(parent?.context).inflate(R.layout.photo_video_item, parent, false) + return MediaAdapter.ViewHolder(view, itemClick) } fun updateDisplayFilenames(display: Boolean) { @@ -60,21 +75,38 @@ class MediaAdapter(private val context: Context, private val media: MutableList< notifyDataSetChanged() } - override fun getCount() = media.size + override fun getItemCount() = media.size - override fun getItem(position: Int) = media[position] + class ViewHolder(view: View, val itemClick: (Medium) -> (Unit)) : SwappingHolder(view, MultiSelector()) { + fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium): View { + itemView.play_outline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE + itemView.file_name.beVisibleIf(displayFilenames) + itemView.file_name.text = medium.name - override fun getItemId(position: Int) = 0L + val path = medium.path + val timestampSignature = StringSignature(medium.timestamp.toString()) + if (medium.isGif) { + Glide.with(activity).load(path).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature).into(itemView.medium_thumbnail) + } else { + Glide.with(activity).load(path).diskCacheStrategy(DiskCacheStrategy.NONE).signature(timestampSignature) + .placeholder(R.color.tmb_background).centerCrop().crossFade().into(itemView.medium_thumbnail) + } - fun updateItems(newPhotos: List) { - media.clear() - media.addAll(newPhotos) - notifyDataSetChanged() + itemView.setOnClickListener { viewClicked(multiSelector, medium) } + + return itemView + } + + fun viewClicked(multiSelector: MultiSelector, medium: Medium) { + if (multiSelector.isSelectable) { + + } else { + itemClick(medium) + } + } } - internal class ViewHolder(view: View) { - val photoThumbnail: ImageView = view.medium_thumbnail - val playOutline: View = view.play_outline - val fileName: TextView = view.file_name + interface MediaOperationsListener { + } } diff --git a/app/src/main/res/layout/activity_media.xml b/app/src/main/res/layout/activity_media.xml index ef27a2d8f..c15507da6 100644 --- a/app/src/main/res/layout/activity_media.xml +++ b/app/src/main/res/layout/activity_media.xml @@ -1,6 +1,7 @@ @@ -10,16 +11,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - + app:layoutManager="android.support.v7.widget.GridLayoutManager" + app:spanCount="@integer/photo_columns"/> diff --git a/app/src/main/res/layout/photo_video_item.xml b/app/src/main/res/layout/photo_video_item.xml index fb7382661..7bd0a67c5 100644 --- a/app/src/main/res/layout/photo_video_item.xml +++ b/app/src/main/res/layout/photo_video_item.xml @@ -3,7 +3,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/media_item_holder" android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content" + android:padding="1px"> @@ -11,9 +12,9 @@ android:id="@+id/play_outline" android:layout_width="@dimen/play_outline_size" android:layout_height="@dimen/play_outline_size" - android:layout_margin="@dimen/tiny_margin" android:layout_alignParentRight="true" android:layout_alignParentTop="true" + android:layout_margin="@dimen/tiny_margin" android:src="@mipmap/play_outline" android:visibility="gone"/> diff --git a/app/src/main/res/values-w480dp/integers.xml b/app/src/main/res/values-w480dp/integers.xml index d3281dba5..e64fab620 100644 --- a/app/src/main/res/values-w480dp/integers.xml +++ b/app/src/main/res/values-w480dp/integers.xml @@ -1,3 +1,4 @@ 3 + 5 diff --git a/app/src/main/res/values-w600dp/integers.xml b/app/src/main/res/values-w600dp/integers.xml index d7fae348a..78dfa5f41 100644 --- a/app/src/main/res/values-w600dp/integers.xml +++ b/app/src/main/res/values-w600dp/integers.xml @@ -1,3 +1,4 @@ 4 + 7 diff --git a/app/src/main/res/values/integers.xml b/app/src/main/res/values/integers.xml index a39cb3fa1..17083bdab 100644 --- a/app/src/main/res/values/integers.xml +++ b/app/src/main/res/values/integers.xml @@ -1,3 +1,4 @@ 2 + 3