Skip to content

Commit cbf625d

Browse files
cypheropdomdomegg
authored andcommitted
Add commons-app#2241: Add long-press to delete specific search (commons-app#2275)
1 parent b46ea0a commit cbf625d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ public void deleteAll() {
9191
}
9292
}
9393

94+
/**
95+
* Deletes a recent search from the database
96+
*/
97+
public void delete(RecentSearch recentSearch) {
98+
99+
ContentProviderClient db = clientProvider.get();
100+
try {
101+
if (recentSearch.getContentUri() == null) {
102+
throw new RuntimeException("tried to delete item with no content URI");
103+
} else {
104+
db.delete(recentSearch.getContentUri(), null, null);
105+
}
106+
} catch (RemoteException e) {
107+
throw new RuntimeException(e);
108+
} finally {
109+
db.release();
110+
}
111+
}
112+
113+
94114
/**
95115
* Find persisted search query in database, based on its name.
96116
* @param name Search query Ex- "butterfly"

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

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.view.LayoutInflater;
66
import android.view.View;
77
import android.view.ViewGroup;
8+
import android.widget.AdapterView;
89
import android.widget.ArrayAdapter;
910
import android.widget.ImageView;
1011
import android.widget.ListView;
@@ -70,6 +71,22 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
7071
recentSearchesList.setAdapter(adapter);
7172
recentSearchesList.setOnItemClickListener((parent, view, position, id) -> (
7273
(SearchActivity)getContext()).updateText(recentSearches.get(position)));
74+
recentSearchesList.setOnItemLongClickListener((parent, view, position, id) -> {
75+
new AlertDialog.Builder(getContext())
76+
.setMessage(R.string.delete_search_dialog)
77+
.setPositiveButton(R.string.delete,((dialog, which) -> {
78+
recentSearchesDao.delete(recentSearchesDao.find(recentSearches.get(position)));
79+
recentSearches = recentSearchesDao.recentSearches(10);
80+
adapter = new ArrayAdapter<>(getContext(), R.layout.item_recent_searches, recentSearches);
81+
recentSearchesList.setAdapter(adapter);
82+
adapter.notifyDataSetChanged();
83+
dialog.dismiss();
84+
}))
85+
.setNegativeButton(android.R.string.cancel,null)
86+
.create()
87+
.show();
88+
return true;
89+
});
7390
updateRecentSearches();
7491
return rootView;
7592
}

app/src/main/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,10 @@
349349

350350
<string name="no_recent_searches">No recent searches</string>
351351
<string name="delete_recent_searches_dialog">Are you sure you want to clear your search history?</string>
352+
<string name="delete_search_dialog">Do you want to delete this search?</string>
352353
<string name="search_history_deleted">Search history deleted</string>
353354
<string name="nominate_delete">Nominate For Deletion</string>
355+
<string name="delete">DELETE</string>
354356

355357
<string name="Achievements">Achievements</string>
356358
<string name="statistics">STATISTICS</string>

0 commit comments

Comments
 (0)