capacitor-google-navigation 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  package com.attributeai.navigation;
2
2
 
3
+ import android.app.Activity;
3
4
  import android.graphics.Color;
4
5
  import android.graphics.Typeface;
5
6
  import android.os.Bundle;
@@ -25,6 +26,7 @@ import com.google.android.libraries.navigation.NavigationView;
25
26
  public class NavigationFragment extends Fragment {
26
27
 
27
28
  private NavigationView navigationView;
29
+ private Button closeButton;
28
30
  private Runnable onCloseListener;
29
31
 
30
32
  public static NavigationFragment newInstance() {
@@ -44,57 +46,29 @@ public class NavigationFragment extends Fragment {
44
46
  ) {
45
47
  navigationView = new NavigationView(requireContext());
46
48
  navigationView.onCreate(savedInstanceState);
47
-
48
- FrameLayout root = new FrameLayout(requireContext());
49
- root.addView(navigationView, new FrameLayout.LayoutParams(
50
- FrameLayout.LayoutParams.MATCH_PARENT,
51
- FrameLayout.LayoutParams.MATCH_PARENT
52
- ));
53
-
54
- Button closeButton = new Button(requireContext());
55
- closeButton.setText("✕");
56
- closeButton.setTextColor(Color.WHITE);
57
- closeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
58
- closeButton.setTypeface(null, Typeface.BOLD);
59
- closeButton.setBackgroundColor(Color.argb(153, 0, 0, 0)); // 60% black
60
- closeButton.setOnClickListener(v -> {
61
- if (onCloseListener != null) onCloseListener.run();
62
- });
63
-
64
- int sizePx = (int) TypedValue.applyDimension(
65
- TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics()
66
- );
67
- int marginPx = (int) TypedValue.applyDimension(
68
- TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()
69
- );
70
-
71
- FrameLayout.LayoutParams btnParams = new FrameLayout.LayoutParams(sizePx, sizePx);
72
- btnParams.gravity = Gravity.TOP | Gravity.START;
73
- btnParams.topMargin = marginPx;
74
- btnParams.leftMargin = marginPx;
75
- root.addView(closeButton, btnParams);
76
-
77
- return root;
78
- }
79
-
80
- @Override
81
- public void onStart() {
82
- super.onStart();
83
- if (navigationView != null) navigationView.onStart();
49
+ return navigationView;
84
50
  }
85
51
 
86
52
  @Override
87
53
  public void onResume() {
88
54
  super.onResume();
89
55
  if (navigationView != null) navigationView.onResume();
56
+ attachCloseButtonToDecorView();
90
57
  }
91
58
 
92
59
  @Override
93
60
  public void onPause() {
61
+ detachCloseButtonFromDecorView();
94
62
  if (navigationView != null) navigationView.onPause();
95
63
  super.onPause();
96
64
  }
97
65
 
66
+ @Override
67
+ public void onStart() {
68
+ super.onStart();
69
+ if (navigationView != null) navigationView.onStart();
70
+ }
71
+
98
72
  @Override
99
73
  public void onStop() {
100
74
  if (navigationView != null) navigationView.onStop();
@@ -103,6 +77,7 @@ public class NavigationFragment extends Fragment {
103
77
 
104
78
  @Override
105
79
  public void onDestroyView() {
80
+ detachCloseButtonFromDecorView();
106
81
  if (navigationView != null) navigationView.onDestroy();
107
82
  super.onDestroyView();
108
83
  }
@@ -112,4 +87,51 @@ public class NavigationFragment extends Fragment {
112
87
  super.onSaveInstanceState(outState);
113
88
  if (navigationView != null) navigationView.onSaveInstanceState(outState);
114
89
  }
90
+
91
+ // MARK: - Close button attached to the Activity decor view, above all SDK UI
92
+
93
+ private void attachCloseButtonToDecorView() {
94
+ Activity activity = getActivity();
95
+ if (activity == null || closeButton != null) return;
96
+
97
+ int sizePx = (int) TypedValue.applyDimension(
98
+ TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics()
99
+ );
100
+ int marginPx = (int) TypedValue.applyDimension(
101
+ TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()
102
+ );
103
+ int statusBarHeight = getStatusBarHeight(activity);
104
+
105
+ Button button = new Button(requireContext());
106
+ button.setText("✕");
107
+ button.setTextColor(Color.WHITE);
108
+ button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
109
+ button.setTypeface(null, Typeface.BOLD);
110
+ button.setBackgroundColor(Color.argb(153, 0, 0, 0));
111
+ button.setOnClickListener(v -> {
112
+ if (onCloseListener != null) onCloseListener.run();
113
+ });
114
+
115
+ FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(sizePx, sizePx);
116
+ params.gravity = Gravity.TOP | Gravity.START;
117
+ params.topMargin = statusBarHeight + marginPx;
118
+ params.leftMargin = marginPx;
119
+
120
+ ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
121
+ decorView.addView(button, params);
122
+ this.closeButton = button;
123
+ }
124
+
125
+ private void detachCloseButtonFromDecorView() {
126
+ Activity activity = getActivity();
127
+ if (activity == null || closeButton == null) return;
128
+ ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
129
+ decorView.removeView(closeButton);
130
+ closeButton = null;
131
+ }
132
+
133
+ private int getStatusBarHeight(Activity activity) {
134
+ int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
135
+ return resourceId > 0 ? activity.getResources().getDimensionPixelSize(resourceId) : 0;
136
+ }
115
137
  }
@@ -5,6 +5,7 @@ import GoogleNavigation
5
5
  class NavigationMapViewController: UIViewController {
6
6
  private let session: GMSNavigationSession
7
7
  private var mapView: GMSMapView?
8
+ private var overlayWindow: UIWindow?
8
9
  var onDismiss: (() -> Void)?
9
10
 
10
11
  init(session: GMSNavigationSession) {
@@ -26,18 +27,41 @@ class NavigationMapViewController: UIViewController {
26
27
  container.addSubview(mapView)
27
28
  self.mapView = mapView
28
29
 
29
- // Overlay sits above the map and all Google-rendered UI
30
- let overlay = UIView()
31
- overlay.translatesAutoresizingMaskIntoConstraints = false
32
- overlay.isUserInteractionEnabled = true
33
- overlay.backgroundColor = .clear
34
- container.addSubview(overlay)
35
- NSLayoutConstraint.activate([
36
- overlay.topAnchor.constraint(equalTo: container.topAnchor),
37
- overlay.bottomAnchor.constraint(equalTo: container.bottomAnchor),
38
- overlay.leadingAnchor.constraint(equalTo: container.leadingAnchor),
39
- overlay.trailingAnchor.constraint(equalTo: container.trailingAnchor),
40
- ])
30
+ self.view = container
31
+ }
32
+
33
+ override func viewDidLoad() {
34
+ super.viewDidLoad()
35
+ guard let mapView = mapView else { return }
36
+ let enabled = mapView.enableNavigation(with: session)
37
+ if enabled {
38
+ mapView.cameraMode = .following
39
+ }
40
+ }
41
+
42
+ override func viewDidAppear(_ animated: Bool) {
43
+ super.viewDidAppear(animated)
44
+ addCloseButtonWindow()
45
+ }
46
+
47
+ override func viewDidDisappear(_ animated: Bool) {
48
+ super.viewDidDisappear(animated)
49
+ tearDownCloseButtonWindow()
50
+ }
51
+
52
+ // MARK: - Close button in a top-level UIWindow above all SDK UI
53
+
54
+ private func addCloseButtonWindow() {
55
+ guard let scene = view.window?.windowScene else { return }
56
+
57
+ let window = UIWindow(windowScene: scene)
58
+ window.windowLevel = .alert + 1
59
+ window.backgroundColor = .clear
60
+ window.isHidden = false
61
+
62
+ let overlayVC = UIViewController()
63
+ overlayVC.view.backgroundColor = .clear
64
+ window.rootViewController = overlayVC
41
65
 
42
66
  let button = UIButton(type: .system)
43
67
  button.setImage(UIImage(systemName: "xmark"), for: .normal)
@@ -46,27 +70,25 @@ class NavigationMapViewController: UIViewController {
46
70
  button.layer.cornerRadius = 20
47
71
  button.translatesAutoresizingMaskIntoConstraints = false
48
72
  button.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
49
- overlay.addSubview(button)
73
+ overlayVC.view.addSubview(button)
74
+
50
75
  NSLayoutConstraint.activate([
51
- button.topAnchor.constraint(equalTo: overlay.safeAreaLayoutGuide.topAnchor, constant: 16),
52
- button.leadingAnchor.constraint(equalTo: overlay.leadingAnchor, constant: 16),
76
+ button.topAnchor.constraint(equalTo: overlayVC.view.safeAreaLayoutGuide.topAnchor, constant: 16),
77
+ button.leadingAnchor.constraint(equalTo: overlayVC.view.leadingAnchor, constant: 16),
53
78
  button.widthAnchor.constraint(equalToConstant: 40),
54
79
  button.heightAnchor.constraint(equalToConstant: 40),
55
80
  ])
56
81
 
57
- self.view = container
82
+ self.overlayWindow = window
58
83
  }
59
84
 
60
- override func viewDidLoad() {
61
- super.viewDidLoad()
62
- guard let mapView = mapView else { return }
63
- let enabled = mapView.enableNavigation(with: session)
64
- if enabled {
65
- mapView.cameraMode = .following
66
- }
85
+ private func tearDownCloseButtonWindow() {
86
+ overlayWindow?.isHidden = true
87
+ overlayWindow = nil
67
88
  }
68
89
 
69
90
  @objc private func closeTapped() {
91
+ tearDownCloseButtonWindow()
70
92
  dismiss(animated: true) { [weak self] in
71
93
  self?.onDismiss?()
72
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-google-navigation",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Google maps turn by turn navigation for capcitor",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",