From 81ea83a42f09eaa9c44e925edaf3e32f9c27b95a Mon Sep 17 00:00:00 2001 From: Pau Date: Thu, 29 May 2025 03:35:54 +0200 Subject: [PATCH] Added listener for favorite-on-click opening map --- .../java/com/example/acloc/MainActivity.java | 25 ++++++- .../acloc/adapter/FavoriteAdapter.java | 19 ++++- .../acloc/fragments/FavoriteFragment.java | 73 +++++++++++++++++-- .../example/acloc/fragments/MapFragment.java | 38 ++++++++++ 4 files changed, 144 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/example/acloc/MainActivity.java b/app/src/main/java/com/example/acloc/MainActivity.java index 754b92d..0d5e3cd 100644 --- a/app/src/main/java/com/example/acloc/MainActivity.java +++ b/app/src/main/java/com/example/acloc/MainActivity.java @@ -7,6 +7,8 @@ import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -42,7 +44,7 @@ import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity implements FavoriteFragment.OnFavoriteInteractionListener{ private static final String TAG = MainActivity.class.getSimpleName(); // UI Components @@ -410,4 +412,25 @@ public class MainActivity extends AppCompatActivity { Log.d(TAG, "No roles found."); } } + + @Override + public void onNavigateToMap() { + bottomNavigationView.setSelectedItemId(R.id.menu_map); + } + + @Override + public void onNavigateToPlace(double latitude, double longitude, String placeName, String placeUuid) { + // navigate to map + bottomNavigationView.setSelectedItemId(R.id.menu_map); + + // delay + new Handler(Looper.getMainLooper()).postDelayed(() -> { + // Obtain current instance of MapFragment + Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.flMainContainer); + if (currentFragment instanceof MapFragment) { + MapFragment mapFragment = (MapFragment) currentFragment; + mapFragment.navigateToPlace(latitude, longitude, placeName, placeUuid); + } + }, 300); + } } diff --git a/app/src/main/java/com/example/acloc/adapter/FavoriteAdapter.java b/app/src/main/java/com/example/acloc/adapter/FavoriteAdapter.java index 112ceaa..0f35503 100644 --- a/app/src/main/java/com/example/acloc/adapter/FavoriteAdapter.java +++ b/app/src/main/java/com/example/acloc/adapter/FavoriteAdapter.java @@ -35,14 +35,19 @@ import retrofit2.Callback; import retrofit2.Response; public class FavoriteAdapter extends RecyclerView.Adapter { + public interface OnFavoriteClickListener { + void onFavoriteClick(Favorite favorite); + } + private OnFavoriteClickListener clickListener; public static final String TAG = FavoriteAdapter.class.getSimpleName(); private final Context context; private List favoriteList; - public FavoriteAdapter(Context context, List favoriteList) { + public FavoriteAdapter(Context context, List favoriteList, OnFavoriteClickListener clickListener) { this.context = context; this.favoriteList = favoriteList; + this.clickListener = clickListener; } @SuppressLint("NotifyDataSetChanged") @@ -76,7 +81,13 @@ public class FavoriteAdapter extends RecyclerView.Adapter removePlaceFromFavorites(SharedPref.getUserUuid(context), favorite.getPlaceUuid(), favorite.getUuid())); - holder.itemView.setOnClickListener(v -> getPlaceByUuid(favorite.getPlaceUuid())); + //holder.itemView.setOnClickListener(v -> getPlaceByUuid(favorite.getPlaceUuid())); + + holder.itemView.setOnClickListener(v -> { + if (clickListener != null) { + clickListener.onFavoriteClick(favorite); + } + }); } } catch (Exception e) { Log.e(TAG, "Error in Favorite Adapter", e); @@ -147,7 +158,7 @@ public class FavoriteAdapter extends RecyclerView.Adapter favoriteList; + // Interface for communication with MainActivity + public interface OnFavoriteInteractionListener { + void onNavigateToMap(); + void onNavigateToPlace(double latitude, double longitude, String placeName, String placeUuid); + } + + private OnFavoriteInteractionListener mListener; + public FavoriteFragment() { } @@ -61,6 +73,20 @@ public class FavoriteFragment extends Fragment { public void onAttach(@NonNull Context context) { super.onAttach(context); this.context = context; + + // Set up the listener for communication with MainActivity + if (context instanceof OnFavoriteInteractionListener) { + mListener = (OnFavoriteInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFavoriteInteractionListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; } @Override @@ -87,6 +113,14 @@ public class FavoriteFragment extends Fragment { rlFavorite = view.findViewById(R.id.rlFavorite); rvFavorite = view.findViewById(R.id.rvFavorite); tvNoData = view.findViewById(R.id.tvNoData); + + // Search button inside Linear Layout + if (tvNoData != null) { + btnExplorePlaces = tvNoData.findViewById(R.id.btnExplorePlaces); + if (btnExplorePlaces == null) { + Log.w(TAG, "Explore Places button not found in empty state layout"); + } + } } private void initObj() { @@ -95,7 +129,14 @@ public class FavoriteFragment extends Fragment { } private void initListener() { - + // Call to action + if (btnExplorePlaces != null) { + btnExplorePlaces.setOnClickListener(v -> { + if (mListener != null) { + mListener.onNavigateToMap(); + } + }); + } } private void loadData() { @@ -111,14 +152,14 @@ public class FavoriteFragment extends Fragment { } } - @SuppressLint("NotifyDataSetChanged") private void setUpRecyclerView() { try { if (adapter != null) { adapter.updateFavoriteList(favoriteList); } else { - adapter = new FavoriteAdapter(context, favoriteList); + // Pass the click listener to the adapter + adapter = new FavoriteAdapter(context, favoriteList, this::onFavoriteItemClick); rvFavorite.setAdapter(adapter); rvFavorite.setLayoutManager(Helper.getVerticalManager(context)); adapter.notifyDataSetChanged(); @@ -131,6 +172,26 @@ public class FavoriteFragment extends Fragment { } } + // Handle favorite item click + private void onFavoriteItemClick(Favorite favorite) { + if (mListener != null) { + try { + double latitude = Double.parseDouble(favorite.getPlaceLat()); + double longitude = Double.parseDouble(favorite.getPlaceLng()); + + mListener.onNavigateToPlace( + latitude, + longitude, + favorite.getPlaceName(), + favorite.getPlaceUuid() + ); + } catch (NumberFormatException e) { + Log.e(TAG, "Error parsing coordinates", e); + Helper.showToast(context, "Error loading place location"); + } + } + } + private void getFavoriteByUserUuid(String userUuid) { DialogUtils.showLoadingDialog(context, context.getString(R.string.Loading_Favorites)); @@ -192,4 +253,4 @@ public class FavoriteFragment extends Fragment { } }); } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/example/acloc/fragments/MapFragment.java b/app/src/main/java/com/example/acloc/fragments/MapFragment.java index fa2cb89..392459b 100644 --- a/app/src/main/java/com/example/acloc/fragments/MapFragment.java +++ b/app/src/main/java/com/example/acloc/fragments/MapFragment.java @@ -48,6 +48,8 @@ import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; +import com.google.android.material.card.MaterialCardView; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.textfield.TextInputEditText; import com.ieslamar.acloc.R; @@ -84,6 +86,7 @@ public class MapFragment extends Fragment { private List suggestionsList = new ArrayList<>(); private ArrayAdapter suggestionsAdapter; private List
addressResults = new ArrayList<>(); + private FloatingActionButton fabCurrentLocation; private List placeList = new ArrayList<>(); @@ -126,6 +129,7 @@ public class MapFragment extends Fragment { lvSuggestions.setBackgroundColor(getResources().getColor(android.R.color.white)); rlMap.addView(lvSuggestions); } + fabCurrentLocation = view.findViewById(R.id.fabCurrentLocation); } private void initObj() { @@ -258,6 +262,21 @@ public class MapFragment extends Fragment { } return true; }); + + if (fabCurrentLocation != null) { + fabCurrentLocation.setOnClickListener(v -> { + if (googleMap != null && ContextCompat.checkSelfPermission(requireContext(), + android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + + fusedLocationClient.getLastLocation().addOnSuccessListener(location -> { + if (location != null) { + LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude()); + googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, DEFAULT_ZOOM)); + } + }); + } + }); + } } private void getSuggestions(String query) { @@ -557,4 +576,23 @@ public class MapFragment extends Fragment { } }); } + + public void navigateToPlace(double latitude, double longitude, String placeName, String placeUuid) { + if (googleMap != null) { + LatLng latLng = new LatLng(latitude, longitude); + + // Animate camera to place + googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, SEARCH_ZOOM)); + + // Search place and show bottom sheet dialog + Place targetPlace = placeList.stream().filter(place -> place.getUuid() != null && place.getUuid().equals(placeUuid)).findFirst().orElse(null); + + if (targetPlace != null) { + // Show botom sheet after delay + new Handler(Looper.getMainLooper()).postDelayed(() -> { + showPlaceBottomSheet(targetPlace); + }, 1000); + } + } + } }