Skip to content

Commit 7fbac96

Browse files
committed
refactoring
1 parent 06281ad commit 7fbac96

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/InAppBrowser.android.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import {
3838
closeAuthSessionPolyfillAsync,
3939
} from './utils.android';
4040

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

4345
let InAppBrowserModuleInstance: InAppBrowserClassMethods;
@@ -100,25 +102,30 @@ function setup() {
100102
const inAppBrowserOptions = getDefaultOptions(url, options);
101103

102104
const builder = new CustomTabsIntent.Builder();
103-
if (inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR]) {
104-
const colorString = inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR];
105-
const color = colorString instanceof Color ? colorString : new Color(colorString);
106-
try {
107-
builder.setToolbarColor(color.android);
108-
this.isLightTheme = toolbarIsLight(color.android);
109-
} catch (error) {
110-
throw new Error(
111-
"Invalid toolbar color '" + colorString + "': " + error.message);
105+
let colorString = inAppBrowserOptions[InAppBrowserModule.KEY_TOOLBAR_COLOR];
106+
if (colorString) {
107+
const color = parseColor(colorString);
108+
if (color) {
109+
try {
110+
builder.setToolbarColor(color.android);
111+
this.isLightTheme = toolbarIsLight(color.android);
112+
} catch (error) {
113+
throw new Error(
114+
"Invalid toolbar color '" + colorString + "': " + error.message);
115+
}
112116
}
117+
113118
}
114-
if (inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR]) {
115-
const colorString = inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR];
116-
const color = colorString instanceof Color ? colorString : new Color(colorString);
117-
try {
118-
builder.setSecondaryToolbarColor(color.android);
119-
} catch (error) {
120-
throw new Error(
121-
"Invalid secondary toolbar color '" + colorString + "': " + error.message);
119+
colorString = inAppBrowserOptions[InAppBrowserModule.KEY_SECONDARY_TOOLBAR_COLOR];
120+
if (colorString) {
121+
const color = parseColor(colorString);
122+
if (color) {
123+
try {
124+
builder.setSecondaryToolbarColor(color.android);
125+
} catch (error) {
126+
throw new Error(
127+
"Invalid secondary toolbar color '" + colorString + "': " + error.message);
128+
}
122129
}
123130
}
124131

src/InAppBrowser.ios.ts

Lines changed: 9 additions & 5 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,13 +99,16 @@ function setup() {
9899

99100
if (Utils.ios.MajorVersion >= 10) {
100101
if (inAppBrowserOptions.preferredBarTintColor) {
101-
const color = inAppBrowserOptions.preferredBarTintColor instanceof Color ? inAppBrowserOptions.preferredBarTintColor : new Color(inAppBrowserOptions.preferredBarTintColor);
102-
103-
this.safariVC.preferredBarTintColor = color.ios;
102+
const color = parseColor(inAppBrowserOptions.preferredBarTintColor);
103+
if (color) {
104+
this.safariVC.preferredBarTintColor = color.ios;
105+
}
104106
}
105107
if (inAppBrowserOptions.preferredControlTintColor) {
106-
const color = inAppBrowserOptions.preferredControlTintColor instanceof Color ? inAppBrowserOptions.preferredControlTintColor : new Color(inAppBrowserOptions.preferredControlTintColor);
107-
this.safariVC.preferredControlTintColor = color.ios;
108+
const color = parseColor(inAppBrowserOptions.preferredBarTintColor);
109+
if (color) {
110+
this.safariVC.preferredControlTintColor = color.ios;
111+
}
108112
}
109113
}
110114

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)