From b829873fb3105bb9cc049e316ce37e7e3d80b091 Mon Sep 17 00:00:00 2001 From: Oskar Walcher Date: Thu, 12 May 2022 15:17:44 +0200 Subject: [PATCH] CATROID-1425 Fix Performance of BrickSearch --- .../catrobat/catroid/common/Constants.java | 4 +- .../ui/adapter/PrototypeBrickAdapter.java | 7 +- .../ui/fragment/BrickSearchFragment.kt | 68 +++++++++++++------ .../main/res/layout/fragment_brick_search.xml | 15 +++- 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/catroid/src/main/java/org/catrobat/catroid/common/Constants.java b/catroid/src/main/java/org/catrobat/catroid/common/Constants.java index 573a1628903..98b51443fc8 100644 --- a/catroid/src/main/java/org/catrobat/catroid/common/Constants.java +++ b/catroid/src/main/java/org/catrobat/catroid/common/Constants.java @@ -45,7 +45,9 @@ public final class Constants { public static final double CURRENT_CATROBAT_LANGUAGE_VERSION = 1.11; public static final String REMOTE_DISPLAY_APP_ID = "CEBB9229"; public static final int CAST_CONNECTION_TIMEOUT = 5000; //in milliseconds - public static final int CAST_NOT_SEEING_DEVICE_TIMEOUT = 3000; //in milliseconds + public static final int CAST_NOT_SEEING_DEVICE_TIMEOUT = 3000; //in + public static final long PROGESSIVE_INPUT_DELAY = 400; + public static final long PROGESSIVE_INPUT_COUNTDOWN_INTERVALL = 500; public static final long RETROFIT_WRITE_TIMEOUT = 15; public static final String PLATFORM_NAME = "Android"; diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java b/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java index 06f46a470bd..63e8238dd4e 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/adapter/PrototypeBrickAdapter.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2018 The Catrobat Team + * Copyright (C) 2010-2022 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -57,4 +57,9 @@ public long getItemId(int position) { public View getView(int position, View convertView, ViewGroup parent) { return brickList.get(position).getPrototypeView(parent.getContext()); } + + public void replaceList(List list) { + this.brickList = list; + notifyDataSetChanged(); + } } diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/fragment/BrickSearchFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/fragment/BrickSearchFragment.kt index bf1879c1e29..111fa4b3c9b 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/fragment/BrickSearchFragment.kt +++ b/catroid/src/main/java/org/catrobat/catroid/ui/fragment/BrickSearchFragment.kt @@ -26,29 +26,33 @@ package org.catrobat.catroid.ui.fragment import android.app.SearchManager import android.content.Context import android.os.Bundle +import android.os.CountDownTimer import android.preference.PreferenceManager import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater import android.view.View import android.view.ViewGroup +import android.widget.AbsListView import android.widget.AdapterView +import android.widget.ProgressBar import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.SearchView import androidx.fragment.app.ListFragment import org.catrobat.catroid.ProjectManager import org.catrobat.catroid.R +import org.catrobat.catroid.common.Constants.PROGESSIVE_INPUT_COUNTDOWN_INTERVALL +import org.catrobat.catroid.common.Constants.PROGESSIVE_INPUT_DELAY import org.catrobat.catroid.content.bricks.Brick import org.catrobat.catroid.ui.BottomBar.hideBottomBar import org.catrobat.catroid.ui.SpriteActivity import org.catrobat.catroid.ui.adapter.PrototypeBrickAdapter +import org.catrobat.catroid.ui.hideKeyboard import org.catrobat.catroid.ui.settingsfragments.AccessibilityProfile import org.catrobat.catroid.ui.settingsfragments.SettingsFragment import org.catrobat.catroid.utils.ToastUtil -import org.catrobat.catroid.ui.hideKeyboard import java.util.Locale -import android.widget.AbsListView class BrickSearchFragment : ListFragment() { @@ -60,7 +64,6 @@ class BrickSearchFragment : ListFragment() { private var searchResults = mutableListOf() private var addBrickListener: AddBrickFragment.OnAddBrickListener? = null private var category: String? = null - private var adapter: PrototypeBrickAdapter? = null override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { @@ -127,28 +130,46 @@ class BrickSearchFragment : ListFragment() { searchView = searchItem if (searchView != null) { - searchView?.setSearchableInfo(searchManager.getSearchableInfo(activity?.componentName)) - queryTextListener = object : SearchView.OnQueryTextListener { - override fun onQueryTextChange(query: String): Boolean { - searchResults.clear() - searchBrick(query) - if (searchResults.isEmpty()) { - ToastUtil.showError(context, context?.getString(R.string.no_results_found)) - } - if (query.isEmpty()) { - searchResults.clear() - } - adapter = PrototypeBrickAdapter(searchResults) - listAdapter = adapter + var countDownTimer: CountDownTimer + adapter = PrototypeBrickAdapter(searchResults) + listAdapter = adapter + searchView?.setSearchableInfo(searchManager.getSearchableInfo(activity?.componentName)) + queryTextListener = object : SearchView.OnQueryTextListener { + override fun onQueryTextChange(query: String): Boolean { + countDownTimer = object : CountDownTimer( + PROGESSIVE_INPUT_DELAY, + PROGESSIVE_INPUT_COUNTDOWN_INTERVALL + ) { + @SuppressWarnings("EmptyFunctionBlock") + override fun onTick(millisUntilFinished: Long) { + } - return true - } + override fun onFinish() { + setShowProgressBar(false) + searchResults.clear() + adapter?.replaceList(searchResults) + searchBrick(query) + if (searchResults.isEmpty()) { + ToastUtil.showError( + context, + context?.getString(R.string.no_results_found) + ) + } + if (query.isEmpty()) { + searchResults.clear() + } + adapter?.replaceList(searchResults) + } + } + countDownTimer.start() + setShowProgressBar(true) + return true + } override fun onQueryTextSubmit(query: String): Boolean { searchResults.clear() searchBrick(query) - adapter = PrototypeBrickAdapter(searchResults) - listAdapter = adapter + adapter?.replaceList(searchResults) if (searchResults.isEmpty()) { ToastUtil.showError(context, context?.getString(R.string.no_results_found)) } else searchView?.clearFocus() @@ -160,6 +181,13 @@ class BrickSearchFragment : ListFragment() { } super.onCreateOptionsMenu(menu, inflater) } + private fun setShowProgressBar(visible: Boolean) { + if (visible) { + view?.findViewById(R.id.progress_bar)?.visibility = View.VISIBLE + } else { + view?.findViewById(R.id.progress_bar)?.visibility = View.INVISIBLE + } + } override fun onPrepareOptionsMenu(menu: Menu) { super.onPrepareOptionsMenu(menu) diff --git a/catroid/src/main/res/layout/fragment_brick_search.xml b/catroid/src/main/res/layout/fragment_brick_search.xml index 9074a3e0cf7..90587dfee87 100644 --- a/catroid/src/main/res/layout/fragment_brick_search.xml +++ b/catroid/src/main/res/layout/fragment_brick_search.xml @@ -1,7 +1,7 @@