Updated deleteAll() method to delete all searches instead of 10(#2181) (#2185)

* Updated deleteAll() method to remove all searches insetead of 10 (#2181)

* Updated deleteAll() method to delete all searches instead of 10 (#2181)

* some minor chages
This commit is contained in:
sp2710 2018-12-20 22:08:29 +05:30 committed by Adam Jones
parent aec1e51339
commit a3b33736b3
2 changed files with 34 additions and 19 deletions

View file

@ -52,14 +52,22 @@ public class RecentSearchesDao {
/** /**
* This method is called on confirmation of delete recent searches. * This method is called on confirmation of delete recent searches.
* It deletes latest 10 recent searches from the database * It deletes all recent searches from the database
* @param recentSearchesStringList list of recent searches to be deleted
*/ */
public void deleteAll(List<String> recentSearchesStringList) { public void deleteAll() {
Cursor cursor = null;
ContentProviderClient db = clientProvider.get(); ContentProviderClient db = clientProvider.get();
for (String recentSearchName : recentSearchesStringList) {
try { try {
RecentSearch recentSearch = find(recentSearchName); cursor = db.query(
RecentSearchesContentProvider.BASE_URI,
Table.ALL_FIELDS,
null,
new String[]{},
Table.COLUMN_LAST_USED + " DESC"
);
while (cursor != null && cursor.moveToNext()) {
try {
RecentSearch recentSearch = find(fromCursor(cursor).getQuery());
if (recentSearch.getContentUri() == null) { if (recentSearch.getContentUri() == null) {
throw new RuntimeException("tried to delete item with no content URI"); throw new RuntimeException("tried to delete item with no content URI");
} else { } else {
@ -74,6 +82,13 @@ public class RecentSearchesDao {
db.release(); db.release();
} }
} }
} catch (RemoteException e) {
throw new RuntimeException(e);
} finally {
if (cursor != null) {
cursor.close();
}
}
} }
/** /**

View file

@ -54,7 +54,7 @@ public class RecentSearchesFragment extends CommonsDaggerSupportFragment {
new AlertDialog.Builder(getContext()) new AlertDialog.Builder(getContext())
.setMessage(getString(R.string.delete_recent_searches_dialog)) .setMessage(getString(R.string.delete_recent_searches_dialog))
.setPositiveButton(android.R.string.yes, (dialog, which) -> { .setPositiveButton(android.R.string.yes, (dialog, which) -> {
recentSearchesDao.deleteAll(recentSearches); recentSearchesDao.deleteAll();
recent_searches_delete_button.setVisibility(View.GONE); recent_searches_delete_button.setVisibility(View.GONE);
recent_searches_text_view.setText(R.string.no_recent_searches); recent_searches_text_view.setText(R.string.no_recent_searches);
Toast.makeText(getContext(),getString(R.string.search_history_deleted),Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(),getString(R.string.search_history_deleted),Toast.LENGTH_SHORT).show();