forked from VideoSpike/nativescript-web-image-cache
-
Notifications
You must be signed in to change notification settings - Fork 2
/
web-image-cache.ios.js
155 lines (155 loc) · 7.65 KB
/
web-image-cache.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var web_image_cache_common_1 = require("./web-image-cache.common");
var view_1 = require("tns-core-modules/ui/core/view");
var appSettings = require("tns-core-modules/application-settings");
var helpers_1 = require("./ios-ts-lib/helpers");
var enums = require("tns-core-modules/ui/enums");
var placeholderProperty = new view_1.Property({
name: "placeholder",
defaultValue: undefined,
affectsLayout: true
}), stretchProperty = new view_1.Property({
name: "stretch",
defaultValue: "none",
affectsLayout: true
});
web_image_cache_common_1.srcProperty.register(web_image_cache_common_1.WebImageCommon);
web_image_cache_common_1.isLoadingProperty.register(web_image_cache_common_1.WebImageCommon);
placeholderProperty.register(web_image_cache_common_1.WebImageCommon);
stretchProperty.register(web_image_cache_common_1.WebImageCommon);
var WebImage = (function (_super) {
__extends(WebImage, _super);
function WebImage() {
return _super.call(this) || this;
}
WebImage.prototype.createNativeView = function () {
var imageView = new UIImageView();
imageView.contentMode = UIViewContentMode.UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = true;
imageView.userInteractionEnabled = true;
return imageView;
};
Object.defineProperty(WebImage.prototype, "ios", {
get: function () {
return this.nativeView;
},
set: function (view) {
this.nativeView = view;
},
enumerable: true,
configurable: true
});
WebImage.prototype.onMeasure = function (widthMeasureSpec, heightMeasureSpec) {
var utils = require("utils/utils"), width = utils.layout.getMeasureSpecSize(widthMeasureSpec), widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec), height = utils.layout.getMeasureSpecSize(heightMeasureSpec), heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec), nativeWidth = this.nativeView ? (this.nativeView.image ? this.nativeView.image.size.width : 0) : 0, nativeHeight = this.nativeView ? (this.nativeView.image ? this.nativeView.image.size.height : 0) : 0, measureWidth = Math.max(nativeWidth, this.minWidth), measureHeight = Math.max(nativeHeight, this.minHeight), finiteWidth = widthMode !== utils.layout.UNSPECIFIED, finiteHeight = heightMode !== utils.layout.UNSPECIFIED;
if (nativeWidth !== 0 && nativeHeight !== 0 && (finiteWidth || finiteHeight)) {
var scale = this.computeScaleFactor(width, height, finiteWidth, finiteHeight, nativeWidth, nativeHeight, this.stretch), resultW = Math.floor(nativeWidth * scale.width), resultH = Math.floor(nativeHeight * scale.height);
measureWidth = finiteWidth ? Math.min(resultW, width) : resultW;
measureHeight = finiteHeight ? Math.min(resultH, height) : resultH;
var trace = require("trace");
trace.write("Image stretch: " + this.stretch +
", nativeWidth: " + nativeWidth +
", nativeHeight: " + nativeHeight, trace.categories.Layout);
}
var view = require("ui/core/view");
var widthAndState = view.View.resolveSizeAndState(measureWidth, width, widthMode, 0);
var heightAndState = view.View.resolveSizeAndState(measureHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
};
WebImage.prototype.computeScaleFactor = function (measureWidth, measureHeight, widthIsFinite, heightIsFinite, nativeWidth, nativeHeight, imageStretch) {
var scaleW = 1, scaleH = 1;
if ((imageStretch === enums.Stretch.aspectFill || imageStretch === enums.Stretch.aspectFit || imageStretch === enums.Stretch.fill) &&
(widthIsFinite || heightIsFinite)) {
scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0;
scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0;
if (!widthIsFinite) {
scaleW = scaleH;
}
else if (!heightIsFinite) {
scaleH = scaleW;
}
else {
switch (imageStretch) {
case enums.Stretch.aspectFit:
scaleH = scaleW < scaleH ? scaleW : scaleH;
scaleW = scaleH;
break;
case enums.Stretch.aspectFill:
scaleH = scaleW > scaleH ? scaleW : scaleH;
scaleW = scaleH;
break;
}
}
}
return {
width: scaleW,
height: scaleH
};
};
WebImage.prototype[placeholderProperty.getDefault] = function () {
return undefined;
};
WebImage.prototype[placeholderProperty.setNative] = function (value) {
};
WebImage.prototype[stretchProperty.getDefault] = function () {
return "none";
};
WebImage.prototype[stretchProperty.setNative] = function (value) {
helpers_1.Helpers.onStretchPropertyChanged(this.nativeView, value);
};
WebImage.prototype[web_image_cache_common_1.isLoadingProperty.getDefault] = function () {
return false;
};
WebImage.prototype[web_image_cache_common_1.isLoadingProperty.setNative] = function (value) {
};
WebImage.prototype[web_image_cache_common_1.srcProperty.getDefault] = function () {
};
WebImage.prototype[web_image_cache_common_1.srcProperty.setNative] = function (value) {
helpers_1.Helpers.onSrcPropertySet(this, value);
};
return WebImage;
}(web_image_cache_common_1.WebImageCommon));
exports.WebImage = WebImage;
function setCacheLimit(numberOfDays) {
var noOfSecondsInAMinute = 60, noOfMinutesInAHour = 60, noOfHoursInADay = 24, noOfSecondsADay = noOfSecondsInAMinute * noOfMinutesInAHour * noOfHoursInADay, noOfSecondsInDays = noOfSecondsADay * numberOfDays, currentSeconds = Math.round(new Date().getTime() / 1000), referenceTime = 0;
if (true === appSettings.getBoolean("isAppOpenedFirstTime") || undefined === appSettings.getBoolean("isAppOpenedFirstTime") || null == appSettings.getBoolean("isAppOpenedFirstTime")) {
appSettings.setBoolean("isAppOpenedFirstTime", false);
this.clearCache();
appSettings.setNumber("cacheTimeReference", currentSeconds);
}
else {
referenceTime = appSettings.getNumber("cacheTimeReference");
if (null == referenceTime || undefined === referenceTime) {
appSettings.setNumber("cacheTimeReference", currentSeconds);
}
else if ((currentSeconds - referenceTime) > noOfSecondsInDays) {
this.clearCache();
appSettings.setNumber("cacheTimeReference", currentSeconds);
}
}
}
exports.setCacheLimit = setCacheLimit;
function clearCache() {
var imageCache = SDImageCache.sharedImageCache();
imageCache.clearMemory();
imageCache.clearDiskOnCompletion(function () { });
}
exports.clearCache = clearCache;
function initializeOnAngular() {
throw new Error("'initializeOnAngular' has been removed from 'nativescript-web-image-cache', see its readme for details!");
}
exports.initializeOnAngular = initializeOnAngular;
function preFetchImage(urls) {
return new Promise(function (resolve, reject) {
if (!urls || !Array.isArray(urls) || urls.length < 1) {
reject("preFetchImage: param should be array of urls");
}
else {
SDWebImagePrefetcher.sharedImagePrefetcher.prefetchURLsProgressCompleted(urls, null, function (finishedCount, skippedCount) {
resolve();
});
}
});
}
exports.preFetchImage = preFetchImage;
//# sourceMappingURL=web-image-cache.ios.js.map