Skip to content

Commit

Permalink
Merge pull request #13 from MarsadMaqsood/main
Browse files Browse the repository at this point in the history
Improved File Download Functionality and Bug Fixes
  • Loading branch information
MarsadMaqsood authored Jul 2, 2023
2 parents 0ad0dc9 + 08a1026 commit 01a7685
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 82 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ migrate_working_dir/
/build/
# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock
*.env

# Symbolication related
app.*.symbols
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.3
- Updated packages to the latest version
- Bug fixes

## 0.2.2
- Updated packages to the latest version

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A package to check for the custom in-app updates for Flutter.
## ⭐ Installing
```
dependencies:
updater: ^0.2.1
updater: ^0.2.3
```

## ⚡ Import
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
.pub-cache/
.pub/
/build/
*.env

# Web related
lib/generated_plugin_registrant.dart
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.marsad.updater_example"
minSdkVersion 16
minSdkVersion 19
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class _MyAppState extends State<MyApp> {
url: 'https://codingwithmarsad.web.app/updater.json',
titleText: 'Stay with time',
// backgroundDownload: false,
allowSkip: true,
allowSkip: false,
contentText:
'Update your app to the latest version to enjoy new feature.',
callBack: (UpdateModel model) {
Expand Down
10 changes: 5 additions & 5 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ packages:
dependency: transitive
description:
name: dio
sha256: b99b1d56dc0d5dece70957023af002dbd49614b4a1bf86d3a254af3fe781bdf2
sha256: a9d76e72985d7087eb7c5e7903224ae52b337131518d127c554b9405936752b8
url: "https://pub.dev"
source: hosted
version: "5.2.0+1"
version: "5.2.1+1"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -100,10 +100,10 @@ packages:
dependency: transitive
description:
name: http
sha256: "4c3f04bfb64d3efd508d06b41b825542f08122d30bda4933fb95c069d22a4fa3"
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
version: "1.1.0"
http_parser:
dependency: transitive
description:
Expand Down Expand Up @@ -323,7 +323,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.2.2"
version: "0.2.3"
vector_math:
dependency: transitive
description:
Expand Down
5 changes: 2 additions & 3 deletions lib/src/api_task.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class APITask {
static final APITask _instance = APITask._internal();
late Dio _dio;

APITask._internal() {
_dio = Dio();
}

factory APITask() => _instance;

late Dio _dio;

Future<Response<T>> get<T>(String url) async {
return await _dio.get<T>(url);
}
Expand Down
80 changes: 33 additions & 47 deletions lib/src/download_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ class DownloadCore {
required this.dismiss,
});

bool _isUpdated = false;
bool _isDisposed = false;

Future<void> startDownload({isResumed = false}) async {
Future<void> startDownload({bool isResumed = false}) async {
Directory tempDirectory = await directory();

List<FileSystemEntity> listEntity = tempDirectory.listSync();
Expand All @@ -47,27 +46,22 @@ class DownloadCore {
totalLength = '${int.parse(totalLength) - 1}';
}

if (!isResumed) {
if (isResumed) {
for (FileSystemEntity entity in listEntity) {
File file = File(entity.path);
downloadedLength += file.lengthSync();
}
} else {
for (FileSystemEntity entity in listEntity) {
File file = File(entity.path);
file.deleteSync();
}
}

listEntity = tempDirectory.listSync();

for (FileSystemEntity entity in listEntity) {
File file = File(entity.path);
downloadedLength = downloadedLength + file.lengthSync();
}

int index = 0;

if (listEntity.isNotEmpty) {
index =
int.tryParse(listEntity.last.path.split('-').last.split('.').first) ??
0;
}
int index = listEntity.isNotEmpty
? int.tryParse(listEntity.last.path.split('-').last.split('.').first) ??
0
: 0;

String fileName = '${tempDirectory.path}/app$id-${index + 1}.apk';

Expand All @@ -77,13 +71,12 @@ class DownloadCore {
fileName,
cancelToken: token,
onReceiveProgress: (currentProgress, totalProgress) {
if (!_isUpdated) {
controller?.setValue(UpdateStatus.Dowloading);
_isUpdated = true;
if (controller?.status == UpdateStatus.none) {
controller?.setValue(UpdateStatus.Downloading);
}

final cp = currentProgress + downloadedLength;
final tp = totalProgress + downloadedLength;
final int cp = currentProgress + downloadedLength;
final int tp = totalProgress + downloadedLength;

controller?.setProgress(cp, tp);

Expand All @@ -99,23 +92,19 @@ class DownloadCore {
'${formatBytes(cp, 1)} / ${formatBytes(tp, 1)}';
}

///Current progress + old progress (the bytes already downloaded)
if (cp == totalProgress) {
//Update Controller
controller?.setValue(UpdateStatus.Completed);

//Dismiss the dialog
if (!_isDisposed) dismiss.call();

///Open the downloaded apk file
// OpenFilex.open('${tempDirectory.path}/app.apk');
OpenFilex.open(fileName);
}

if (currentProgress == totalProgress && isResumed) {
controller?.setValue(UpdateStatus.Completed);
dismiss.call();
_mergeFiles(tempDirectory);
if (currentProgress == totalProgress) {
//if the download was resumed then merge all the files
// otherwise open the downloaded file
if (isResumed) {
_mergeFiles(tempDirectory);
} else {
OpenFilex.open(fileName);
}

if (!_isDisposed) {
dismiss.call();
controller?.setValue(UpdateStatus.Completed);
}
}

if (currentProgress > totalProgress) {
Expand Down Expand Up @@ -148,7 +137,7 @@ class DownloadCore {
int length = 0;

for (FileSystemEntity entity in listEntity) {
length = length + File(entity.path).lengthSync();
length += File(entity.path).lengthSync();
}

String totalLength = await checkFileSize();
Expand All @@ -174,18 +163,15 @@ class DownloadCore {
await file.delete();
}

List<int> list = [];
IOSink outputFile = file.openWrite(mode: FileMode.write);

for (FileSystemEntity entity in listEntity) {
var byte = await File(entity.path).readAsBytes();
list.addAll(byte);
List<int> bytes = await File(entity.path).readAsBytes();
outputFile.add(bytes);
}

await file.writeAsBytes(
list,
);
await outputFile.close();

// OpenFile.open(file.path);
OpenFilex.open(file.path);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum UpdateStatus {
Pending,

///when an update starts downloading
Dowloading,
Downloading,

///when the update is downloaded and ready to install
Completed,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/update_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UpdateDialog extends StatefulWidget {
this.cancelText,
required this.elevation,
required this.token,
this.status = UpdateStatus.Dowloading,
this.status = UpdateStatus.Downloading,
required this.id,
required this.enableResume,
});
Expand Down Expand Up @@ -168,7 +168,7 @@ class _UpdateDialogState extends State<UpdateDialog> {
setState(() {
_changeDialog = true;
});
status = UpdateStatus.Dowloading;
status = UpdateStatus.Downloading;

core.startDownload();
},
Expand Down Expand Up @@ -299,7 +299,7 @@ class _UpdateDialogState extends State<UpdateDialog> {
core.cancel();
return;
}
if (status == UpdateStatus.Dowloading ||
if (status == UpdateStatus.Downloading ||
status == UpdateStatus.Resume) {
core.pause();
_updateStatus(UpdateStatus.Paused);
Expand All @@ -316,7 +316,7 @@ class _UpdateDialogState extends State<UpdateDialog> {
icon: Icon(
!widget.enableResume
? Icons.clear_rounded
: (status == UpdateStatus.Dowloading
: (status == UpdateStatus.Downloading
? Icons.clear_rounded
: status == UpdateStatus.Resume
? Icons.pause_rounded
Expand Down
24 changes: 12 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ packages:
dependency: "direct dev"
description:
name: build_runner
sha256: "5e1929ad37d48bd382b124266cb8e521de5548d406a45a5ae6656c13dab73e37"
sha256: "10c6bcdbf9d049a0b666702cf1cee4ddfdc38f02a19d35ae392863b47519848b"
url: "https://pub.dev"
source: hosted
version: "2.4.5"
version: "2.4.6"
build_runner_core:
dependency: transitive
description:
Expand Down Expand Up @@ -133,10 +133,10 @@ packages:
dependency: transitive
description:
name: code_builder
sha256: "0d43dd1288fd145de1ecc9a3948ad4a6d5a82f0a14c4fdd0892260787d975cbe"
sha256: "4ad01d6e56db961d29661561effde45e519939fdaeb46c351275b182eac70189"
url: "https://pub.dev"
source: hosted
version: "4.4.0"
version: "4.5.0"
collection:
dependency: transitive
description:
Expand Down Expand Up @@ -173,10 +173,10 @@ packages:
dependency: "direct main"
description:
name: dio
sha256: b99b1d56dc0d5dece70957023af002dbd49614b4a1bf86d3a254af3fe781bdf2
sha256: a9d76e72985d7087eb7c5e7903224ae52b337131518d127c554b9405936752b8
url: "https://pub.dev"
source: hosted
version: "5.2.0+1"
version: "5.2.1+1"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -218,10 +218,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
version: "2.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -260,10 +260,10 @@ packages:
dependency: "direct main"
description:
name: http
sha256: "4c3f04bfb64d3efd508d06b41b825542f08122d30bda4933fb95c069d22a4fa3"
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
version: "1.1.0"
http_multi_server:
dependency: transitive
description:
Expand Down Expand Up @@ -356,10 +356,10 @@ packages:
dependency: "direct dev"
description:
name: mockito
sha256: "8b46d7eb40abdda92d62edd01546051f0c27365e65608c284de336dccfef88cc"
sha256: "7d5b53bcd556c1bc7ffbe4e4d5a19c3e112b7e925e9e172dd7c6ad0630812616"
url: "https://pub.dev"
source: hosted
version: "5.4.1"
version: "5.4.2"
open_filex:
dependency: "direct main"
description:
Expand Down
Loading

0 comments on commit 01a7685

Please sign in to comment.