From 5dc987d0b8ffd3e7001d0fe5b6151ba5e4905d3c Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Sat, 16 Feb 2019 16:03:49 +0400 Subject: [PATCH] Made recent search suggestions --- app/src/main/AndroidManifest.xml | 2 ++ .../additional/RecentSuggestionProvider.java | 12 +++++++ .../vrcatalog/view/SearchableActivity.java | 5 +++ .../vrcatalog/view/SettingsActivity.java | 31 +++++++++++++++++++ app/src/main/res/xml/activity_settings.xml | 3 ++ app/src/main/res/xml/searchable.xml | 4 ++- 6 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/ru/volgorobot/vrcatalog/additional/RecentSuggestionProvider.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e5aa12d..c565685 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,6 +41,8 @@ + \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/additional/RecentSuggestionProvider.java b/app/src/main/java/ru/volgorobot/vrcatalog/additional/RecentSuggestionProvider.java new file mode 100644 index 0000000..7e3f3cc --- /dev/null +++ b/app/src/main/java/ru/volgorobot/vrcatalog/additional/RecentSuggestionProvider.java @@ -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); + } +} \ No newline at end of file diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java index 19d1a40..6bd5091 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/SearchableActivity.java @@ -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); } } diff --git a/app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java b/app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java index 8f57d8a..1ebc8ad 100644 --- a/app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java +++ b/app/src/main/java/ru/volgorobot/vrcatalog/view/SettingsActivity.java @@ -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 diff --git a/app/src/main/res/xml/activity_settings.xml b/app/src/main/res/xml/activity_settings.xml index a97929d..dc6bce0 100644 --- a/app/src/main/res/xml/activity_settings.xml +++ b/app/src/main/res/xml/activity_settings.xml @@ -5,4 +5,7 @@ android:title="Адрес API-сервера" android:summary="API-сервер необходим для доступа к БД деталей" android:defaultValue="http://example.com"/> + \ No newline at end of file diff --git a/app/src/main/res/xml/searchable.xml b/app/src/main/res/xml/searchable.xml index c913d4b..02fc4c1 100644 --- a/app/src/main/res/xml/searchable.xml +++ b/app/src/main/res/xml/searchable.xml @@ -1,5 +1,7 @@ + android:hint="Поиск деталей по имени" + android:searchSuggestAuthority="ru.volgorobot.vrcatalog.additional.RecentSuggestionProvider" + android:searchSuggestSelection=" ?" > \ No newline at end of file