Skip to content

Commit a3b3373

Browse files
cypheropdomdomegg
authored andcommitted
Updated deleteAll() method to delete all searches instead of 10(commons-app#2181) (commons-app#2185)
* Updated deleteAll() method to remove all searches insetead of 10 (commons-app#2181) * Updated deleteAll() method to delete all searches instead of 10 (commons-app#2181) * some minor chages
1 parent aec1e51 commit a3b3373

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

app/src/main/java/fr/free/nrw/commons/explore/recentsearches/RecentSearchesDao.java

+32-17
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,41 @@ public void save(RecentSearch recentSearch) {
5252

5353
/**
5454
* This method is called on confirmation of delete recent searches.
55-
* It deletes latest 10 recent searches from the database
56-
* @param recentSearchesStringList list of recent searches to be deleted
55+
* It deletes all recent searches from the database
5756
*/
58-
public void deleteAll(List<String> recentSearchesStringList) {
57+
public void deleteAll() {
58+
Cursor cursor = null;
5959
ContentProviderClient db = clientProvider.get();
60-
for (String recentSearchName : recentSearchesStringList) {
61-
try {
62-
RecentSearch recentSearch = find(recentSearchName);
63-
if (recentSearch.getContentUri() == null) {
64-
throw new RuntimeException("tried to delete item with no content URI");
65-
} else {
66-
Timber.d("QUERY_NAME %s - delete tried", recentSearch.getContentUri());
67-
db.delete(recentSearch.getContentUri(), null, null);
68-
Timber.d("QUERY_NAME %s - query deleted", recentSearch.getQuery());
60+
try {
61+
cursor = db.query(
62+
RecentSearchesContentProvider.BASE_URI,
63+
Table.ALL_FIELDS,
64+
null,
65+
new String[]{},
66+
Table.COLUMN_LAST_USED + " DESC"
67+
);
68+
while (cursor != null && cursor.moveToNext()) {
69+
try {
70+
RecentSearch recentSearch = find(fromCursor(cursor).getQuery());
71+
if (recentSearch.getContentUri() == null) {
72+
throw new RuntimeException("tried to delete item with no content URI");
73+
} else {
74+
Timber.d("QUERY_NAME %s - delete tried", recentSearch.getContentUri());
75+
db.delete(recentSearch.getContentUri(), null, null);
76+
Timber.d("QUERY_NAME %s - query deleted", recentSearch.getQuery());
77+
}
78+
} catch (RemoteException e) {
79+
Timber.e(e, "query deleted");
80+
throw new RuntimeException(e);
81+
} finally {
82+
db.release();
6983
}
70-
} catch (RemoteException e) {
71-
Timber.e(e, "query deleted");
72-
throw new RuntimeException(e);
73-
} finally {
74-
db.release();
84+
}
85+
} catch (RemoteException e) {
86+
throw new RuntimeException(e);
87+
} finally {
88+
if (cursor != null) {
89+
cursor.close();
7590
}
7691
}
7792
}

app/src/main/java/fr/free/nrw/commons/explore/recentsearches/RecentSearchesFragment.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5454
new AlertDialog.Builder(getContext())
5555
.setMessage(getString(R.string.delete_recent_searches_dialog))
5656
.setPositiveButton(android.R.string.yes, (dialog, which) -> {
57-
recentSearchesDao.deleteAll(recentSearches);
57+
recentSearchesDao.deleteAll();
5858
recent_searches_delete_button.setVisibility(View.GONE);
5959
recent_searches_text_view.setText(R.string.no_recent_searches);
6060
Toast.makeText(getContext(),getString(R.string.search_history_deleted),Toast.LENGTH_SHORT).show();
@@ -70,7 +70,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7070
});
7171
currentThemeIsDark = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("theme", false);
7272
setAdapterForThemes(getContext(), currentThemeIsDark);
73-
73+
7474
recentSearchesList.setAdapter(adapter);
7575
recentSearchesList.setOnItemClickListener((parent, view, position, id) -> (
7676
(SearchActivity)getContext()).updateText(recentSearches.get(position)));

0 commit comments

Comments
 (0)