Working delete

This commit is contained in:
Daniele 2021-09-08 09:29:13 +02:00
parent b8551cbb13
commit 809616aefa
5 changed files with 35 additions and 17 deletions

View File

@ -82,6 +82,17 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter<GeofavoriteAdapter.
return geofavoriteListFiltered.get(position);
}
public void removeById(int id) {
for (Geofavorite g : geofavoriteList) {
if (g.getId() == id) {
geofavoriteList.remove(g);
geofavoriteListFiltered.remove(g);
break;
}
}
notifyDataSetChanged();
}
public int getSortRule() {
return sortRule;
}

View File

@ -281,6 +281,15 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti
geofavoriteAdapter.setGeofavoriteList(geofavorite_list);
}
@Override
public void onGeofavoriteDeleted(int id) {
// Update list
runOnUiThread(() -> {
geofavoriteAdapter.removeById(id);
Toast.makeText(MainActivity.this, R.string.list_geofavorite_deleted, Toast.LENGTH_LONG).show();
});
}
@Override
public void onErrorLoading(String message) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
@ -320,12 +329,13 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti
private void showGeofavoriteDeteleDialog(Geofavorite item) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(R.string.dialog_delete_message)
.setTitle(getString(R.string.dialog_delete_title).replace("{name}", item.getName()))
builder.setMessage(getString(R.string.dialog_delete_message).replace("{name}", item.getName()))
.setTitle(R.string.dialog_delete_title)
.setPositiveButton(R.string.dialog_delete_delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
presenter.deleteGeofavorite(item.getId());
dialog.dismiss();
// Callback is onGeofavoriteDeleted
}
})
.setNegativeButton(R.string.dialog_delete_cancel, new DialogInterface.OnClickListener() {
@ -333,7 +343,8 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti
dialog.dismiss();
}
});
builder.create();
AlertDialog ad = builder.create();
ad.show();
}
private void showGeofavoriteDetailActivity(Geofavorite item) {

View File

@ -66,24 +66,18 @@ public class MainPresenter {
public void deleteGeofavorite(int id) {
view.showLoading();
Call<List<Geofavorite>> call = ApiProvider.getAPI().getGeofavorites();
call.enqueue(new Callback<List<Geofavorite>>() {
Call<Geofavorite> call = ApiProvider.getAPI().deleteGeofavorite(id);
call.enqueue(new Callback<Geofavorite>() {
@Override
public void onResponse(@NonNull Call<List<Geofavorite>> call, @NonNull Response<List<Geofavorite>> response) {
((AppCompatActivity) view).runOnUiThread(() -> {
public void onResponse(Call<Geofavorite> call, Response<Geofavorite> response) {
view.hideLoading();
if (response.isSuccessful() && response.body() != null) {
view.onGetResult(response.body());
}
});
view.onGeofavoriteDeleted(id);
}
@Override
public void onFailure(@NonNull Call<List<Geofavorite>> call, @NonNull Throwable t) {
((AppCompatActivity) view).runOnUiThread(() -> {
public void onFailure(Call<Geofavorite> call, Throwable t) {
view.hideLoading();
view.onErrorLoading(t.getLocalizedMessage());
});
}
});
}

View File

@ -28,5 +28,6 @@ public interface MainView {
void showLoading();
void hideLoading();
void onGetResult(List<Geofavorite> geofavorites);
void onGeofavoriteDeleted(int id);
void onErrorLoading(String message);
}

View File

@ -37,6 +37,7 @@
<string name="dialog_delete_message">You are about to delete geobookmark {name}. Proceed?</string>
<string name="dialog_delete_delete">Delete</string>
<string name="dialog_delete_cancel">Maintain</string>
<string name="list_geofavorite_deleted">Geofavorite deleted</string>
<!-- Sort dialog -->
<string name="sort_by">Sort by</string>