Skip to content

Commit 559c5d3

Browse files
authored
Merge pull request #50 from farfromrefug/color_fix
fix colors not working with instance of Color
2 parents 4e6ef6d + d222da2 commit 559c5d3

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

src/InAppBrowser.android.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import Bundle = android.os.Bundle;
33
import TextUtils = android.text.TextUtils;
44
import Intent = android.content.Intent;
55
import Context = android.content.Context;
6-
import Color = android.graphics.Color;
76
import BitmapFactory = android.graphics.BitmapFactory;
87
import Browser = android.provider.Browser;
98
import Pattern = java.util.regex.Pattern;
109
import AssertionError = java.lang.AssertionError;
1110

12-
import { Utils, Application, EventData } from '@nativescript/core';
11+
import { Utils, Application, EventData, Color } from '@nativescript/core';
1312
import {
1413
ChromeTabsEvent,
1514
BROWSER_ACTIVITY_EVENTS,
@@ -39,6 +38,8 @@ import {
3938
closeAuthSessionPolyfillAsync,
4039
} from './utils.android';
4140

41+
import { parseColor } from './utils.common';
42+
4243
declare let global: any;
4344

4445
let InAppBrowserModuleInstance: InAppBrowserClassMethods;
@@ -101,20 +102,26 @@ function setup() {
101102
const inAppBrowserOptions = getDefaultOptions(url, options);
102103

103104
const builder = new CustomTabsIntent.Builder();
104-
if (inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR]) {
105-
const colorString = inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR];
105+
let colorString = inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR];
106+
if (colorString) {
106107
try {
107-
builder.setToolbarColor(Color.parseColor(colorString));
108-
this.isLightTheme = toolbarIsLight(colorString);
108+
const color = parseColor(colorString);
109+
if (color) {
110+
builder.setToolbarColor(color.android);
111+
this.isLightTheme = toolbarIsLight(color.android);
112+
}
109113
} catch (error) {
110114
throw new Error(
111115
"Invalid toolbar color '" + colorString + "': " + error.message);
112116
}
113117
}
114-
if (inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR]) {
115-
const colorString = inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR];
118+
colorString = inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR];
119+
if (colorString) {
116120
try {
117-
builder.setSecondaryToolbarColor(Color.parseColor(colorString));
121+
const color = parseColor(colorString);
122+
if (color) {
123+
builder.setSecondaryToolbarColor(color.android);
124+
}
118125
} catch (error) {
119126
throw new Error(
120127
"Invalid secondary toolbar color '" + colorString + "': " + error.message);

src/InAppBrowser.common.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Color } from "@nativescript/core";
2+
13
export interface RedirectEvent {
24
url: 'string';
35
}
@@ -14,8 +16,8 @@ export interface RedirectResult {
1416

1517
type InAppBrowseriOSOptions = {
1618
dismissButtonStyle?: 'done' | 'close' | 'cancel',
17-
preferredBarTintColor?: string,
18-
preferredControlTintColor?: string,
19+
preferredBarTintColor?: string | Color,
20+
preferredControlTintColor?: string | Color,
1921
readerMode?: boolean,
2022
animated?: boolean,
2123
modalPresentationStyle?:
@@ -48,8 +50,8 @@ export type Animations = {
4850

4951
type InAppBrowserAndroidOptions = {
5052
showTitle?: boolean,
51-
toolbarColor?: string,
52-
secondaryToolbarColor?: string,
53+
toolbarColor?: string | Color,
54+
secondaryToolbarColor?: string | Color,
5355
enableUrlBarHiding?: boolean,
5456
enableDefaultShare?: boolean,
5557
forceCloseOnRedirection?: boolean,

src/InAppBrowser.ios.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Color, Utils } from '@nativescript/core';
2+
import { parseColor } from './utils.common';
23

34
import {
45
BrowserResult,
@@ -98,10 +99,16 @@ function setup() {
9899

99100
if (Utils.ios.MajorVersion >= 10) {
100101
if (inAppBrowserOptions.preferredBarTintColor) {
101-
this.safariVC.preferredBarTintColor = new Color(inAppBrowserOptions.preferredBarTintColor).ios;
102+
const color = parseColor(inAppBrowserOptions.preferredBarTintColor);
103+
if (color) {
104+
this.safariVC.preferredBarTintColor = color.ios;
105+
}
102106
}
103107
if (inAppBrowserOptions.preferredControlTintColor) {
104-
this.safariVC.preferredControlTintColor = new Color(inAppBrowserOptions.preferredControlTintColor).ios;
108+
const color = parseColor(inAppBrowserOptions.preferredBarTintColor);
109+
if (color) {
110+
this.safariVC.preferredControlTintColor = color.ios;
111+
}
105112
}
106113
}
107114

src/utils.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ export function getPreferredPackages(context: Context): List<ResolveInfo> {
156156
return resolveInfos;
157157
}
158158

159-
export function toolbarIsLight(themeColor: string): boolean {
160-
return ColorUtils.calculateLuminance(Color.parseColor(themeColor)) > 0.5;
159+
export function toolbarIsLight(themeColor: number): boolean {
160+
return ColorUtils.calculateLuminance(themeColor) > 0.5;
161161
}
162162

163163
export function getDefaultBrowser(context: Context): string {

src/utils.common.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Color } from "@nativescript/core";
2+
3+
export function parseColor(color: string | Color) {
4+
if (color && !(color instanceof Color)) {
5+
return new Color(color);
6+
}
7+
return color as Color;
8+
}

0 commit comments

Comments
 (0)