1
1
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' ;
4
4
5
5
export { ViewWithBottomSheetBase } from './bottomsheet-common' ;
6
6
@@ -16,6 +16,52 @@ function getId(id: string) {
16
16
return context . getResources ( ) . getIdentifier ( id , 'id' , context . getPackageName ( ) ) ;
17
17
}
18
18
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
+
19
65
export class ViewWithBottomSheet extends ViewWithBottomSheetBase {
20
66
_bottomSheetFragment : com . nativescript . material . bottomsheet . BottomSheetDialogFragment ;
21
67
protected _hideNativeBottomSheet ( parent : View , whenClosedCallback : ( ) => void ) {
@@ -51,10 +97,10 @@ export class ViewWithBottomSheet extends ViewWithBottomSheetBase {
51
97
} ,
52
98
53
99
onBackPressed ( dialog : com . nativescript . material . bottomsheet . BottomSheetDialog ) {
54
- if ( bottomSheetOptions . options && bottomSheetOptions . options . dismissOnBackButton === false ) {
100
+ if ( bottomSheetOptions . options && bottomSheetOptions . options . dismissOnBackButton === false ) {
55
101
return true ;
56
102
}
57
-
103
+
58
104
if ( ! owner ) {
59
105
return false ;
60
106
}
@@ -133,13 +179,19 @@ export class ViewWithBottomSheet extends ViewWithBottomSheetBase {
133
179
// disable peek/collapsed state
134
180
behavior . setSkipCollapsed ( true ) ;
135
181
}
136
-
137
182
const peekHeight = bottomSheetOptions . options ?. peekHeight ;
138
- if ( peekHeight ) {
183
+ if ( peekHeight ) {
139
184
behavior . setState ( com . google . android . material . bottomsheet . BottomSheetBehavior . STATE_COLLAPSED ) ;
140
185
behavior . setPeekHeight ( Utils . layout . toDevicePixels ( peekHeight ) ) ;
141
186
}
142
187
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
+
143
195
if ( owner && ! owner . isLoaded ) {
144
196
owner . callLoaded ( ) ;
145
197
}
0 commit comments