WIP implementing MVVM
This commit is contained in:
		
							
								
								
									
										2
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								.idea/misc.xml
									
									
									
										generated
									
									
									
								
							| @@ -10,7 +10,7 @@ | |||||||
|         <entry key="app/src/main/res/drawable/ic_nav.xml" value="0.6083333333333333" /> |         <entry key="app/src/main/res/drawable/ic_nav.xml" value="0.6083333333333333" /> | ||||||
|         <entry key="app/src/main/res/drawable/ic_share.xml" value="0.8828125" /> |         <entry key="app/src/main/res/drawable/ic_share.xml" value="0.8828125" /> | ||||||
|         <entry key="app/src/main/res/layout/activity_geofavorite_detail.xml" value="0.4" /> |         <entry key="app/src/main/res/layout/activity_geofavorite_detail.xml" value="0.4" /> | ||||||
|         <entry key="app/src/main/res/layout/activity_list_view.xml" value="0.5307291666666667" /> |         <entry key="app/src/main/res/layout/activity_list_view.xml" value="0.4" /> | ||||||
|         <entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" /> |         <entry key="app/src/main/res/layout/activity_main.xml" value="0.5307291666666667" /> | ||||||
|         <entry key="app/src/main/res/layout/item_geofav.xml" value="0.5307291666666667" /> |         <entry key="app/src/main/res/layout/item_geofav.xml" value="0.5307291666666667" /> | ||||||
|         <entry key="app/src/main/res/menu/list_context_menu.xml" value="0.41944444444444445" /> |         <entry key="app/src/main/res/menu/list_context_menu.xml" value="0.41944444444444445" /> | ||||||
|   | |||||||
| @@ -37,6 +37,8 @@ import androidx.annotation.Nullable; | |||||||
| import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||||
| import androidx.appcompat.content.res.AppCompatResources; | import androidx.appcompat.content.res.AppCompatResources; | ||||||
| import androidx.core.app.ActivityCompat; | import androidx.core.app.ActivityCompat; | ||||||
|  | import androidx.lifecycle.Observer; | ||||||
|  | import androidx.lifecycle.ViewModelProvider; | ||||||
| import androidx.preference.PreferenceManager; | import androidx.preference.PreferenceManager; | ||||||
|  |  | ||||||
| import org.osmdroid.api.IMapController; | import org.osmdroid.api.IMapController; | ||||||
| @@ -48,9 +50,11 @@ import org.threeten.bp.format.DateTimeFormatter; | |||||||
| import org.threeten.bp.format.FormatStyle; | import org.threeten.bp.format.FormatStyle; | ||||||
|  |  | ||||||
| import java.util.Date; | import java.util.Date; | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
| import it.danieleverducci.nextcloudmaps.R; | import it.danieleverducci.nextcloudmaps.R; | ||||||
| import it.danieleverducci.nextcloudmaps.activity.main.MainActivity; | import it.danieleverducci.nextcloudmaps.activity.main.MainActivity; | ||||||
|  | import it.danieleverducci.nextcloudmaps.activity.main.MainActivityViewModel; | ||||||
| import it.danieleverducci.nextcloudmaps.api.ApiProvider; | import it.danieleverducci.nextcloudmaps.api.ApiProvider; | ||||||
| import it.danieleverducci.nextcloudmaps.databinding.ActivityGeofavoriteDetailBinding; | import it.danieleverducci.nextcloudmaps.databinding.ActivityGeofavoriteDetailBinding; | ||||||
| import it.danieleverducci.nextcloudmaps.model.Geofavorite; | import it.danieleverducci.nextcloudmaps.model.Geofavorite; | ||||||
| @@ -68,6 +72,7 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca | |||||||
|  |  | ||||||
|     private ViewHolder mViewHolder; |     private ViewHolder mViewHolder; | ||||||
|     private Geofavorite mGeofavorite; |     private Geofavorite mGeofavorite; | ||||||
|  |     private GeofavoriteDetailActivityViewModel mViewModel; | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     protected void onCreate(@Nullable Bundle savedInstanceState) { |     protected void onCreate(@Nullable Bundle savedInstanceState) { | ||||||
| @@ -112,9 +117,31 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca | |||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         if (getIntent().hasExtra(ARG_GEOFAVORITE)) { |         mViewModel = new ViewModelProvider(this).get(GeofavoriteDetailActivityViewModel.class); | ||||||
|  |         mViewModel.init(); | ||||||
|  |         mViewModel.getIsUpdating().observe(this, new Observer<Boolean>() { | ||||||
|  |             @Override | ||||||
|  |             public void onChanged(@Nullable Boolean updating) { | ||||||
|  |                 mViewHolder.setUpdating(updating); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         mViewModel.getIsFailed().observe(this, new Observer<Boolean>() { | ||||||
|  |             @Override | ||||||
|  |             public void onChanged(@Nullable Boolean failed) { | ||||||
|  |                 if(failed){ | ||||||
|  |                     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(); | ||||||
|  |                     finish(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         if (getIntent().hasExtra(ARG_GEOFAVORITE) && getIntent().getIntExtra(ARG_GEOFAVORITE, 0) != 0) { | ||||||
|             // Opening geofavorite from list |             // Opening geofavorite from list | ||||||
|             mGeofavorite = (Geofavorite) getIntent().getSerializableExtra(ARG_GEOFAVORITE); |             mGeofavorite = mViewModel.getGeofavorite( | ||||||
|  |                     getIntent().getIntExtra(ARG_GEOFAVORITE, 0) | ||||||
|  |             ); | ||||||
|             mViewHolder.hideAccuracy(); |             mViewHolder.hideAccuracy(); | ||||||
|         } else { |         } else { | ||||||
|             // New geofavorite |             // New geofavorite | ||||||
| @@ -169,35 +196,7 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         Call<Geofavorite> call; |         mViewModel.saveGeofavorite(mGeofavorite); | ||||||
|         if (mGeofavorite.getId() == 0) { |  | ||||||
|             // New geofavorite |  | ||||||
|             call = ApiProvider.getAPI().createGeofavorite(mGeofavorite); |  | ||||||
|         } else { |  | ||||||
|             // Update existing geofavorite |  | ||||||
|             call = ApiProvider.getAPI().updateGeofavorite(mGeofavorite.getId(), mGeofavorite); |  | ||||||
|         } |  | ||||||
|         call.enqueue(new Callback<Geofavorite>() { |  | ||||||
|             @Override |  | ||||||
|             public void onResponse(Call<Geofavorite> call, Response<Geofavorite> response) { |  | ||||||
|                 if (response.isSuccessful()) |  | ||||||
|                     finish(); |  | ||||||
|                 else |  | ||||||
|                     onGeofavoriteSaveFailed(); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             @Override |  | ||||||
|             public void onFailure(Call<Geofavorite> call, Throwable t) { |  | ||||||
|                 onGeofavoriteSaveFailed(); |  | ||||||
|                 Log.e(TAG, "Unable to update geofavorite: " + t.getMessage()); |  | ||||||
|             } |  | ||||||
|         }); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void onGeofavoriteSaveFailed() { |  | ||||||
|         runOnUiThread(() -> |  | ||||||
|             Toast.makeText(GeofavoriteDetailActivity.this, R.string.error_saving_geofavorite, Toast.LENGTH_SHORT).show() |  | ||||||
|         ); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -334,6 +333,10 @@ public class GeofavoriteDetailActivity extends AppCompatActivity implements Loca | |||||||
|             item.setDateModified(System.currentTimeMillis() / 1000); |             item.setDateModified(System.currentTimeMillis() / 1000); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         public void setUpdating(boolean updating) { | ||||||
|  |             binding.progress.setVisibility(updating ? View.VISIBLE : View.GONE); | ||||||
|  |         } | ||||||
|  |  | ||||||
|         public void setAccuracy(float accuracy) { |         public void setAccuracy(float accuracy) { | ||||||
|             binding.accuracyTv.setText(getString(R.string.accuracy).replace("{accuracy}", ((int)accuracy) + "")); |             binding.accuracyTv.setText(getString(R.string.accuracy).replace("{accuracy}", ((int)accuracy) + "")); | ||||||
|             // Color the accuracy background with a scale from red (MINIMUM_ACCEPTABLE_ACCURACY) to green (0 meters) |             // Color the accuracy background with a scale from red (MINIMUM_ACCEPTABLE_ACCURACY) to green (0 meters) | ||||||
|   | |||||||
| @@ -0,0 +1,52 @@ | |||||||
|  | package it.danieleverducci.nextcloudmaps.activity.detail; | ||||||
|  |  | ||||||
|  | import androidx.lifecycle.LiveData; | ||||||
|  | import androidx.lifecycle.MutableLiveData; | ||||||
|  | import androidx.lifecycle.ViewModel; | ||||||
|  |  | ||||||
|  | import it.danieleverducci.nextcloudmaps.model.Geofavorite; | ||||||
|  | import it.danieleverducci.nextcloudmaps.repository.GeofavoriteRepository; | ||||||
|  |  | ||||||
|  | public class GeofavoriteDetailActivityViewModel extends ViewModel implements GeofavoriteRepository.OnFinished { | ||||||
|  |     private GeofavoriteRepository mRepo; | ||||||
|  |     private MutableLiveData<Boolean> mIsUpdating = new MutableLiveData<>(); | ||||||
|  |     private MutableLiveData<Boolean> mIsFailed = new MutableLiveData<>(); | ||||||
|  |  | ||||||
|  |     public void init() { | ||||||
|  |         mRepo = GeofavoriteRepository.getInstance(); | ||||||
|  |         mRepo.setOnFinishedListener(this); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Geofavorite getGeofavorite(int id) { | ||||||
|  |         return mRepo.getGeofavorite(id); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void saveGeofavorite(Geofavorite geofav) { | ||||||
|  |         mRepo.saveGeofavorite(geofav); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     public LiveData<Boolean> getIsUpdating(){ | ||||||
|  |         return mIsUpdating; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public LiveData<Boolean> getIsFailed(){ | ||||||
|  |         return mIsFailed; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @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); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -67,9 +67,6 @@ import androidx.lifecycle.Observer; | |||||||
|  |  | ||||||
| public class MainActivity extends AppCompatActivity implements OnSortingOrderListener { | public class MainActivity extends AppCompatActivity implements OnSortingOrderListener { | ||||||
|  |  | ||||||
|     private static final int INTENT_ADD = 100; |  | ||||||
|     private static final int INTENT_EDIT = 200; |  | ||||||
|  |  | ||||||
|     private static final String TAG = "MainActivity"; |     private static final String TAG = "MainActivity"; | ||||||
|  |  | ||||||
|     private static final String NAVIGATION_KEY_ADD_GEOFAVORITE = "add"; |     private static final String NAVIGATION_KEY_ADD_GEOFAVORITE = "add"; | ||||||
| @@ -98,8 +95,6 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|         super.onCreate(savedInstanceState); |         super.onCreate(savedInstanceState); | ||||||
|         setContentView(R.layout.activity_main); |         setContentView(R.layout.activity_main); | ||||||
|  |  | ||||||
|         mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class); |  | ||||||
|         mMainActivityViewModel.init(); |  | ||||||
|         preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); |         preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); | ||||||
|  |  | ||||||
|         int sortRule = preferences.getInt(getString(R.string.setting_sort_by), SORT_BY_CREATED); |         int sortRule = preferences.getInt(getString(R.string.setting_sort_by), SORT_BY_CREATED); | ||||||
| @@ -135,8 +130,39 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|         recyclerView.setAdapter(geofavoriteAdapter); |         recyclerView.setAdapter(geofavoriteAdapter); | ||||||
|         geofavoriteAdapter.setSortRule(sortRule); |         geofavoriteAdapter.setSortRule(sortRule); | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class); | ||||||
|  |         mMainActivityViewModel.init(); | ||||||
|  |         mMainActivityViewModel.getIsUpdating().observe(this, new Observer<Boolean>() { | ||||||
|  |             @Override | ||||||
|  |             public void onChanged(@Nullable Boolean aBoolean) { | ||||||
|  |                 if(aBoolean){ | ||||||
|  |                     swipeRefresh.setRefreshing(true); | ||||||
|  |                 } | ||||||
|  |                 else{ | ||||||
|  |                     swipeRefresh.setRefreshing(false); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         mMainActivityViewModel.getIsFailed().observe(this, new Observer<Boolean>() { | ||||||
|  |             @Override | ||||||
|  |             public void onChanged(@Nullable Boolean aBoolean) { | ||||||
|  |                 if(aBoolean){ | ||||||
|  |                     Toast.makeText(MainActivity.this, R.string.list_geofavorite_connection_error, Toast.LENGTH_LONG).show(); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         mMainActivityViewModel.getGeofavorites().observe(this, new Observer<List<Geofavorite>>() { | ||||||
|  |             @Override | ||||||
|  |             public void onChanged(List<Geofavorite> geofavorites) { | ||||||
|  |                 geofavoriteAdapter.setGeofavoriteList(geofavorites); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         mMainActivityViewModel.updateGeofavorites(); | ||||||
|  |  | ||||||
|         swipeRefresh = findViewById(R.id.swipe_refresh); |         swipeRefresh = findViewById(R.id.swipe_refresh); | ||||||
|         swipeRefresh.setOnRefreshListener(() -> updateGeofavorites()); |         swipeRefresh.setOnRefreshListener(() -> | ||||||
|  |                 mMainActivityViewModel.updateGeofavorites()); | ||||||
|  |  | ||||||
|         fab = findViewById(R.id.add); |         fab = findViewById(R.id.add); | ||||||
|         fab.setOnClickListener(view -> addGeofavorite()); |         fab.setOnClickListener(view -> addGeofavorite()); | ||||||
| @@ -188,14 +214,6 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|         updateGridIcon(gridViewEnabled); |         updateGridIcon(gridViewEnabled); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     protected void onStart() { |  | ||||||
|         super.onStart(); |  | ||||||
|  |  | ||||||
|         // Update list |  | ||||||
|         mMainActivityViewModel.getGeofavorites().observe(this, this); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void setupNavigationMenu() { |     private void setupNavigationMenu() { | ||||||
|         ArrayList<NavigationItem> navItems = new ArrayList<>(); |         ArrayList<NavigationItem> navItems = new ArrayList<>(); | ||||||
|  |  | ||||||
| @@ -238,19 +256,10 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|         SortingOrderDialogFragment.newInstance(sortOrder).show(fragmentTransaction, SortingOrderDialogFragment.SORTING_ORDER_FRAGMENT); |         SortingOrderDialogFragment.newInstance(sortOrder).show(fragmentTransaction, SortingOrderDialogFragment.SORTING_ORDER_FRAGMENT); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public void onActivityResult(int requestCode, int resultCode, Intent data) { |  | ||||||
|         super.onActivityResult(requestCode, resultCode, data); |  | ||||||
|         if (requestCode == INTENT_ADD && resultCode == RESULT_OK) { |  | ||||||
|             mMainActivityViewModel.getGeofavorites().observe(this, this); |  | ||||||
|         } else if (requestCode == INTENT_EDIT && resultCode == RESULT_OK) { |  | ||||||
|             mMainActivityViewModel.getGeofavorites().observe(this, this); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void addGeofavorite() { |     private void addGeofavorite() { | ||||||
|         startActivityForResult( |         startActivity( | ||||||
|                 new Intent(this, GeofavoriteDetailActivity.class), INTENT_ADD); |                 new Intent(this, GeofavoriteDetailActivity.class) | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void show_about() { |     private void show_about() { | ||||||
| @@ -263,13 +272,6 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|         startActivity(intent); |         startActivity(intent); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
| //    @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 |     @Override | ||||||
|     public void onSortingOrderChosen(int sortSelection) { |     public void onSortingOrderChosen(int sortSelection) { | ||||||
|         geofavoriteAdapter.setSortRule(sortSelection); |         geofavoriteAdapter.setSortRule(sortSelection); | ||||||
| @@ -308,9 +310,8 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|                 .setTitle(R.string.dialog_delete_title) |                 .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) { | ||||||
|                         // TODO presenter.deleteGeofavorite(item.getId()); |                         mMainActivityViewModel.deleteGeofavorite(item); | ||||||
|                         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() { | ||||||
| @@ -324,30 +325,8 @@ public class MainActivity extends AppCompatActivity implements OnSortingOrderLis | |||||||
|  |  | ||||||
|     private void showGeofavoriteDetailActivity(Geofavorite item) { |     private void showGeofavoriteDetailActivity(Geofavorite item) { | ||||||
|         Intent i = new Intent(this, GeofavoriteDetailActivity.class); |         Intent i = new Intent(this, GeofavoriteDetailActivity.class); | ||||||
|         i.putExtra(GeofavoriteDetailActivity.ARG_GEOFAVORITE, item); |         i.putExtra(GeofavoriteDetailActivity.ARG_GEOFAVORITE, item.getId()); | ||||||
|         startActivity(i); |         startActivity(i); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void updateGeofavorites() { |  | ||||||
|         mMainActivityViewModel.getGeofavorites().observe(this, new Observer<List<Geofavorite>>() { |  | ||||||
|             @Override |  | ||||||
|             public void onChanged(List<Geofavorite> geofavorites) { |  | ||||||
|                 geofavoriteAdapter.setGeofavoriteList(geofavorites); |  | ||||||
|             } |  | ||||||
|         }); |  | ||||||
|  |  | ||||||
|         // TODO: è possibile registrare un solo listener? |  | ||||||
|         mMainActivityViewModel.getIsUpdating().observe(this, new Observer<Boolean>() { |  | ||||||
|             @Override |  | ||||||
|             public void onChanged(@Nullable Boolean aBoolean) { |  | ||||||
|                 if(aBoolean){ |  | ||||||
|                     swipeRefresh.setRefreshing(true); |  | ||||||
|                 } |  | ||||||
|                 else{ |  | ||||||
|                     swipeRefresh.setRefreshing(false); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         }); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| } | } | ||||||
| @@ -9,25 +9,50 @@ 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 { | public class MainActivityViewModel extends ViewModel implements GeofavoriteRepository.OnFinished { | ||||||
|  |  | ||||||
|     private MutableLiveData<List<Geofavorite>> mGeofavorites; |  | ||||||
|     private GeofavoriteRepository mRepo; |     private GeofavoriteRepository mRepo; | ||||||
|     private MutableLiveData<Boolean> mIsUpdating = new MutableLiveData<>(); |     private MutableLiveData<Boolean> mIsUpdating = new MutableLiveData<>(); | ||||||
|  |     private MutableLiveData<Boolean> mIsFailed = new MutableLiveData<>(); | ||||||
|  |  | ||||||
|     public void init() { |     public void init() { | ||||||
|         if (mGeofavorites != null) |  | ||||||
|             return; |  | ||||||
|  |  | ||||||
|         mRepo = GeofavoriteRepository.getInstance(); |         mRepo = GeofavoriteRepository.getInstance(); | ||||||
|         mGeofavorites = mRepo.getGeofavorites(); |         mRepo.setOnFinishedListener(this); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public LiveData<List<Geofavorite>> getGeofavorites(){ |     public LiveData<List<Geofavorite>> getGeofavorites(){ | ||||||
|         return mGeofavorites; |         mRepo.updateGeofavorites(); | ||||||
|  |         return mRepo.getGeofavorites(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void updateGeofavorites() { | ||||||
|  |         mRepo.updateGeofavorites(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void deleteGeofavorite(Geofavorite geofav) { | ||||||
|  |         mRepo.deleteGeofavorite(geofav); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public LiveData<Boolean> getIsUpdating(){ |     public LiveData<Boolean> getIsUpdating(){ | ||||||
|         return mIsUpdating; |         return mIsUpdating; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public LiveData<Boolean> getIsFailed(){ | ||||||
|  |         return mIsFailed; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @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); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,5 +1,7 @@ | |||||||
| package it.danieleverducci.nextcloudmaps.repository; | package it.danieleverducci.nextcloudmaps.repository; | ||||||
|  |  | ||||||
|  | import android.util.Log; | ||||||
|  |  | ||||||
| import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||||
| import androidx.appcompat.app.AppCompatActivity; | import androidx.appcompat.app.AppCompatActivity; | ||||||
| import androidx.lifecycle.MutableLiveData; | import androidx.lifecycle.MutableLiveData; | ||||||
| @@ -19,7 +21,8 @@ import retrofit2.Response; | |||||||
| public class GeofavoriteRepository { | public class GeofavoriteRepository { | ||||||
|  |  | ||||||
|     private static GeofavoriteRepository instance; |     private static GeofavoriteRepository instance; | ||||||
|     private ArrayList<Geofavorite> dataSet = new ArrayList<>(); |     private MutableLiveData<List<Geofavorite>> mGeofavorites; | ||||||
|  |     private OnFinished listener; | ||||||
|  |  | ||||||
|     public static GeofavoriteRepository getInstance() { |     public static GeofavoriteRepository getInstance() { | ||||||
|         if(instance == null){ |         if(instance == null){ | ||||||
| @@ -29,13 +32,23 @@ public class GeofavoriteRepository { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public MutableLiveData<List<Geofavorite>> getGeofavorites(){ |     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 |         // 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>>() { | ||||||
|             @Override |             @Override | ||||||
|             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) { | ||||||
|                     dataSet.addAll(response.body()); |                     mGeofavorites.postValue(response.body()); | ||||||
|  |                     if (listener != null) listener.onSuccess(); | ||||||
|                 } else { |                 } else { | ||||||
|                     onFailure(call, new Throwable("Dataset is empty")); |                     onFailure(call, new Throwable("Dataset is empty")); | ||||||
|                 } |                 } | ||||||
| @@ -43,13 +56,83 @@ 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) { | ||||||
|                 // TODO |                 if (listener != null) listener.onFailure(); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|         MutableLiveData<List<Geofavorite>> data = new MutableLiveData<>(); |     public Geofavorite getGeofavorite(int id) { | ||||||
|         data.setValue(dataSet); |         for (Geofavorite g : mGeofavorites.getValue()) { | ||||||
|         return data; |             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(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
| @@ -206,6 +206,13 @@ | |||||||
|                     android:layout_height="wrap_content" |                     android:layout_height="wrap_content" | ||||||
|                     android:textAlignment="textEnd" /> |                     android:textAlignment="textEnd" /> | ||||||
|  |  | ||||||
|  |                 <ProgressBar | ||||||
|  |                     android:id="@+id/progress" | ||||||
|  |                     android:layout_width="match_parent" | ||||||
|  |                     android:layout_height="wrap_content" | ||||||
|  |                     android:indeterminate="true" | ||||||
|  |                     android:visibility="gone"/> | ||||||
|  |  | ||||||
|                 <Button |                 <Button | ||||||
|                     android:id="@+id/submit_bt" |                     android:id="@+id/submit_bt" | ||||||
|                     style="@style/Widget.AppCompat.Button.Colored" |                     style="@style/Widget.AppCompat.Button.Colored" | ||||||
|   | |||||||
| @@ -57,6 +57,7 @@ | |||||||
|     <string name="location_permission_required">Location permission is required to create a geofavorite.</string> |     <string name="location_permission_required">Location permission is required to create a geofavorite.</string> | ||||||
|     <string name="confirm">Save</string> |     <string name="confirm">Save</string> | ||||||
|     <string name="error_saving_geofavorite">Unable to save geofavorite</string> |     <string name="error_saving_geofavorite">Unable to save geofavorite</string> | ||||||
|  |     <string name="geofavorite_saved">Geofavorite saved</string> | ||||||
|     <string name="incomplete_geofavorite">Incomplete geofavorite: Name and GPS coordinates are mandatory</string> |     <string name="incomplete_geofavorite">Incomplete geofavorite: Name and GPS coordinates are mandatory</string> | ||||||
|  |  | ||||||
|     <!-- About --> |     <!-- About --> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user