Added listener for favorite-on-click opening map
This commit is contained in:
parent
417debd21b
commit
81ea83a42f
|
|
@ -7,6 +7,8 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
|
@ -42,7 +44,7 @@ import retrofit2.Call;
|
||||||
import retrofit2.Callback;
|
import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity implements FavoriteFragment.OnFavoriteInteractionListener{
|
||||||
private static final String TAG = MainActivity.class.getSimpleName();
|
private static final String TAG = MainActivity.class.getSimpleName();
|
||||||
|
|
||||||
// UI Components
|
// UI Components
|
||||||
|
|
@ -410,4 +412,25 @@ public class MainActivity extends AppCompatActivity {
|
||||||
Log.d(TAG, "No roles found.");
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,14 +35,19 @@ import retrofit2.Callback;
|
||||||
import retrofit2.Response;
|
import retrofit2.Response;
|
||||||
|
|
||||||
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHolder> {
|
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHolder> {
|
||||||
|
public interface OnFavoriteClickListener {
|
||||||
|
void onFavoriteClick(Favorite favorite);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OnFavoriteClickListener clickListener;
|
||||||
public static final String TAG = FavoriteAdapter.class.getSimpleName();
|
public static final String TAG = FavoriteAdapter.class.getSimpleName();
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private List<Favorite> favoriteList;
|
private List<Favorite> favoriteList;
|
||||||
|
|
||||||
public FavoriteAdapter(Context context, List<Favorite> favoriteList) {
|
public FavoriteAdapter(Context context, List<Favorite> favoriteList, OnFavoriteClickListener clickListener) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.favoriteList = favoriteList;
|
this.favoriteList = favoriteList;
|
||||||
|
this.clickListener = clickListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
|
|
@ -76,7 +81,13 @@ public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHo
|
||||||
// handle favorite on click
|
// handle favorite on click
|
||||||
holder.ivFavorite.setOnClickListener(v -> removePlaceFromFavorites(SharedPref.getUserUuid(context), favorite.getPlaceUuid(), favorite.getUuid()));
|
holder.ivFavorite.setOnClickListener(v -> 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) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Error in Favorite Adapter", e);
|
Log.e(TAG, "Error in Favorite Adapter", e);
|
||||||
|
|
@ -147,7 +158,7 @@ public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHo
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getPlaceByUuid(String placeUuid) {
|
/*private void getPlaceByUuid(String placeUuid) {
|
||||||
DialogUtils.showLoadingDialog(context, context.getString(R.string.Please_wait));
|
DialogUtils.showLoadingDialog(context, context.getString(R.string.Please_wait));
|
||||||
|
|
||||||
String token = "Bearer " + SharedPref.getAccessToken(context);
|
String token = "Bearer " + SharedPref.getAccessToken(context);
|
||||||
|
|
@ -191,5 +202,5 @@ public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHo
|
||||||
Log.e(TAG, "Get Reports Failure: ", t);
|
Log.e(TAG, "Get Reports Failure: ", t);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ package com.example.acloc.fragments;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
@ -13,7 +15,7 @@ import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.LinearLayout;
|
||||||
|
|
||||||
import com.ieslamar.acloc.R;
|
import com.ieslamar.acloc.R;
|
||||||
import com.example.acloc.adapter.FavoriteAdapter;
|
import com.example.acloc.adapter.FavoriteAdapter;
|
||||||
|
|
@ -23,6 +25,7 @@ import com.example.acloc.service.FavoriteService;
|
||||||
import com.example.acloc.utility.DialogUtils;
|
import com.example.acloc.utility.DialogUtils;
|
||||||
import com.example.acloc.utility.Helper;
|
import com.example.acloc.utility.Helper;
|
||||||
import com.example.acloc.utility.SharedPref;
|
import com.example.acloc.utility.SharedPref;
|
||||||
|
import com.google.android.material.button.MaterialButton;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
|
@ -38,12 +41,21 @@ public class FavoriteFragment extends Fragment {
|
||||||
private View view;
|
private View view;
|
||||||
private FrameLayout rlFavorite;
|
private FrameLayout rlFavorite;
|
||||||
private RecyclerView rvFavorite;
|
private RecyclerView rvFavorite;
|
||||||
private TextView tvNoData;
|
private LinearLayout tvNoData;
|
||||||
|
private MaterialButton btnExplorePlaces;
|
||||||
private Context context;
|
private Context context;
|
||||||
private FavoriteAdapter adapter;
|
private FavoriteAdapter adapter;
|
||||||
private Favorite favoriteEntity;
|
private Favorite favoriteEntity;
|
||||||
private List<Favorite> favoriteList;
|
private List<Favorite> 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() {
|
public FavoriteFragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,6 +73,20 @@ public class FavoriteFragment extends Fragment {
|
||||||
public void onAttach(@NonNull Context context) {
|
public void onAttach(@NonNull Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
this.context = 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
|
@Override
|
||||||
|
|
@ -87,6 +113,14 @@ public class FavoriteFragment extends Fragment {
|
||||||
rlFavorite = view.findViewById(R.id.rlFavorite);
|
rlFavorite = view.findViewById(R.id.rlFavorite);
|
||||||
rvFavorite = view.findViewById(R.id.rvFavorite);
|
rvFavorite = view.findViewById(R.id.rvFavorite);
|
||||||
tvNoData = view.findViewById(R.id.tvNoData);
|
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() {
|
private void initObj() {
|
||||||
|
|
@ -95,7 +129,14 @@ public class FavoriteFragment extends Fragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initListener() {
|
private void initListener() {
|
||||||
|
// Call to action
|
||||||
|
if (btnExplorePlaces != null) {
|
||||||
|
btnExplorePlaces.setOnClickListener(v -> {
|
||||||
|
if (mListener != null) {
|
||||||
|
mListener.onNavigateToMap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadData() {
|
private void loadData() {
|
||||||
|
|
@ -111,14 +152,14 @@ public class FavoriteFragment extends Fragment {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
private void setUpRecyclerView() {
|
private void setUpRecyclerView() {
|
||||||
try {
|
try {
|
||||||
if (adapter != null) {
|
if (adapter != null) {
|
||||||
adapter.updateFavoriteList(favoriteList);
|
adapter.updateFavoriteList(favoriteList);
|
||||||
} else {
|
} else {
|
||||||
adapter = new FavoriteAdapter(context, favoriteList);
|
// Pass the click listener to the adapter
|
||||||
|
adapter = new FavoriteAdapter(context, favoriteList, this::onFavoriteItemClick);
|
||||||
rvFavorite.setAdapter(adapter);
|
rvFavorite.setAdapter(adapter);
|
||||||
rvFavorite.setLayoutManager(Helper.getVerticalManager(context));
|
rvFavorite.setLayoutManager(Helper.getVerticalManager(context));
|
||||||
adapter.notifyDataSetChanged();
|
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) {
|
private void getFavoriteByUserUuid(String userUuid) {
|
||||||
DialogUtils.showLoadingDialog(context, context.getString(R.string.Loading_Favorites));
|
DialogUtils.showLoadingDialog(context, context.getString(R.string.Loading_Favorites));
|
||||||
|
|
||||||
|
|
@ -192,4 +253,4 @@ public class FavoriteFragment extends Fragment {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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.LatLng;
|
||||||
import com.google.android.gms.maps.model.Marker;
|
import com.google.android.gms.maps.model.Marker;
|
||||||
import com.google.android.gms.maps.model.MarkerOptions;
|
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.google.android.material.textfield.TextInputEditText;
|
||||||
import com.ieslamar.acloc.R;
|
import com.ieslamar.acloc.R;
|
||||||
|
|
||||||
|
|
@ -84,6 +86,7 @@ public class MapFragment extends Fragment {
|
||||||
private List<String> suggestionsList = new ArrayList<>();
|
private List<String> suggestionsList = new ArrayList<>();
|
||||||
private ArrayAdapter<String> suggestionsAdapter;
|
private ArrayAdapter<String> suggestionsAdapter;
|
||||||
private List<Address> addressResults = new ArrayList<>();
|
private List<Address> addressResults = new ArrayList<>();
|
||||||
|
private FloatingActionButton fabCurrentLocation;
|
||||||
|
|
||||||
private List<Place> placeList = new ArrayList<>();
|
private List<Place> placeList = new ArrayList<>();
|
||||||
|
|
||||||
|
|
@ -126,6 +129,7 @@ public class MapFragment extends Fragment {
|
||||||
lvSuggestions.setBackgroundColor(getResources().getColor(android.R.color.white));
|
lvSuggestions.setBackgroundColor(getResources().getColor(android.R.color.white));
|
||||||
rlMap.addView(lvSuggestions);
|
rlMap.addView(lvSuggestions);
|
||||||
}
|
}
|
||||||
|
fabCurrentLocation = view.findViewById(R.id.fabCurrentLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initObj() {
|
private void initObj() {
|
||||||
|
|
@ -258,6 +262,21 @@ public class MapFragment extends Fragment {
|
||||||
}
|
}
|
||||||
return true;
|
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) {
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue