-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfab.android.ts
More file actions
198 lines (175 loc) · 5.44 KB
/
fab.android.ts
File metadata and controls
198 lines (175 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
backgroundColorProperty,
backgroundInternalProperty,
Color,
ImageSource,
Utils
} from '@nativescript/core';
import {
AndroidScaleType,
androidScaleTypeProperty,
FloatingActionButtonBase,
iconProperty,
rippleColorProperty,
textProperty
} from './fab-common';
declare let global: any;
const FABNamespace = useAndroidX()
? com.google.android.material.floatingactionbutton
: (android.support as any).design.widget;
function useAndroidX() {
return (
global.androidx &&
com.google &&
com.google.android &&
com.google.android.material
);
}
export class Fab extends FloatingActionButtonBase {
private _androidViewId: number;
private _android: com.google.android.material.floatingactionbutton.FloatingActionButton;
static tapEvent = 'tap';
// get android(): com.google.android.material.floatingactionbutton.FloatingActionButton {
// return this.nativeView;
// }
createNativeView() {
this._android = new FABNamespace.FloatingActionButton(this._context);
return this._android;
}
initNativeView() {
this._androidViewId = android.view.View.generateViewId();
this.nativeView.setId(this._androidViewId);
const clickListener = new ClickListenerImpl(this);
this.nativeView.setOnClickListener(clickListener);
(<any>this.nativeView).clickListener = clickListener;
this.nativeView.setScaleType(android.widget.ImageView.ScaleType.CENTER);
}
[backgroundColorProperty.getDefault](): android.content.res.ColorStateList {
return this.nativeView.getBackgroundTintList();
}
[backgroundColorProperty.setNative](
value: Color | android.content.res.ColorStateList
) {
let newValue: android.content.res.ColorStateList;
if (value instanceof Color) {
newValue = android.content.res.ColorStateList.valueOf(value.android);
} else {
// Resetting with the default value;
newValue = value;
}
try {
this.nativeView.setBackgroundTintList(newValue);
} catch (err) {
console.log(`Error setNative backgroundColorProperty: `, err);
}
}
[backgroundInternalProperty.setNative](value: any) {
// NOOP
}
[colorProperty.setNative](value) {
let newValue;
if (value instanceof Color) {
newValue = android.content.res.ColorStateList.valueOf(value.android);
}
else {
newValue = value;
}
try {
this.nativeView.setSupportImageTintList(newValue);
}
catch (err) {
console.log(`Error setNative colorProperty: `, err);
}
}
[rippleColorProperty.setNative](value: Color) {
this.nativeView.setRippleColor(value.android);
}
[iconProperty.setNative](value: any) {
let iconDrawable = null;
if (!value) {
return;
}
if (Utils.isFileOrResourcePath(value)) {
iconDrawable = ImageSource.fromFileOrResourceSync(value);
if (iconDrawable) {
this.nativeView.setImageBitmap(iconDrawable.android);
} else {
console.log(
'The icon: ' +
value +
' was not found. Check your icon property value. Be sure to rebuild the project after adding images.'
);
}
} else {
const drawableId = android.content.res.Resources.getSystem().getIdentifier(
value,
'drawable',
'android'
);
iconDrawable = android.content.res.Resources.getSystem().getDrawable(
drawableId
);
if (iconDrawable) {
this.nativeView.setImageDrawable(iconDrawable);
} else {
console.log(
'The icon: ' +
value +
' was not found. Check your icon property value. Be sure to rebuild the project after adding images.'
);
}
}
}
[textProperty.setNative](value: string) {
const image = this.getImageFromText(value);
if (image) {
this.nativeView.setImageBitmap(image.android);
}
}
[androidScaleTypeProperty.setNative](value: AndroidScaleType) {
let scaleType = android.widget.ImageView.ScaleType.CENTER;
switch (value.trim().toLowerCase()) {
case 'centercrop':
scaleType = android.widget.ImageView.ScaleType.CENTER_CROP;
break;
case 'centerinside':
scaleType = android.widget.ImageView.ScaleType.CENTER_INSIDE;
break;
case 'fitcenter':
scaleType = android.widget.ImageView.ScaleType.FIT_CENTER;
break;
case 'fitend':
scaleType = android.widget.ImageView.ScaleType.FIT_END;
break;
case 'fitstart':
scaleType = android.widget.ImageView.ScaleType.FIT_START;
break;
case 'fitxy':
scaleType = android.widget.ImageView.ScaleType.FIT_XY;
break;
case 'matrix':
scaleType = android.widget.ImageView.ScaleType.MATRIX;
break;
}
this.nativeView.setScaleType(scaleType);
}
}
interface ClickListener {
new (owner: FloatingActionButtonBase): android.view.View.OnClickListener;
}
@NativeClass()
@Interfaces([android.view.View.OnClickListener])
class ClickListenerImpl
extends java.lang.Object
implements android.view.View.OnClickListener {
constructor(public owner: FloatingActionButtonBase) {
super();
return global.__native(this);
}
onClick(v: android.view.View): void {
const owner = this.owner;
if (owner) {
(<any>owner)._emit('tap');
}
}
}