From 4123f67ad3fb64147e69b0561ca408dd98f6fe9c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 6 Jan 2018 16:10:13 +0100 Subject: [PATCH] rewrite scrolling to improve performance --- app/build.gradle | 2 +- .../gallery/activities/MainActivity.kt | 12 ++++++++- .../gallery/activities/MediaActivity.kt | 12 ++++++++- .../gallery/adapters/DirectoryAdapter.kt | 4 ++- .../gallery/adapters/ManageFoldersAdapter.kt | 2 +- .../adapters/ManageHiddenFoldersAdapter.kt | 2 +- .../gallery/adapters/MediaAdapter.kt | 25 ++++++++++++++++--- app/src/main/res/layout/activity_main.xml | 8 +++--- app/src/main/res/layout/activity_media.xml | 8 +++--- 9 files changed, 59 insertions(+), 16 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3c49e690e..b18fe5c96 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -42,7 +42,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:3.5.0' + implementation 'com.simplemobiletools:commons:3.5.9' implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.9.0' implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0' implementation 'com.android.support:multidex:1.0.2' 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 9694a0348..756273df9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -402,12 +402,16 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun increaseColumnCount() { + directories_vertical_fastscroller.measureRecyclerViewOnRedraw() + directories_horizontal_fastscroller.measureRecyclerViewOnRedraw() config.dirColumnCnt = ++(directories_grid.layoutManager as GridLayoutManager).spanCount invalidateOptionsMenu() directories_grid.adapter?.notifyDataSetChanged() } private fun reduceColumnCount() { + directories_vertical_fastscroller.measureRecyclerViewOnRedraw() + directories_horizontal_fastscroller.measureRecyclerViewOnRedraw() config.dirColumnCnt = --(directories_grid.layoutManager as GridLayoutManager).spanCount invalidateOptionsMenu() directories_grid.adapter?.notifyDataSetChanged() @@ -533,6 +537,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { directories_empty_text_label.beVisibleIf(dirs.isEmpty() && !isFromCache) directories_empty_text.beVisibleIf(dirs.isEmpty() && !isFromCache) + directories_grid.beVisibleIf(directories_empty_text_label.isGone()) + + val allowHorizontalScroll = config.scrollHorizontally && config.viewTypeFiles == VIEW_TYPE_GRID + directories_vertical_fastscroller.beVisibleIf(directories_grid.isVisible() && !allowHorizontalScroll) + directories_horizontal_fastscroller.beVisibleIf(directories_grid.isVisible() && allowHorizontalScroll) checkLastMediaChanged() if (dirs.hashCode() == mDirs.hashCode()) { @@ -559,7 +568,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { val currAdapter = directories_grid.adapter if (currAdapter == null) { initZoomListener() - DirectoryAdapter(this, mDirs, this, directories_grid, isPickIntent(intent) || isGetAnyContentIntent(intent)) { + val fastscroller = if (config.scrollHorizontally) directories_horizontal_fastscroller else directories_vertical_fastscroller + DirectoryAdapter(this, mDirs, this, directories_grid, isPickIntent(intent) || isGetAnyContentIntent(intent), fastscroller) { itemClicked((it as Directory).path) }.apply { setupZoomListener(mZoomListener) 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 5f0ab42fa..db51d1c59 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -182,7 +182,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { val currAdapter = media_grid.adapter if (currAdapter == null) { initZoomListener() - MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid) { + val fastscroller = if (config.scrollHorizontally) media_horizontal_fastscroller else media_vertical_fastscroller + MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller) { itemClicked((it as Medium).path) }.apply { setupZoomListener(mZoomListener) @@ -466,12 +467,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } private fun increaseColumnCount() { + media_vertical_fastscroller.measureRecyclerViewOnRedraw() + media_horizontal_fastscroller.measureRecyclerViewOnRedraw() config.mediaColumnCnt = ++(media_grid.layoutManager as GridLayoutManager).spanCount invalidateOptionsMenu() media_grid.adapter?.notifyDataSetChanged() } private fun reduceColumnCount() { + media_vertical_fastscroller.measureRecyclerViewOnRedraw() + media_horizontal_fastscroller.measureRecyclerViewOnRedraw() config.mediaColumnCnt = --(media_grid.layoutManager as GridLayoutManager).spanCount invalidateOptionsMenu() media_grid.adapter?.notifyDataSetChanged() @@ -548,6 +553,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { media_empty_text_label.beVisibleIf(media.isEmpty() && !isFromCache) media_empty_text.beVisibleIf(media.isEmpty() && !isFromCache) + media_grid.beVisibleIf(media_empty_text_label.isGone()) + + val allowHorizontalScroll = config.scrollHorizontally && config.viewTypeFiles == VIEW_TYPE_GRID + media_vertical_fastscroller.beVisibleIf(media_grid.isVisible() && !allowHorizontalScroll) + media_horizontal_fastscroller.beVisibleIf(media_grid.isVisible() && allowHorizontalScroll) checkLastMediaChanged() if (mLastDrawnHashCode == 0) 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 a8ee3f75f..9ed24a0c8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -11,6 +11,7 @@ import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog @@ -24,7 +25,8 @@ import java.io.File import java.util.* class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList, val listener: DirOperationsListener?, recyclerView: MyRecyclerView, - val isPickIntent: Boolean, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { + val isPickIntent: Boolean, fastScroller: FastScroller? = null, itemClick: (Any) -> Unit) : + MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { private val config = activity.config private val isListViewType = config.viewTypeFolders == VIEW_TYPE_LIST diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageFoldersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageFoldersAdapter.kt index 73818a600..2974f0b2e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageFoldersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageFoldersAdapter.kt @@ -14,7 +14,7 @@ import kotlinx.android.synthetic.main.item_manage_folder.view.* import java.util.* class ManageFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList, val isShowingExcludedFolders: Boolean, val listener: RefreshRecyclerViewListener?, - recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { + recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { private val config = activity.config diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageHiddenFoldersAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageHiddenFoldersAdapter.kt index 4126a5c21..8bc51ea3a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageHiddenFoldersAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/ManageHiddenFoldersAdapter.kt @@ -16,7 +16,7 @@ import java.io.File import java.util.* class ManageHiddenFoldersAdapter(activity: BaseSimpleActivity, var folders: ArrayList, val listener: RefreshRecyclerViewListener?, - recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { + recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { private val config = activity.config 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 f2c3dfb17..a4a06b2d7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/MediaAdapter.kt @@ -1,6 +1,8 @@ package com.simplemobiletools.gallery.adapters import android.net.Uri +import android.os.Handler +import android.os.Looper import android.view.Menu import android.view.View import android.view.ViewGroup @@ -12,22 +14,26 @@ import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.beVisibleIf import com.simplemobiletools.commons.extensions.isActivityDestroyed +import com.simplemobiletools.commons.views.FastScroller import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST import com.simplemobiletools.gallery.models.Medium -import kotlinx.android.synthetic.main.photo_video_item_list.view.* +import kotlinx.android.synthetic.main.photo_video_item_grid.view.* import java.io.File import java.util.* class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, - val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { + val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, fastScroller: FastScroller? = null, + itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) { private val config = activity.config private val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST private var skipConfirmationDialog = false + private var visibleItemPaths = ArrayList() + private var delayHandler = Handler(Looper.getMainLooper()) private var scrollHorizontally = config.scrollHorizontally private var animateGifs = config.animateGifs @@ -51,6 +57,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { val medium = media[position] + visibleItemPaths.add(medium.path) val view = holder.bindView(medium, !allowMultiplePicks) { itemView, layoutPosition -> setupView(itemView, medium) } @@ -92,7 +99,9 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, override fun onViewRecycled(holder: ViewHolder?) { super.onViewRecycled(holder) if (!activity.isActivityDestroyed()) { - Glide.with(activity).clear(holder?.itemView?.medium_thumbnail) + val itemView = holder?.itemView + visibleItemPaths.remove(itemView?.photo_name?.tag) + Glide.with(activity).clear(itemView?.medium_thumbnail) } } @@ -259,7 +268,15 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, play_outline.beVisibleIf(medium.video) photo_name.beVisibleIf(displayFilenames || isListViewType) photo_name.text = medium.name - activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) + photo_name.tag = medium.path + + medium_thumbnail.isHorizontalScrolling = scrollHorizontally + delayHandler.postDelayed({ + val isVisible = visibleItemPaths.contains(medium.path) + if (isVisible) { + activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) + } + }, 200) if (isListViewType) { photo_name.setTextColor(textColor) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a01449b1a..033563bb1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -36,7 +36,7 @@ @@ -48,7 +48,8 @@ android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:paddingLeft="@dimen/normal_margin" - android:paddingStart="@dimen/normal_margin"> + android:paddingStart="@dimen/normal_margin" + android:visibility="gone"> @@ -61,7 +62,8 @@ android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" - android:paddingTop="@dimen/normal_margin"> + android:paddingTop="@dimen/normal_margin" + android:visibility="gone"> diff --git a/app/src/main/res/layout/activity_media.xml b/app/src/main/res/layout/activity_media.xml index 918e39428..b4bcb9c16 100644 --- a/app/src/main/res/layout/activity_media.xml +++ b/app/src/main/res/layout/activity_media.xml @@ -36,7 +36,7 @@ @@ -48,7 +48,8 @@ android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:paddingLeft="@dimen/normal_margin" - android:paddingStart="@dimen/normal_margin"> + android:paddingStart="@dimen/normal_margin" + android:visibility="gone"> @@ -61,7 +62,8 @@ android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" - android:paddingTop="@dimen/normal_margin"> + android:paddingTop="@dimen/normal_margin" + android:visibility="gone">