diff --git a/app/src/main/java/fr/free/nrw/commons/di/FragmentBuilderModule.java b/app/src/main/java/fr/free/nrw/commons/di/FragmentBuilderModule.java index a61967336..a146fb3d5 100644 --- a/app/src/main/java/fr/free/nrw/commons/di/FragmentBuilderModule.java +++ b/app/src/main/java/fr/free/nrw/commons/di/FragmentBuilderModule.java @@ -14,6 +14,7 @@ import fr.free.nrw.commons.explore.recentsearches.RecentSearchesFragment; import fr.free.nrw.commons.media.MediaDetailFragment; import fr.free.nrw.commons.media.MediaDetailPagerFragment; import fr.free.nrw.commons.nearby.NearbyListFragment; +import fr.free.nrw.commons.nearby.SupportMapFragment; import fr.free.nrw.commons.nearby.mvp.fragments.NearbyParentFragment; import fr.free.nrw.commons.review.ReviewImageFragment; import fr.free.nrw.commons.settings.SettingsFragment; @@ -58,6 +59,9 @@ public abstract class FragmentBuilderModule { @ContributesAndroidInjector abstract ContributionsFragment bindContributionsFragment(); + @ContributesAndroidInjector + abstract SupportMapFragment bindSupportMapFragment(); + @ContributesAndroidInjector abstract NearbyParentFragment bindNearbyParentFragment(); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java index fe1d8a493..b9cb3b533 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java @@ -18,7 +18,6 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.maps.SupportMapFragment; import javax.inject.Inject; @@ -85,6 +84,8 @@ public class NearbyTestLayersFragment extends Fragment implements NearbyParentFr mapboxMap.setStyle(Style.SATELLITE, new Style.OnStyleLoaded() { @Override public void onStyleLoaded(@NonNull Style style) { + //NearbyTestLayersFragment.this.childMapFragmentAttached(); + Log.d("NearbyTests","Fragment inside fragment with map works"); // Map is set up and the style has loaded. Now you can add data or make other map adjustments @@ -100,6 +101,7 @@ public class NearbyTestLayersFragment extends Fragment implements NearbyParentFr * it is attached. */ public void childMapFragmentAttached() { + Log.d("denemeTest","this:"+this); nearbyParentFragmentPresenter = new NearbyParentFragmentPresenter (this, null, locationManager); Timber.d("Child fragment attached"); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java new file mode 100644 index 000000000..75019fd97 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java @@ -0,0 +1,228 @@ +package fr.free.nrw.commons.nearby; + +import android.content.Context; +import android.os.Bundle; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.mapbox.mapboxsdk.maps.MapFragment; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.MapboxMapOptions; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.utils.MapFragmentUtils; + +import java.util.ArrayList; +import java.util.List; + +import dagger.android.support.DaggerFragment; +import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; + +/** + * Support Fragment wrapper around a map view. + *

+ * A Map component in an app. This fragment is the simplest way to place a map in an application. + * It's a wrapper around a view of a map to automatically handle the necessary life cycle needs. + * Being a fragment, this component can be added to an activity's layout or can dynamically be added + * using a FragmentManager. + *

+ *

+ * To get a reference to the MapView, use {@link #getMapAsync(OnMapReadyCallback)}} + *

+ * + * @see #getMapAsync(OnMapReadyCallback) + */ +public class SupportMapFragment extends CommonsDaggerSupportFragment implements OnMapReadyCallback { + + private final List mapReadyCallbackList = new ArrayList<>(); + private MapFragment.OnMapViewReadyCallback mapViewReadyCallback; + private MapboxMap mapboxMap; + private MapView map; + + /** + * Creates a default MapFragment instance + * + * @return MapFragment created + */ + public static SupportMapFragment newInstance() { + return new SupportMapFragment(); + } + + /** + * Creates a MapFragment instance + * + * @param mapboxMapOptions The configuration options to be used. + * @return MapFragment created. + */ + @NonNull + public static SupportMapFragment newInstance(@Nullable MapboxMapOptions mapboxMapOptions) { + SupportMapFragment mapFragment = new SupportMapFragment(); + mapFragment.setArguments(MapFragmentUtils.createFragmentArgs(mapboxMapOptions)); + return mapFragment; + } + + /** + * Called when the context attaches to this fragment. + * + * @param context the context attaching + */ + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof MapFragment.OnMapViewReadyCallback) { + mapViewReadyCallback = (MapFragment.OnMapViewReadyCallback) context; + } + } + + /** + * Called when this fragment is inflated, parses XML tag attributes. + * + * @param context The context inflating this fragment. + * @param attrs The XML tag attributes. + * @param savedInstanceState The saved instance state for the map fragment. + */ + @Override + public void onInflate(@NonNull Context context, AttributeSet attrs, Bundle savedInstanceState) { + super.onInflate(context, attrs, savedInstanceState); + setArguments(MapFragmentUtils.createFragmentArgs(MapboxMapOptions.createFromAttributes(context, attrs))); + } + + /** + * Creates the fragment view hierarchy. + * + * @param inflater Inflater used to inflate content. + * @param container The parent layout for the map fragment. + * @param savedInstanceState The saved instance state for the map fragment. + * @return The view created + */ + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + super.onCreateView(inflater, container, savedInstanceState); + Context context = inflater.getContext(); + map = new MapView(context, MapFragmentUtils.resolveArgs(context, getArguments())); + return map; + } + + /** + * Called when the fragment view hierarchy is created. + * + * @param view The content view of the fragment + * @param savedInstanceState THe saved instance state of the framgnt + */ + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + map.onCreate(savedInstanceState); + map.getMapAsync(this); + + // notify listeners about MapView creation + if (mapViewReadyCallback != null) { + mapViewReadyCallback.onMapViewReady(map); + } + } + + @Override + public void onMapReady(@NonNull MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + for (OnMapReadyCallback onMapReadyCallback : mapReadyCallbackList) { + onMapReadyCallback.onMapReady(mapboxMap); + } + } + + /** + * Called when the fragment is visible for the users. + */ + @Override + public void onStart() { + super.onStart(); + map.onStart(); + } + + /** + * Called when the fragment is ready to be interacted with. + */ + @Override + public void onResume() { + super.onResume(); + map.onResume(); + } + + /** + * Called when the fragment is pausing. + */ + @Override + public void onPause() { + super.onPause(); + map.onPause(); + } + + /** + * Called when the fragment state needs to be saved. + * + * @param outState The saved state + */ + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + if (map != null) { + map.onSaveInstanceState(outState); + } + } + + /** + * Called when the fragment is no longer visible for the user. + */ + @Override + public void onStop() { + super.onStop(); + map.onStop(); + } + + /** + * Called when the fragment receives onLowMemory call from the hosting Activity. + */ + @Override + public void onLowMemory() { + super.onLowMemory(); + if (map != null) { + map.onLowMemory(); + } + } + + /** + * Called when the fragment is view hierarchy is being destroyed. + */ + @Override + public void onDestroyView() { + super.onDestroyView(); + map.onDestroy(); + } + + /** + * Called when the fragment is destroyed. + */ + @Override + public void onDestroy() { + super.onDestroy(); + mapReadyCallbackList.clear(); + } + + /** + * Sets a callback object which will be triggered when the MapboxMap instance is ready to be used. + * + * @param onMapReadyCallback The callback to be invoked. + */ + public void getMapAsync(@NonNull final OnMapReadyCallback onMapReadyCallback) { + if (mapboxMap == null) { + mapReadyCallbackList.add(onMapReadyCallback); + } else { + onMapReadyCallback.onMapReady(mapboxMap); + } + } +} +