Skip to content

Commit

Permalink
Merge pull request #1 from fluttercommunity/master
Browse files Browse the repository at this point in the history
update from master
  • Loading branch information
atrope authored Jun 10, 2021
2 parents 3d6831b + 04da820 commit 770e2ee
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 42 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.6.1 - 29-05-2021

* fix bug on Android: `getContentLength`
* upgrade Android dependencies (WorkerManager 2.5.0)

## 1.6.0 - 12-04-2021

* Stable version of nullsafety
Expand Down
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ android {
}

dependencies {
implementation 'androidx.work:work-runtime:2.4.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.core:core:1.3.1'
implementation 'androidx.fragment:fragment:1.2.5'
implementation 'androidx.work:work-runtime:2.5.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.core:core:1.5.0'
implementation 'androidx.fragment:fragment:1.3.4'
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri

if ((responseCode == HttpURLConnection.HTTP_OK || (isResume && responseCode == HttpURLConnection.HTTP_PARTIAL)) && !isStopped()) {
String contentType = httpConn.getContentType();
long contentLength = httpConn.getContentLengthLong();
long contentLength = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N ? httpConn.getContentLengthLong() : httpConn.getContentLength();
log("Content-Type = " + contentType);
log("Content-Length = " + contentLength);

Expand Down
71 changes: 42 additions & 29 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,10 @@ class _MyHomePageState extends State<MyHomePage> {

if (_tasks != null && _tasks!.isNotEmpty) {
final task = _tasks!.firstWhere((task) => task.taskId == id);
if (task != null) {
setState(() {
task.status = status;
task.progress = progress;
});
}
setState(() {
task.status = status;
task.progress = progress;
});
}
});
}
Expand Down Expand Up @@ -209,7 +207,7 @@ class _MyHomePageState extends State<MyHomePage> {
}
});
},
onAtionClick: (task) {
onActionClick: (task) {
if (task.status == DownloadTaskStatus.undefined) {
_requestDownload(task);
} else if (task.status == DownloadTaskStatus.running) {
Expand Down Expand Up @@ -255,11 +253,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
FlatButton(
onPressed: () {
_checkPermission().then((hasGranted) {
setState(() {
_permissionReady = hasGranted;
});
});
_retryRequestPermission();
},
child: Text(
'Retry',
Expand All @@ -273,6 +267,18 @@ class _MyHomePageState extends State<MyHomePage> {
),
);

Future<void> _retryRequestPermission() async {
final hasGranted = await _checkPermission();

if (hasGranted) {
await _prepareSaveDir();
}

setState(() {
_permissionReady = hasGranted;
});
}

void _requestDownload(_TaskInfo task) async {
task.taskId = await FlutterDownloader.enqueue(
url: task.link!,
Expand Down Expand Up @@ -366,7 +372,7 @@ class _MyHomePageState extends State<MyHomePage> {
count++;
}

tasks?.forEach((task) {
tasks!.forEach((task) {
for (_TaskInfo info in _tasks!) {
if (info.link == task.url) {
info.taskId = task.taskId;
Expand All @@ -378,33 +384,40 @@ class _MyHomePageState extends State<MyHomePage> {

_permissionReady = await _checkPermission();

_localPath = (await _findLocalPath()) + Platform.pathSeparator + 'Download';
if (_permissionReady) {
await _prepareSaveDir();
}

setState(() {
_isLoading = false;
});
}

Future<void> _prepareSaveDir() async {
_localPath =
(await _findLocalPath())! + Platform.pathSeparator + 'Download';

final savedDir = Directory(_localPath);
bool hasExisted = await savedDir.exists();
if (!hasExisted) {
savedDir.create();
}

setState(() {
_isLoading = false;
});
}

Future<String> _findLocalPath() async {
Future<String?> _findLocalPath() async {
final directory = widget.platform == TargetPlatform.android
? await (getExternalStorageDirectory() as FutureOr<Directory>)
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
return directory.path;
return directory?.path;
}
}

class DownloadItem extends StatelessWidget {
final _ItemHolder? data;
final Function(_TaskInfo?)? onItemClick;
final Function(_TaskInfo)? onAtionClick;
final Function(_TaskInfo)? onActionClick;

DownloadItem({this.data, this.onItemClick, this.onAtionClick});
DownloadItem({this.data, this.onItemClick, this.onActionClick});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -450,7 +463,7 @@ class DownloadItem extends StatelessWidget {
),
)
: Container()
].where((child) => child != null).toList(),
].toList(),
),
),
);
Expand All @@ -460,7 +473,7 @@ class DownloadItem extends StatelessWidget {
if (task.status == DownloadTaskStatus.undefined) {
return RawMaterialButton(
onPressed: () {
onAtionClick!(task);
onActionClick!(task);
},
child: Icon(Icons.file_download),
shape: CircleBorder(),
Expand All @@ -469,7 +482,7 @@ class DownloadItem extends StatelessWidget {
} else if (task.status == DownloadTaskStatus.running) {
return RawMaterialButton(
onPressed: () {
onAtionClick!(task);
onActionClick!(task);
},
child: Icon(
Icons.pause,
Expand All @@ -481,7 +494,7 @@ class DownloadItem extends StatelessWidget {
} else if (task.status == DownloadTaskStatus.paused) {
return RawMaterialButton(
onPressed: () {
onAtionClick!(task);
onActionClick!(task);
},
child: Icon(
Icons.play_arrow,
Expand All @@ -501,7 +514,7 @@ class DownloadItem extends StatelessWidget {
),
RawMaterialButton(
onPressed: () {
onAtionClick!(task);
onActionClick!(task);
},
child: Icon(
Icons.delete_forever,
Expand All @@ -522,7 +535,7 @@ class DownloadItem extends StatelessWidget {
Text('Failed', style: TextStyle(color: Colors.red)),
RawMaterialButton(
onPressed: () {
onAtionClick!(task);
onActionClick!(task);
},
child: Icon(
Icons.refresh,
Expand Down
8 changes: 4 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ description: Demonstrates how to use the flutter_downloader plugin.
version: 1.0.0+1

environment:
sdk: '>=2.12.0-259.16.beta <3.0.0'
flutter: ^1.12.13
sdk: '>=2.12.0 <3.0.0'
flutter: '>=1.20.0'

dependencies:
flutter:
sdk: flutter
path_provider: ^2.0.0
permission_handler: ^5.0.1+1
path_provider: ^2.0.2
permission_handler: ^8.0.0+2
flutter_downloader:
path: ../

Expand Down
2 changes: 1 addition & 1 deletion lib/src/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DownloadTask {
final DownloadTaskStatus status;
final int progress;
final String url;
final String filename;
final String? filename;
final String savedDir;
final int timeCreated;

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_downloader
description: A plugin for creating and managing download tasks. Supports iOS and Android.
version: 1.6.0
version: 1.6.1
homepage: https://github.com/fluttercommunity/flutter_downloader
maintainer: Hung Duy Ha (@hnvn)

Expand All @@ -14,8 +14,8 @@ flutter:
pluginClass: FlutterDownloaderPlugin

environment:
sdk: '>=2.12.0-259.16.beta <3.0.0'
flutter: ^1.12.13
sdk: '>=2.12.0 <3.0.0'
flutter: '>=1.20.0'

dependencies:
flutter:
Expand Down

0 comments on commit 770e2ee

Please sign in to comment.