Skip to content

Commit

Permalink
Convert DownloadTaskStatus into enum (#835)
Browse files Browse the repository at this point in the history
* convert `DownloadTaskStatus` into enum

* remove redundant toString()

* bump version to 1.11.0
  • Loading branch information
bartekpacia authored Jul 30, 2023
1 parent d59e16d commit 4554a16
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.11.0

- Convert `DownloadTaskStatus` into an `enum` (#835)

## 1.10.7

- Override `operator ==` and `hashCode` for `DownloadTask` (#875)
Expand Down
2 changes: 1 addition & 1 deletion example/lib/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _MyHomePageState extends State<MyHomePage> {
}
_port.listen((dynamic data) {
final taskId = (data as List<dynamic>)[0] as String;
final status = DownloadTaskStatus(data[1] as int);
final status = DownloadTaskStatus.fromInt(data[1] as int);
final progress = data[2] as int;

print(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class FlutterDownloader {
(dynamic item) {
return DownloadTask(
taskId: item['task_id'] as String,
status: DownloadTaskStatus(item['status'] as int),
status: DownloadTaskStatus.fromInt(item['status'] as int),
progress: item['progress'] as int,
url: item['url'] as String,
filename: item['file_name'] as String?,
Expand Down Expand Up @@ -211,7 +211,7 @@ class FlutterDownloader {
(dynamic item) {
return DownloadTask(
taskId: item['task_id'] as String,
status: DownloadTaskStatus(item['status'] as int),
status: DownloadTaskStatus.fromInt(item['status'] as int),
progress: item['progress'] as int,
url: item['url'] as String,
filename: item['file_name'] as String?,
Expand Down
61 changes: 30 additions & 31 deletions lib/src/models.dart
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
/// Defines a set of possible states which a [DownloadTask] can be in.
@pragma('vm:entry-point')
class DownloadTaskStatus {
/// Creates a new [DownloadTaskStatus].
const DownloadTaskStatus(int value) : _value = value;

final int _value;

/// The underlying index of this status.
int get value => _value;

enum DownloadTaskStatus {
/// Status of the task is either unknown or corrupted.
static const undefined = DownloadTaskStatus(0);
undefined,

/// The task is scheduled, but is not running yet.
static const enqueued = DownloadTaskStatus(1);
enqueued,

/// The task is in progress.
static const running = DownloadTaskStatus(2);
running,

/// The task has completed successfully.
static const complete = DownloadTaskStatus(3);
complete,

/// The task has failed.
static const failed = DownloadTaskStatus(4);
failed,

/// The task was canceled and cannot be resumed.
static const canceled = DownloadTaskStatus(5);

/// The task was paused and can be resumed.
static const paused = DownloadTaskStatus(6);

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
canceled,

/// The task was paused and can be resumed
paused;

/// Creates a new [DownloadTaskStatus] from an [int].
factory DownloadTaskStatus.fromInt(int value) {
switch (value) {
case 0:
return DownloadTaskStatus.undefined;
case 1:
return DownloadTaskStatus.enqueued;
case 2:
return DownloadTaskStatus.running;
case 3:
return DownloadTaskStatus.complete;
case 4:
return DownloadTaskStatus.failed;
case 5:
return DownloadTaskStatus.canceled;
case 6:
return DownloadTaskStatus.paused;
default:
throw ArgumentError('Invalid value: $value');
}

return other is DownloadTaskStatus && other._value == _value;
}

@override
int get hashCode => _value.hashCode;

@override
String toString() => 'DownloadTaskStatus($_value)';
}

/// Encapsulates all information of a single download task.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_downloader
description: Powerful plugin making it easy to download files.
version: 1.10.7
version: 1.11.0
repository: https://github.com/fluttercommunity/flutter_downloader
issue_tracker: https://github.com/fluttercommunity/flutter_downloader/issues
maintainer: Bartek Pacia (@bartekpacia)
Expand Down

0 comments on commit 4554a16

Please sign in to comment.