Skip to content

Commit 28581d6

Browse files
committed
fix(android): snackbar correctly handle the view option
1 parent 5b45648 commit 28581d6

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/snackbar/snackbar.android.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Color } from '@nativescript/core/color';
22
import { Frame } from '@nativescript/core/ui/frame';
3+
import { Page } from '@nativescript/core/ui/page';
34
import { DismissReasons, SnackBarAction, SnackBarBase, SnackBarOptions } from './snackbar-common';
45
import { android as androidApp } from '@nativescript/core/application';
56

@@ -56,7 +57,22 @@ export class SnackBar extends SnackBarBase {
5657
while (attachView['_modal']) {
5758
attachView = attachView['_modal'];
5859
}
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);
6076

6177
this._snackbar.setText(options.message);
6278
this._snackbar.setDuration(options.hideDelay);
@@ -121,30 +137,32 @@ export class SnackBar extends SnackBarBase {
121137

122138
// set the action text, click listener
123139
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);
147140
}
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);
148166
}
149167

150168
public show() {

0 commit comments

Comments
 (0)