|
1 | 1 | import { Color } from '@nativescript/core/color'; |
2 | 2 | import { Frame } from '@nativescript/core/ui/frame'; |
| 3 | +import { Page } from '@nativescript/core/ui/page'; |
3 | 4 | import { DismissReasons, SnackBarAction, SnackBarBase, SnackBarOptions } from './snackbar-common'; |
4 | 5 | import { android as androidApp } from '@nativescript/core/application'; |
5 | 6 |
|
@@ -56,7 +57,22 @@ export class SnackBar extends SnackBarBase { |
56 | 57 | while (attachView['_modal']) { |
57 | 58 | attachView = attachView['_modal']; |
58 | 59 | } |
59 | | - this._snackbar = com.google.android.material.snackbar.Snackbar.make(attachView.nativeViewProtected, options.message, options.hideDelay); |
| 60 | + let nView = attachView.nativeViewProtected as android.view.View; |
| 61 | + if (attachView instanceof Page) { |
| 62 | + // in case of a page we try to handle it correctly |
| 63 | + nView = nView.getParent().getParent() as any; |
| 64 | + } |
| 65 | + let nCoordinatorLayout: androidx.coordinatorlayout.widget.CoordinatorLayout; |
| 66 | + if (!(nView instanceof androidx.coordinatorlayout.widget.CoordinatorLayout) && nView instanceof android.view.ViewGroup) { |
| 67 | + nCoordinatorLayout = new androidx.coordinatorlayout.widget.CoordinatorLayout(attachView._context); |
| 68 | + console.log('adding nCoordinatorLayout', attachView, nView, nView.getParent(), nCoordinatorLayout); |
| 69 | + (nView as android.view.ViewGroup).addView( |
| 70 | + nCoordinatorLayout, |
| 71 | + new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT) |
| 72 | + ); |
| 73 | + nView = nCoordinatorLayout; |
| 74 | + } |
| 75 | + this._snackbar = com.google.android.material.snackbar.Snackbar.make(nView, options.message, options.hideDelay); |
60 | 76 |
|
61 | 77 | this._snackbar.setText(options.message); |
62 | 78 | this._snackbar.setDuration(options.hideDelay); |
@@ -121,30 +137,32 @@ export class SnackBar extends SnackBarBase { |
121 | 137 |
|
122 | 138 | // set the action text, click listener |
123 | 139 | this._snackbar.setAction(options.actionText, listener); |
124 | | - |
125 | | - const callbackListener = new com.nativescript.material.snackbar.SnackCallback.SnackCallbackListener({ |
126 | | - onDismissed(snackbar: com.google.android.material.snackbar.Snackbar, event: number) { |
127 | | - // if the dismiss was not caused by the action button click listener |
128 | | - const resolve = (cb as any).resolve; |
129 | | - if (event !== com.google.android.material.snackbar.BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_ACTION && resolve) { |
130 | | - resolve({ |
131 | | - command: SnackBarAction.DISMISS, |
132 | | - reason: _getReason(event), |
133 | | - event: event, |
134 | | - }); |
135 | | - (cb as any).resolve.resolve = null; |
136 | | - } |
137 | | - (cb as any).nListener = null; |
138 | | - }, |
139 | | - |
140 | | - onShown(snackbar: com.google.android.material.snackbar.Snackbar) {}, |
141 | | - }); |
142 | | - const cb = (this._snackbarCallback = new com.nativescript.material.snackbar.SnackCallback()); |
143 | | - cb.setListener(callbackListener); |
144 | | - (cb as any).nListener = callbackListener; // handles the resolve of the promise |
145 | | - (cb as any).resolve = resolve; // handles the resolve of the promise |
146 | | - this._snackbar.addCallback(cb); |
147 | 140 | } |
| 141 | + const cb = (this._snackbarCallback = new com.nativescript.material.snackbar.SnackCallback()); |
| 142 | + const callbackListener = new com.nativescript.material.snackbar.SnackCallback.SnackCallbackListener({ |
| 143 | + onDismissed(snackbar: com.google.android.material.snackbar.Snackbar, event: number) { |
| 144 | + // if the dismiss was not caused by the action button click listener |
| 145 | + const resolve = (cb as any).resolve; |
| 146 | + if (event !== com.google.android.material.snackbar.BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_ACTION && resolve) { |
| 147 | + resolve({ |
| 148 | + command: SnackBarAction.DISMISS, |
| 149 | + reason: _getReason(event), |
| 150 | + event: event, |
| 151 | + }); |
| 152 | + (cb as any).resolve.resolve = null; |
| 153 | + } |
| 154 | + (cb as any).nListener = null; |
| 155 | + if (nCoordinatorLayout) { |
| 156 | + (nCoordinatorLayout.getParent() as android.view.ViewGroup).removeView(nCoordinatorLayout); |
| 157 | + } |
| 158 | + }, |
| 159 | + |
| 160 | + onShown(snackbar: com.google.android.material.snackbar.Snackbar) {}, |
| 161 | + }); |
| 162 | + cb.setListener(callbackListener); |
| 163 | + (cb as any).nListener = callbackListener; // handles the resolve of the promise |
| 164 | + (cb as any).resolve = resolve; // handles the resolve of the promise |
| 165 | + this._snackbar.addCallback(cb); |
148 | 166 | } |
149 | 167 |
|
150 | 168 | public show() { |
|
0 commit comments