Inject MapFragment

pull/795/head
Alex Baker 6 years ago
parent a4796d9952
commit 865755f521

@ -2,6 +2,7 @@ package org.tasks.location;
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.fragment.app.FragmentManager;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
@ -21,18 +22,28 @@ import org.tasks.data.Place;
public class GoogleMapFragment implements MapFragment, OnMapReadyCallback, OnMarkerClickListener {
private static final String FRAG_TAG_MAP = "frag_tag_map";
private final Context context;
private final MapFragmentCallback callbacks;
private final boolean dark;
private final List<Marker> markers = new ArrayList<>();
private MapFragmentCallback callbacks;
private boolean dark;
private GoogleMap map;
GoogleMapFragment(
Context context, SupportMapFragment fragment, MapFragmentCallback callbacks, boolean dark) {
public GoogleMapFragment(Context context) {
this.context = context;
}
@Override
public void init(FragmentManager fragmentManager, MapFragmentCallback callbacks, boolean dark) {
this.callbacks = callbacks;
this.dark = dark;
fragment.getMapAsync(this);
SupportMapFragment mapFragment =
(SupportMapFragment) fragmentManager.findFragmentByTag(FRAG_TAG_MAP);
if (mapFragment == null) {
mapFragment = new SupportMapFragment();
fragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
}
@Override

@ -28,7 +28,6 @@ import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.widget.Toolbar.OnMenuItemClickListener;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -37,11 +36,9 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.AppBarLayout.Behavior;
import com.google.common.base.Strings;
import com.mapbox.mapboxsdk.Mapbox;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
@ -69,7 +66,6 @@ import org.tasks.location.MapFragment.MapFragmentCallback;
import org.tasks.preferences.ActivityPermissionRequestor;
import org.tasks.preferences.PermissionChecker;
import org.tasks.preferences.PermissionRequestor;
import org.tasks.preferences.Preferences;
import org.tasks.themes.Theme;
import org.tasks.themes.ThemeColor;
import org.tasks.ui.MenuColorizer;
@ -86,7 +82,6 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
private static final String EXTRA_MAP_POSITION = "extra_map_position";
private static final String EXTRA_APPBAR_OFFSET = "extra_appbar_offset";
private static final String FRAG_TAG_MAP = "frag_tag_map";
private static final Pattern pattern = Pattern.compile("(\\d+):(\\d+):(\\d+\\.\\d+)");
private static final int SEARCH_DEBOUNCE_TIMEOUT = 300;
@ -116,14 +111,13 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
@Inject Toaster toaster;
@Inject Inventory inventory;
@Inject PlayServices playServices;
@Inject Preferences preferences;
@Inject LocationDao locationDao;
@Inject PlaceSearchProvider searchProvider;
@Inject PermissionChecker permissionChecker;
@Inject ActivityPermissionRequestor permissionRequestor;
@Inject DialogBuilder dialogBuilder;
@Inject MapFragment map;
private MapFragment map;
private FusedLocationProviderClient fusedLocationProviderClient;
private CompositeDisposable disposables;
private MapPosition mapPosition;
@ -200,11 +194,7 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
themeColor.applyToStatusBarIcons(this);
themeColor.applyToNavigationBar(this);
if (preferences.useGoogleMaps()) {
initGoogleMaps();
} else {
initMapboxMaps();
}
map.init(getSupportFragmentManager(), this, theme.getThemeBase().isDarkTheme(this));
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
@ -249,31 +239,6 @@ public class LocationPickerActivity extends InjectingAppCompatActivity
recyclerView.setAdapter(search.isActionViewExpanded() ? searchAdapter : recentsAdapter);
}
private void initGoogleMaps() {
FragmentManager supportFragmentManager = getSupportFragmentManager();
SupportMapFragment mapFragment =
(SupportMapFragment) supportFragmentManager.findFragmentByTag(FRAG_TAG_MAP);
if (mapFragment == null) {
mapFragment = new SupportMapFragment();
supportFragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit();
}
new GoogleMapFragment(context, mapFragment, this, theme.getThemeBase().isDarkTheme(this));
}
private void initMapboxMaps() {
Mapbox.getInstance(this, getString(R.string.mapbox_key));
FragmentManager supportFragmentManager = getSupportFragmentManager();
com.mapbox.mapboxsdk.maps.SupportMapFragment mapFragment =
(com.mapbox.mapboxsdk.maps.SupportMapFragment)
supportFragmentManager.findFragmentByTag(FRAG_TAG_MAP);
if (mapFragment == null) {
mapFragment = new com.mapbox.mapboxsdk.maps.SupportMapFragment();
supportFragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit();
}
new MapboxMapFragment(context, mapFragment, this, theme.getThemeBase().isDarkTheme(this));
}
@Override
public void onMapReady(MapFragment mapFragment) {
map = mapFragment;

@ -3,6 +3,8 @@ package org.tasks.location;
import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
@ -16,26 +18,38 @@ import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.MapboxMap.OnMarkerClickListener;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.tasks.R;
import org.tasks.data.Place;
public class MapboxMapFragment implements MapFragment, OnMapReadyCallback, OnMarkerClickListener {
private static final String FRAG_TAG_MAP = "frag_tag_map";
private final Context context;
private final MapFragmentCallback callbacks;
private final boolean dark;
private MapFragmentCallback callbacks;
private boolean dark;
private MapboxMap map;
private Map<Marker, Place> markers = new HashMap<>();
MapboxMapFragment(
Context context, SupportMapFragment fragment, MapFragmentCallback callbacks, boolean dark) {
public MapboxMapFragment(Context context) {
this.context = context;
}
@Override
public void init(FragmentManager fragmentManager, MapFragmentCallback callbacks, boolean dark) {
this.callbacks = callbacks;
this.dark = dark;
fragment.getMapAsync(this);
Mapbox.getInstance(context, context.getString(R.string.mapbox_key));
com.mapbox.mapboxsdk.maps.SupportMapFragment mapFragment =
(com.mapbox.mapboxsdk.maps.SupportMapFragment)
fragmentManager.findFragmentByTag(FRAG_TAG_MAP);
if (mapFragment == null) {
mapFragment = new com.mapbox.mapboxsdk.maps.SupportMapFragment();
fragmentManager.beginTransaction().replace(R.id.map, mapFragment).commit();
}
mapFragment.getMapAsync(this);
}
@Override
@ -63,6 +77,9 @@ public class MapboxMapFragment implements MapFragment, OnMapReadyCallback, OnMar
@Override
public void setMarkers(List<Place> places) {
if (map == null) {
return;
}
for (Marker marker : map.getMarkers()) {
map.removeMarker(marker);
}

@ -8,7 +8,10 @@ import org.tasks.R;
import org.tasks.billing.Inventory;
import org.tasks.fragments.TaskEditControlSetFragmentManager;
import org.tasks.gtasks.PlayServices;
import org.tasks.location.GoogleMapFragment;
import org.tasks.location.GooglePlacesSearchProvider;
import org.tasks.location.MapFragment;
import org.tasks.location.MapboxMapFragment;
import org.tasks.location.MapboxSearchProvider;
import org.tasks.location.PlaceSearchProvider;
import org.tasks.preferences.Preferences;
@ -73,4 +76,12 @@ public class ActivityModule {
? new GooglePlacesSearchProvider(activity)
: new MapboxSearchProvider(activity);
}
@Provides
@ActivityScope
public MapFragment getMapFragment(Preferences preferences) {
return preferences.useGoogleMaps()
? new GoogleMapFragment(activity)
: new MapboxMapFragment(activity);
}
}

@ -1,10 +1,13 @@
package org.tasks.location;
import androidx.fragment.app.FragmentManager;
import java.util.List;
import org.tasks.data.Place;
public interface MapFragment {
void init(FragmentManager fragmentManager, MapFragmentCallback callback, boolean dark);
MapPosition getMapPosition();
void movePosition(MapPosition mapPosition, boolean animate);

Loading…
Cancel
Save