Merge pull request #81 from esensar/feature/72-drawer-search

Add search bar to apps drawer
This commit is contained in:
Tibor Kaputa 2023-07-18 14:26:47 +02:00 committed by GitHub
commit 29acee4f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 110 additions and 6 deletions

View File

@ -219,7 +219,9 @@ class MainActivity : SimpleActivity(), FlingListener {
override fun onBackPressed() {
if (isAllAppsFragmentExpanded()) {
hideFragment(all_apps_fragment)
if ((all_apps_fragment as? AllAppsFragment)?.onBackPressed() == false) {
hideFragment(all_apps_fragment)
}
} else if (isWidgetsFragmentExpanded()) {
hideFragment(widgets_fragment)
} else if (home_screen_grid.resize_frame.isVisible) {

View File

@ -36,6 +36,7 @@ class SettingsActivity : SimpleActivity() {
setupCustomizeColors()
setupUseEnglish()
setupDrawerColumnCount()
setupDrawerSearchBar()
setupLanguage()
setupManageHiddenIcons()
updateTextColors(settings_holder)
@ -105,6 +106,15 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupDrawerSearchBar() {
val showSearchBar = config.showSearchBar
settings_show_search_bar.isChecked = showSearchBar
settings_drawer_search_holder.setOnClickListener {
settings_show_search_bar.toggle()
config.showSearchBar = settings_show_search_bar.isChecked
}
}
private fun setupLanguage() {
settings_language.text = Locale.getDefault().displayLanguage
settings_language_holder.beVisibleIf(isTiramisuPlus())

View File

@ -22,7 +22,7 @@ import kotlinx.android.synthetic.main.item_launcher_label.view.*
class LaunchersAdapter(
val activity: SimpleActivity,
var launchers: ArrayList<AppLauncher>,
launchers: ArrayList<AppLauncher>,
val allAppsListener: AllAppsListener,
val itemClick: (Any) -> Unit
) : RecyclerView.Adapter<LaunchersAdapter.ViewHolder>(), RecyclerViewFastScroller.OnPopupTextUpdate {
@ -30,6 +30,14 @@ class LaunchersAdapter(
private var textColor = activity.getProperTextColor()
private var iconPadding = 0
private var wereFreshIconsLoaded = false
private var filterQuery: String? = null
private var filteredLaunchers: List<AppLauncher> = launchers
var launchers: ArrayList<AppLauncher> = launchers
set(value) {
field = value
updateFilter()
}
init {
calculateIconWidth()
@ -41,10 +49,10 @@ class LaunchersAdapter(
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindView(launchers[position])
holder.bindView(filteredLaunchers[position])
}
override fun getItemCount() = launchers.size
override fun getItemCount() = filteredLaunchers.size
private fun calculateIconWidth() {
val currentColumnCount = activity.config.drawerColumnCount
@ -57,8 +65,10 @@ class LaunchersAdapter(
val itemToRemove = launchers.firstOrNull { it.getLauncherIdentifier() == item.getItemIdentifier() }
if (itemToRemove != null) {
val position = launchers.indexOfFirst { it.getLauncherIdentifier() == item.getItemIdentifier() }
val filteredPosition = filteredLaunchers.indexOfFirst { it.getLauncherIdentifier() == item.getItemIdentifier() }
launchers.removeAt(position)
notifyItemRemoved(position)
updateFilter()
notifyItemRemoved(filteredPosition)
}
}
@ -72,6 +82,14 @@ class LaunchersAdapter(
}
}
fun updateSearchQuery(newQuery: String?) {
if (filterQuery != newQuery) {
filterQuery = newQuery
updateFilter()
notifyDataSetChanged()
}
}
fun updateTextColor(newTextColor: Int) {
if (newTextColor != textColor) {
textColor = newTextColor
@ -79,6 +97,10 @@ class LaunchersAdapter(
}
}
private fun updateFilter() {
filteredLaunchers = launchers.filter { filterQuery == null || it.title.contains(filterQuery!!, ignoreCase = true) }
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(launcher: AppLauncher): View {
itemView.apply {
@ -107,5 +129,5 @@ class LaunchersAdapter(
}
}
override fun onChange(position: Int) = launchers.getOrNull(position)?.getBubbleText() ?: ""
override fun onChange(position: Int) = filteredLaunchers.getOrNull(position)?.getBubbleText() ?: ""
}

View File

@ -174,6 +174,21 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
setPadding(0, topPadding, 0, 0)
background = ColorDrawable(context.getProperBackgroundColor())
(all_apps_grid.adapter as? LaunchersAdapter)?.updateTextColor(context.getProperTextColor())
search_bar.beVisibleIf(context.config.showSearchBar)
search_bar.getToolbar()?.beGone()
search_bar.updateColors()
search_bar.setupMenu()
search_bar.onSearchTextChangedListener = {
(all_apps_grid.adapter as? LaunchersAdapter)?.updateSearchQuery(it)
showNoResultsPlaceholderIfNeeded()
}
}
private fun showNoResultsPlaceholderIfNeeded() {
val itemCount = all_apps_grid.adapter?.itemCount
no_results_placeholder.beVisibleIf(itemCount != null && itemCount == 0)
}
override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) {
@ -197,5 +212,16 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
activity?.showHomeIconMenu(x, y, gridItem, true)
ignoreTouches = true
search_bar.closeSearch()
}
fun onBackPressed(): Boolean {
if (search_bar.isSearchOpen) {
search_bar.closeSearch()
return true
}
return false
}
}

View File

@ -16,4 +16,8 @@ class Config(context: Context) : BaseConfig(context) {
var drawerColumnCount: Int
get() = prefs.getInt(DRAWER_COLUMN_COUNT, context.resources.getInteger(R.integer.portrait_column_count))
set(drawerColumnCount) = prefs.edit().putInt(DRAWER_COLUMN_COUNT, drawerColumnCount).apply()
var showSearchBar: Boolean
get() = prefs.getBoolean(SHOW_SEARCH_BAR, true)
set(showSearchBar) = prefs.edit().putBoolean(SHOW_SEARCH_BAR, showSearchBar).apply()
}

View File

@ -6,6 +6,7 @@ const val WIDGET_LIST_ITEMS_HOLDER = 1
// shared prefs
const val WAS_HOME_SCREEN_INIT = "was_home_screen_init"
const val DRAWER_COLUMN_COUNT = "drawer_column_count"
const val SHOW_SEARCH_BAR = "show_search_bar"
// default home screen grid size
const val ROW_COUNT = 6

View File

@ -170,6 +170,22 @@
tools:text="3" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_drawer_search_holder"
style="@style/SettingsHolderTextViewOneLinerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_bottom_corners">
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_show_search_bar"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_search" />
</RelativeLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -5,10 +5,16 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MySearchMenu
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
android:id="@+id/all_apps_fastscroller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/search_bar"
app:fastScrollEnabled="false">
<com.simplemobiletools.commons.views.MyRecyclerView
@ -20,4 +26,21 @@
app:spanCount="@integer/portrait_column_count" />
</com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/no_results_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/search_bar"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/bigger_margin"
android:alpha="0.8"
android:gravity="center"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="@string/no_items_found"
android:textSize="@dimen/bigger_text_size"
android:textStyle="italic"
android:visibility="gone" />
</com.simplemobiletools.launcher.fragments.AllAppsFragment>