diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/PickDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/PickDirectoryDialog.kt index bf43fe15f..2a069b75a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/PickDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/PickDirectoryDialog.kt @@ -1,6 +1,11 @@ package com.simplemobiletools.gallery.pro.dialogs +import android.graphics.Color import android.view.KeyEvent +import android.view.View +import android.view.inputmethod.EditorInfo +import android.widget.EditText +import android.widget.ImageView import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.recyclerview.widget.RecyclerView @@ -9,6 +14,7 @@ import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID import com.simplemobiletools.commons.views.MyGridLayoutManager +import com.simplemobiletools.commons.views.MySearchMenu import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.adapters.DirectoryAdapter import com.simplemobiletools.gallery.pro.extensions.* @@ -32,6 +38,10 @@ class PickDirectoryDialog( private var isGridViewType = activity.config.viewTypeFolders == VIEW_TYPE_GRID private var showHidden = activity.config.shouldShowHidden private var currentPathPrefix = "" + private val config = view.context.config + private val searchView = view.folder_search_view + private val searchEditText = view.findViewById(R.id.top_toolbar_search) + private val searchViewAppBarLayout = view.findViewById(R.id.top_app_bar_layout) init { (view.directories_grid.layoutManager as MyGridLayoutManager).apply { @@ -41,6 +51,8 @@ class PickDirectoryDialog( view.directories_fastscroller.updateColors(activity.getProperPrimaryColor()) + configureSearchView() + val builder = activity.getAlertDialogBuilder() .setPositiveButton(R.string.ok, null) .setNegativeButton(R.string.cancel, null) @@ -72,6 +84,75 @@ class PickDirectoryDialog( fetchDirectories(false) } + private fun configureSearchView() = with(searchView) { + updateHintText(context.getString(R.string.search_folders)) + searchEditText.imeOptions = EditorInfo.IME_ACTION_DONE + + toggleHideOnScroll(!config.scrollHorizontally) + setupMenu() + setSearchViewListeners() + updateSearchViewUi() + } + + private fun MySearchMenu.updateSearchViewUi() { + getToolbar().beInvisible() + updateColors() + setBackgroundColor(Color.TRANSPARENT) + searchViewAppBarLayout.setBackgroundColor(Color.TRANSPARENT) + } + + private fun MySearchMenu.setSearchViewListeners() { + onSearchOpenListener = { + updateSearchViewLeftIcon(R.drawable.ic_cross_vector) + } + onSearchClosedListener = { + searchEditText.clearFocus() + activity.hideKeyboard(searchEditText) + updateSearchViewLeftIcon(R.drawable.ic_search_vector) + } + + onSearchTextChangedListener = { text -> + filterFolderListBySearchQuery(text) + } + } + + private fun updateSearchViewLeftIcon(iconResId: Int) = with(view.findViewById(R.id.top_toolbar_search_icon)) { + post { + setImageResource(iconResId) + } + } + + private fun filterFolderListBySearchQuery(query: String) { + val adapter = view.directories_grid.adapter as? DirectoryAdapter + var dirsToShow = allDirectories + if (query.isNotEmpty()) { + dirsToShow = dirsToShow.filter { it.name.contains(query, true) }.sortedBy { !it.name.startsWith(query, true) } + .toMutableList() as ArrayList + } + checkPlaceholderVisibility(dirsToShow) + + val filteredFolderListUpdated = adapter?.dirs != dirsToShow + if (filteredFolderListUpdated) { + adapter?.updateDirs(dirsToShow) + + view.directories_grid.apply { + post { + scrollToPosition(0) + } + } + } + } + + private fun checkPlaceholderVisibility(dirs: ArrayList) = with(view) { + directories_empty_placeholder.beVisibleIf(dirs.isEmpty()) + + if (folder_search_view.isSearchOpen) { + directories_empty_placeholder.text = context.getString(R.string.no_items_found) + } + + directories_fastscroller.beVisibleIf(directories_empty_placeholder.isGone()) + } + private fun fetchDirectories(forceShowHiddenAndExcluded: Boolean) { activity.getCachedDirectories(forceShowHidden = forceShowHiddenAndExcluded, forceShowExcluded = forceShowHiddenAndExcluded) { if (it.isNotEmpty()) { @@ -143,7 +224,9 @@ class PickDirectoryDialog( } private fun backPressed() { - if (activity.config.groupDirectSubfolders) { + if (searchView.isSearchOpen) { + searchView.closeSearch() + } else if (activity.config.groupDirectSubfolders) { if (currentPathPrefix.isEmpty()) { dialog?.dismiss() } else { diff --git a/app/src/main/res/layout/dialog_directory_picker.xml b/app/src/main/res/layout/dialog_directory_picker.xml index cc37ccf14..683c9cf43 100644 --- a/app/src/main/res/layout/dialog_directory_picker.xml +++ b/app/src/main/res/layout/dialog_directory_picker.xml @@ -9,12 +9,35 @@ android:id="@+id/directories_grid_holder" android:layout_width="match_parent" android:layout_height="wrap_content" - android:paddingTop="@dimen/activity_margin"> + android:minHeight="@dimen/directory_picker_dialog_min_height" + android:paddingTop="@dimen/medium_margin"> + + + + + android:layout_height="wrap_content" + android:layout_below="@+id/directories_empty_placeholder"> 40dp 30dp 180dp + 180dp