Merge branch 'master' into move-tns-core-modules-widgets

This commit is contained in:
Svetoslav
2019-03-20 18:16:08 +02:00
committed by GitHub
23 changed files with 234 additions and 178 deletions

View File

@@ -8,7 +8,7 @@
</ActionBar>
</Page.actionBar>
<StackLayout>
<Button text="go to cleared page" tap="navigate"/>
<Button text="go to cleared page" automationText="goToClearedPage" tap="navigate"/>
<Button text="undefined" tap="onChangeRenderingMode"/>
</StackLayout>
</Page>

View File

@@ -8,7 +8,7 @@
</ActionBar>
</Page.actionBar>
<StackLayout>
<Button text="go to cleared page" tap="navigate"/>
<Button text="go to cleared page" automationText="goToClearedPage" tap="navigate"/>
<Button text="undefined" tap="onChangeRenderingMode"/>
</StackLayout>
</Page>

View File

@@ -1,17 +0,0 @@
import { EventData } from "tns-core-modules/data/observable";
import { SubMainPageViewModel } from "../sub-main-page-view-model";
import { WrapLayout } from "tns-core-modules/ui/layouts/wrap-layout";
import { Page } from "tns-core-modules/ui/page";
export function pageLoaded(args: EventData) {
const page = <Page>args.object;
const wrapLayout = <WrapLayout>page.getViewById("wrapLayoutWithExamples");
page.bindingContext = new SubMainPageViewModel(wrapLayout, loadExamples());
}
export function loadExamples() {
const examples = new Map<string, string>();
examples.set("open-file", "intent/open-file");
return examples;
}

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Page loaded="pageLoaded">
<ScrollView orientation="vertical" row="1">
<WrapLayout id="wrapLayoutWithExamples"/>
</ScrollView>
</Page>

View File

@@ -1,5 +0,0 @@
<Page navigatingTo="navigatingTo">
<GridLayout id="root">
<Button text="Open File" tap="openFile" height="40"></Button>
</GridLayout>
</Page>

View File

@@ -30,6 +30,7 @@ export function loadExamples() {
examples.set("ng-repo-1599", "issues/issue-ng-repo-1599");
examples.set("ng-repo-1626", "issues/issue-ng-repo-1626");
examples.set("6439", "issues/issue-6439");
examples.set("open-file-6895", "issues/open-file-6895")
return examples;
}

View File

@@ -0,0 +1,5 @@
<Page navigatingTo="navigatingTo">
<GridLayout id="root">
<Button text="Open File" automationText="openFile" tap="openFile" height="40"></Button>
</GridLayout>
</Page>

View File

@@ -1,19 +1,36 @@
export function onOuterWrapLayoutTap() {
console.log("on outer wrap layout tap");
}
import { EventData, Page } from "tns-core-modules/ui/page/page";
import { Label } from "tns-core-modules/ui/label/label";
export function onStackLayoutThrowTap() {
throw new Error("Should not tap layout with IsPassThroughParentEnabled=true");
}
export function onUserInteractionDisabledTap() {
throw new Error("Should not tap button with IsUserInteractionEnabled=false");
}
export function onDisabledThrowTap() {
throw new Error("Should not tap button with IsEnabled=false");
}
export function onTap() {
const setLabelTextAndLog = (args, text: string) => {
const page = <Page>args.object.page;
const label = <Label>page.getViewById("resultLabel");
label.set("text", text);
console.log("on button tap");
}
}
export function onTap(args: EventData) {
setLabelTextAndLog(args, "onButtonTapResult");
}
export function clearResult(args: EventData) {
setLabelTextAndLog(args, "none");
}
export function onOuterWrapLayoutTap(args) {
setLabelTextAndLog(args, "onOuterWrapLayoutTapResult");
}
export function onStackLayoutThrowTap(args: EventData) {
setLabelTextAndLog(args, "Should not tap layout with IsPassThroughParentEnabled=true");
// throw new Error("Should not tap layout with IsPassThroughParentEnabled=true");
}
export function onUserInteractionDisabledTap(args: EventData) {
setLabelTextAndLog(args, "Should not tap button with IsUserInteractionEnabled=false");
// throw new Error("Should not tap button with IsUserInteractionEnabled=false");
}
export function onDisabledThrowTap(args: EventData) {
setLabelTextAndLog(args, "Should not tap button with IsEnabled=false");
// throw new Error("Should not tap button with IsEnabled=false");
}

View File

@@ -1,24 +1,28 @@
<Page class="page" actionBarHidden="true" xmlns="http://schemas.nativescript.org/tns.xsd">
<Page class="page" actionBarHidden="true"
xmlns="http://schemas.nativescript.org/tns.xsd">
<WrapLayout tap="onOuterWrapLayoutTap" backgroundColor="#bed3f4">
<StackLayout tap="onStackLayoutThrowTap" backgroundColor="#f3f9db" height="200" isPassThroughParentEnabled="true">
<Label text="isPassThroughParentEnabled='true'" isUserInteractionEnabled="false" />
<Button isUserInteractionEnabled="false" tap="onUserInteractionDisabledThrowTap" text="isUserInteractionEnabled='false'"></Button>
<Button isEnabled="false" tap="onDisabledThrowTap" text="isEnabled='false'"></Button>
<Button tap="onTap" text="TAP"></Button>
<WrapLayout tap="onOuterWrapLayoutTap" automationText="onOuterWrapLayoutTap" backgroundColor="#bed3f4" padding="5">
<StackLayout tap="onStackLayoutThrowTap" automationText="stackLayout1" backgroundColor="#f3f9db" height="180" isPassThroughParentEnabled="true">
<Label automationText="label1" text="isPassThroughParentEnabled='true'" isUserInteractionEnabled="false" />
<Button automationText="onUserInteractionDisabledThrowTap1" isUserInteractionEnabled="false" tap="onUserInteractionDisabledThrowTap" text="isUserInteractionEnabled='false'"></Button>
<Button automationText="onDisabledThrowTap1" isEnabled="false" tap="onDisabledThrowTap" text="isEnabled='false'"></Button>
<Button tap="onTap" text="TAP" automationText="btn1"></Button>
</StackLayout>
<StackLayout tap="onStackLayoutThrowTap" style.margin="20" height="300" width="300" backgroundColor="#f5edf7" isPassThroughParentEnabled="true">
<Label text="isPassThroughParentEnabled='true'" isUserInteractionEnabled="false" />
<StackLayout tap="onStackLayoutThrowTap" backgroundColor="#f3f9db" height="200" isPassThroughParentEnabled="true">
<Label text="isPassThroughParentEnabled='true'" isUserInteractionEnabled="false" />
<Button isUserInteractionEnabled="false" tap="onUserInteractionDisabledThrowTap" text="isUserInteractionEnabled='false'"></Button>
<Button isEnabled="false" tap="onDisabledThrowTap" text="isEnabled='false'"></Button>
<Button tap="onTap" text="TAP"></Button>
<StackLayout tap="onStackLayoutThrowTap" automationText="stackLayout2" style.margin="20" height="250" width="300" backgroundColor="#f5edf7" isPassThroughParentEnabled="true">
<Label automationText="label2" text="isPassThroughParentEnabled='true'" isUserInteractionEnabled="false" />
<StackLayout tap="onStackLayoutThrowTap" automationText="stackLayout3" backgroundColor="#f3f9db" height="200" isPassThroughParentEnabled="true">
<Label automationText="label3" text="isPassThroughParentEnabled='true'" isUserInteractionEnabled="false" />
<Button automationText="onUserInteractionDisabledThrowTap2" isUserInteractionEnabled="false" tap="onUserInteractionDisabledThrowTap" text="isUserInteractionEnabled='false'"></Button>
<Button automationText="onDisabledThrowTap2" isEnabled="false" tap="onDisabledThrowTap" text="isEnabled='false'"></Button>
<Button automationText="btn2" tap="onTap" text="TAP"></Button>
</StackLayout>
</StackLayout>
</WrapLayout>
<Button text="x" automationText="clearResult" tap="clearResult" height="35" width="40" padding="0" />
<StackLayout>
<Label id="resultLabel" textWrap="true" />
</StackLayout>
</WrapLayout>
</Page>

View File

@@ -36,7 +36,6 @@ export function pageLoaded(args: EventData) {
examples.set("progress-bar", "progress-bar/main-page");
examples.set("date-picker", "date-picker/date-picker");
examples.set("nested-frames", "nested-frames/main-page");
examples.set("intent", "intent/main-page");
page.bindingContext = new MainPageViewModel(wrapLayout, examples);
const parent = page.getViewById("parentLayout");

View File

@@ -2,16 +2,16 @@
<ScrollView id="scroll-view">
<StackLayout>
<GridLayout rows="auto, auto, auto" backgroundColor="lightgray">
<Button row="0" text="Change w/ Visibility" tap="{{ onChangeVisibility }}"></Button>
<Button row="1" text="Scroll To Bottom" tap="{{ onScrollToBottom }}"></Button>
<Button row="0" text="Change w/ Visibility" automationText="changeVisibilityTop" tap="{{ onChangeVisibility }}"></Button>
<Button row="1" text="Scroll To Bottom" automationText="scrollToBottom" tap="{{ onScrollToBottom }}"></Button>
<Label row="2" visibility="{{ isVisible ? 'visible' : 'collapsed' }}" text="{{ content }}" color="black"></Label>
</GridLayout>
<GridLayout height="2000" backgroundColor="yellow"></GridLayout>
<GridLayout rows="auto, auto, auto" backgroundColor="lightgray">
<Button row="0" text="Change w/ Visibility" tap="{{ onChangeVisibility }}"></Button>
<Button row="1" text="Scroll To Top" tap="{{ onScrollToTop }}"></Button>
<Button row="0" text="Change w/ Visibility" automationText="changeVisibilityBottom" tap="{{ onChangeVisibility }}"></Button>
<Button row="1" text="Scroll To Top" automationText="scrollToTop" tap="{{ onScrollToTop }}"></Button>
<Label row="2" visibility="{{ isVisible ? 'visible' : 'collapsed' }}" text="{{ content }}" color="black"></Label>
</GridLayout>
</StackLayout>

View File

@@ -1,21 +1,21 @@
<Page>
<TabView style="color: green;">
<TabView.items>
<TabViewItem title="Title" iconSource="res://add_to_fav">
<TabViewItem title="Title" automationText="tab1" iconSource="res://add_to_fav">
<TabViewItem.view>
<GridLayout>
<Label text="Title and icon" verticalAlignment="center" horizontalAlignment="center"/>
</GridLayout>
</TabViewItem.view>
</TabViewItem>
<TabViewItem iconSource="res://add_to_fav">
<TabViewItem iconSource="res://add_to_fav" automationText="tab2" >
<TabViewItem.view>
<GridLayout>
<Label text="Only icon" verticalAlignment="center" horizontalAlignment="center"/>
</GridLayout>
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Title">
<TabViewItem title="Title" automationText="tab3">
<TabViewItem.view>
<GridLayout>
<Label text="Only title" verticalAlignment="center" horizontalAlignment="center"/>

View File

@@ -7,7 +7,7 @@
<TabView id="tabViewContainer">
<TabView.items>
<TabViewItem title="Tab 1">
<TabViewItem title="Tab1" >
<TabViewItem.view>
<GridLayout rows="*, auto" columns="*">
<StackLayout row="0" backgroundColor="lightblue"></StackLayout>
@@ -15,7 +15,7 @@
</GridLayout>
</TabViewItem.view>
</TabViewItem>
<TabViewItem title="Tab 2">
<TabViewItem title="Tab2" >
<TabViewItem.view>
<GridLayout rows="*, auto" columns="*">
<StackLayout row="0" backgroundColor="lightblue"></StackLayout>

View File

@@ -38,6 +38,11 @@ export function onNavigateToTabsBottomRoot(args: EventData) {
application._resetRootView({ moduleName: "tab-root/tab-bottom-root" });
}
export function onNavigateToSomePage(args: EventData) {
const button = <Button>args.object;
button.page.frame.navigate("some-page/some-page");
}
export function onFrameToNestedFrame(args: EventData) {
const button = <Button>args.object;
button.page.frame.navigate({

View File

@@ -14,6 +14,7 @@
<Button text="Page w/ tabs (bottom)" tap="onNavigateToTabsBottomPage" />
<Button text="Root tabs (top)" tap="onNavigateToTabsTopRoot" />
<Button text="Root tabs (bottom)" tap="onNavigateToTabsBottomRoot" />
<Button text="Some page on root" automationText="somePageOnRoot" tap="onNavigateToSomePage" />
<Button text="Frame to NestedFrame (non-default transition)" tap="onFrameToNestedFrame" />
</StackLayout>
</GridLayout>

View File

@@ -0,0 +1,55 @@
import { AppiumDriver, createDriver } from "nativescript-dev-appium";
import { Screen } from "./screen";
import { suspendTime, dontKeepActivities } from "./config";
describe("issues", () => {
let driver: AppiumDriver;
let screen: Screen;
before(async () => {
driver = await createDriver();
screen = new Screen(driver);
if (dontKeepActivities) {
await driver.setDontKeepActivities(true);
}
});
after(async () => {
if (dontKeepActivities) {
await driver.setDontKeepActivities(false);
}
await driver.quit();
console.log("Quit driver!");
});
afterEach(async function () {
if (this.currentTest.state === "failed") {
await driver.logTestArtifacts(this.currentTest.title);
await driver.resetApp();
}
});
it("issue-6488", async () => {
await screen.loadedHome();
const showSomePage = async () => {
const somePageBtn = await driver.waitForElement("somePageOnRoot");
await somePageBtn.tap();
await screen.loadedSomePage();
}
await showSomePage();
await driver.navBack();
await screen.loadedHome();
await driver.backgroundApp(suspendTime);
await showSomePage();
await driver.navBack();
await showSomePage();
await driver.navBack();
await screen.loadedHome();
});
});

View File

@@ -278,7 +278,7 @@ export class Screen {
loadedHome = async () => {
const lblHome = await this._driver.waitForElement(home);
assert.isNotNull(lblHome);
assert.isDefined(lblHome);
console.log(home + " loaded!");
};
@@ -329,7 +329,7 @@ export class Screen {
loadedPlayersList = async () => {
const lblPlayerOne = await this._driver.waitForElement(playersData["playerOneDefault"].name);
assert.isNotNull(lblPlayerOne);
assert.isDefined(lblPlayerOne);
console.log(players + " loaded!");
}
@@ -339,7 +339,7 @@ export class Screen {
loadedTeamsList = async () => {
const lblTeamOne = await this._driver.waitForElement(teamsData["teamOneDefault"].name);
assert.isNotNull(lblTeamOne);
assert.isDefined(lblTeamOne);
console.log(teams + " loaded!");
}
@@ -349,7 +349,7 @@ export class Screen {
loadedElement = async (element: string) => {
const el = await this._driver.waitForElement(element);
assert.isNotNull(el);
assert.isDefined(el);
console.log(`${element} loaded!`);
};
@@ -361,7 +361,7 @@ export class Screen {
private loadedPage = async (page: string) => {
const lblPage = await this._driver.waitForElement(page);
assert.isNotNull(lblPage);
assert.isDefined(lblPage);
console.log(page + " loaded!");
};
@@ -373,10 +373,10 @@ export class Screen {
private loadedItem = async (item: Item) => {
const lblItemName = await this._driver.waitForElement(item.name);
assert.isNotNull(lblItemName);
assert.isDefined(lblItemName);
const lblItemDescription = await this._driver.waitForElement(item.description);
assert.isNotNull(lblItemDescription);
assert.isDefined(lblItemDescription);
console.log(item.name + " loaded!");
}

View File

@@ -158,9 +158,9 @@ export var test_getJSON_fail_when_result_is_not_JSONP = function (done) {
export var test_gzip_request_explicit = function(done) {
var result;
http.request({
url: "https://postman-echo.com/gzip",
method: "GET",
http.request({
url: "https://postman-echo.com/gzip",
method: "GET",
headers: {
"Accept-Encoding": "gzip"
}}).then(function (r) {
@@ -180,8 +180,8 @@ export var test_gzip_request_explicit = function(done) {
export var test_gzip_request_implicit = function(done) {
var result;
http.request({
url: "https://postman-echo.com/gzip",
http.request({
url: "https://postman-echo.com/gzip",
method: "GET"}).then(function (r) {
result = r;
try {
@@ -524,6 +524,24 @@ export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = fu
done(e);
});
};
export var test_request_responseContentToFileFromUrlShouldReturnCorrectFileAndCreateDirPathIfNecesary = function (done) {
var result;
http.request({ url: "https://raw.githubusercontent.com/NativeScript/NativeScript/master/tests/app/logo.png", method: "GET" }).then(function (response) {
const filePath = fs.path.join(fs.knownFolders.temp().path, "test", "some", "path", "logo.png");
result = response.content.toFile(filePath);
try {
TKUnit.assert(result instanceof fs.File, "Result from toFile() should be valid File object!");
TKUnit.assert(result.size > 0, "result from to file should be greater than 0 in size");
done(null);
}
catch (err) {
done(err);
}
}, function (e) {
done(e);
});
};
export var test_request_responseContentToFileFromContentShouldReturnCorrectFile = function (done) {
var result;

View File

@@ -6,10 +6,10 @@
"nativescript": {
"id": "org.nativescript.UnitTestApp",
"tns-ios": {
"version": "5.1.0"
"version": "5.2.0"
},
"tns-android": {
"version": "5.1.0"
"version": "5.2.1"
}
},
"dependencies": {

View File

@@ -163,6 +163,9 @@ export function _resetRootView(entry?: NavigationEntry | string) {
createRootFrame.value = false;
mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
const callbacks: AndroidActivityCallbacks = activity[CALLBACKS];
if (!callbacks) {
throw new Error("Cannot find android activity callbacks.");
}
callbacks.resetActivityContent(activity);
}

View File

@@ -1,9 +1,9 @@
/**
* Android specific http request implementation.
*/
import * as imageSourceModule from "../../image-source";
import * as platformModule from "../../platform";
import * as fsModule from "../../file-system";
import { fromNativeSource } from "../../image-source";
import { screen } from "../../platform";
import { File } from "../../file-system";
import { getFilenameFromUrl } from "./http-request-common";
// this is imported for definition purposes only
@@ -17,7 +17,7 @@ export enum HttpResponseEncoding {
}
function parseJSON(source: string): any {
var src = source.trim();
const src = source.trim();
if (src.lastIndexOf(")") === src.length - 1) {
return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")")));
}
@@ -25,24 +25,10 @@ function parseJSON(source: string): any {
return JSON.parse(src);
}
var requestIdCounter = 0;
var pendingRequests = {};
let requestIdCounter = 0;
const pendingRequests = {};
var imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require("image-source");
}
}
var platform: typeof platformModule;
function ensurePlatform() {
if (!platform) {
platform = require("platform");
}
}
var completeCallback: org.nativescript.widgets.Async.CompleteCallback;
let completeCallback: org.nativescript.widgets.Async.CompleteCallback;
function ensureCompleteCallback() {
if (completeCallback) {
return;
@@ -60,7 +46,7 @@ function ensureCompleteCallback() {
}
function onRequestComplete(requestId: number, result: org.nativescript.widgets.Async.Http.RequestResult) {
var callbacks = pendingRequests[requestId];
const callbacks = pendingRequests[requestId];
delete pendingRequests[requestId];
if (result.error) {
@@ -69,13 +55,12 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
}
// read the headers
var headers: http.Headers = {};
const headers: http.Headers = {};
if (result.headers) {
var jHeaders = result.headers;
var length = jHeaders.size();
var i;
var pair: org.nativescript.widgets.Async.Http.KeyValuePair;
for (i = 0; i < length; i++) {
const jHeaders = result.headers;
const length = jHeaders.size();
let pair: org.nativescript.widgets.Async.Http.KeyValuePair;
for (let i = 0; i < length; i++) {
pair = jHeaders.get(i);
addHeader(headers, pair.key, pair.value);
}
@@ -112,11 +97,9 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
return parseJSON(str);
},
toImage: () => {
ensureImageSource();
return new Promise<any>((resolveImage, rejectImage) => {
if (result.responseAsImage != null) {
resolveImage(imageSource.fromNativeSource(result.responseAsImage));
resolveImage(fromNativeSource(result.responseAsImage));
}
else {
rejectImage(new Error("Response content may not be converted to an Image"));
@@ -124,17 +107,19 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
});
},
toFile: (destinationFilePath: string) => {
var fs: typeof fsModule = require("file-system");
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(callbacks.url);
}
var stream: java.io.FileOutputStream;
let stream: java.io.FileOutputStream;
try {
var javaFile = new java.io.File(destinationFilePath);
// ensure destination path exists by creating any missing parent directories
const file = File.fromPath(destinationFilePath);
const javaFile = new java.io.File(destinationFilePath);
stream = new java.io.FileOutputStream(javaFile);
stream.write(result.raw.toByteArray());
return fs.File.fromPath(destinationFilePath);
return file;
}
catch (exception) {
throw new Error(`Cannot save file with path: ${destinationFilePath}.`);
@@ -152,7 +137,7 @@ function onRequestComplete(requestId: number, result: org.nativescript.widgets.A
}
function onRequestError(error: string, requestId: number) {
var callbacks = pendingRequests[requestId];
const callbacks = pendingRequests[requestId];
delete pendingRequests[requestId];
if (callbacks) {
callbacks.rejectCallback(new Error(error));
@@ -164,7 +149,7 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
throw new Error("Http request must provide a valid url.");
}
var javaOptions = new org.nativescript.widgets.Async.Http.RequestOptions();
const javaOptions = new org.nativescript.widgets.Async.Http.RequestOptions();
javaOptions.url = options.url;
@@ -182,22 +167,19 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
}
if (options.headers) {
var arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>();
var pair = org.nativescript.widgets.Async.Http.KeyValuePair;
const arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>();
const pair = org.nativescript.widgets.Async.Http.KeyValuePair;
for (var key in options.headers) {
for (let key in options.headers) {
arrayList.add(new pair(key, options.headers[key] + ""));
}
javaOptions.headers = arrayList;
}
ensurePlatform();
// pass the maximum available image size to the request options in case we need a bitmap conversion
var screen = platform.screen.mainScreen;
javaOptions.screenWidth = screen.widthPixels;
javaOptions.screenHeight = screen.heightPixels;
javaOptions.screenWidth = screen.mainScreen.widthPixels;
javaOptions.screenHeight = screen.mainScreen.heightPixels;
return javaOptions;
}
@@ -211,7 +193,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
return new Promise<http.HttpResponse>((resolve, reject) => {
try {
// initialize the options
var javaOptions = buildJavaOptions(options);
const javaOptions = buildJavaOptions(options);
// send request data to network debugger
if (global.__inspector && global.__inspector.isConnected) {
@@ -219,7 +201,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
}
// remember the callbacks so that we can use them when the CompleteCallback is called
var callbacks = {
const callbacks = {
url: options.url,
resolveCallback: resolve,
rejectCallback: reject
@@ -252,7 +234,7 @@ export function addHeader(headers: http.Headers, key: string, value: string): vo
} else if (Array.isArray(headers[key])) {
(<string[]>headers[key]).push(value);
} else {
let values: string[] = [<string>headers[key]];
const values: string[] = [<string>headers[key]];
values.push(value);
headers[key] = values;
}

View File

@@ -2,10 +2,10 @@
* iOS specific http request implementation.
*/
import * as http from "../../http";
import { HttpRequestOptions, HttpResponse, Headers } from "../../http";
import * as types from "../../utils/types";
import * as imageSourceModule from "../../image-source";
import * as fsModule from "../../file-system";
import { fromNativeSource } from "../../image-source";
import { File } from "../../file-system";
import * as utils from "../../utils/utils";
import getter = utils.ios.getter;
@@ -18,18 +18,18 @@ export enum HttpResponseEncoding {
GBK
}
var currentDevice = utils.ios.getter(UIDevice, UIDevice.currentDevice);
var device = currentDevice.userInterfaceIdiom === UIUserInterfaceIdiom.Phone ? "Phone" : "Pad";
var osVersion = currentDevice.systemVersion;
const currentDevice = getter(UIDevice, UIDevice.currentDevice);
const device = currentDevice.userInterfaceIdiom === UIUserInterfaceIdiom.Phone ? "Phone" : "Pad";
const osVersion = currentDevice.systemVersion;
var GET = "GET";
var USER_AGENT_HEADER = "User-Agent";
var USER_AGENT = `Mozilla/5.0 (i${device}; CPU OS ${osVersion.replace(".", "_")} like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/${osVersion} Mobile/10A5355d Safari/8536.25`;
var sessionConfig = getter(NSURLSessionConfiguration, NSURLSessionConfiguration.defaultSessionConfiguration);
var queue = getter(NSOperationQueue, NSOperationQueue.mainQueue);
const GET = "GET";
const USER_AGENT_HEADER = "User-Agent";
const USER_AGENT = `Mozilla/5.0 (i${device}; CPU OS ${osVersion.replace(".", "_")} like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/${osVersion} Mobile/10A5355d Safari/8536.25`;
const sessionConfig = getter(NSURLSessionConfiguration, NSURLSessionConfiguration.defaultSessionConfiguration);
const queue = getter(NSOperationQueue, NSOperationQueue.mainQueue);
function parseJSON(source: string): any {
var src = source.trim();
const src = source.trim();
if (src.lastIndexOf(")") === src.length - 1) {
return JSON.parse(src.substring(src.indexOf("(") + 1, src.lastIndexOf(")")));
}
@@ -43,31 +43,24 @@ class NSURLSessionTaskDelegateImpl extends NSObject implements NSURLSessionTaskD
completionHandler(null);
}
}
var sessionTaskDelegateInstance: NSURLSessionTaskDelegateImpl = <NSURLSessionTaskDelegateImpl>NSURLSessionTaskDelegateImpl.new();
const sessionTaskDelegateInstance: NSURLSessionTaskDelegateImpl = <NSURLSessionTaskDelegateImpl>NSURLSessionTaskDelegateImpl.new();
var defaultSession;
let defaultSession;
function ensureDefaultSession() {
if (!defaultSession) {
defaultSession = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
}
}
var sessionNotFollowingRedirects;
let sessionNotFollowingRedirects;
function ensureSessionNotFollowingRedirects() {
if (!sessionNotFollowingRedirects) {
sessionNotFollowingRedirects = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, sessionTaskDelegateInstance, queue);
}
}
var imageSource: typeof imageSourceModule;
function ensureImageSource() {
if (!imageSource) {
imageSource = require("image-source");
}
}
export function request(options: http.HttpRequestOptions): Promise<http.HttpResponse> {
return new Promise<http.HttpResponse>((resolve, reject) => {
export function request(options: HttpRequestOptions): Promise<HttpResponse> {
return new Promise<HttpResponse>((resolve, reject) => {
if (!options.url) {
reject(new Error("Request url was empty."));
@@ -75,10 +68,10 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
}
try {
var network = domainDebugger.getNetwork();
var debugRequest = network && network.create();
const network = domainDebugger.getNetwork();
const debugRequest = network && network.create();
var urlRequest = NSMutableURLRequest.requestWithURL(
const urlRequest = NSMutableURLRequest.requestWithURL(
NSURL.URLWithString(options.url));
urlRequest.HTTPMethod = types.isDefined(options.method) ? options.method : GET;
@@ -86,7 +79,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
urlRequest.setValueForHTTPHeaderField(USER_AGENT, USER_AGENT_HEADER);
if (options.headers) {
for (var header in options.headers) {
for (let header in options.headers) {
urlRequest.setValueForHTTPHeaderField(options.headers[header] + "", header);
}
}
@@ -99,7 +92,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
urlRequest.timeoutInterval = options.timeout / 1000;
}
var session;
let session;
if (types.isBoolean(options.dontFollowRedirects) && options.dontFollowRedirects) {
ensureSessionNotFollowingRedirects();
session = sessionNotFollowingRedirects;
@@ -108,14 +101,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
session = defaultSession;
}
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
const dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
if (error) {
reject(new Error(error.localizedDescription));
} else {
var headers: http.Headers = {};
const headers: Headers = {};
if (response && response.allHeaderFields) {
var headerFields = response.allHeaderFields;
const headerFields = response.allHeaderFields;
headerFields.enumerateKeysAndObjectsUsingBlock((key, value, stop) => {
addHeader(headers, key, value);
@@ -125,7 +118,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
if (debugRequest) {
debugRequest.mimeType = response.MIMEType;
debugRequest.data = data;
var debugResponse = {
const debugResponse = {
url: options.url,
status: response.statusCode,
statusText: NSHTTPURLResponse.localizedStringForStatusCode(response.statusCode),
@@ -143,26 +136,27 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
toString: (encoding?: any) => NSDataToString(data, encoding),
toJSON: (encoding?: any) => parseJSON(NSDataToString(data, encoding)),
toImage: () => {
ensureImageSource();
return new Promise((resolve, reject) => {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
if (image) {
resolve(imageSource.fromNativeSource(image))
resolve(fromNativeSource(image));
} else {
reject(new Error("Response content may not be converted to an Image"));
}
});
});
},
toFile: (destinationFilePath?: string) => {
var fs: typeof fsModule = require("file-system");
toFile: (destinationFilePath?: string) => {
if (!destinationFilePath) {
destinationFilePath = getFilenameFromUrl(options.url);
}
if (data instanceof NSData) {
// ensure destination path exists by creating any missing parent directories
const file = File.fromPath(destinationFilePath);
data.writeToFileAtomically(destinationFilePath, true);
return fs.File.fromPath(destinationFilePath);
return file;
} else {
reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
}
@@ -175,7 +169,7 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
});
if (options.url && debugRequest) {
var request = {
const request = {
url: options.url,
method: "GET",
headers: options.headers
@@ -198,13 +192,13 @@ function NSDataToString(data: any, encoding?: HttpResponseEncoding): string {
return NSString.alloc().initWithDataEncoding(data, code).toString();
}
export function addHeader(headers: http.Headers, key: string, value: string): void {
export function addHeader(headers: Headers, key: string, value: string): void {
if (!headers[key]) {
headers[key] = value;
} else if (Array.isArray(headers[key])) {
(<string[]>headers[key]).push(value);
} else {
let values: string[] = [<string>headers[key]];
const values: string[] = [<string>headers[key]];
values.push(value);
headers[key] = values;
}