Conversion to MVVM completed
This commit is contained in:
parent
8b7e89f43b
commit
a694246a56
@ -125,14 +125,14 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca
|
|||||||
mViewHolder.setUpdating(updating);
|
mViewHolder.setUpdating(updating);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mViewModel.getIsFailed().observe(this, new Observer<Boolean>() {
|
mViewModel.getOnFinished().observe(this, new Observer<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(@Nullable Boolean failed) {
|
public void onChanged(@Nullable Boolean success) {
|
||||||
if(failed){
|
if(success){
|
||||||
Toast.makeText(GeofavoriteDetailActivity.this, R.string.error_saving_geofavorite, Toast.LENGTH_SHORT).show();
|
|
||||||
} else {
|
|
||||||
Toast.makeText(GeofavoriteDetailActivity.this, R.string.geofavorite_saved, Toast.LENGTH_SHORT).show();
|
Toast.makeText(GeofavoriteDetailActivity.this, R.string.geofavorite_saved, Toast.LENGTH_SHORT).show();
|
||||||
finish();
|
finish();
|
||||||
|
} else {
|
||||||
|
Toast.makeText(GeofavoriteDetailActivity.this, R.string.error_saving_geofavorite, Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -7,14 +7,11 @@ import androidx.lifecycle.ViewModel;
|
|||||||
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
||||||
import it.danieleverducci.nextcloudmaps.repository.GeofavoriteRepository;
|
import it.danieleverducci.nextcloudmaps.repository.GeofavoriteRepository;
|
||||||
|
|
||||||
public class GeofavoriteDetailActivityViewModel extends ViewModel implements GeofavoriteRepository.OnFinished {
|
public class GeofavoriteDetailActivityViewModel extends ViewModel {
|
||||||
private GeofavoriteRepository mRepo;
|
private GeofavoriteRepository mRepo;
|
||||||
private MutableLiveData<Boolean> mIsUpdating = new MutableLiveData<>();
|
|
||||||
private MutableLiveData<Boolean> mIsFailed = new MutableLiveData<>();
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
mRepo = GeofavoriteRepository.getInstance();
|
mRepo = GeofavoriteRepository.getInstance();
|
||||||
mRepo.setOnFinishedListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Geofavorite getGeofavorite(int id) {
|
public Geofavorite getGeofavorite(int id) {
|
||||||
@ -27,26 +24,11 @@ public class GeofavoriteDetailActivityViewModel extends ViewModel implements Geo
|
|||||||
|
|
||||||
|
|
||||||
public LiveData<Boolean> getIsUpdating(){
|
public LiveData<Boolean> getIsUpdating(){
|
||||||
return mIsUpdating;
|
return mRepo.isUpdating();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<Boolean> getIsFailed(){
|
public LiveData<Boolean> getOnFinished(){
|
||||||
return mIsFailed;
|
return mRepo.onFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoading() {
|
|
||||||
mIsUpdating.postValue(true);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void onSuccess() {
|
|
||||||
mIsUpdating.postValue(false);
|
|
||||||
mIsFailed.postValue(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure() {
|
|
||||||
mIsUpdating.postValue(false);
|
|
||||||
mIsFailed.postValue(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -144,10 +144,10 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mMainActivityViewModel.getIsFailed().observe(this, new Observer<Boolean>() {
|
mMainActivityViewModel.getOnFinished().observe(this, new Observer<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(@Nullable Boolean aBoolean) {
|
public void onChanged(@Nullable Boolean success) {
|
||||||
if(aBoolean){
|
if(!success){
|
||||||
Toast.makeText(MainActivity.this, R.string.list_geofavorite_connection_error, Toast.LENGTH_LONG).show();
|
Toast.makeText(MainActivity.this, R.string.list_geofavorite_connection_error, Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,11 @@ import java.util.List;
|
|||||||
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
||||||
import it.danieleverducci.nextcloudmaps.repository.GeofavoriteRepository;
|
import it.danieleverducci.nextcloudmaps.repository.GeofavoriteRepository;
|
||||||
|
|
||||||
public class MainActivityViewModel extends ViewModel implements GeofavoriteRepository.OnFinished {
|
public class MainActivityViewModel extends ViewModel {
|
||||||
private GeofavoriteRepository mRepo;
|
private GeofavoriteRepository mRepo;
|
||||||
private MutableLiveData<Boolean> mIsUpdating = new MutableLiveData<>();
|
|
||||||
private MutableLiveData<Boolean> mIsFailed = new MutableLiveData<>();
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
mRepo = GeofavoriteRepository.getInstance();
|
mRepo = GeofavoriteRepository.getInstance();
|
||||||
mRepo.setOnFinishedListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<List<Geofavorite>> getGeofavorites(){
|
public LiveData<List<Geofavorite>> getGeofavorites(){
|
||||||
@ -33,26 +30,11 @@ public class MainActivityViewModel extends ViewModel implements GeofavoriteRepos
|
|||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<Boolean> getIsUpdating(){
|
public LiveData<Boolean> getIsUpdating(){
|
||||||
return mIsUpdating;
|
return mRepo.isUpdating();
|
||||||
}
|
}
|
||||||
|
|
||||||
public LiveData<Boolean> getIsFailed(){
|
public LiveData<Boolean> getOnFinished(){
|
||||||
return mIsFailed;
|
return mRepo.onFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onLoading() {
|
|
||||||
mIsUpdating.postValue(true);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void onSuccess() {
|
|
||||||
mIsUpdating.postValue(false);
|
|
||||||
mIsFailed.postValue(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure() {
|
|
||||||
mIsUpdating.postValue(false);
|
|
||||||
mIsFailed.postValue(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import it.danieleverducci.nextcloudmaps.api.ApiProvider;
|
import it.danieleverducci.nextcloudmaps.api.ApiProvider;
|
||||||
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
import it.danieleverducci.nextcloudmaps.model.Geofavorite;
|
||||||
|
import it.danieleverducci.nextcloudmaps.utils.SingleLiveEvent;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
@ -20,9 +21,11 @@ import retrofit2.Response;
|
|||||||
*/
|
*/
|
||||||
public class GeofavoriteRepository {
|
public class GeofavoriteRepository {
|
||||||
|
|
||||||
|
private static final String TAG = "GeofavoriteRepository";
|
||||||
private static GeofavoriteRepository instance;
|
private static GeofavoriteRepository instance;
|
||||||
private MutableLiveData<List<Geofavorite>> mGeofavorites;
|
private MutableLiveData<List<Geofavorite>> mGeofavorites;
|
||||||
private OnFinished listener;
|
private MutableLiveData<Boolean> mIsUpdating = new MutableLiveData<>(false);
|
||||||
|
private SingleLiveEvent<Boolean> mOnFinished = new SingleLiveEvent<>();
|
||||||
|
|
||||||
public static GeofavoriteRepository getInstance() {
|
public static GeofavoriteRepository getInstance() {
|
||||||
if(instance == null){
|
if(instance == null){
|
||||||
@ -39,8 +42,16 @@ public class GeofavoriteRepository {
|
|||||||
return mGeofavorites;
|
return mGeofavorites;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MutableLiveData<Boolean> isUpdating() {
|
||||||
|
return mIsUpdating;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SingleLiveEvent<Boolean> onFinished() {
|
||||||
|
return mOnFinished;
|
||||||
|
}
|
||||||
|
|
||||||
public void updateGeofavorites() {
|
public void updateGeofavorites() {
|
||||||
if (listener != null) listener.onLoading();
|
mIsUpdating.postValue(true);
|
||||||
// Obtain geofavorites
|
// Obtain geofavorites
|
||||||
Call<List<Geofavorite>> call = ApiProvider.getAPI().getGeofavorites();
|
Call<List<Geofavorite>> call = ApiProvider.getAPI().getGeofavorites();
|
||||||
call.enqueue(new Callback<List<Geofavorite>>() {
|
call.enqueue(new Callback<List<Geofavorite>>() {
|
||||||
@ -48,7 +59,8 @@ public class GeofavoriteRepository {
|
|||||||
public void onResponse(@NonNull Call<List<Geofavorite>> call, @NonNull Response<List<Geofavorite>> response) {
|
public void onResponse(@NonNull Call<List<Geofavorite>> call, @NonNull Response<List<Geofavorite>> response) {
|
||||||
if (response.isSuccessful() && response.body() != null) {
|
if (response.isSuccessful() && response.body() != null) {
|
||||||
mGeofavorites.postValue(response.body());
|
mGeofavorites.postValue(response.body());
|
||||||
if (listener != null) listener.onSuccess();
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(true);
|
||||||
} else {
|
} else {
|
||||||
onFailure(call, new Throwable("Dataset is empty"));
|
onFailure(call, new Throwable("Dataset is empty"));
|
||||||
}
|
}
|
||||||
@ -56,7 +68,8 @@ public class GeofavoriteRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call<List<Geofavorite>> call, @NonNull Throwable t) {
|
public void onFailure(@NonNull Call<List<Geofavorite>> call, @NonNull Throwable t) {
|
||||||
if (listener != null) listener.onFailure();
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -70,6 +83,7 @@ public class GeofavoriteRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveGeofavorite(Geofavorite geofav) {
|
public void saveGeofavorite(Geofavorite geofav) {
|
||||||
|
mIsUpdating.postValue(true);
|
||||||
Call<Geofavorite> call;
|
Call<Geofavorite> call;
|
||||||
if (geofav.getId() == 0) {
|
if (geofav.getId() == 0) {
|
||||||
// New geofavorite
|
// New geofavorite
|
||||||
@ -88,21 +102,25 @@ public class GeofavoriteRepository {
|
|||||||
}
|
}
|
||||||
geofavs.add(geofav);
|
geofavs.add(geofav);
|
||||||
mGeofavorites.postValue(geofavs);
|
mGeofavorites.postValue(geofavs);
|
||||||
if (listener != null) listener.onSuccess();
|
mIsUpdating.postValue(false);
|
||||||
} else if (listener != null) {
|
mOnFinished.postValue(true);
|
||||||
listener.onFailure();
|
} else {
|
||||||
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<Geofavorite> call, Throwable t) {
|
public void onFailure(Call<Geofavorite> call, Throwable t) {
|
||||||
if (listener != null) listener.onFailure();
|
Log.e(TAG, t.getMessage());
|
||||||
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteGeofavorite(Geofavorite geofav) {
|
public void deleteGeofavorite(Geofavorite geofav) {
|
||||||
if (listener != null) listener.onLoading();
|
mIsUpdating.postValue(true);
|
||||||
// Delete Geofavorite
|
// Delete Geofavorite
|
||||||
Call<Geofavorite> call = ApiProvider.getAPI().deleteGeofavorite(geofav.getId());
|
Call<Geofavorite> call = ApiProvider.getAPI().deleteGeofavorite(geofav.getId());
|
||||||
call.enqueue(new Callback<Geofavorite>() {
|
call.enqueue(new Callback<Geofavorite>() {
|
||||||
@ -111,29 +129,21 @@ public class GeofavoriteRepository {
|
|||||||
List<Geofavorite> geofavs = mGeofavorites.getValue();
|
List<Geofavorite> geofavs = mGeofavorites.getValue();
|
||||||
if (geofavs.remove(geofav)) {
|
if (geofavs.remove(geofav)) {
|
||||||
mGeofavorites.postValue(geofavs);
|
mGeofavorites.postValue(geofavs);
|
||||||
if (listener != null) listener.onSuccess();
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(true);
|
||||||
} else {
|
} else {
|
||||||
// Should never happen
|
// Should never happen
|
||||||
if (listener != null) listener.onFailure();
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<Geofavorite> call, Throwable t) {
|
public void onFailure(Call<Geofavorite> call, Throwable t) {
|
||||||
if (listener != null) listener.onFailure();
|
mIsUpdating.postValue(false);
|
||||||
|
mOnFinished.postValue(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOnFinishedListener(OnFinished listener) {
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface OnFinished {
|
|
||||||
void onLoading();
|
|
||||||
void onSuccess();
|
|
||||||
void onFailure();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package it.danieleverducci.nextcloudmaps.utils;
|
||||||
|
|
||||||
|
import androidx.annotation.MainThread;
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
import androidx.lifecycle.Observer;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Events implementation in LiveData
|
||||||
|
* From: https://gist.github.com/teegarcs/319a3e7e4736a0cce8eba2216c52b0ca#file-singleliveevent
|
||||||
|
* @param <T>
|
||||||
|
*/
|
||||||
|
public class SingleLiveEvent<T> extends MutableLiveData<T> {
|
||||||
|
AtomicBoolean mPending = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
@MainThread
|
||||||
|
@Override
|
||||||
|
public void setValue(T value) {
|
||||||
|
mPending.set(true);
|
||||||
|
super.setValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainThread
|
||||||
|
@Override
|
||||||
|
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
|
||||||
|
super.observe(owner, new Observer<T>() {
|
||||||
|
@Override
|
||||||
|
public void onChanged(T t) {
|
||||||
|
if (mPending.compareAndSet(true, false)) {
|
||||||
|
observer.onChanged(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Util function for Void implementations.
|
||||||
|
*/
|
||||||
|
public void call() {
|
||||||
|
setValue(null);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user