1
- import { Color , CssProperty , Property , Style , View , booleanConverter , fontSizeProperty } from '@nativescript/core' ;
2
- import { BoxType } from './common' ;
1
+ import { Color } from '@nativescript/core' ;
2
+ import { BoxType , CheckBoxBase , checkedProperty , fillColorProperty , tintColorProperty } from './common' ;
3
3
4
- export const checkedProperty = new Property < CheckBox , boolean > ( {
5
- name : 'checked' ,
6
- defaultValue : false ,
7
- valueConverter : booleanConverter ,
8
- valueChanged : onCheckedPropertyChanged
9
- } ) ;
10
-
11
- export const textProperty = new Property < CheckBox , string > ( {
12
- name : 'text' ,
13
- defaultValue : '' ,
14
- valueChanged : onTextPropertyChanged
15
- } ) ;
16
-
17
- export const fillColorProperty = new CssProperty < Style , string > ( {
18
- name : 'fillColor' ,
19
- cssName : 'fill-color' ,
20
- valueConverter : ( v ) => String ( v )
21
- } ) ;
22
-
23
- export const tintColorProperty = new CssProperty < Style , string > ( {
24
- name : 'tintColor' ,
25
- cssName : 'tint-color' ,
26
- valueConverter : ( v ) => String ( v )
27
- } ) ;
28
-
29
- export class CheckBox extends View {
30
- checked : boolean ;
31
- fillColor : Color | string ;
32
- tintColor : Color | string ;
4
+ export class CheckBox extends CheckBoxBase {
33
5
nativeViewProtected : androidx . appcompat . widget . AppCompatCheckBox ;
34
- private _boxType : string ;
35
- private _checkStyle : string ;
36
- private _checkPadding : string ;
37
- private _checkPaddingLeft : string ;
38
- private _checkPaddingTop : string ;
39
- private _checkPaddingRight : string ;
40
- private _checkPaddingBottom : string ;
41
-
42
- constructor ( ) {
43
- super ( ) ;
44
- }
45
-
46
- set boxType ( value : string ) {
47
- this . _boxType = value ;
48
- }
49
-
50
- get boxType ( ) {
51
- return this . _boxType ;
52
- }
53
-
54
- get checkStyle ( ) {
55
- return this . _checkStyle ;
56
- }
57
-
58
- set checkStyle ( style ) {
59
- this . _checkStyle = style ;
60
- }
61
-
62
- set checkPadding ( padding ) {
63
- this . _checkPadding = padding ;
64
- }
65
-
66
- get checkPadding ( ) {
67
- return this . _checkPadding ;
68
- }
69
-
70
- set checkPaddingLeft ( padding ) {
71
- this . _checkPaddingLeft = padding ;
72
- }
73
-
74
- get checkPaddingLeft ( ) {
75
- return this . _checkPaddingLeft ;
76
- }
77
-
78
- set checkPaddingTop ( padding ) {
79
- this . _checkPaddingTop = padding ;
80
- }
81
-
82
- get checkPaddingTop ( ) {
83
- return this . _checkPaddingTop ;
84
- }
85
-
86
- set checkPaddingRight ( padding ) {
87
- this . _checkPaddingRight = padding ;
88
- }
89
-
90
- get checkPaddingRight ( ) {
91
- return this . _checkPaddingRight ;
92
- }
93
-
94
- set checkPaddingBottom ( padding ) {
95
- this . _checkPaddingBottom = padding ;
96
- }
6
+ checkStyle : string ;
7
+ checkPadding : string ;
8
+ checkPaddingLeft : string ;
9
+ checkPaddingTop : string ;
10
+ checkPaddingRight : string ;
11
+ checkPaddingBottom : string ;
12
+ ignoreChange = false ;
97
13
98
- get checkPaddingBottom ( ) {
99
- return this . _checkPaddingBottom ;
100
- }
101
- [ checkedProperty . getDefault ] ( ) : boolean {
102
- return false ;
103
- }
104
14
[ checkedProperty . setNative ] ( value : boolean ) {
105
- this . nativeViewProtected . setChecked ( Boolean ( value ) ) ;
106
- }
107
- [ textProperty . getDefault ] ( ) : string {
108
- return '' ;
109
- }
110
- [ textProperty . setNative ] ( value : string ) {
111
- this . nativeViewProtected . setText ( java . lang . String . valueOf ( value ) ) ;
112
- }
113
- [ fontSizeProperty . getDefault ] ( ) {
114
- return { nativeSize : this . nativeViewProtected . getTextSize ( ) } ;
115
- }
116
- [ fontSizeProperty . setNative ] ( value ) {
117
- if ( typeof value === 'number' ) {
118
- this . nativeViewProtected . setTextSize ( value ) ;
119
- } else {
120
- this . nativeViewProtected . setTextSize ( android . util . TypedValue . COMPLEX_UNIT_PX , value . nativeSize ) ;
121
- }
15
+ this . ignoreChange = true ;
16
+ this . nativeViewProtected . setChecked ( value ) ;
17
+ this . ignoreChange = false ;
122
18
}
123
19
124
- get fontSize ( ) : number {
125
- return this . style . fontSize ;
126
- }
127
- set fontSize ( size : number ) {
128
- this . style . fontSize = size ;
129
- }
130
-
131
- // get fillColor(): string {
132
- // return this.style.fillColor;
133
- // }
134
- // set fillColor(color: string) {
135
- // (this.style as any).fillColor = color;
136
- // if (this.nativeViewProtected && Device.sdkVersion >= '21') {
137
- // this.nativeViewProtected.setSupportButtonTintList(android.content.res.ColorStateList.valueOf(new Color(color).android));
138
- // }
139
- // }
140
-
141
20
[ fillColorProperty . setNative ] ( value ) {
142
21
const color = ! value || value instanceof Color ? value : new Color ( value ) ;
143
22
this . nativeViewProtected . setButtonTintList ( color ? android . content . res . ColorStateList . valueOf ( color . android ) : null ) ;
144
23
}
145
-
146
24
[ tintColorProperty . setNative ] ( value ) {
147
- // there is no difference between tint and fill on the android widget
148
25
this [ fillColorProperty . setNative ] ( value ) ;
149
26
}
27
+
150
28
createNativeView ( ) {
151
29
let view : androidx . appcompat . widget . AppCompatRadioButton | androidx . appcompat . widget . AppCompatCheckBox ;
152
30
if ( BoxType [ this . boxType ] === BoxType . circle ) {
153
- view = new androidx . appcompat . widget . AppCompatRadioButton ( this . _context , null ) ;
31
+ view = new androidx . appcompat . widget . AppCompatRadioButton ( this . _context ) ;
154
32
} else {
155
- view = new androidx . appcompat . widget . AppCompatCheckBox ( this . _context , null ) ;
33
+ view = new androidx . appcompat . widget . AppCompatCheckBox ( this . _context ) ;
156
34
}
157
35
158
36
if ( this . checkPaddingLeft ) {
@@ -188,42 +66,28 @@ export class CheckBox extends View {
188
66
break ;
189
67
}
190
68
}
191
-
192
- if ( this . style . color ) {
193
- view . setTextColor ( this . style . color . android ) ;
194
- }
195
-
196
- if ( ! this . style . fontSize ) {
197
- this . style . fontSize = 14 ;
198
- }
199
-
200
- view . setTextSize ( this . style . fontSize ) ;
201
-
202
- const typeface = this . style . fontInternal ?. getAndroidTypeface ( ) ;
203
- if ( typeface ) {
204
- view . setTypeface ( typeface ) ;
205
- }
206
-
207
- if ( this . _checkStyle ) {
208
- const drawable = this . _context . getResources ( ) . getIdentifier ( this . _checkStyle , 'drawable' , this . _context . getPackageName ( ) ) ;
69
+ if ( this . checkStyle ) {
70
+ const drawable = this . _context . getResources ( ) . getIdentifier ( this . checkStyle , 'drawable' , this . _context . getPackageName ( ) ) ;
209
71
view . setButtonDrawable ( drawable ) ;
210
72
}
211
73
212
74
// if (view) {
213
- // if (this.fillColor) {
214
- // view.setSupportButtonTintList(android.content.res.ColorStateList.valueOf(new Color(this.fillColor).android));
215
- // }
75
+ // if (this.fillColor) {
76
+ // view.setSupportButtonTintList(android.content.res.ColorStateList.valueOf(new Color(this.fillColor).android));
77
+ // }
216
78
// }
217
79
return view ;
218
80
}
219
81
220
82
initNativeView ( ) {
221
- const that = new WeakRef ( this ) ;
83
+ const that = new WeakRef < CheckBox > ( this ) ;
222
84
this . nativeViewProtected . setOnCheckedChangeListener (
223
85
new android . widget . CompoundButton . OnCheckedChangeListener ( {
224
86
onCheckedChanged : ( sender , isChecked ) => {
225
- if ( that . get ( ) ) {
226
- checkedProperty . nativeValueChange ( that . get ( ) , isChecked ) ;
87
+ const owner = that . get ( ) ;
88
+ if ( owner && ! owner . ignoreChange ) {
89
+ console . log ( 'onCheckedChanged' , isChecked ) ;
90
+ checkedProperty . nativeValueChange ( owner , isChecked ) ;
227
91
}
228
92
}
229
93
} )
@@ -237,29 +101,4 @@ export class CheckBox extends View {
237
101
toggle ( ) : void {
238
102
this . nativeViewProtected . toggle ( ) ;
239
103
}
240
-
241
- _onCheckedPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
242
- if ( ! this . nativeViewProtected ) {
243
- return ;
244
- }
245
- checkedProperty . nativeValueChange ( this , newValue ) ;
246
- }
247
- _onTextPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
248
- if ( ! this . nativeViewProtected ) {
249
- return ;
250
- }
251
- textProperty . nativeValueChange ( this , newValue ) ;
252
- }
253
104
}
254
-
255
- function onCheckedPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
256
- checkbox . _onCheckedPropertyChanged ( checkbox , oldValue , newValue ) ;
257
- }
258
- function onTextPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
259
- checkbox . _onTextPropertyChanged ( checkbox , oldValue , newValue ) ;
260
- }
261
-
262
- checkedProperty . register ( CheckBox ) ;
263
- textProperty . register ( CheckBox ) ;
264
- fillColorProperty . register ( Style ) ;
265
- tintColorProperty . register ( Style ) ;
0 commit comments