diff --git a/app/src/main/java/com/aldo/apps/familyhelpers/ShareLocationActivity.java b/app/src/main/java/com/aldo/apps/familyhelpers/ShareLocationActivity.java index 91aae59..5503a33 100644 --- a/app/src/main/java/com/aldo/apps/familyhelpers/ShareLocationActivity.java +++ b/app/src/main/java/com/aldo/apps/familyhelpers/ShareLocationActivity.java @@ -35,6 +35,7 @@ import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MapStyleOptions; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.materialswitch.MaterialSwitch; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; @@ -144,11 +145,23 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea */ private RecyclerView mActiveShares; + /** + * The {@link FloatingActionButton} to recenter the camera on the followed location. + */ + private FloatingActionButton mRecenterFollowBtn; + + private LocationObject mCurrentlyFollowingLocation; + /** * The {@link ActiveShareAdapter} to populate the activeShare view. */ private ActiveShareAdapter mActiveShareAdapter; + /** + * Flag indicating whether currently the user wants to be following another user or not. + */ + private boolean mIsFollowing; + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -165,10 +178,12 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea mInfoBoxTimeStamp = findViewById(R.id.share_location_info_timestamp); mNoActiveShares = findViewById(R.id.tv_no_active_shares); mActiveShares = findViewById(R.id.active_share_layout); + mRecenterFollowBtn = findViewById(R.id.fab_recenter_on_follower); mActiveShares.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); mActiveShareAdapter = new ActiveShareAdapter(this); + mRecenterFollowBtn.setOnClickListener(v -> startFollowingAndCenterCamera()); mActiveShares.setAdapter(mActiveShareAdapter); mapFragment.getMapAsync(this); mDbHelper = new DatabaseHelper(); @@ -270,6 +285,11 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea updateExistingOrCreateValidMarkers(locationObjectMap); } + /** + * Helper method to update the active share fragment with all available sharing users. + * + * @param locationObjectMap The {@link Map} of ids and {@link LocationObject}s. + */ private void updateActiveShares(final Map locationObjectMap) { final List activeShares = new ArrayList<>(); for (final String shareId : locationObjectMap.keySet()) { @@ -298,6 +318,8 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea if (TextUtils.equals(removeKey, mCurrentlyFollowing)) { clearInfoBox(); mCurrentlyFollowing = null; + mIsFollowing = false; + mCurrentlyFollowingLocation = null; } mMarkerMap.remove(removeKey); } @@ -315,14 +337,6 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea final LatLng receivedLocation = new LatLng(newReceived.getLatitude(), newReceived.getLongitude()); final String sharingId = locationSet.getKey(); final User locationUser = mDbHelper.getUserForId(sharingId); - - if (TextUtils.equals(mCurrentlyFollowing, sharingId)) { - final LocationObject locationObject = locationSet.getValue(); - updateInfoBox(locationObject); - mGmap.animateCamera(CameraUpdateFactory.newLatLngZoom( - new LatLng(locationObject.getLatitude(), locationObject.getLongitude()), - 15.0f)); - } if (mMarkerMap.containsKey(sharingId)) { final Marker existing = mMarkerMap.get(sharingId); if (existing == null) { @@ -339,6 +353,14 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea .title(locationUser == null ? "" : locationUser.getDisplayName())); mMarkerMap.put(locationSet.getKey(), marker); } + if (TextUtils.equals(mCurrentlyFollowing, sharingId)) { + final LocationObject locationObject = locationSet.getValue(); + mCurrentlyFollowingLocation = locationObject; + updateInfoBox(locationObject); + if (mIsFollowing) { + startFollowingAndCenterCamera(); + } + } } } @@ -422,6 +444,7 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea private void handleCurrentlyFollowingChanged(final String shareId) { Log.d(TAG, "onCreate: Currently selected = [" + shareId + "]"); mCurrentlyFollowing = shareId; + mIsFollowing = !TextUtils.isEmpty(mCurrentlyFollowing); // Re-setup the subscription. if (mLocationUpdateSubscription != null) { mLocationUpdateSubscription.dispose(); @@ -439,6 +462,18 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea Log.e(TAG, "handleUserSubscriptionFailed: Subscription to all users failed, keep cached state"); } + /** + * Helper method to start following and recenter the camera on the location followed. + */ + private void startFollowingAndCenterCamera() { + mIsFollowing = true; + mGmap.animateCamera(CameraUpdateFactory.newLatLngZoom( + new LatLng(mCurrentlyFollowingLocation.getLatitude(), + mCurrentlyFollowingLocation.getLongitude()), + 15.0f)); + mRecenterFollowBtn.setVisibility(View.GONE); + } + @Override public void onMapReady(@NonNull final GoogleMap googleMap) { mGmap = googleMap; @@ -456,6 +491,14 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea } else { mGmapsMarker.setPosition(defaultHome); } + mGmap.setOnCameraMoveStartedListener(reason -> { + if (GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == reason + && !TextUtils.isEmpty(mCurrentlyFollowing)) { + Log.d(TAG, "onCameraMoveStarted: User moved map, set following to false."); + mIsFollowing = false; + mRecenterFollowBtn.setVisibility(View.VISIBLE); + } + }); mGmap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultHome, 8.0f)); } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_share_location.xml b/app/src/main/res/layout/activity_share_location.xml index a0d44ca..e78419e 100644 --- a/app/src/main/res/layout/activity_share_location.xml +++ b/app/src/main/res/layout/activity_share_location.xml @@ -31,6 +31,17 @@ + +