Change image.android to use the new image.Cache class from widgets. (#2832)

* Change image.android to use the new image.Cahce class from widgets.

* fix npm scripts

* npm tsc will compile all so no need for npm run dev-tsc-tests

* fix tslint error

* image-tests use memory only cache.

* fix exception in image.android

* Change image-tests so that Image won't be GC immediately.

* Change cacheMode to enum
This commit is contained in:
Hristo Hristov
2016-10-05 13:09:08 +03:00
committed by GitHub
parent 3b99ed3974
commit 98deeccb21
14 changed files with 336 additions and 206 deletions

151
.vscode/launch.json vendored
View File

@ -1,85 +1,70 @@
{ {
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Launch on iOS Device", "name": "Sync on iOS",
"type": "nativescript", "type": "nativescript",
"platform": "ios", "platform": "ios",
"request": "launch", "request": "launch",
"appRoot": "${workspaceRoot}/tests", "appRoot": "${workspaceRoot}",
"sourceMaps": true, "sourceMaps": true,
"diagnosticLogging": false, "diagnosticLogging": false,
"emulator": false "emulator": false,
}, "rebuild": false,
{ "syncAllFiles": false
"name": "Attach on iOS Device", },
"type": "nativescript", {
"platform": "ios", "name": "Launch on iOS",
"request": "attach", "type": "nativescript",
"appRoot": "${workspaceRoot}/tests", "platform": "ios",
"sourceMaps": true, "request": "launch",
"diagnosticLogging": false, "appRoot": "${workspaceRoot}",
"emulator": false "sourceMaps": true,
}, "diagnosticLogging": false,
{ "emulator": false,
"name": "Launch on iOS Emulator", "rebuild": true
"type": "nativescript", },
"platform": "ios", {
"request": "launch", "name": "Attach on iOS",
"appRoot": "${workspaceRoot}/tests", "type": "nativescript",
"sourceMaps": true, "platform": "ios",
"diagnosticLogging": false, "request": "attach",
"emulator": true "appRoot": "${workspaceRoot}",
}, "sourceMaps": true,
{ "diagnosticLogging": false,
"name": "Attach on iOS Emulator", "emulator": false
"type": "nativescript", },
"platform": "ios", {
"request": "attach", "name": "Sync on Android",
"appRoot": "${workspaceRoot}/tests", "type": "nativescript",
"sourceMaps": true, "platform": "android",
"diagnosticLogging": false, "request": "launch",
"emulator": true "appRoot": "${workspaceRoot}",
}, "sourceMaps": true,
{ "diagnosticLogging": false,
"name": "Launch on Android Device", "emulator": false,
"type": "nativescript", "rebuild": false
"platform": "android", },
"request": "launch", {
"appRoot": "${workspaceRoot}/tests", "name": "Launch on Android",
"sourceMaps": true, "type": "nativescript",
"diagnosticLogging": false, "platform": "android",
"emulator": false "request": "launch",
}, "appRoot": "${workspaceRoot}",
{ "sourceMaps": true,
"name": "Launch on Android Emulator", "diagnosticLogging": false,
"type": "nativescript", "emulator": false,
"platform": "android", "rebuild": true
"request": "launch", },
"appRoot": "${workspaceRoot}/tests", {
"sourceMaps": true, "name": "Attach on Android",
"diagnosticLogging": false, "type": "nativescript",
"emulator": true "platform": "android",
}, "request": "attach",
{ "appRoot": "${workspaceRoot}",
"name": "Attach on Android Device", "sourceMaps": true,
"type": "nativescript", "diagnosticLogging": false,
"platform": "android", "emulator": false
"request": "attach", }
"appRoot": "${workspaceRoot}/tests", ]
"sourceMaps": true,
"diagnosticLogging": false,
"emulator": false
},
{
"name": "Attach on Android Emulator",
"type": "nativescript",
"platform": "android",
"request": "attach",
"appRoot": "${workspaceRoot}/tests",
"sourceMaps": true,
"diagnosticLogging": false,
"emulator": true
}
]
} }

View File

@ -16,6 +16,7 @@
"tns-core-modules": "2.1.0" "tns-core-modules": "2.1.0"
}, },
"devDependencies": { "devDependencies": {
"tns-platform-declarations": "*",
"babel-traverse": "6.10.4", "babel-traverse": "6.10.4",
"babel-types": "6.11.1", "babel-types": "6.11.1",
"babylon": "6.8.3", "babylon": "6.8.3",

View File

@ -36,17 +36,16 @@
"tsc": "tsc", "tsc": "tsc",
"tsc-w": "tsc --skipLibCheck -w", "tsc-w": "tsc --skipLibCheck -w",
"dev-tsc-tns-platform-declarations": "tsc -p tns-platform-declarations", "dev-tsc-tns-platform-declarations": "tsc -p tns-platform-declarations",
"dev-tsc-tns-core-modules": "tsc -p tns-core-modules",
"dev-tsc-tests": "tsc -p tests", "dev-tsc-tests": "tsc -p tests",
"dev-tsc-apps": "tsc -p apps", "dev-tsc-apps": "tsc -p apps",
"dev-tsc-all": "npm run dev-tsc-tns-platform-declarations && npm run dev-tsc-tns-core-modules && npm run dev-tsc-tests && npm run dev-tsc-apps", "dev-tsc-all": "npm run dev-tsc-tns-platform-declarations && npm run tsc && npm run dev-tsc-tests && npm run dev-tsc-apps",
"dev-link-tns-platform-declarations": "cd tns-platform-declarations && npm link", "dev-link-tns-platform-declarations": "cd tns-platform-declarations && npm link",
"dev-link-tns-core-modules": "cd tns-core-modules && npm link", "dev-link-tns-core-modules": "cd tns-core-modules && npm link",
"dev-link-tests": "cd tests && npm link tns-platform-declarations && npm link tns-core-modules", "dev-link-tests": "cd tests && npm link tns-platform-declarations && npm link tns-core-modules",
"dev-link-apps": "cd apps && npm link tns-platform-declarations && npm link tns-core-modules", "dev-link-apps": "cd apps && npm link tns-platform-declarations && npm link tns-core-modules",
"dev-declarations": "npm run setup && rm -rf tns-platform-declarations/ios/objc* && TNS_TYPESCRIPT_DECLARATIONS_PATH=$PWD/tns-platform-declarations/ios/objc tns build ios --path tests", "dev-declarations": "npm run setup && rm -rf tns-platform-declarations/ios/objc* && TNS_TYPESCRIPT_DECLARATIONS_PATH=$PWD/tns-platform-declarations/ios/objc tns build ios --path tests",
"test": "npm run test-android && npm run test-ios", "test": "npm run test-android && npm run test-ios",
"pretest": "npm run setup && npm run dev-tsc-tns-core-modules && npm run dev-tsc-tests", "pretest": "npm run setup && npm run tsc",
"test-android": "tns run android --path tests --justlaunch", "test-android": "tns run android --path tests --justlaunch",
"test-ios": "tns run ios --path tests --justlaunch", "test-ios": "tns run ios --path tests --justlaunch",
"test-watch-android": "npm run pretest && concurrently --kill-others \"npm run tsc-tiw\" \"tns livesync android --path tests --watch\"", "test-watch-android": "npm run pretest && concurrently --kill-others \"npm run tsc-tiw\" \"tns livesync android --path tests --watch\"",

10
tests/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-p", "."],
"showOutput": "silent",
"problemMatcher": "$tsc"
}

View File

@ -214,7 +214,7 @@ export function assertNotEqual(actual: any, expected: any, message?: string) {
} }
} }
export function assertEqual(actual: any, expected: any, message?: string) { export function assertEqual<T extends { equals?(arg: T): boolean }>(actual: T, expected: T, message?: string) {
if (!types.isNullOrUndefined(actual) if (!types.isNullOrUndefined(actual)
&& !types.isNullOrUndefined(expected) && !types.isNullOrUndefined(expected)
&& types.getClass(actual) === types.getClass(expected) && types.getClass(actual) === types.getClass(expected)
@ -222,11 +222,11 @@ export function assertEqual(actual: any, expected: any, message?: string) {
// Use the equals method // Use the equals method
if (!actual.equals(expected)) { if (!actual.equals(expected)) {
throw new Error(`${message} Actual: <${actual}>(${typeof(actual)}). Expected: <${expected}>(${typeof(expected)})`); throw new Error(`${message} Actual: <${actual}>(${typeof (actual)}). Expected: <${expected}>(${typeof (expected)})`);
} }
} }
else if (actual !== expected) { else if (actual !== expected) {
throw new Error(`${message} Actual: <${actual}>(${typeof(actual)}). Expected: <${expected}>(${typeof(expected)})`); throw new Error(`${message} Actual: <${actual}>(${typeof (actual)}). Expected: <${expected}>(${typeof (expected)})`);
} }
}; };

View File

@ -501,11 +501,11 @@ export function test_ObservableShouldEmitPropertyChangeWithSameObjectUsingWrappe
export function test_CorrectEventArgsWhenWrappedValueIsUsed() { export function test_CorrectEventArgsWhenWrappedValueIsUsed() {
let testArray = [1]; let testArray = [1];
let testObservable = new observable.Observable({ "property1": testArray}); let testObservable = new observable.Observable({ "property1": testArray});
let actualArgsValue = 0; let actualArgsValue;
let propertyChangeHandler = function (args) { let propertyChangeHandler = function (args) {
actualArgsValue = args.value; actualArgsValue = args.value;
} }
testObservable.on(observable.Observable.propertyChangeEvent, propertyChangeHandler); testObservable.on(observable.Observable.propertyChangeEvent, propertyChangeHandler);
testArray.push(2); testArray.push(2);

View File

@ -1,8 +1,8 @@
import {Image} from "ui/image"; import { Image } from "ui/image";
import {StackLayout} from "ui/layouts/stack-layout"; import { StackLayout } from "ui/layouts/stack-layout";
import {GridLayout} from "ui/layouts/grid-layout"; import { GridLayout } from "ui/layouts/grid-layout";
import {isIOS} from "platform"; import { isIOS, isAndroid } from "platform";
import {PropertyChangeData} from "data/observable"; import { PropertyChangeData } from "data/observable";
import utils = require("utils/utils"); import utils = require("utils/utils");
// import {target} from "../../TKUnit"; // import {target} from "../../TKUnit";
@ -24,6 +24,11 @@ import color = require("color");
var imagePath = fs.path.join(__dirname, "../../logo.png"); var imagePath = fs.path.join(__dirname, "../../logo.png");
if (isAndroid) {
var imageModule = require("ui/image");
imageModule.currentMode = imageModule.CacheMode.memory; // use memory cache only.
}
export var test_Image_Members = function () { export var test_Image_Members = function () {
var image = new ImageModule.Image(); var image = new ImageModule.Image();
TKUnit.assert(types.isUndefined(image.src), "Image.src is defined"); TKUnit.assert(types.isUndefined(image.src), "Image.src is defined");
@ -63,10 +68,10 @@ function runImageTest(done, image: ImageModule.Image, src: string) {
testModel.off(ObservableModule.Observable.propertyChangeEvent, handler); testModel.off(ObservableModule.Observable.propertyChangeEvent, handler);
try { try {
let imageIsLoaded = !!image.imageSource; let imageIsLoaded = isIOS ? !!image.imageSource : true;
TKUnit.assertTrue(!image.isLoading, "Image.isLoading should be false."); TKUnit.assertTrue(!image.isLoading, "Image.isLoading should be false.");
TKUnit.assertTrue(!testModel.get("imageIsLoading"), "imageIsLoading on viewModel should be false."); TKUnit.assertTrue(!testModel.get("imageIsLoading"), "imageIsLoading on viewModel should be false.");
TKUnit.assertTrue(imageIsLoaded, "imageIsLoading should be true."); TKUnit.assertTrue(imageIsLoaded, "imageSource should be set.");
if (done) { if (done) {
done(null); done(null);
} }
@ -86,6 +91,8 @@ function runImageTest(done, image: ImageModule.Image, src: string) {
twoWay: true twoWay: true
}, testModel); }, testModel);
let page = helper.getCurrentPage();
page.content = image;
image.src = src; image.src = src;
testModel.on(ObservableModule.Observable.propertyChangeEvent, handler); testModel.on(ObservableModule.Observable.propertyChangeEvent, handler);
if (done) { if (done) {
@ -102,6 +109,7 @@ export var test_SettingImageSrc = function (done) {
var image = new ImageModule.Image(); var image = new ImageModule.Image();
image.src = "https://www.google.com/images/errors/logo_sm_2.png"; image.src = "https://www.google.com/images/errors/logo_sm_2.png";
// << img-create-src // << img-create-src
(<any>image).useCache = false;
runImageTest(done, image, image.src) runImageTest(done, image, image.src)
} }
@ -125,6 +133,7 @@ export var test_SettingImageSrcToDataURI = function () {
export var test_SettingImageSrcToFileWithinAppAsync = function (done) { export var test_SettingImageSrcToFileWithinAppAsync = function (done) {
var image = new ImageModule.Image(); var image = new ImageModule.Image();
(<any>image).useCache = false;
image.loadMode = "async"; image.loadMode = "async";
image.src = "~/logo.png"; image.src = "~/logo.png";
runImageTest(done, image, image.src) runImageTest(done, image, image.src)
@ -140,7 +149,7 @@ export var test_SettingImageSrcToDataURIAsync = function (done) {
// NOTE: This tests that setting multiple times src will not show the imageSource of a previous src value. // NOTE: This tests that setting multiple times src will not show the imageSource of a previous src value.
// It however will never be reliable as to properly detect failure we need to use somewhat large timeout // It however will never be reliable as to properly detect failure we need to use somewhat large timeout
// waiting for imageSource to be set to the wrong value. // waiting for imageSource to be set to the wrong value.
export var __test_SettingImageSrcTwiceMustNotMismatch = function(done) { export var __test_SettingImageSrcTwiceMustNotMismatch = function (done) {
var image = new Image(); var image = new Image();
image.on("propertyChange", (args: PropertyChangeData) => { image.on("propertyChange", (args: PropertyChangeData) => {
if (args.propertyName === "isLoading" && args.value === false) { if (args.propertyName === "isLoading" && args.value === false) {
@ -170,7 +179,7 @@ export var test_SettingStretch_AspectFit = function () {
// << img-set-stretch // << img-set-stretch
var testFunc = function (views: Array<ViewModule.View>) { var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0]; var testImage = <ImageModule.Image>views[0];
if (image.android) { if (image.android) {
var actualScaleType = testImage.android.getScaleType(); var actualScaleType = testImage.android.getScaleType();
@ -192,7 +201,7 @@ export var test_SettingStretch_Default = function () {
image.imageSource = ImageSourceModule.fromFile(imagePath); image.imageSource = ImageSourceModule.fromFile(imagePath);
var testFunc = function (views: Array<ViewModule.View>) { var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0]; var testImage = <ImageModule.Image>views[0];
if (image.android) { if (image.android) {
var actualScaleType = testImage.android.getScaleType(); var actualScaleType = testImage.android.getScaleType();
@ -215,7 +224,7 @@ export var test_SettingStretch_AspectFill = function () {
image.stretch = enumsModule.Stretch.aspectFill; image.stretch = enumsModule.Stretch.aspectFill;
var testFunc = function (views: Array<ViewModule.View>) { var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0]; var testImage = <ImageModule.Image>views[0];
if (image.android) { if (image.android) {
var actualScaleType = testImage.android.getScaleType(); var actualScaleType = testImage.android.getScaleType();
@ -238,7 +247,7 @@ export var test_SettingStretch_Fill = function () {
image.stretch = enumsModule.Stretch.fill; image.stretch = enumsModule.Stretch.fill;
var testFunc = function (views: Array<ViewModule.View>) { var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0]; var testImage = <ImageModule.Image>views[0];
if (image.android) { if (image.android) {
var actualScaleType = testImage.android.getScaleType(); var actualScaleType = testImage.android.getScaleType();
@ -261,7 +270,7 @@ export var test_SettingStretch_none = function () {
image.stretch = enumsModule.Stretch.none; image.stretch = enumsModule.Stretch.none;
var testFunc = function (views: Array<ViewModule.View>) { var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0]; var testImage = <ImageModule.Image>views[0];
if (image.android) { if (image.android) {
var actualScaleType = testImage.android.getScaleType(); var actualScaleType = testImage.android.getScaleType();
@ -336,9 +345,10 @@ export var test_SettingImageSourceWhenSizedToContentShouldInvalidate = ios(() =>
TKUnit.assertTrue(called, "image.requestLayout should be called."); TKUnit.assertTrue(called, "image.requestLayout should be called.");
}); });
export var test_DimensionsAreRoundedAfterScale = function() { export var test_DimensionsAreRoundedAfterScale = function () {
let host = new StackLayout(); let host = new StackLayout();
let image = new Image(); let image = new Image();
(<any>image).useCache = false;
image.src = "~/ui/image/700x50.png"; image.src = "~/ui/image/700x50.png";
let imageWidth = 700; let imageWidth = 700;
let imageHeight = 50; let imageHeight = 50;
@ -350,8 +360,7 @@ export var test_DimensionsAreRoundedAfterScale = function() {
host.addChild(image); host.addChild(image);
let mainPage = helper.getCurrentPage(); let mainPage = helper.getCurrentPage();
mainPage.content = host; mainPage.content = host;
TKUnit.waitUntilReady(() => host.isLoaded); TKUnit.waitUntilReady(() => host.isLayoutValid);
TKUnit.waitUntilReady(() => image.isLayoutValid);
let scale = hostWidth / imageWidth; let scale = hostWidth / imageWidth;
let expectedHeight = Math.round(imageHeight * scale); let expectedHeight = Math.round(imageHeight * scale);
@ -365,7 +374,7 @@ export var test_tintColor = function () {
image.imageSource = ImageSourceModule.fromFile(imagePath); image.imageSource = ImageSourceModule.fromFile(imagePath);
var testFunc = function (views: Array<ViewModule.View>) { var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0]; var testImage = <ImageModule.Image>views[0];
if (image.android) { if (image.android) {
var tintColor = testImage.android.getColorFilter(); var tintColor = testImage.android.getColorFilter();

View File

@ -756,7 +756,7 @@ export function test_border_color() {
let blue = new color.Color("blue"); let blue = new color.Color("blue");
let hex = blue.hex; let hex = blue.hex;
testView.style.borderColor = hex; testView.style.borderColor = hex;
TKUnit.assertEqual(testView.style.borderColor, blue, "all"); TKUnit.assertEqual((<any>testView.style.borderColor), blue, "all");
TKUnit.assertEqual(testView.style.borderTopColor, blue, "top"); TKUnit.assertEqual(testView.style.borderTopColor, blue, "top");
TKUnit.assertEqual(testView.style.borderRightColor, blue, "right"); TKUnit.assertEqual(testView.style.borderRightColor, blue, "right");
TKUnit.assertEqual(testView.style.borderBottomColor, blue, "bottom"); TKUnit.assertEqual(testView.style.borderBottomColor, blue, "bottom");
@ -774,7 +774,7 @@ export function test_border_width() {
TKUnit.assertEqual(testView.style.borderLeftWidth, 10, "left"); TKUnit.assertEqual(testView.style.borderLeftWidth, 10, "left");
testView.style.borderWidth = "20"; testView.style.borderWidth = "20";
TKUnit.assertEqual(testView.style.borderWidth, 20, "all"); TKUnit.assertEqual((<any>testView.style.borderWidth), 20, "all");
TKUnit.assertEqual(testView.style.borderTopWidth, 20, "top"); TKUnit.assertEqual(testView.style.borderTopWidth, 20, "top");
TKUnit.assertEqual(testView.style.borderRightWidth, 20, "right"); TKUnit.assertEqual(testView.style.borderRightWidth, 20, "right");
TKUnit.assertEqual(testView.style.borderBottomWidth, 20, "bottom"); TKUnit.assertEqual(testView.style.borderBottomWidth, 20, "bottom");
@ -792,7 +792,7 @@ export function test_border_radius() {
TKUnit.assertEqual(testView.style.borderBottomLeftRadius, 10, "left"); TKUnit.assertEqual(testView.style.borderBottomLeftRadius, 10, "left");
testView.style.borderRadius = "20"; testView.style.borderRadius = "20";
TKUnit.assertEqual(testView.style.borderRadius, 20, "all"); TKUnit.assertEqual((<any>testView.style.borderRadius), 20, "all");
TKUnit.assertEqual(testView.style.borderTopLeftRadius, 20, "top"); TKUnit.assertEqual(testView.style.borderTopLeftRadius, 20, "top");
TKUnit.assertEqual(testView.style.borderTopRightRadius, 20, "right"); TKUnit.assertEqual(testView.style.borderTopRightRadius, 20, "right");
TKUnit.assertEqual(testView.style.borderBottomRightRadius, 20, "bottom"); TKUnit.assertEqual(testView.style.borderBottomRightRadius, 20, "bottom");

View File

@ -16,6 +16,7 @@
"tns-core-modules": "2.3.0" "tns-core-modules": "2.3.0"
}, },
"devDependencies": { "devDependencies": {
"tns-platform-declarations": "*",
"babel-traverse": "6.9.0", "babel-traverse": "6.9.0",
"babel-types": "6.9.0", "babel-types": "6.9.0",
"babylon": "6.8.0", "babylon": "6.8.0",

View File

@ -96,7 +96,7 @@ export class Image extends view.View implements definition.Image {
/** /**
* @internal * @internal
*/ */
_createImageSourceFromSrc(): void { public _createImageSourceFromSrc(): void {
var value = this.src; var value = this.src;
if (types.isString(value)) { if (types.isString(value)) {
value = value.trim(); value = value.trim();

View File

@ -1,27 +1,36 @@
import imageCommon = require("./image-common"); import imageCommon = require("./image-common");
import dependencyObservable = require("ui/core/dependency-observable"); import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy"); import proxy = require("ui/core/proxy");
import * as enumsModule from "ui/enums";
import style = require("ui/styling/style"); import style = require("ui/styling/style");
import view = require("ui/core/view"); import view = require("ui/core/view");
import enums = require("ui/enums");
import types = require("utils/types");
import imageSource = require("image-source");
import utils = require("utils/utils");
import * as fs from "file-system";
import * as application from "application";
global.moduleMerge(imageCommon, exports); global.moduleMerge(imageCommon, exports);
var enums: typeof enumsModule; const FILE_PREFIX = "file:///";
function ensureEnums() { let ASYNC = "async";
if (!enums) { let imageFetcher: org.nativescript.widgets.image.Fetcher;
enums = require("ui/enums"); let imageCache: org.nativescript.widgets.image.Cache;
}
export enum CacheMode {
none,
memory,
diskAndMemory
} }
export let currentCacheMode: CacheMode;
function onStretchPropertyChanged(data: dependencyObservable.PropertyChangeData) { function onStretchPropertyChanged(data: dependencyObservable.PropertyChangeData) {
var image = <Image>data.object; let image = <Image>data.object;
if (!image.android) { if (!image.android) {
return; return;
} }
ensureEnums();
switch (data.newValue) { switch (data.newValue) {
case enums.Stretch.aspectFit: case enums.Stretch.aspectFit:
image.android.setScaleType(android.widget.ImageView.ScaleType.FIT_CENTER); image.android.setScaleType(android.widget.ImageView.ScaleType.FIT_CENTER);
@ -48,6 +57,34 @@ function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeD
image._setNativeImage(data.newValue); image._setNativeImage(data.newValue);
} }
export function initImageCache(context: android.content.Context, mode = CacheMode.diskAndMemory, memoryCacheSize: number = 0.25, diskCacheSize: number = 10 * 1024 * 1024): void {
if (currentCacheMode === mode) {
return;
}
currentCacheMode = mode;
if (!imageFetcher) {
imageFetcher = new org.nativescript.widgets.image.Fetcher(context);
}
// Disable cache.
if (mode === CacheMode.none) {
if (imageCache != null && imageFetcher != null) {
imageFetcher.clearCache();
}
}
let params = new org.nativescript.widgets.image.Cache.CacheParams(context, "_imageCache");
params.setMemCacheSizePercent(memoryCacheSize); // Set memory cache to % of app memory
params.diskCacheEnabled = mode === CacheMode.diskAndMemory;
params.diskCacheSize = diskCacheSize;
imageCache = org.nativescript.widgets.image.Cache.getInstance(params);
imageFetcher.addImageCache(imageCache);
imageFetcher.initCache();
}
initImageCache(application.android.nativeApp);
// register the setNativeValue callback // register the setNativeValue callback
(<proxy.PropertyMetadata>imageCommon.Image.imageSourceProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged; (<proxy.PropertyMetadata>imageCommon.Image.imageSourceProperty.metadata).onSetNativeValue = onImageSourcePropertyChanged;
(<proxy.PropertyMetadata>imageCommon.Image.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged; (<proxy.PropertyMetadata>imageCommon.Image.stretchProperty.metadata).onSetNativeValue = onStretchPropertyChanged;
@ -55,21 +92,79 @@ function onImageSourcePropertyChanged(data: dependencyObservable.PropertyChangeD
export class Image extends imageCommon.Image { export class Image extends imageCommon.Image {
private _android: org.nativescript.widgets.ImageView; private _android: org.nativescript.widgets.ImageView;
public decodeWidth = 0;
public decodeHeight = 0;
public useCache = true;
get android(): org.nativescript.widgets.ImageView { get android(): org.nativescript.widgets.ImageView {
return this._android; return this._android;
} }
public _createUI() { public _createUI() {
this._android = new org.nativescript.widgets.ImageView(this._context); this._android = new org.nativescript.widgets.ImageView(this._context);
this._createImageSourceFromSrc();
} }
public _setNativeImage(nativeImage: any) { public _setNativeImage(nativeImage: any) {
let rotation = (nativeImage && nativeImage.rotationAngle) ? nativeImage.rotationAngle : 0 ; if (!nativeImage) {
return;
}
let rotation = nativeImage.rotationAngle ? nativeImage.rotationAngle : 0 ;
if (rotation > 0) { if (rotation > 0) {
this.android.setRotationAngle(rotation); this.android.setRotationAngle(rotation);
} }
this.android.setImageBitmap(nativeImage.android); this.android.setImageBitmap(nativeImage.android);
} }
public _createImageSourceFromSrc() {
let imageView = this._android;
if (!imageView) {
return;
}
let value = this.src;
let async = this.loadMode === ASYNC;
let owner = new WeakRef<Image>(this);
let listener = new org.nativescript.widgets.image.Worker.OnImageLoadedListener({
onImageLoaded: function (success) {
let that = owner.get();
if (that) {
that._setValue(Image.isLoadingProperty, false);
}
}
});
this._resetValue(Image.imageSourceProperty);
if (types.isString(value)) {
value = value.trim();
this._setValue(Image.isLoadingProperty, true);
if (utils.isDataURI(value)) {
// TODO: Check with runtime what should we do in case of base64 string.
super._createImageSourceFromSrc();
}
else if (imageSource.isFileOrResourcePath(value)) {
if (value.indexOf(utils.RESOURCE_PREFIX) === 0) {
imageFetcher.loadImage(value, imageView, this.decodeWidth, this.decodeHeight, this.useCache, async, listener);
}
else {
let fileName = value;
if (fileName.indexOf("~/") === 0) {
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
}
imageFetcher.loadImage(FILE_PREFIX + fileName, imageView, this.decodeWidth, this.decodeHeight, this.useCache, async, listener);
}
}
else {
// For backwards compatibility http always use async loading.
imageFetcher.loadImage(value, imageView, this.decodeWidth, this.decodeHeight, this.useCache, true, listener);
}
} else {
super._createImageSourceFromSrc();
}
}
} }
export class ImageStyler implements style.Styler { export class ImageStyler implements style.Styler {

View File

@ -54,53 +54,3 @@ declare module android {
} }
} }
} }
declare module org {
export module nativescript {
export module widgets {
export module Async {
export class CompleteCallback {
constructor(implementation: ICompleteCallback);
onComplete(result: Object, context: Object): void;
}
export interface ICompleteCallback {
onComplete(result: Object, context: Object): void;
}
export module Image {
export function download(url: string, callback: CompleteCallback, context: any);
}
export module Http {
export class KeyValuePair {
public key: string;
public value: string;
constructor(key: string, value: string);
}
export class RequestOptions {
public url: string;
public method: string;
public headers: java.util.ArrayList<KeyValuePair>;
public content: string;
public timeout: number;
public screenWidth: number;
public screenHeight: number;
}
export class RequestResult {
public raw: java.io.ByteArrayOutputStream;
public headers: java.util.ArrayList<KeyValuePair>;
public statusCode: number;
public responseAsString: string;
public responseAsImage: android.graphics.Bitmap;
public error: java.lang.Exception;
}
export function MakeRequest(options: RequestOptions, callback: CompleteCallback, context: any);
}
}
}
}
}

View File

@ -1,6 +1,50 @@
declare module org { declare module org {
module nativescript { module nativescript {
module widgets { module widgets {
export module Async {
export class CompleteCallback {
constructor(implementation: ICompleteCallback);
onComplete(result: Object, context: Object): void;
}
export interface ICompleteCallback {
onComplete(result: Object, context: Object): void;
}
export module Image {
export function download(url: string, callback: CompleteCallback, context: any);
}
export module Http {
export class KeyValuePair {
public key: string;
public value: string;
constructor(key: string, value: string);
}
export class RequestOptions {
public url: string;
public method: string;
public headers: java.util.ArrayList<KeyValuePair>;
public content: string;
public timeout: number;
public screenWidth: number;
public screenHeight: number;
}
export class RequestResult {
public raw: java.io.ByteArrayOutputStream;
public headers: java.util.ArrayList<KeyValuePair>;
public statusCode: number;
public responseAsString: string;
public responseAsImage: android.graphics.Bitmap;
public error: java.lang.Exception;
}
export function MakeRequest(options: RequestOptions, callback: CompleteCallback, context: any);
}
}
export class BorderDrawable extends android.graphics.drawable.ColorDrawable { export class BorderDrawable extends android.graphics.drawable.ColorDrawable {
constructor(density: number); constructor(density: number);
public refresh( public refresh(
@ -309,6 +353,48 @@
iconId: number; iconId: number;
iconDrawable: android.graphics.drawable.Drawable; iconDrawable: android.graphics.drawable.Drawable;
} }
export namespace image {
export class Cache {
private constructor();
public static getInstance(cacheParams: Cache.CacheParams): Cache;
}
export class Worker {
}
export namespace Worker {
interface IOnImageLoadedListener {
onImageLoaded(success: boolean): void;
}
export class OnImageLoadedListener implements IOnImageLoadedListener {
constructor(implementation: IOnImageLoadedListener);
public onImageLoaded(success: boolean): void;
}
}
export class Fetcher extends Worker {
constructor(context: android.content.Context);
public addImageCache(cache: Cache): void;
public initCache(): void;
public clearCache(): void;
public loadImage(data: Object, imageView: ImageView,
decodeWidth: number, decodeHeight: number, useCache: boolean, async: boolean,
listener: Worker.IOnImageLoadedListener): void;
}
export namespace Cache {
export class CacheParams {
constructor(context: android.content.Context, diskCacheDirectoryName: string);
public setMemCacheSizePercent(percent: number): void;
public diskCacheEnabled: boolean;
public diskCacheSize: number;
}
}
}
} }
} }
} }

View File

@ -1,12 +1,6 @@
{ {
"name": "tns-platform-declarations", "name": "tns-platform-declarations",
"version": "2.4.0", "version": "2.4.0",
"nativescript": {
"platforms": {
"ios": "2.4.0",
"android": "2.4.0"
}
},
"description": "Platform-specific TypeScript declarations for NativeScript for accessing native objects", "description": "Platform-specific TypeScript declarations for NativeScript for accessing native objects",
"main": "", "main": "",
"scripts": { "scripts": {