Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform automatically a search after 500 milliseconds #71

Merged
merged 4 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 88 additions & 7 deletions app/src/main/java/com/odysee/app/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import android.os.Looper;
import android.provider.MediaStore;
import android.support.v4.media.session.MediaSessionCompat;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.TypefaceSpan;
import android.util.Base64;
import android.util.Log;
Expand Down Expand Up @@ -418,6 +420,10 @@ public class MainActivity extends AppCompatActivity implements SharedPreferences
private ScheduledFuture<?> scheduledWalletUpdater;
private boolean walletSyncScheduled;

ScheduledExecutorService searchWorker;
ScheduledFuture<?> scheduledSearchFuture;
private boolean autoSearchEnabled = false;

ChannelCreateDialogFragment channelCreationBottomSheet;

AccountManager accountManager;
Expand Down Expand Up @@ -734,9 +740,72 @@ public void onClick(View view) {
findViewById(R.id.fragment_container_search).setVisibility(View.VISIBLE);
String query = queryText.getText().toString();

SearchFragment fragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SEARCH");
currentDisplayFragment = fragment;
fragment.search(query, 0);
SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SEARCH");

if (searchFragment != null) {
searchFragment.search(query, 0);
}
}
}
});

((EditText)findViewById(R.id.search_query_text)).addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Context context = getApplicationContext();
if (context != null) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
autoSearchEnabled = sp.getBoolean("com.odysee.app.preference.userinterface.Autosearch", false);
}
if (autoSearchEnabled) {
if (searchWorker == null) {
searchWorker = Executors.newSingleThreadScheduledExecutor();
}

// Cancel any previously scheduled search as soon as possible if not yet running.
// Let it finish otherwise, as it will be re-scheduled on aftertextChanged()
if (scheduledSearchFuture != null && !scheduledSearchFuture.isCancelled()) {
scheduledSearchFuture.cancel(false);
}
}
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (autoSearchEnabled) {
if (!s.toString().equals("")) {
Runnable runnable = new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
EditText queryText = findViewById(R.id.search_query_text);

findViewById(R.id.fragment_container_search).setVisibility(View.VISIBLE);
String query = queryText.getText().toString();

SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SEARCH");

if (searchFragment != null) {
searchFragment.search(query, 0);
}
}
});
}
};
scheduledSearchFuture = searchWorker.schedule(runnable, 500, TimeUnit.MILLISECONDS);
} else {
SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SEARCH");

if (searchFragment != null) {
searchFragment.search("", 0);
}
}
}
}
});
Expand All @@ -756,18 +825,22 @@ public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
@Override
public void onClick(View view) {
EditText queryText = findViewById(R.id.search_query_text);
InputMethodManager inputMethodManager = (InputMethodManager) queryText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(queryText.getWindowToken(), 0);

getSupportFragmentManager().beginTransaction()
.remove(getSupportFragmentManager().findFragmentByTag("SEARCH")).commit();
Fragment searchFragment = getSupportFragmentManager().findFragmentByTag("SEARCH");
if (searchFragment != null) {
getSupportFragmentManager().beginTransaction().remove(searchFragment).commit();
}

((EditText)findViewById(R.id.search_query_text)).setText("");
showBottomNavigation();
switchToolbarForSearch(false);

// On tablets, multiple fragments could be visible. Don't show Home Screen when File View is visible
if (findViewById(R.id.main_activity_other_fragment).getVisibility() != View.VISIBLE)
if (findViewById(R.id.main_activity_other_fragment).getVisibility() != View.VISIBLE) {
findViewById(R.id.fragment_container_main_activity).setVisibility(View.VISIBLE);
}

showWalletBalance();
findViewById(R.id.fragment_container_search).setVisibility(View.GONE);
Expand Down Expand Up @@ -994,6 +1067,14 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}

public void cancelScheduledSearchFuture() {
if (scheduledSearchFuture != null && !scheduledSearchFuture.isCancelled()) {
scheduledSearchFuture.cancel(true);
}
if (searchWorker != null && !searchWorker.isShutdown()) {
searchWorker.shutdown();
}
}
public void hideToolbar() {
findViewById(R.id.toolbar).setVisibility(View.GONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public void onStop() {
if (activity != null) {
activity.resetCurrentDisplayFragment();
activity.showBottomNavigation();
activity.cancelScheduledSearchFuture();
}

super.onStop();
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
<string name="enable_background_playback_pip_mode">Continue playing media in the background after closing picture-in-picture mode</string>
<string name="enable_autoplay">Autoplay media files</string>
<string name="enable_dark_mode">Enable dark theme</string>
<string name="enable_autosearch">Perform search while typing</string>
<string name="show_mature_content">Show mature content</string>
<string name="show_url_suggestions">Show URL suggestions</string>
<string name="notifications">Notifications</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
app:title="@string/enable_dark_mode"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
app:key="com.odysee.app.preference.userinterface.Autosearch"
app:title="@string/enable_autosearch"
app:defaultValue="false"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
<!--SwitchPreferenceCompat
app:key="com.odysee.app.preference.userinterface.ShowMatureContent"
app:title="@string/show_mature_content"
Expand Down