Skip to content

Commit 4554a16

Browse files
authored
Convert DownloadTaskStatus into enum (#835)
* convert `DownloadTaskStatus` into enum * remove redundant toString() * bump version to 1.11.0
1 parent d59e16d commit 4554a16

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.11.0
2+
3+
- Convert `DownloadTaskStatus` into an `enum` (#835)
4+
15
## 1.10.7
26

37
- Override `operator ==` and `hashCode` for `DownloadTask` (#875)

example/lib/home_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class _MyHomePageState extends State<MyHomePage> {
6464
}
6565
_port.listen((dynamic data) {
6666
final taskId = (data as List<dynamic>)[0] as String;
67-
final status = DownloadTaskStatus(data[1] as int);
67+
final status = DownloadTaskStatus.fromInt(data[1] as int);
6868
final progress = data[2] as int;
6969

7070
print(

lib/src/downloader.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class FlutterDownloader {
153153
(dynamic item) {
154154
return DownloadTask(
155155
taskId: item['task_id'] as String,
156-
status: DownloadTaskStatus(item['status'] as int),
156+
status: DownloadTaskStatus.fromInt(item['status'] as int),
157157
progress: item['progress'] as int,
158158
url: item['url'] as String,
159159
filename: item['file_name'] as String?,
@@ -211,7 +211,7 @@ class FlutterDownloader {
211211
(dynamic item) {
212212
return DownloadTask(
213213
taskId: item['task_id'] as String,
214-
status: DownloadTaskStatus(item['status'] as int),
214+
status: DownloadTaskStatus.fromInt(item['status'] as int),
215215
progress: item['progress'] as int,
216216
url: item['url'] as String,
217217
filename: item['file_name'] as String?,

lib/src/models.dart

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
11
/// Defines a set of possible states which a [DownloadTask] can be in.
22
@pragma('vm:entry-point')
3-
class DownloadTaskStatus {
4-
/// Creates a new [DownloadTaskStatus].
5-
const DownloadTaskStatus(int value) : _value = value;
6-
7-
final int _value;
8-
9-
/// The underlying index of this status.
10-
int get value => _value;
11-
3+
enum DownloadTaskStatus {
124
/// Status of the task is either unknown or corrupted.
13-
static const undefined = DownloadTaskStatus(0);
5+
undefined,
146

157
/// The task is scheduled, but is not running yet.
16-
static const enqueued = DownloadTaskStatus(1);
8+
enqueued,
179

1810
/// The task is in progress.
19-
static const running = DownloadTaskStatus(2);
11+
running,
2012

2113
/// The task has completed successfully.
22-
static const complete = DownloadTaskStatus(3);
14+
complete,
2315

2416
/// The task has failed.
25-
static const failed = DownloadTaskStatus(4);
17+
failed,
2618

2719
/// The task was canceled and cannot be resumed.
28-
static const canceled = DownloadTaskStatus(5);
29-
30-
/// The task was paused and can be resumed.
31-
static const paused = DownloadTaskStatus(6);
32-
33-
@override
34-
bool operator ==(Object other) {
35-
if (identical(this, other)) {
36-
return true;
20+
canceled,
21+
22+
/// The task was paused and can be resumed
23+
paused;
24+
25+
/// Creates a new [DownloadTaskStatus] from an [int].
26+
factory DownloadTaskStatus.fromInt(int value) {
27+
switch (value) {
28+
case 0:
29+
return DownloadTaskStatus.undefined;
30+
case 1:
31+
return DownloadTaskStatus.enqueued;
32+
case 2:
33+
return DownloadTaskStatus.running;
34+
case 3:
35+
return DownloadTaskStatus.complete;
36+
case 4:
37+
return DownloadTaskStatus.failed;
38+
case 5:
39+
return DownloadTaskStatus.canceled;
40+
case 6:
41+
return DownloadTaskStatus.paused;
42+
default:
43+
throw ArgumentError('Invalid value: $value');
3744
}
38-
39-
return other is DownloadTaskStatus && other._value == _value;
4045
}
41-
42-
@override
43-
int get hashCode => _value.hashCode;
44-
45-
@override
46-
String toString() => 'DownloadTaskStatus($_value)';
4746
}
4847

4948
/// Encapsulates all information of a single download task.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_downloader
22
description: Powerful plugin making it easy to download files.
3-
version: 1.10.7
3+
version: 1.11.0
44
repository: https://github.com/fluttercommunity/flutter_downloader
55
issue_tracker: https://github.com/fluttercommunity/flutter_downloader/issues
66
maintainer: Bartek Pacia (@bartekpacia)

0 commit comments

Comments
 (0)