diff --git a/android/src/com/mapswithme/maps/MwmActivity.java b/android/src/com/mapswithme/maps/MwmActivity.java index 3f844a6076..167c7d039f 100644 --- a/android/src/com/mapswithme/maps/MwmActivity.java +++ b/android/src/com/mapswithme/maps/MwmActivity.java @@ -30,6 +30,7 @@ import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; +import androidx.recyclerview.widget.RecyclerView; import com.mapswithme.maps.Framework.PlacePageActivationListener; import com.mapswithme.maps.activity.CustomNavigateUpListener; import com.mapswithme.maps.ads.LikesManager; @@ -71,6 +72,9 @@ import com.mapswithme.maps.maplayer.MapLayerCompositeController; import com.mapswithme.maps.maplayer.Mode; import com.mapswithme.maps.maplayer.OnGuidesLayerToggleListener; import com.mapswithme.maps.maplayer.OnIsolinesLayerToggleListener; +import com.mapswithme.maps.maplayer.ToggleMapLayerDialog; +import com.mapswithme.maps.maplayer.guides.GuidesManager; +import com.mapswithme.maps.maplayer.guides.GuidesState; import com.mapswithme.maps.maplayer.isolines.IsolinesManager; import com.mapswithme.maps.maplayer.isolines.IsolinesState; import com.mapswithme.maps.maplayer.subway.OnSubwayLayerToggleListener; @@ -156,6 +160,7 @@ import com.mapswithme.util.statistics.Statistics; import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt; import java.util.List; +import java.util.Objects; import java.util.Stack; public class MwmActivity extends BaseMwmFragmentActivity @@ -212,7 +217,10 @@ public class MwmActivity extends BaseMwmFragmentActivity public static final int REQ_CODE_DRIVING_OPTIONS = 6; public static final int REQ_CODE_CATALOG_UNLIMITED_ACCESS = 7; private static final int REQ_CODE_ISOLINES_ERROR = 8; + private static final int REQ_CODE_GUIDES_FATAL_ERROR = 9; + public static final String ERROR_DRIVING_OPTIONS_DIALOG_TAG = "error_driving_options_dialog_tag"; + public static final String GUIDES_FATAL_ERROR_DIALOG_TAG = "guides_fatal_error_dialog_tag"; public static final String CATALOG_UNLIMITED_ACCESS_DIALOG_TAG = "catalog_unlimited_access_dialog_tag"; private static final String ISOLINES_ERROR_DIALOG_TAG = "isolines_dialog_tag"; @@ -1448,6 +1456,7 @@ public class MwmActivity extends BaseMwmFragmentActivity BookmarkManager.INSTANCE.addCatalogListener(this); RoutingController.get().attach(this); IsolinesManager.from(getApplicationContext()).attach(this::onIsolinesStateChanged); + GuidesManager.from(getApplicationContext()).attach(this::onGuidesStateChanged); if (MapFragment.nativeIsEngineCreated()) LocationHelper.INSTANCE.attach(this); mPlacePageController.onActivityStarted(this); @@ -1466,6 +1475,42 @@ public class MwmActivity extends BaseMwmFragmentActivity mPlacePageController.onActivityStopped(this); MwmApplication.backgroundTracker(getActivity()).removeListener(this); IsolinesManager.from(getApplicationContext()).detach(); + GuidesManager.from(getApplicationContext()).detach(); + } + + private void onGuidesStateChanged(@NonNull GuidesState state) + { + if (state == GuidesState.FATAL_NETWORK_ERROR) + onGuidesFatalError(); + else + state.activate(getApplicationContext()); + } + + private void onGuidesFatalError() + { + mToggleMapLayerController.turnOff(); + showGuidesFatalErrorDialog(); + ToggleMapLayerDialog frag = ToggleMapLayerDialog.getInstance(this); + if (frag == null) + return; + + RecyclerView recycler = frag.getRootView().findViewById(R.id.recycler); + Objects.requireNonNull(recycler.getAdapter()).notifyDataSetChanged(); + } + + private void showGuidesFatalErrorDialog() + { + com.mapswithme.maps.dialog.AlertDialog dialog = + new com.mapswithme.maps.dialog.AlertDialog.Builder() + .setTitleId(R.string.connection_error_dialog_guides_title) + .setMessageId(R.string.common_check_internet_connection_dialog) + .setPositiveBtnId(R.string.ok) + .setDialogViewStrategyType(com.mapswithme.maps.dialog.AlertDialog.DialogViewStrategyType.DEFAULT) + .setReqCode(REQ_CODE_GUIDES_FATAL_ERROR) + .setFragManagerStrategyType(com.mapswithme.maps.dialog.AlertDialog + .FragManagerStrategyType.ACTIVITY_FRAGMENT_MANAGER) + .build(); + dialog.show(this, GUIDES_FATAL_ERROR_DIALOG_TAG); } @CallSuper diff --git a/android/src/com/mapswithme/maps/maplayer/ToggleMapLayerDialog.java b/android/src/com/mapswithme/maps/maplayer/ToggleMapLayerDialog.java index 58b91ec1c1..6430aa62d5 100644 --- a/android/src/com/mapswithme/maps/maplayer/ToggleMapLayerDialog.java +++ b/android/src/com/mapswithme/maps/maplayer/ToggleMapLayerDialog.java @@ -7,6 +7,7 @@ import android.view.LayoutInflater; import android.view.View; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; @@ -28,16 +29,20 @@ public class ToggleMapLayerDialog extends DialogFragment @SuppressWarnings("NullableProblems") private LayersAdapter mAdapter; + @SuppressWarnings("NullableProblems") + @NonNull + private View mRoot; + @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { BottomSheetDialog dialog = new BottomSheetDialog(requireActivity()); LayoutInflater inflater = requireActivity().getLayoutInflater(); - View root = inflater.inflate(R.layout.fragment_toggle_map_layer, null, false); + mRoot = inflater.inflate(R.layout.fragment_toggle_map_layer, null, false); dialog.setOnShowListener(this::onShow); - dialog.setContentView(root); - initChildren(root); + dialog.setContentView(mRoot); + initChildren(mRoot); return dialog; } @@ -91,6 +96,18 @@ public class ToggleMapLayerDialog extends DialogFragment fm.executePendingTransactions(); } + @NonNull + public View getRootView() + { + return mRoot; + } + + @Nullable + public static ToggleMapLayerDialog getInstance(@NonNull AppCompatActivity activity) { + String tag = ToggleMapLayerDialog.class.getCanonicalName(); + return (ToggleMapLayerDialog) activity.getSupportFragmentManager().findFragmentByTag(tag); + } + private class SubwayItemClickListener extends DefaultClickListener { private SubwayItemClickListener() diff --git a/android/src/com/mapswithme/maps/maplayer/guides/GuidesManager.java b/android/src/com/mapswithme/maps/maplayer/guides/GuidesManager.java index 18338bc22d..9ddaf683b4 100644 --- a/android/src/com/mapswithme/maps/maplayer/guides/GuidesManager.java +++ b/android/src/com/mapswithme/maps/maplayer/guides/GuidesManager.java @@ -7,13 +7,14 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.mapswithme.maps.Framework; import com.mapswithme.maps.MwmApplication; +import com.mapswithme.maps.base.Detachable; import com.mapswithme.maps.base.Initializable; import com.mapswithme.maps.guides.GuidesGallery; import java.util.ArrayList; import java.util.List; -public class GuidesManager implements Initializable +public class GuidesManager implements Initializable, Detachable { @NonNull private final OnGuidesChangedListener mListener; @@ -23,6 +24,9 @@ public class GuidesManager implements Initializable private final List mGalleryChangedListeners = new ArrayList<>(); + @Nullable + private GuidesErrorDialogListener mGuidesDialogListener; + public GuidesManager(@NonNull Application application) { mApplication = application; @@ -32,7 +36,10 @@ public class GuidesManager implements Initializable private void onStateChanged(int index) { GuidesState state = GuidesState.values()[index]; - state.activate(mApplication); + if (mGuidesDialogListener == null) + state.activate(mApplication); + else + mGuidesDialogListener.onStateChanged(state); } public boolean isEnabled() @@ -94,6 +101,18 @@ public class GuidesManager implements Initializable return nativeGetGallery(); } + @Override + public void attach(@NonNull GuidesErrorDialogListener listener) + { + mGuidesDialogListener = listener; + } + + @Override + public void detach() + { + mGuidesDialogListener = null; + } + @NonNull public static GuidesManager from(@NonNull Context context) { diff --git a/android/src/com/mapswithme/maps/maplayer/guides/GuidesState.java b/android/src/com/mapswithme/maps/maplayer/guides/GuidesState.java index 82f8b11761..5d7717a31f 100644 --- a/android/src/com/mapswithme/maps/maplayer/guides/GuidesState.java +++ b/android/src/com/mapswithme/maps/maplayer/guides/GuidesState.java @@ -31,7 +31,15 @@ public enum GuidesState Toast.makeText(context, R.string.no_routes_in_the_area_toast, Toast.LENGTH_SHORT).show(); } }, - NETWORK_ERROR, + NETWORK_ERROR + { + @Override + public void activate(@NonNull Context context) + { + Toast.makeText(context, R.string.connection_error_toast_guides, Toast.LENGTH_SHORT) + .show(); + } + }, FATAL_NETWORK_ERROR; public void activate(@NonNull Context context)