Skip to content

Commit 8eb02c3

Browse files
committed
feat: BottomSheet Android add onChangeState function
1 parent 571fe28 commit 8eb02c3

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/bottomsheet/bottomsheet-common.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ export abstract class ViewWithBottomSheetBase extends View {
5757

5858
_bottomSheetFragment: any; // com.google.android.material.bottomsheet.BottomSheetDialogFragment
5959
protected abstract _hideNativeBottomSheet(parent, whenClosedCallback);
60+
6061
protected _bottomSheetContext: any;
62+
6163
_raiseShownBottomSheetEvent() {
6264
const args: ShownBottomSheetData = {
6365
eventName: shownInBottomSheetEvent,
@@ -68,6 +70,7 @@ export abstract class ViewWithBottomSheetBase extends View {
6870

6971
this.notify(args);
7072
}
73+
7174
public _bottomSheetClosed(): void {
7275
const _rootModalViews = this._getRootModalViews();
7376
const modalIndex = _rootModalViews.indexOf(this);
@@ -82,7 +85,9 @@ export abstract class ViewWithBottomSheetBase extends View {
8285
// return true;
8386
// });
8487
}
88+
8589
protected abstract _showNativeBottomSheet(parent: View, options: BottomSheetOptions);
90+
8691
protected _commonShowNativeBottomSheet(parent: View, options: BottomSheetOptions) {
8792
this._getRootModalViews().push(this);
8893
this.cssClasses.add(CSSUtils.MODAL_ROOT_VIEW_CSS_CLASS);
@@ -134,6 +139,7 @@ export abstract class ViewWithBottomSheetBase extends View {
134139

135140
this._bottomSheetContext.closeCallback = this._closeBottomSheetCallback;
136141
}
142+
137143
protected _raiseShowingBottomSheetEvent() {
138144
const args: ShownBottomSheetData = {
139145
eventName: showingInBottomSheetEvent,
@@ -143,6 +149,7 @@ export abstract class ViewWithBottomSheetBase extends View {
143149
};
144150
this.notify(args);
145151
}
152+
146153
public closeBottomSheet(...args) {
147154
const closeCallback = this._closeBottomSheetCallback;
148155
if (closeCallback) {
@@ -169,4 +176,4 @@ export abstract class ViewWithBottomSheetBase extends View {
169176
return view;
170177
}
171178
}
172-
}
179+
}

src/bottomsheet/bottomsheet.android.ts

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { applyMixins } from '@nativescript-community/ui-material-core';
2-
import { AndroidActivityBackPressedEventData, Application, View, fromObject, Utils } from '@nativescript/core';
3-
import { BottomSheetOptions, ViewWithBottomSheetBase } from './bottomsheet-common';
2+
import { AndroidActivityBackPressedEventData, Application, Utils, View, fromObject } from '@nativescript/core';
3+
import { BottomSheetOptions, StateBottomSheet, ViewWithBottomSheetBase } from './bottomsheet-common';
44

55
export { ViewWithBottomSheetBase } from './bottomsheet-common';
66

@@ -16,6 +16,52 @@ function getId(id: string) {
1616
return context.getResources().getIdentifier(id, 'id', context.getPackageName());
1717
}
1818

19+
@NativeClass
20+
class ChangeStateBottomSheet extends com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback {
21+
private owner: ViewWithBottomSheet;
22+
23+
constructor(owner: ViewWithBottomSheet) {
24+
super();
25+
this.owner = owner;
26+
return global.__native(this);
27+
}
28+
29+
onSlide(bottomSheet: android.view.View, slideOffset: number) {
30+
if (this.checkActiveCallback()) {
31+
this.owner._onChangeStateBottomSheetCallback(StateBottomSheet.DRAGGING, slideOffset);
32+
}
33+
}
34+
35+
onStateChanged(bottomSheet: android.view.View, newState: number) {
36+
if (this.checkActiveCallback()) {
37+
const status = this.getStateBottomSheetFromBottomSheetBehavior(newState);
38+
if (status !== undefined) {
39+
this.owner._onChangeStateBottomSheetCallback(status);
40+
}
41+
}
42+
}
43+
44+
private checkActiveCallback() {
45+
return this.owner && this.owner._onChangeStateBottomSheetCallback;
46+
}
47+
48+
public getStateBottomSheetFromBottomSheetBehavior(state: number): StateBottomSheet | undefined {
49+
switch (state) {
50+
case com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_HIDDEN:
51+
return StateBottomSheet.CLOSED;
52+
case com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED:
53+
return StateBottomSheet.EXPANDED;
54+
case com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED:
55+
return StateBottomSheet.COLLAPSED;
56+
case com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_DRAGGING:
57+
return StateBottomSheet.DRAGGING;
58+
default:
59+
// @ts-ignore
60+
return;
61+
}
62+
}
63+
}
64+
1965
export class ViewWithBottomSheet extends ViewWithBottomSheetBase {
2066
_bottomSheetFragment: com.nativescript.material.bottomsheet.BottomSheetDialogFragment;
2167
protected _hideNativeBottomSheet(parent: View, whenClosedCallback: () => void) {
@@ -51,10 +97,10 @@ export class ViewWithBottomSheet extends ViewWithBottomSheetBase {
5197
},
5298

5399
onBackPressed(dialog: com.nativescript.material.bottomsheet.BottomSheetDialog) {
54-
if(bottomSheetOptions.options && bottomSheetOptions.options.dismissOnBackButton === false){
100+
if (bottomSheetOptions.options && bottomSheetOptions.options.dismissOnBackButton === false) {
55101
return true;
56102
}
57-
103+
58104
if (!owner) {
59105
return false;
60106
}
@@ -133,13 +179,19 @@ export class ViewWithBottomSheet extends ViewWithBottomSheetBase {
133179
// disable peek/collapsed state
134180
behavior.setSkipCollapsed(true);
135181
}
136-
137182
const peekHeight = bottomSheetOptions.options?.peekHeight;
138-
if(peekHeight){
183+
if (peekHeight) {
139184
behavior.setState(com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED);
140185
behavior.setPeekHeight(Utils.layout.toDevicePixels(peekHeight));
141186
}
142187

188+
const onChangeState = bottomSheetOptions.options?.onChangeState;
189+
if (onChangeState) {
190+
const changeStateBottomSheet = new ChangeStateBottomSheet(owner);
191+
behavior.addBottomSheetCallback(changeStateBottomSheet);
192+
owner._onChangeStateBottomSheetCallback(changeStateBottomSheet.getStateBottomSheetFromBottomSheetBehavior(behavior.getState()));
193+
}
194+
143195
if (owner && !owner.isLoaded) {
144196
owner.callLoaded();
145197
}

0 commit comments

Comments
 (0)