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,26 +52,41 @@ public class RecentSearchesDao {
/**
* This method is called on confirmation of delete recent searches.
* It deletes latest 10 recent searches from the database
* @param recentSearchesStringList list of recent searches to be deleted
* It deletes all recent searches from the database
*/
public void deleteAll(List<String> recentSearchesStringList) {
public void deleteAll() {
Cursor cursor = null;
ContentProviderClient db = clientProvider.get();
for (String recentSearchName : recentSearchesStringList) {
try {
RecentSearch recentSearch = find(recentSearchName);
if (recentSearch.getContentUri() == null) {
throw new RuntimeException("tried to delete item with no content URI");
} else {
Timber.d("QUERY_NAME %s - delete tried", recentSearch.getContentUri());
db.delete(recentSearch.getContentUri(), null, null);
Timber.d("QUERY_NAME %s - query deleted", recentSearch.getQuery());
try {
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) {
throw new RuntimeException("tried to delete item with no content URI");
} else {
Timber.d("QUERY_NAME %s - delete tried", recentSearch.getContentUri());
db.delete(recentSearch.getContentUri(), null, null);
Timber.d("QUERY_NAME %s - query deleted", recentSearch.getQuery());
}
} catch (RemoteException e) {
Timber.e(e, "query deleted");
throw new RuntimeException(e);
} finally {
db.release();
}
} catch (RemoteException e) {
Timber.e(e, "query deleted");
throw new RuntimeException(e);
} finally {
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())
.setMessage(getString(R.string.delete_recent_searches_dialog))
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
recentSearchesDao.deleteAll(recentSearches);
recentSearchesDao.deleteAll();
recent_searches_delete_button.setVisibility(View.GONE);
recent_searches_text_view.setText(R.string.no_recent_searches);
Toast.makeText(getContext(),getString(R.string.search_history_deleted),Toast.LENGTH_SHORT).show();