first commit

This commit is contained in:
Elad Gil
2018-04-24 11:52:58 +03:00
commit ac1fe56e3f
18 changed files with 1490 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
import { NativeModules } from 'react-native';
const { RNBackgroundDownload } = NativeModules;
export default class DownloadTask {
state = 'PENDING';
percent = 0;
bytesWritten = 0;
totalBytes = 0;
constructor(taskInfo) {
if (typeof taskInfo === 'string') {
this.id = taskInfo;
} else {
this.id = taskInfo.id;
this.percent = taskInfo.percent;
this.bytesWritten = taskInfo.bytesWritten;
this.totalBytes = taskInfo.totalBytes;
}
}
begin(handler) {
this._beginHandler = handler;
return this;
}
progress(handler) {
this._progressHandler = handler;
return this;
}
done(handler) {
this._doneHandler = handler;
return this;
}
error(handler) {
this._errorHandler = handler;
return this;
}
_onBegin(expectedBytes) {
this.state = 'DOWNLOADING';
this._beginHandler && this._beginHandler(expectedBytes);
}
_onProgress(percent, bytesWritten, totalBytes) {
this.percent = percent;
this.bytesWritten = bytesWritten;
this.totalBytes = totalBytes;
this._progressHandler && this._progressHandler(percent, bytesWritten, totalBytes);
}
_onDone() {
this.state = 'DONE';
this._doneHandler && this._doneHandler();
}
_onError(error) {
this.state = 'FAILED';
this._errorHandler && this._errorHandler(error);
}
pause() {
this.state = 'PAUSED';
RNBackgroundDownload.puaseTask(this.id);
}
resume() {
this.state = 'DOWNLOADING';
RNBackgroundDownload.resumeTask(this.id);
}
stop() {
this.state = 'STOPPED';
RNBackgroundDownload.stopTask(this.id);
}
}