From be910bcabeb7e21d8455d9396067b1e9745e790c Mon Sep 17 00:00:00 2001 From: Daniele Date: Wed, 15 Sep 2021 09:16:40 +0200 Subject: [PATCH] WIP refactoring geofavorites list activity into fragment --- .idea/compiler.xml | 2 +- .idea/misc.xml | 7 +- app/build.gradle | 1 + .../activity/main/GeofavoriteActivity.java | 11 ++ .../activity/main/MainActivity.java | 122 +++---------- .../fragments/GeofavoriteListConsumer.java | 12 ++ .../fragments/GeofavoriteListFragment.java | 152 ++++++++++++++++ .../nextcloudmaps/fragments/MapFragment.java | 66 +++++++ .../fragments/SortableListFragment.java | 5 + app/src/main/res/drawable/ic_view_list.xml | 23 --- app/src/main/res/drawable/ic_view_module.xml | 25 --- .../main/res/layout/activity_list_view.xml | 166 ------------------ app/src/main/res/layout/activity_main.xml | 122 ++++++++++++- .../res/layout/fragment_geofavorite_list.xml | 55 ++++++ app/src/main/res/layout/fragment_map.xml | 14 ++ app/src/main/res/values/strings.xml | 1 - 16 files changed, 460 insertions(+), 324 deletions(-) create mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteActivity.java create mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListConsumer.java create mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListFragment.java create mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/MapFragment.java create mode 100644 app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/SortableListFragment.java delete mode 100644 app/src/main/res/drawable/ic_view_list.xml delete mode 100644 app/src/main/res/drawable/ic_view_module.xml delete mode 100644 app/src/main/res/layout/activity_list_view.xml create mode 100644 app/src/main/res/layout/fragment_geofavorite_list.xml create mode 100644 app/src/main/res/layout/fragment_map.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fb7f4a8..61a9130 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 80d7605..d39afe7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,15 +4,16 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index f8e32af..6ed9bbc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -60,6 +60,7 @@ dependencies { implementation "androidx.cardview:cardview:1.0.0" implementation 'androidx.constraintlayout:constraintlayout:2.1.0' implementation "androidx.preference:preference:1.1.1" + implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteActivity.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteActivity.java new file mode 100644 index 0000000..beb4a86 --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/GeofavoriteActivity.java @@ -0,0 +1,11 @@ +package it.danieleverducci.nextcloudmaps.activity.main; + +import it.danieleverducci.nextcloudmaps.model.Geofavorite; + +public interface GeofavoriteActivity { + void showGeofavoriteDetailActivity(Geofavorite item); + void showGeofavoriteDeteleDialog(Geofavorite item); + void updateGeofavorites(); + void getGeofavorites(); + +} diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java index 577de6d..2c403ac 100644 --- a/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/activity/main/MainActivity.java @@ -33,6 +33,7 @@ import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.preference.PreferenceManager; @@ -61,7 +62,7 @@ import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter. import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_CREATED; import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_TITLE; -public class MainActivity extends AppCompatActivity implements MainView, OnSortingOrderListener { +public class MainActivity extends AppCompatActivity implements MainView, GeofavoriteActivity { private static final int INTENT_ADD = 100; private static final int INTENT_EDIT = 200; @@ -72,20 +73,13 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti private static final String NAVIGATION_KEY_SHOW_ABOUT = "about"; private static final String NAVIGATION_KEY_SWITCH_ACCOUNT = "switch_account"; - private SharedPreferences preferences; - private DrawerLayout drawerLayout; private Toolbar toolbar; private MaterialCardView homeToolbar; private SearchView searchView; - private SwipeRefreshLayout swipeRefresh; - private RecyclerView recyclerView; - private StaggeredGridLayoutManager layoutManager; private FloatingActionButton fab; private MainPresenter presenter; - private GeofavoriteAdapter geofavoriteAdapter; - private ItemClickListener rvItemClickListener; NavigationAdapter navigationCommonAdapter; @@ -94,56 +88,8 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - - int sortRule = preferences.getInt(getString(R.string.setting_sort_by), SORT_BY_CREATED); - boolean gridViewEnabled = preferences.getBoolean(getString(R.string.setting_grid_view_enabled), false); - - recyclerView = findViewById(R.id.recycler_view); - layoutManager = new StaggeredGridLayoutManager(gridViewEnabled ? 2 : 1, StaggeredGridLayoutManager.VERTICAL); - recyclerView.setLayoutManager(layoutManager); - presenter = new MainPresenter(this); - rvItemClickListener = new ItemClickListener() { - @Override - public void onItemClick(Geofavorite geofavorite) { - Intent i = new Intent(); - i.setAction(Intent.ACTION_VIEW); - i.setData(geofavorite.getGeoUri()); - startActivity(i); - } - - @Override - public void onItemShareClick(Geofavorite item) { - Intent i = new Intent(); - i.setAction(Intent.ACTION_SEND); - i.setType("text/plain"); - String shareMessage = getString(R.string.share_message) - .replace("{lat}", ""+item.getLat()) - .replace("{lng}", ""+item.getLng()); - i.putExtra(Intent.EXTRA_TEXT, shareMessage ); - startActivity(Intent.createChooser(i, getString(R.string.share_via))); - } - - @Override - public void onItemDetailsClick(Geofavorite item) { - showGeofavoriteDetailActivity(item); - } - - @Override - public void onItemDeleteClick(Geofavorite item) { - showGeofavoriteDeteleDialog(item); - } - }; - - geofavoriteAdapter = new GeofavoriteAdapter(getApplicationContext(), rvItemClickListener); - recyclerView.setAdapter(geofavoriteAdapter); - geofavoriteAdapter.setSortRule(sortRule); - - swipeRefresh = findViewById(R.id.swipe_refresh); - swipeRefresh.setOnRefreshListener(() -> presenter.getGeofavorites()); - fab = findViewById(R.id.add); fab.setOnClickListener(view -> addGeofavorite()); @@ -184,14 +130,7 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti AppCompatImageButton menuButton = findViewById(R.id.menu_button); menuButton.setOnClickListener(view -> drawerLayout.openDrawer(GravityCompat.START)); - AppCompatImageView viewButton = findViewById(R.id.view_mode); - viewButton.setOnClickListener(view -> { - boolean gridEnabled = layoutManager.getSpanCount() == 1; - onGridIconChosen(gridEnabled); - }); - updateSortingIcon(sortRule); - updateGridIcon(gridViewEnabled); } @Override @@ -271,24 +210,24 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti @Override public void showLoading() { - swipeRefresh.setRefreshing(true); + //todo notify GeofavoriteListConsumer.geoFavoriteLoading(true) } @Override public void hideLoading() { - swipeRefresh.setRefreshing(false); + //todo notify GeofavoriteListConsumer.geoFavoriteLoading(false) } @Override public void onGetResult(List geofavorite_list) { - geofavoriteAdapter.setGeofavoriteList(geofavorite_list); + //todo notify GeofavoriteListConsumer } @Override public void onGeofavoriteDeleted(int id) { - // Update list + // Notify fragment runOnUiThread(() -> { - geofavoriteAdapter.removeById(id); + //todo notify GeofavoriteListConsumer Toast.makeText(MainActivity.this, R.string.list_geofavorite_deleted, Toast.LENGTH_LONG).show(); }); } @@ -300,38 +239,7 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti } @Override - public void onSortingOrderChosen(int sortSelection) { - geofavoriteAdapter.setSortRule(sortSelection); - updateSortingIcon(sortSelection); - - preferences.edit().putInt(getString(R.string.setting_sort_by), sortSelection).apply(); - } - - public void updateSortingIcon(int sortSelection) { - AppCompatImageView sortButton = findViewById(R.id.sort_mode); - switch (sortSelection) { - case SORT_BY_TITLE: - sortButton.setImageResource(R.drawable.ic_alphabetical_asc); - break; - case SORT_BY_CREATED: - sortButton.setImageResource(R.drawable.ic_modification_asc); - break; - } - } - - public void onGridIconChosen(boolean gridEnabled) { - layoutManager.setSpanCount(gridEnabled ? 2 : 1); - updateGridIcon(gridEnabled); - - preferences.edit().putBoolean(getString(R.string.setting_grid_view_enabled), gridEnabled).apply(); - } - - public void updateGridIcon(boolean gridEnabled) { - AppCompatImageView viewButton = findViewById(R.id.view_mode); - viewButton.setImageResource(gridEnabled ? R.drawable.ic_view_list : R.drawable.ic_view_module); - } - - private void showGeofavoriteDeteleDialog(Geofavorite item) { + public void showGeofavoriteDeteleDialog(Geofavorite item) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage(getString(R.string.dialog_delete_message).replace("{name}", item.getName())) .setTitle(R.string.dialog_delete_title) @@ -351,10 +259,22 @@ public class MainActivity extends AppCompatActivity implements MainView, OnSorti ad.show(); } - private void showGeofavoriteDetailActivity(Geofavorite item) { + @Override + public void updateGeofavorites() { + presenter.getGeofavorites(); + } + + @Override + public void getGeofavorites() { + + } + + @Override + public void showGeofavoriteDetailActivity(Geofavorite item) { Intent i = new Intent(this, GeofavoriteDetailActivity.class); i.putExtra(GeofavoriteDetailActivity.ARG_GEOFAVORITE, item); startActivity(i); } + } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListConsumer.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListConsumer.java new file mode 100644 index 0000000..c9e29fd --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListConsumer.java @@ -0,0 +1,12 @@ +package it.danieleverducci.nextcloudmaps.fragments; + +import java.util.List; + +import it.danieleverducci.nextcloudmaps.model.Geofavorite; + +public interface GeofavoriteListConsumer { + void onGeofavoritesUpdated(List geofavorites); + void onGeofavoriteRemoved(Geofavorite geofavorite); + void onGeofavoriteAdded(Geofavorite geofavorite); + void onGeofavoriteLoading(boolean isLoading); +} diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListFragment.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListFragment.java new file mode 100644 index 0000000..a1c512b --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/GeofavoriteListFragment.java @@ -0,0 +1,152 @@ +package it.danieleverducci.nextcloudmaps.fragments; + +import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_CREATED; +import static it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter.SORT_BY_TITLE; + +import android.app.Activity; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.AppCompatImageView; +import androidx.fragment.app.Fragment; +import androidx.preference.PreferenceManager; +import androidx.recyclerview.widget.StaggeredGridLayoutManager; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import java.util.List; + +import it.danieleverducci.nextcloudmaps.R; +import it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteActivity; +import it.danieleverducci.nextcloudmaps.activity.main.GeofavoriteAdapter; +import it.danieleverducci.nextcloudmaps.activity.main.MainActivity; +import it.danieleverducci.nextcloudmaps.activity.main.SortingOrderDialogFragment; +import it.danieleverducci.nextcloudmaps.databinding.FragmentGeofavoriteListBinding; +import it.danieleverducci.nextcloudmaps.model.Geofavorite; + +/** + * Geofavorites list + */ +public class GeofavoriteListFragment extends Fragment implements GeofavoriteListConsumer, SortingOrderDialogFragment.OnSortingOrderListener { + private FragmentGeofavoriteListBinding binding; + private GeofavoriteAdapter geofavoriteAdapter; + private SharedPreferences preferences; + + public GeofavoriteListFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + if (binding == null) { + binding = FragmentGeofavoriteListBinding.inflate( + LayoutInflater.from(container.getContext()), + container, + false + ); + } + + preferences = PreferenceManager.getDefaultSharedPreferences(getContext()); + + StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); + binding.recyclerView.setLayoutManager(layoutManager); + + int sortRule = preferences.getInt(getString(R.string.setting_sort_by), SORT_BY_CREATED); + GeofavoriteAdapter.ItemClickListener rvItemClickListener = new GeofavoriteAdapter.ItemClickListener() { + @Override + public void onItemClick(Geofavorite geofavorite) { + Intent i = new Intent(); + i.setAction(Intent.ACTION_VIEW); + i.setData(geofavorite.getGeoUri()); + startActivity(i); + } + + @Override + public void onItemShareClick(Geofavorite item) { + Intent i = new Intent(); + i.setAction(Intent.ACTION_SEND); + i.setType("text/plain"); + String shareMessage = getString(R.string.share_message) + .replace("{lat}", ""+item.getLat()) + .replace("{lng}", ""+item.getLng()); + i.putExtra(Intent.EXTRA_TEXT, shareMessage ); + startActivity(Intent.createChooser(i, getString(R.string.share_via))); + } + + @Override + public void onItemDetailsClick(Geofavorite item) { + if (getActivity() instanceof GeofavoriteActivity) + ((GeofavoriteActivity)getActivity()).showGeofavoriteDetailActivity(item); + else + throw new IllegalStateException("Expected activity implementing GeofavoriteAcivity"); + } + + @Override + public void onItemDeleteClick(Geofavorite item) { + if (getActivity() instanceof GeofavoriteActivity) + ((GeofavoriteActivity)getActivity()).showGeofavoriteDeteleDialog(item); + else + throw new IllegalStateException("Expected activity implementing GeofavoriteAcivity"); + } + }; + geofavoriteAdapter = new GeofavoriteAdapter(getContext(), rvItemClickListener); + binding.recyclerView.setAdapter(geofavoriteAdapter); + geofavoriteAdapter.setSortRule(sortRule); + + binding.swipeRefresh.setOnRefreshListener(() -> + ((GeofavoriteActivity)getActivity()).updateGeofavorites()); + + return binding.root; + } + + + @Override + public void onGeofavoritesUpdated(List geofavorites) { + if (geofavoriteAdapter != null) + geofavoriteAdapter.setGeofavoriteList(geofavorites); + } + + @Override + public void onGeofavoriteRemoved(Geofavorite geofavorite) { + // Update list + geofavoriteAdapter.removeById(geofavorite.getId()); + } + + @Override + public void onGeofavoriteAdded(Geofavorite geofavorite) { + + } + + @Override + public void onGeofavoriteLoading(boolean isLoading) { + binding.swipeRefresh.setRefreshing(isLoading); + } + + + @Override + public void onSortingOrderChosen(int sortSelection) { + geofavoriteAdapter.setSortRule(sortSelection); + updateSortingIcon(sortSelection); + + preferences.edit().putInt(getString(R.string.setting_sort_by), sortSelection).apply(); + } + + public void updateSortingIcon(int sortSelection) { + // TODO: Gestire il bottone come bottone generico da aggiungere all'activity + AppCompatImageView sortButton = findViewById(R.id.sort_mode); + switch (sortSelection) { + case SORT_BY_TITLE: + sortButton.setImageResource(R.drawable.ic_alphabetical_asc); + break; + case SORT_BY_CREATED: + sortButton.setImageResource(R.drawable.ic_modification_asc); + break; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/MapFragment.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/MapFragment.java new file mode 100644 index 0000000..0c2615e --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/MapFragment.java @@ -0,0 +1,66 @@ +package it.danieleverducci.nextcloudmaps.fragments; + +import android.os.Bundle; + +import androidx.fragment.app.Fragment; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import it.danieleverducci.nextcloudmaps.R; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link MapFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class MapFragment extends Fragment { + + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + // TODO: Rename and change types of parameters + private String mParam1; + private String mParam2; + + public MapFragment() { + // Required empty public constructor + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param param1 Parameter 1. + * @param param2 Parameter 2. + * @return A new instance of fragment MapFragment. + */ + // TODO: Rename and change types and number of parameters + public static MapFragment newInstance(String param1, String param2) { + MapFragment fragment = new MapFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_map, container, false); + } +} \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/SortableListFragment.java b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/SortableListFragment.java new file mode 100644 index 0000000..bed3398 --- /dev/null +++ b/app/src/main/java/it/danieleverducci/nextcloudmaps/fragments/SortableListFragment.java @@ -0,0 +1,5 @@ +package it.danieleverducci.nextcloudmaps.fragments; + +public interface SortableListFragment { + +} diff --git a/app/src/main/res/drawable/ic_view_list.xml b/app/src/main/res/drawable/ic_view_list.xml deleted file mode 100644 index 4be12a9..0000000 --- a/app/src/main/res/drawable/ic_view_list.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - diff --git a/app/src/main/res/drawable/ic_view_module.xml b/app/src/main/res/drawable/ic_view_module.xml deleted file mode 100644 index 0a8190e..0000000 --- a/app/src/main/res/drawable/ic_view_module.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - diff --git a/app/src/main/res/layout/activity_list_view.xml b/app/src/main/res/layout/activity_list_view.xml deleted file mode 100644 index e71eea7..0000000 --- a/app/src/main/res/layout/activity_list_view.xml +++ /dev/null @@ -1,166 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 643f2b1..d22ac20 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -26,12 +26,126 @@ tools:openDrawer="start" tools:context=".activity.main.MainActivity"> - + android:layout_height="match_parent" + android:id="@+id/root"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_map.xml b/app/src/main/res/layout/fragment_map.xml new file mode 100644 index 0000000..844943f --- /dev/null +++ b/app/src/main/res/layout/fragment_map.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e324793..cc41d87 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,6 +81,5 @@ SETTING_SORT_BY - SETTING_GRID_VIEW_ENABLED