diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java index 8f2b2f1..4fe61d3 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteAdapter.java @@ -82,17 +82,6 @@ public class GeofavoriteAdapter extends RecyclerView.Adapter presenter.getGeofavorites()); + swipeRefresh.setOnRefreshListener(() -> updateGeofavorites()); fab = findViewById(R.id.add); fab.setOnClickListener(view -> addGeofavorite()); @@ -190,7 +193,7 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti super.onStart(); // Update list - presenter.getGeofavorites(); + mMainActivityViewModel.getGeofavorites().observe(this, this); } private void setupNavigationMenu() { @@ -239,9 +242,9 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == INTENT_ADD && resultCode == RESULT_OK) { - presenter.getGeofavorites(); + mMainActivityViewModel.getGeofavorites().observe(this, this); } else if (requestCode == INTENT_EDIT && resultCode == RESULT_OK) { - presenter.getGeofavorites(); + mMainActivityViewModel.getGeofavorites().observe(this, this); } } @@ -260,35 +263,12 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti startActivity(intent); } - @Override - public void showLoading() { - swipeRefresh.setRefreshing(true); - } - @Override - public void hideLoading() { - swipeRefresh.setRefreshing(false); - } - - @Override - public void onGetResult(List 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 - public void onErrorLoading(String message) { - Toast.makeText(MainActivity.this, R.string.list_geofavorite_connection_error, Toast.LENGTH_LONG).show(); - Log.e(TAG, "Unable to obtain geofavorites list: " + message); - } +// @Override +// public void onErrorLoading(String message) { +// Toast.makeText(MainActivity.this, R.string.list_geofavorite_connection_error, Toast.LENGTH_LONG).show(); +// Log.e(TAG, "Unable to obtain geofavorites list: " + message); +// } @Override public void onSortingOrderChosen(int sortSelection) { @@ -328,7 +308,7 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti .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()); + // TODO presenter.deleteGeofavorite(item.getId()); dialog.dismiss(); // Callback is onGeofavoriteDeleted } @@ -348,4 +328,26 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti startActivity(i); } + private void updateGeofavorites() { + mMainActivityViewModel.getGeofavorites().observe(this, new Observer>() { + @Override + public void onChanged(List geofavorites) { + geofavoriteAdapter.setGeofavoriteList(geofavorites); + } + }); + + // TODO: รจ possibile registrare un solo listener? + mMainActivityViewModel.getIsUpdating().observe(this, new Observer() { + @Override + public void onChanged(@Nullable Boolean aBoolean) { + if(aBoolean){ + swipeRefresh.setRefreshing(true); + } + else{ + swipeRefresh.setRefreshing(false); + } + } + }); + } + } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivityViewModel.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivityViewModel.java new file mode 100644 index 0000000..e5b46fc --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivityViewModel.java @@ -0,0 +1,33 @@ +package it.danieleverducci.nextcloudmaps.activity.main; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import java.util.List; + +import it.danieleverducci.nextcloudmaps.model.Geofavorite; +import it.danieleverducci.nextcloudmaps.repository.GeofavoriteRepository; + +public class MainActivityViewModel extends ViewModel { + + private MutableLiveData> mGeofavorites; + private GeofavoriteRepository mRepo; + private MutableLiveData mIsUpdating = new MutableLiveData<>(); + + public void init() { + if (mGeofavorites != null) + return; + + mRepo = GeofavoriteRepository.getInstance(); + mGeofavorites = mRepo.getGeofavorites(); + } + + public LiveData> getGeofavorites(){ + return mGeofavorites; + } + + public LiveData getIsUpdating(){ + return mIsUpdating; + } +} diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java deleted file mode 100644 index 51112c9..0000000 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainPresenter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Nextcloud Maps Geofavorites for Android - * - * @copyright Copyright (c) 2020 John Doe - * @author John Doe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package it.danieleverducci.nextcloudmaps.activity.main; - -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AppCompatActivity; - -import java.util.List; - -import it.danieleverducci.nextcloudmaps.api.ApiProvider; -import it.danieleverducci.nextcloudmaps.model.Geofavorite; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -public class MainPresenter { - private MainView view; - - public MainPresenter(MainView view) { - this.view = view; - } - - public void getGeofavorites() { - view.showLoading(); - Call> call = ApiProvider.getAPI().getGeofavorites(); - call.enqueue(new Callback>() { - @Override - public void onResponse(@NonNull Call> call, @NonNull Response> response) { - ((AppCompatActivity) view).runOnUiThread(() -> { - view.hideLoading(); - if (response.isSuccessful() && response.body() != null) { - view.onGetResult(response.body()); - } else { - ((AppCompatActivity) view).runOnUiThread(() -> { - view.hideLoading(); - view.onErrorLoading(response.raw().message()); - }); - } - }); - } - - @Override - public void onFailure(@NonNull Call> call, @NonNull Throwable t) { - ((AppCompatActivity) view).runOnUiThread(() -> { - view.hideLoading(); - view.onErrorLoading(t.getLocalizedMessage()); - }); - } - }); - } - - public void deleteGeofavorite(int id) { - view.showLoading(); - Call call = ApiProvider.getAPI().deleteGeofavorite(id); - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - view.hideLoading(); - view.onGeofavoriteDeleted(id); - } - - @Override - public void onFailure(Call call, Throwable t) { - view.hideLoading(); - view.onErrorLoading(t.getLocalizedMessage()); - } - }); - } -} diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainView.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainView.java deleted file mode 100644 index de44b00..0000000 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainView.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Nextcloud Maps Geofavorites for Android - * - * @copyright Copyright (c) 2020 John Doe - * @author John Doe - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package it.danieleverducci.nextcloudmaps.activity.main; - -import java.util.List; - -import it.danieleverducci.nextcloudmaps.model.Geofavorite; - -public interface MainView { - void showLoading(); - void hideLoading(); - void onGetResult(List geofavorites); - void onGeofavoriteDeleted(int id); - void onErrorLoading(String message); -} diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/repository/GeofavoriteRepository.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/repository/GeofavoriteRepository.java new file mode 100644 index 0000000..c3e9789 --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/repository/GeofavoriteRepository.java @@ -0,0 +1,56 @@ +package it.danieleverducci.nextcloudmaps.repository; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.MutableLiveData; + +import java.util.ArrayList; +import java.util.List; + +import it.danieleverducci.nextcloudmaps.api.ApiProvider; +import it.danieleverducci.nextcloudmaps.model.Geofavorite; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +/** + * Singleton pattern + */ +public class GeofavoriteRepository { + + private static GeofavoriteRepository instance; + private ArrayList dataSet = new ArrayList<>(); + + public static GeofavoriteRepository getInstance() { + if(instance == null){ + instance = new GeofavoriteRepository(); + } + return instance; + } + + public MutableLiveData> getGeofavorites(){ + // Obtain geofavorites + Call> call = ApiProvider.getAPI().getGeofavorites(); + call.enqueue(new Callback>() { + @Override + public void onResponse(@NonNull Call> call, @NonNull Response> response) { + if (response.isSuccessful() && response.body() != null) { + dataSet.addAll(response.body()); + } else { + onFailure(call, new Throwable("Dataset is empty")); + } + } + + @Override + public void onFailure(@NonNull Call> call, @NonNull Throwable t) { + // TODO + } + }); + + MutableLiveData> data = new MutableLiveData<>(); + data.setValue(dataSet); + return data; + } + + +}