Skip to content

Commit

Permalink
CATROID-1425 Fix Performance of BrickSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Walcho1125 committed Jun 1, 2022
1 parent 2b51ec5 commit b829873
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -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<Brick> list) {
this.brickList = list;
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -60,7 +64,6 @@ class BrickSearchFragment : ListFragment() {
private var searchResults = mutableListOf<Brick>()
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? {
Expand Down Expand Up @@ -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()
Expand All @@ -160,6 +181,13 @@ class BrickSearchFragment : ListFragment() {
}
super.onCreateOptionsMenu(menu, inflater)
}
private fun setShowProgressBar(visible: Boolean) {
if (visible) {
view?.findViewById<ProgressBar>(R.id.progress_bar)?.visibility = View.VISIBLE
} else {
view?.findViewById<ProgressBar>(R.id.progress_bar)?.visibility = View.INVISIBLE
}
}

override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
Expand Down
15 changes: 12 additions & 3 deletions catroid/src/main/res/layout/fragment_brick_search.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Catroid: An on-device visual programming system for Android devices
~ Copyright (C) 2010-2021 The Catrobat Team
~ Copyright (C) 2010-2022 The Catrobat Team
~ (<http://developer.catrobat.org/credits>)
~
~ This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -31,7 +31,16 @@
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent" />
android:divider="@android:color/transparent">
</ListView>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"
android:visibility="invisible"
/>
</RelativeLayout>

0 comments on commit b829873

Please sign in to comment.