Add search bar to apps drawer

This closes #72
This commit is contained in:
Ensar Sarajčić 2023-07-17 15:58:59 +02:00
parent dda1080ccd
commit 913cbb303d
9 changed files with 86 additions and 7 deletions

View File

@ -63,7 +63,7 @@ android {
}
dependencies {
implementation 'com.github.SimpleMobileTools:Simple-Commons:4c83ec8740'
implementation 'com.github.SimpleMobileTools:Simple-Commons:42733f39a4'
kapt "androidx.room:room-compiler:2.5.1"
implementation "androidx.room:room-runtime:2.5.1"

View File

@ -219,7 +219,9 @@ class MainActivity : SimpleActivity(), FlingListener {
override fun onBackPressed() {
if (isAllAppsFragmentExpanded()) {
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 useSearchBar = config.useSearchBar
settings_show_search.isChecked = useSearchBar
settings_drawer_search_holder.setOnClickListener {
settings_show_search.toggle()
config.useSearchBar = settings_show_search.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,15 @@ 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.useSearchBar)
search_bar.getToolbar()?.beGone()
search_bar.updateColors()
search_bar.setupMenu()
search_bar.onSearchTextChangedListener = {
(all_apps_grid.adapter as? LaunchersAdapter)?.updateSearchQuery(it)
}
}
override fun onAppLauncherLongPressed(x: Float, y: Float, appLauncher: AppLauncher) {
@ -198,4 +207,13 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
activity?.showHomeIconMenu(x, y, gridItem, true)
ignoreTouches = true
}
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 useSearchBar: Boolean
get() = prefs.getBoolean(USE_SEARCH_BAR, true)
set(searchBarEnabled) = prefs.edit().putBoolean(USE_SEARCH_BAR, searchBarEnabled).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 USE_SEARCH_BAR = "use_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"
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