From 02b3f4d5c206165a48e8cc04bdca3b8b449c9a29 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 26 Jun 2019 12:47:13 +0200 Subject: [PATCH] copying some file thumbnail related code to the search screen --- .../gallery/pro/activities/SearchActivity.kt | 207 +++++++++++++++++- app/src/main/res/layout/activity_search.xml | 12 - 2 files changed, 198 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt index b4c678cc2..58574ecb5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt @@ -2,17 +2,31 @@ package com.simplemobiletools.gallery.pro.activities import android.app.SearchManager import android.content.Context +import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.appcompat.widget.SearchView import androidx.core.view.MenuItemCompat +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.models.FileDirItem +import com.simplemobiletools.commons.views.MyGridLayoutManager import com.simplemobiletools.gallery.pro.R +import com.simplemobiletools.gallery.pro.adapters.MediaAdapter import com.simplemobiletools.gallery.pro.asynctasks.GetMediaAsynctask +import com.simplemobiletools.gallery.pro.extensions.config import com.simplemobiletools.gallery.pro.extensions.getCachedMedia +import com.simplemobiletools.gallery.pro.extensions.openPath +import com.simplemobiletools.gallery.pro.helpers.* +import com.simplemobiletools.gallery.pro.interfaces.MediaOperationsListener +import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.models.ThumbnailItem +import com.simplemobiletools.gallery.pro.models.ThumbnailSection +import kotlinx.android.synthetic.main.activity_search.* -class SearchActivity : SimpleActivity() { +class SearchActivity : SimpleActivity(), MediaOperationsListener { private var mIsSearchOpen = false private var mSearchMenuItem: MenuItem? = null private var mCurrAsyncTask: GetMediaAsynctask? = null @@ -21,14 +35,10 @@ class SearchActivity : SimpleActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_search) + media_empty_text_label.setTextColor(config.textColor) getAllMedia() } - override fun onStop() { - super.onStop() - mSearchMenuItem?.collapseActionView() - } - override fun onDestroy() { super.onDestroy() mCurrAsyncTask?.stopFetching() @@ -68,7 +78,6 @@ class SearchActivity : SimpleActivity() { override fun onMenuItemActionCollapse(item: MenuItem?): Boolean { if (mIsSearchOpen) { mIsSearchOpen = false - textChanged("") } return true } @@ -77,13 +86,184 @@ class SearchActivity : SimpleActivity() { } private fun textChanged(text: String) { + Thread { + try { + val filtered = mAllMedia.filter { it is Medium && it.name.contains(text, true) } as ArrayList + filtered.sortBy { it is Medium && !it.name.startsWith(text, true) } + val grouped = MediaFetcher(applicationContext).groupMedia(filtered as ArrayList, "") + runOnUiThread { + if (grouped.isEmpty()) { + media_empty_text_label.text = getString(R.string.no_items_found) + media_empty_text_label.beVisible() + } else { + media_empty_text_label.beGone() + } + getMediaAdapter()?.updateMedia(grouped) + measureRecyclerViewContent(grouped) + } + } catch (ignored: Exception) { + } + }.start() + } + + private fun setupAdapter() { + val currAdapter = media_grid.adapter + if (currAdapter == null) { + val fastscroller = if (config.scrollHorizontally) media_horizontal_fastscroller else media_vertical_fastscroller + MediaAdapter(this, ArrayList(), this, false, false, "", media_grid, fastscroller) { + if (it is Medium) { + itemClicked(it.path) + } + }.apply { + media_grid.adapter = this + } + setupLayoutManager() + } else { + (currAdapter as MediaAdapter).updateMedia(MediaActivity.mMedia) + } + + measureRecyclerViewContent(mAllMedia) + setupScrollDirection() + } + + private fun getMediaAdapter() = media_grid.adapter as? MediaAdapter + + private fun itemClicked(path: String) { + val isVideo = path.isVideoFast() + if (isVideo) { + openPath(path, false) + } else { + Intent(this, ViewPagerActivity::class.java).apply { + putExtra(PATH, path) + putExtra(SHOW_ALL, false) + startActivity(this) + } + } + } + + private fun setupLayoutManager() { + val viewType = config.getFolderViewType(SHOW_ALL) + if (viewType == VIEW_TYPE_GRID) { + setupGridLayoutManager() + } else { + setupListLayoutManager() + } + } + + private fun setupGridLayoutManager() { + val layoutManager = media_grid.layoutManager as MyGridLayoutManager + /*if (config.scrollHorizontally) { + layoutManager.orientation = RecyclerView.HORIZONTAL + media_refresh_layout.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT) + } else { + layoutManager.orientation = RecyclerView.VERTICAL + media_refresh_layout.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + }*/ + + layoutManager.spanCount = config.mediaColumnCnt + val adapter = getMediaAdapter() + layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { + override fun getSpanSize(position: Int): Int { + return if (adapter?.isASectionTitle(position) == true) { + layoutManager.spanCount + } else { + 1 + } + } + } + } + + private fun setupListLayoutManager() { + val layoutManager = media_grid.layoutManager as MyGridLayoutManager + layoutManager.spanCount = 1 + layoutManager.orientation = RecyclerView.VERTICAL + } + + private fun setupScrollDirection() { + val viewType = config.getFolderViewType(SHOW_ALL) + val allowHorizontalScroll = config.scrollHorizontally && viewType == VIEW_TYPE_GRID + media_vertical_fastscroller.isHorizontal = false + media_vertical_fastscroller.beGoneIf(allowHorizontalScroll) + + media_horizontal_fastscroller.isHorizontal = true + media_horizontal_fastscroller.beVisibleIf(allowHorizontalScroll) + + val sorting = config.getFileSorting(SHOW_ALL) + if (allowHorizontalScroll) { + media_horizontal_fastscroller.allowBubbleDisplay = config.showInfoBubble + media_horizontal_fastscroller.setViews(media_grid) { + media_horizontal_fastscroller.updateBubbleText(getBubbleTextItem(it, sorting)) + } + } else { + media_vertical_fastscroller.allowBubbleDisplay = config.showInfoBubble + media_vertical_fastscroller.setViews(media_grid) { + media_vertical_fastscroller.updateBubbleText(getBubbleTextItem(it, sorting)) + } + } + } + + private fun getBubbleTextItem(index: Int, sorting: Int): String { + var realIndex = index + val mediaAdapter = getMediaAdapter() + if (mediaAdapter?.isASectionTitle(index) == true) { + realIndex++ + } + return mediaAdapter?.getItemBubbleText(realIndex, sorting) ?: "" + } + + private fun measureRecyclerViewContent(media: ArrayList) { + media_grid.onGlobalLayout { + if (config.scrollHorizontally) { + calculateContentWidth(media) + } else { + calculateContentHeight(media) + } + } + } + + private fun calculateContentWidth(media: ArrayList) { + val layoutManager = media_grid.layoutManager as MyGridLayoutManager + val thumbnailWidth = layoutManager.getChildAt(0)?.width ?: 0 + val fullWidth = ((media.size - 1) / layoutManager.spanCount + 1) * thumbnailWidth + media_horizontal_fastscroller.setContentWidth(fullWidth) + media_horizontal_fastscroller.setScrollToX(media_grid.computeHorizontalScrollOffset()) + } + + private fun calculateContentHeight(media: ArrayList) { + val layoutManager = media_grid.layoutManager as MyGridLayoutManager + val pathToCheck = SHOW_ALL + val hasSections = config.getFolderGrouping(pathToCheck) and GROUP_BY_NONE == 0 && !config.scrollHorizontally + val sectionTitleHeight = if (hasSections) layoutManager.getChildAt(0)?.height ?: 0 else 0 + val thumbnailHeight = if (hasSections) layoutManager.getChildAt(1)?.height ?: 0 else layoutManager.getChildAt(0)?.height ?: 0 + + var fullHeight = 0 + var curSectionItems = 0 + media.forEach { + if (it is ThumbnailSection) { + fullHeight += sectionTitleHeight + if (curSectionItems != 0) { + val rows = ((curSectionItems - 1) / layoutManager.spanCount + 1) + fullHeight += rows * thumbnailHeight + } + curSectionItems = 0 + } else { + curSectionItems++ + } + } + + fullHeight += ((curSectionItems - 1) / layoutManager.spanCount + 1) * thumbnailHeight + media_vertical_fastscroller.setContentHeight(fullHeight) + media_vertical_fastscroller.setScrollToY(media_grid.computeVerticalScrollOffset()) } private fun getAllMedia() { getCachedMedia("") { if (it.isNotEmpty()) { - mAllMedia = it + mAllMedia = it.clone() as ArrayList + } + runOnUiThread { + setupAdapter() } startAsyncTask() } @@ -92,9 +272,18 @@ class SearchActivity : SimpleActivity() { private fun startAsyncTask() { mCurrAsyncTask?.stopFetching() mCurrAsyncTask = GetMediaAsynctask(applicationContext, "", showAll = true) { - mAllMedia = it + mAllMedia = it.clone() as ArrayList } mCurrAsyncTask!!.execute() } + + override fun refreshItems() { + } + + override fun tryDeleteFiles(fileDirItems: ArrayList) { + } + + override fun selectedPaths(paths: ArrayList) { + } } diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index 7b5f29da2..13f9fb57e 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -18,18 +18,6 @@ android:textSize="@dimen/bigger_text_size" android:visibility="gone"/> - -