Skip to content

Commit 37c8df8

Browse files
committed
fix(android): a lot of improvements
1 parent 9895c56 commit 37c8df8

File tree

1 file changed

+41
-180
lines changed

1 file changed

+41
-180
lines changed

Diff for: src/label.android.ts

+41-180
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,23 @@ Font.prototype.getAndroidTypeface = function() {
5959
declare module 'tns-core-modules/text/formatted-string' {
6060
interface FormattedString {
6161
toNativeString(): string;
62+
addPropertyChangeHandler(): void;
63+
removePropertyChangeHandler(): void;
6264
}
6365
}
6466
declare module 'tns-core-modules/text/span' {
6567
interface Span {
6668
toNativeString(): string;
69+
addPropertyChangeHandler(): void;
70+
removePropertyChangeHandler(): void;
6771
}
6872
}
6973

74+
FormattedString.prototype.addPropertyChangeHandler = function() {}
75+
FormattedString.prototype.removePropertyChangeHandler = function() {}
76+
Span.prototype.addPropertyChangeHandler = function() {}
77+
Span.prototype.removePropertyChangeHandler = function() {}
78+
7079
FormattedString.prototype.toNativeString = function() {
7180
let result = '';
7281
const length = this._spans.length;
@@ -117,159 +126,9 @@ Span.prototype.toNativeString = function() {
117126

118127
return result;
119128
};
120-
// import { Span } from 'tns-core-modules/text/span';
121129

122130
export * from './label-common';
123131

124-
// let _useAndroidX;
125-
// function useAndroidX() {
126-
// if (_useAndroidX === undefined) {
127-
// _useAndroidX = !!(global as any).androidx && !!(global as any).androidx.appcompat;
128-
// }
129-
// return _useAndroidX;
130-
// }
131-
// let _HtmlCompat: typeof androidx.core.text.HtmlCompat;
132-
// function HtmlCompat() {
133-
// if (_HtmlCompat === undefined) {
134-
// _HtmlCompat = useAndroidX() ? (global as any).androidx.core.text.HtmlCompat : android.text.Html;
135-
// }
136-
// return _HtmlCompat;
137-
// }
138-
// let _ContentPackageName: typeof androidx.core.content;
139-
// function ContentPackageName() {
140-
// if (_ContentPackageName === undefined) {
141-
// _ContentPackageName = useAndroidX() ? (global as any).androidx.core.content : (android as any).support.v4.content;
142-
// }
143-
// return _ContentPackageName;
144-
// }
145-
// let appAssets: android.content.res.AssetManager;
146-
// const typefaceCache = new Map<string, android.graphics.Typeface>();
147-
// const FONTS_BASE_PATH = '/fonts/';
148-
// function loadFontFromFile(fontFamily: string): android.graphics.Typeface {
149-
// if (fontFamily.startsWith('res/')) {
150-
// let result = typefaceCache.get(fontFamily);
151-
// if (!result) {
152-
// const context = application.android.context;
153-
// const fontID = context.getResources().getIdentifier(fontFamily.slice(4), 'font', context.getPackageName());
154-
// result = ContentPackageName().res.ResourcesCompat.getFont(context, fontID);
155-
// if (result) {
156-
// typefaceCache.set(fontFamily, result);
157-
// }
158-
// return result;
159-
// }
160-
// }
161-
// appAssets = appAssets || application.android.context.getAssets();
162-
// if (!appAssets) {
163-
// return null;
164-
// }
165-
166-
// let result = typefaceCache.get(fontFamily);
167-
// // Check for undefined explicitly as null mean we tried to load the font, but failed.
168-
// if (result === undefined) {
169-
// result = null;
170-
171-
// let fontAssetPath: string;
172-
// const basePath = fs.path.join(fs.knownFolders.currentApp().path, 'fonts', fontFamily);
173-
// if (fs.File.exists(basePath + '.ttf')) {
174-
// fontAssetPath = FONTS_BASE_PATH + fontFamily + '.ttf';
175-
// } else if (fs.File.exists(basePath + '.otf')) {
176-
// fontAssetPath = FONTS_BASE_PATH + fontFamily + '.otf';
177-
// } else {
178-
// if (traceEnabled()) {
179-
// traceWrite('Could not find font file for ' + fontFamily, traceCategories.Error, traceMessageType.error);
180-
// }
181-
// }
182-
183-
// if (fontAssetPath) {
184-
// try {
185-
// fontAssetPath = fs.path.join(fs.knownFolders.currentApp().path, fontAssetPath);
186-
// result = android.graphics.Typeface.createFromFile(fontAssetPath);
187-
// } catch (e) {
188-
// if (traceEnabled()) {
189-
// traceWrite('Error loading font asset: ' + fontAssetPath, traceCategories.Error, traceMessageType.error);
190-
// }
191-
// }
192-
// }
193-
// typefaceCache.set(fontFamily, result);
194-
// }
195-
196-
// return result;
197-
// }
198-
199-
// function createTypeface(font: Font): android.graphics.Typeface {
200-
// let fontStyle = 0;
201-
// if (font.isBold) {
202-
// fontStyle |= android.graphics.Typeface.BOLD;
203-
// }
204-
// if (font.isItalic) {
205-
// fontStyle |= android.graphics.Typeface.ITALIC;
206-
// }
207-
208-
// // http://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to
209-
// const fonts = parseFontFamily(font.fontFamily);
210-
// let result = null;
211-
// for (let i = 0; i < fonts.length; i++) {
212-
// switch (fonts[i].toLowerCase()) {
213-
// case genericFontFamilies.serif:
214-
// result = android.graphics.Typeface.create('serif' + getFontWeightSuffix(font.fontWeight), fontStyle);
215-
// break;
216-
217-
// case genericFontFamilies.sansSerif:
218-
// case genericFontFamilies.system:
219-
// result = android.graphics.Typeface.create('sans-serif' + getFontWeightSuffix(font.fontWeight), fontStyle);
220-
// break;
221-
222-
// case genericFontFamilies.monospace:
223-
// result = android.graphics.Typeface.create('monospace' + getFontWeightSuffix(font.fontWeight), fontStyle);
224-
// break;
225-
226-
// default:
227-
// result = loadFontFromFile(fonts[i]);
228-
// if (result && fontStyle) {
229-
// result = android.graphics.Typeface.create(result, fontStyle);
230-
// }
231-
// break;
232-
// }
233-
234-
// if (result) {
235-
// // Found the font!
236-
// break;
237-
// }
238-
// }
239-
240-
// if (!result) {
241-
// result = android.graphics.Typeface.create('sans-serif' + getFontWeightSuffix(font.fontWeight), fontStyle);
242-
// }
243-
244-
// return result;
245-
// }
246-
247-
// function getFontWeightSuffix(fontWeight: FontWeight): string {
248-
// switch (fontWeight) {
249-
// case FontWeight.THIN:
250-
// return android.os.Build.VERSION.SDK_INT >= 16 ? '-thin' : '';
251-
// case FontWeight.EXTRA_LIGHT:
252-
// case FontWeight.LIGHT:
253-
// return android.os.Build.VERSION.SDK_INT >= 16 ? '-light' : '';
254-
// case FontWeight.NORMAL:
255-
// case '400':
256-
// case undefined:
257-
// case null:
258-
// return '';
259-
// case FontWeight.MEDIUM:
260-
// case FontWeight.SEMI_BOLD:
261-
// return android.os.Build.VERSION.SDK_INT >= 21 ? '-medium' : '';
262-
// case FontWeight.BOLD:
263-
// case '700':
264-
// case FontWeight.EXTRA_BOLD:
265-
// return '';
266-
// case FontWeight.BLACK:
267-
// return android.os.Build.VERSION.SDK_INT >= 21 ? '-black' : '';
268-
// default:
269-
// throw new Error(`Invalid font weight: "${fontWeight}"`);
270-
// }
271-
// }
272-
273132
let TextView: typeof android.widget.TextView;
274133

275134
const CHILD_SPAN = 'Span';
@@ -368,12 +227,12 @@ class LabelBase extends View implements LabelViewDefinition {
368227
this.requestLayout();
369228
}
370229

371-
eachChild(callback: (child: ViewBase) => boolean): void {
372-
let text = this.formattedText;
373-
if (text) {
374-
callback(text);
375-
}
376-
}
230+
// eachChild(callback: (child: ViewBase) => boolean): void {
231+
// let text = this.formattedText;
232+
// if (text) {
233+
// callback(text);
234+
// }
235+
// }
377236

378237
_setNativeText(reset: boolean = false): void {
379238
//
@@ -424,17 +283,18 @@ export class Label extends LabelBase {
424283
// })();
425284
}
426285

427-
@profile
428-
setHtml(value: string) {
429-
const nativeView = this.nativeViewProtected;
430-
if (value) {
431-
nativeView.setText((com as any).nativescript.label.Font.stringBuilderFromHtmlString(context, fontPath, value));
432-
} else {
433-
nativeView.setText(null);
434-
}
435-
}
286+
// @profile
287+
// setHtml(value: string) {
288+
// const nativeView = this.nativeViewProtected;
289+
// if (value) {
290+
// nativeView.setText((com as any).nativescript.label.Font.stringBuilderFromHtmlString(context, fontPath, value));
291+
// } else {
292+
// nativeView.setText(null);
293+
// }
294+
// }
436295
[htmlProperty.setNative](value: string) {
437-
this.setHtml(value);
296+
// this.setHtml(value);
297+
this._setNativeText();
438298
// textProperty.nativeValueChange(this, value === null || value === undefined ? '' : value.toString());
439299
}
440300

@@ -531,7 +391,8 @@ export class Label extends LabelBase {
531391

532392
[formattedTextProperty.setNative](value: FormattedString) {
533393
// profile('formattedTextProperty', () => {
534-
const nativeView = this.nativeTextViewProtected;
394+
// const nativeView = this.nativeTextViewProtected;
395+
this._setNativeText();
535396
// if (!value) {
536397
// if (nativeView instanceof android.widget.Button && nativeView.getTransformationMethod() instanceof TextTransformation) {
537398
// nativeView.setTransformationMethod(this._defaultTransformationMethod);
@@ -543,10 +404,10 @@ export class Label extends LabelBase {
543404
// return;
544405
// }
545406

546-
const spannableStringBuilder = createSpannableStringBuilder(value);
547-
nativeView.setText(<any>spannableStringBuilder);
407+
// const spannableStringBuilder = createSpannableStringBuilder(value);
408+
// nativeView.setText(<any>spannableStringBuilder);
548409

549-
textProperty.nativeValueChange(this, value === null || value === undefined ? '' : value.toString());
410+
// textProperty.nativeValueChange(this, value === null || value === undefined ? '' : value.toString());
550411

551412
// if (spannableStringBuilder && nativeView instanceof android.widget.Button && !(nativeView.getTransformationMethod() instanceof TextTransformation)) {
552413
// // Replace Android Button's default transformation (in case the developer has not already specified a text-transform) method
@@ -622,13 +483,11 @@ export class Label extends LabelBase {
622483
// }
623484
[colorProperty.setNative](value: Color | android.content.res.ColorStateList) {
624485
// profile('colorProperty', () => {
625-
if (!this.formattedText || !(value instanceof Color)) {
626486
if (value instanceof Color) {
627487
this.nativeTextViewProtected.setTextColor(value.android);
628488
} else {
629489
this.nativeTextViewProtected.setTextColor(value);
630490
}
631-
}
632491
// })();
633492
}
634493

@@ -638,13 +497,11 @@ export class Label extends LabelBase {
638497
// }
639498
[fontSizeProperty.setNative](value: number | { nativeSize: number }) {
640499
// profile('fontSizeProperty', () => {
641-
if (!this.formattedText || typeof value !== 'number') {
642500
if (typeof value === 'number') {
643501
this.nativeTextViewProtected.setTextSize(value);
644502
} else {
645503
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
646504
}
647-
}
648505
// })();
649506
}
650507

@@ -664,7 +521,7 @@ export class Label extends LabelBase {
664521
// }
665522
[fontInternalProperty.setNative](value: Font | android.graphics.Typeface) {
666523
// profile('fontInternalProperty', () => {
667-
if (!this.formattedText || !(value instanceof Font)) {
524+
if (!(value instanceof Font)) {
668525
this.nativeTextViewProtected.setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
669526
}
670527
// })();
@@ -752,8 +609,12 @@ export class Label extends LabelBase {
752609
}
753610

754611
let transformedText: any;
755-
if (this.formattedText) {
612+
if (this.html) {
613+
transformedText = (com as any).nativescript.label.Font.stringBuilderFromHtmlString(context, fontPath, this.html);
614+
textProperty.nativeValueChange(this, this.html === null || this.html === undefined ? '' : this.html);
615+
} else if (this.formattedText) {
756616
transformedText = createSpannableStringBuilder(this.formattedText);
617+
textProperty.nativeValueChange(this, this.formattedText === null || this.formattedText === undefined ? '' : this.formattedText.toString());
757618
} else {
758619
const text = this.text;
759620
const stringValue = text === null || text === undefined ? '' : text.toString();
@@ -842,11 +703,11 @@ export class Label extends LabelBase {
842703
this._resumeNativeUpdates(SuspendType.UISetup);
843704
// })();
844705

845-
this.eachChild(child => {
846-
child._setupUI(context);
706+
// this.eachChild(child => {
707+
// child._setupUI(context);
847708

848-
return true;
849-
});
709+
// return true;
710+
// });
850711
}
851712
}
852713

0 commit comments

Comments
 (0)