Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-17327]: Add ceiling and floor to httpClient's onDataStream progress #6424

Merged
merged 7 commits into from
Dec 5, 2014
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class NetworkModule extends KrollModule {
@Kroll.constant public static final int TLS_VERSION_1_0 = 1;
@Kroll.constant public static final int TLS_VERSION_1_1 = 2;
@Kroll.constant public static final int TLS_VERSION_1_2 = 3;

@Kroll.constant public static final int PROGRESS_UNKNOWN = -1;

public enum State {
UNKNOWN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,12 @@ else if (contentLength > maxBufferSize) {

TiBlob blob = TiBlob.blobFromData(blobData, contentType);
callbackData.put("blob", blob);
callbackData.put("progress", ((double)totalSize)/((double)contentLength));
double progress = ((double)totalSize)/((double)contentLength);
// return progress as -1 if it is outside the valid range
if (progress > 1 || progress < 0) {
progress = NetworkModule.PROGRESS_UNKNOWN;
}
callbackData.put("progress", progress);

dispatchCallback("ondatastream", callbackData);
}
Expand Down
3 changes: 2 additions & 1 deletion apidoc/Titanium/Network/HTTPClient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ properties:
Must be set before calling `open`.

The `progress` property of the event will contain a value from 0.0-1.0 with the progress of
the request.
the request. On iOS and Android as of 3.5.0, if the progress can not be calculated,
the value will be [PROGRESS_UNKNOWN](Titanium.Network.PROGRESS_UNKNOWN).
type: Callback<Object>

- name: onerror
Expand Down
7 changes: 7 additions & 0 deletions apidoc/Titanium/Network/Network.yml
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ properties:
permission: read-only
since: {android: "3.6.0", iphone: "1.8.0.1", ipad: "1.8.0.1"}

- name: PROGRESS_UNKNOWN
summary: Constant value specifying that the progress of a download can not be calculated.
type: Number
permission: read-only
platforms: [android, iphone, ipad]
since: "3.5.0"

- name: allHTTPCookies
summary: A list of all cookies in the cookie storage.
type: Array<Titanium.Network.Cookie>
Expand Down
2 changes: 2 additions & 0 deletions iphone/Classes/NetworkModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ -(NSNumber*)networkType
MAKE_SYSTEM_PROP(TLS_VERSION_1_1, TLS_VERSION_1_1);
MAKE_SYSTEM_PROP(TLS_VERSION_1_2, TLS_VERSION_1_2);

MAKE_SYSTEM_NUMBER(PROGRESS_UNKNOWN, NUMINT(-1));

#pragma mark Push Notifications

- (NSString*) remoteDeviceUUID
Expand Down
7 changes: 6 additions & 1 deletion iphone/Classes/TiNetworkHTTPClientProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ -(void)request:(APSHTTPRequest *)request onDataStream:(APSHTTPResponse *)respons
if(_downloadTime == 0 || diff > TI_HTTP_REQUEST_PROGRESS_INTERVAL || [response readyState] == APSHTTPResponseStateDone) {
_downloadTime = 0;
NSDictionary *eventDict = [NSMutableDictionary dictionary];
[eventDict setValue:[NSNumber numberWithFloat: [response downloadProgress]] forKey:@"progress"];
float downloadProgress = [response downloadProgress];
// return progress as -1 if it is outside the valid range
if (downloadProgress > 1 || downloadProgress < 0) {
downloadProgress = -1.0f;
}
[eventDict setValue:[NSNumber numberWithFloat: downloadProgress] forKey:@"progress"];
[self fireCallback:@"ondatastream" withArg:eventDict withSource:self];
}
if(_downloadTime == 0) {
Expand Down