Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d2e8f977 authored by Isaac_Chen's avatar Isaac_Chen
Browse files

Enhancement for 769: Currently, If the search string changes too frequently,...

Enhancement for 769: Currently, If the search string changes too frequently, there will be many search tasks running. We add a delay in this commit to prevent this. If the search string does not change in $delay ms, then the search task will start.
parent b674d635
Loading
Loading
Loading
Loading
+40 −1
Original line number Original line Diff line number Diff line
package it.niedermann.owncloud.notes.android.fragment;
package it.niedermann.owncloud.notes.android.fragment;


import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.text.Layout;
import android.text.Layout;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.Log;
import android.util.Log;
@@ -107,6 +108,10 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
        }
        }


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            private final int delay = 20; // If the search string does not change after $delay ms, then the search task starts.
            private DelayQueryRunnable delayQueryTask;
            private Handler handler = new Handler();

            @Override
            @Override
            public boolean onQueryTextSubmit(String query) {
            public boolean onQueryTextSubmit(String query) {
                currentOccurrence++;
                currentOccurrence++;
@@ -116,6 +121,11 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {


            @Override
            @Override
            public boolean onQueryTextChange(String newText) {
            public boolean onQueryTextChange(String newText) {
                queryWithHandler(newText);
                return true;
            }

            private void queryMatch(String newText) {
                searchQuery = newText;
                searchQuery = newText;
                occurrenceCount = countOccurrences(getContent(), searchQuery);
                occurrenceCount = countOccurrences(getContent(), searchQuery);
                if (occurrenceCount > 1) {
                if (occurrenceCount > 1) {
@@ -126,7 +136,36 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
                currentOccurrence = 1;
                currentOccurrence = 1;
                jumpToOccurrence();
                jumpToOccurrence();
                colorWithText(searchQuery, currentOccurrence);
                colorWithText(searchQuery, currentOccurrence);
                return true;
            }

            private void queryWithHandler(String newText) {
                if (delayQueryTask != null) {
                    delayQueryTask.cancel();
                    handler.removeCallbacksAndMessages(null);
                }
                delayQueryTask = new DelayQueryRunnable(newText);
                handler.postDelayed(delayQueryTask, delay);
            }

            class DelayQueryRunnable implements Runnable {
                String mText;
                private boolean canceled = false;

                public DelayQueryRunnable(String text) {
                    this.mText = text;
                }

                @Override
                public void run() {
                    if (canceled) {
                        return;
                    }
                    queryMatch(mText);
                }

                public void cancel() {
                    canceled = true;
                }
            }
            }
        });
        });
    }
    }