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); 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() { public int getSortRule() {
return sortRule; return sortRule;
} }

View File

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

View File

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

View File

@ -28,5 +28,6 @@ public interface MainView {
void showLoading(); void showLoading();
void hideLoading(); void hideLoading();
void onGetResult(List<Geofavorite> geofavorites); void onGetResult(List<Geofavorite> geofavorites);
void onGeofavoriteDeleted(int id);
void onErrorLoading(String message); 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_message">You are about to delete geobookmark {name}. Proceed?</string>
<string name="dialog_delete_delete">Delete</string> <string name="dialog_delete_delete">Delete</string>
<string name="dialog_delete_cancel">Maintain</string> <string name="dialog_delete_cancel">Maintain</string>
<string name="list_geofavorite_deleted">Geofavorite deleted</string>
<!-- Sort dialog --> <!-- Sort dialog -->
<string name="sort_by">Sort by</string> <string name="sort_by">Sort by</string>