11import { 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
55export { 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+
1965export 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