WIP implementing MVVM
This commit is contained in:
+89
-6
@@ -1,5 +1,7 @@
|
||||
package it.danieleverducci.nextcloudmaps.repository;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
@@ -19,7 +21,8 @@ import retrofit2.Response;
|
||||
public class GeofavoriteRepository {
|
||||
|
||||
private static GeofavoriteRepository instance;
|
||||
private ArrayList<Geofavorite> dataSet = new ArrayList<>();
|
||||
private MutableLiveData<List<Geofavorite>> mGeofavorites;
|
||||
private OnFinished listener;
|
||||
|
||||
public static GeofavoriteRepository getInstance() {
|
||||
if(instance == null){
|
||||
@@ -29,13 +32,23 @@ public class GeofavoriteRepository {
|
||||
}
|
||||
|
||||
public MutableLiveData<List<Geofavorite>> getGeofavorites(){
|
||||
if (mGeofavorites == null) {
|
||||
mGeofavorites = new MutableLiveData<>();
|
||||
mGeofavorites.setValue(new ArrayList<>());
|
||||
}
|
||||
return mGeofavorites;
|
||||
}
|
||||
|
||||
public void updateGeofavorites() {
|
||||
if (listener != null) listener.onLoading();
|
||||
// Obtain geofavorites
|
||||
Call<List<Geofavorite>> call = ApiProvider.getAPI().getGeofavorites();
|
||||
call.enqueue(new Callback<List<Geofavorite>>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<List<Geofavorite>> call, @NonNull Response<List<Geofavorite>> response) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
dataSet.addAll(response.body());
|
||||
mGeofavorites.postValue(response.body());
|
||||
if (listener != null) listener.onSuccess();
|
||||
} else {
|
||||
onFailure(call, new Throwable("Dataset is empty"));
|
||||
}
|
||||
@@ -43,13 +56,83 @@ public class GeofavoriteRepository {
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<List<Geofavorite>> call, @NonNull Throwable t) {
|
||||
// TODO
|
||||
if (listener != null) listener.onFailure();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
MutableLiveData<List<Geofavorite>> data = new MutableLiveData<>();
|
||||
data.setValue(dataSet);
|
||||
return data;
|
||||
public Geofavorite getGeofavorite(int id) {
|
||||
for (Geofavorite g : mGeofavorites.getValue()) {
|
||||
if (g.getId() == id)
|
||||
return g;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void saveGeofavorite(Geofavorite geofav) {
|
||||
Call<Geofavorite> call;
|
||||
if (geofav.getId() == 0) {
|
||||
// New geofavorite
|
||||
call = ApiProvider.getAPI().createGeofavorite(geofav);
|
||||
} else {
|
||||
// Update existing geofavorite
|
||||
call = ApiProvider.getAPI().updateGeofavorite(geofav.getId(), geofav);
|
||||
}
|
||||
call.enqueue(new Callback<Geofavorite>() {
|
||||
@Override
|
||||
public void onResponse(Call<Geofavorite> call, Response<Geofavorite> response) {
|
||||
if (response.isSuccessful()) {
|
||||
List<Geofavorite> geofavs = mGeofavorites.getValue();
|
||||
if (geofav.getId() != 0) {
|
||||
geofavs.remove(geofav);
|
||||
}
|
||||
geofavs.add(geofav);
|
||||
mGeofavorites.postValue(geofavs);
|
||||
if (listener != null) listener.onSuccess();
|
||||
} else if (listener != null) {
|
||||
listener.onFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Geofavorite> call, Throwable t) {
|
||||
if (listener != null) listener.onFailure();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void deleteGeofavorite(Geofavorite geofav) {
|
||||
if (listener != null) listener.onLoading();
|
||||
// Delete Geofavorite
|
||||
Call<Geofavorite> call = ApiProvider.getAPI().deleteGeofavorite(geofav.getId());
|
||||
call.enqueue(new Callback<Geofavorite>() {
|
||||
@Override
|
||||
public void onResponse(Call<Geofavorite> call, Response<Geofavorite> response) {
|
||||
List<Geofavorite> geofavs = mGeofavorites.getValue();
|
||||
if (geofavs.remove(geofav)) {
|
||||
mGeofavorites.postValue(geofavs);
|
||||
if (listener != null) listener.onSuccess();
|
||||
} else {
|
||||
// Should never happen
|
||||
if (listener != null) listener.onFailure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Geofavorite> call, Throwable t) {
|
||||
if (listener != null) listener.onFailure();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void setOnFinishedListener(OnFinished listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public interface OnFinished {
|
||||
void onLoading();
|
||||
void onSuccess();
|
||||
void onFailure();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user