Made recent search suggestions

This commit is contained in:
ChronosX88 2019-02-16 16:03:49 +04:00
parent 804bd7d892
commit 5dc987d0b8
No known key found for this signature in database
GPG Key ID: 8F92E090A87804AA
6 changed files with 56 additions and 1 deletions

View File

@ -41,6 +41,8 @@
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<provider android:name=".additional.RecentSuggestionProvider"
android:authorities="ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider" />
</application>
</manifest>

View File

@ -0,0 +1,12 @@
package ru.volgorobot.vrcatalog.additional;
import android.content.SearchRecentSuggestionsProvider;
public class RecentSuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public RecentSuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}

View File

@ -3,6 +3,7 @@ package ru.volgorobot.vrcatalog.view;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
@ -21,6 +22,7 @@ import me.texy.treeview.TreeView;
import ru.volgorobot.vrcatalog.MainContract;
import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.SearchablePresenter;
import ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider;
import ru.volgorobot.vrcatalog.additional.SearchableNodeViewFactory;
import ru.volgorobot.vrcatalog.model.FirstLevelModel;
@ -59,6 +61,9 @@ public class SearchableActivity extends AppCompatActivity implements MainContrac
detailName = intent.getStringExtra(SearchManager.QUERY);
getSupportActionBar().setTitle("Поиск: " + detailName);
searchablePresenter.fetchDetailsTreeByName(detailName);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
RecentSuggestionProvider.AUTHORITY, RecentSuggestionProvider.MODE);
suggestions.saveRecentQuery(detailName, null);
}
}

View File

@ -1,10 +1,15 @@
package ru.volgorobot.vrcatalog.view;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.provider.SearchRecentSuggestions;
import android.view.MenuItem;
import ru.volgorobot.vrcatalog.R;
import ru.volgorobot.vrcatalog.additional.AppCompatPreferenceActivity;
import ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider;
public class SettingsActivity extends AppCompatPreferenceActivity {
@ -15,6 +20,32 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Preference clearSearchHistoryButton = findPreference("clearSearchHistoryButton");
clearSearchHistoryButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this)
.setTitle("Подтвердите действие")
.setMessage("Вы уверены, что хотите очистить историю вашего поиска?")
.setPositiveButton("ОК", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(SettingsActivity.this,
RecentSuggestionProvider.AUTHORITY, RecentSuggestionProvider.MODE);
suggestions.clearHistory();
}
})
.setNegativeButton("Отмена", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Nothing to do
}
});
dialogBuilder.show();
return true;
}
});
}
@Override

View File

@ -5,4 +5,7 @@
android:title="Адрес API-сервера"
android:summary="API-сервер необходим для доступа к БД деталей"
android:defaultValue="http://example.com"/>
<Preference
android:title="Очистить историю поиска"
android:key="clearSearchHistoryButton"/>
</PreferenceScreen>

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Поиск деталей по имени" >
android:hint="Поиск деталей по имени"
android:searchSuggestAuthority="ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider"
android:searchSuggestSelection=" ?" >
</searchable>