Skip to content
This repository was archived by the owner on Sep 22, 2024. It is now read-only.

Commit d7605cb

Browse files
committed
Support working on background #7
general improvements
1 parent c9f6932 commit d7605cb

File tree

6 files changed

+114
-50
lines changed

6 files changed

+114
-50
lines changed

integration_test/app_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22
import 'dart:math';
33
import 'package:file_picker/file_picker.dart';
4+
import 'package:weepy/classes/database.dart';
45
import 'package:weepy/classes/discover.dart';
56
import 'package:weepy/classes/exceptions.dart';
67
import 'package:weepy/classes/sender.dart';
@@ -17,6 +18,7 @@ void main() {
1718
var sendingFiles = <File>[];
1819
var platformFiles = <PlatformFile>[];
1920
late Directory subdir;
21+
//TODO: Refactor
2022
setUpAll(() async {
2123
final tempDir = await getTemporaryDirectory();
2224
subdir = tempDir.createTempSync("sending");
@@ -43,6 +45,32 @@ void main() {
4345
name: path.basename(sendingFiles[index].path),
4446
path: sendingFiles[index].path));
4547
});
48+
49+
group("Database tests", () {
50+
final db = DatabaseManager();
51+
setUp(() => db.clear());
52+
testWidgets("Downloaded file insert", (_) async {
53+
final file = DbFile(
54+
name: "test1",
55+
path: "/.../../",
56+
time: DateTime.now(),
57+
fileStatus: DbFileStatus.download);
58+
await db.insert(file);
59+
final savedFiles = await db.files;
60+
expect(savedFiles, equals([file]));
61+
});
62+
testWidgets("Uploaded file insert", (_) async {
63+
final file = DbFile(
64+
name: "test1",
65+
path: "/.../../",
66+
time: DateTime.now(),
67+
fileStatus: DbFileStatus.upload);
68+
await db.insert(file);
69+
final savedFiles = await db.files;
70+
expect(savedFiles, equals([file]));
71+
});
72+
tearDown(() => db.close());
73+
});
4674
group('IO tests', () {
4775
var downloadedFiles = <DbFile>[];
4876
Receiver? receiver;

integration_test/mobile_test.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,33 @@ void main() {
4242
name: path.basename(sendingFiles[index].path),
4343
path: sendingFiles[index].path));
4444
});
45+
46+
final dbStatusVariant = ValueVariant({true, false});
47+
4548
testWidgets("Test IsolatedReceiver & IsolatedSender", (_) async {
4649
receiver = IsolatedReceiver(
4750
saveToTemp: true,
48-
useDb: false,
51+
useDb: dbStatusVariant.currentValue!,
4952
onAllFilesDownloaded: (files) => downloadedFiles = files,
5053
onDownloadError: (error) => throw error,
51-
progressNotification: false);
54+
progressNotification: true);
5255
final code = await receiver!.listen();
5356
var allDevices = <Device>[];
5457
while (allDevices.isEmpty) {
5558
allDevices = await Discover.discover();
5659
}
5760
final devices = allDevices.where((device) => device.code == code);
5861
expect(devices, isNotEmpty, reason: "Expected to discover itself");
59-
await IsolatedSender(progressNotification: false)
60-
.send(devices.first, platformFiles, useDb: false);
62+
await IsolatedSender(progressNotification: false).send(
63+
devices.first, platformFiles,
64+
useDb: dbStatusVariant.currentValue!);
6165
for (var i = 0; i < sendingFiles.length; i++) {
6266
final gidenDosya = sendingFiles[i];
6367
final gelenDosya = File(downloadedFiles[i].path);
6468
expect(gidenDosya.readAsBytesSync(), equals(gelenDosya.readAsBytesSync()),
6569
reason: "All sent files expected to has same content as originals");
6670
}
67-
});
71+
}, variant: dbStatusVariant);
6872
tearDown(() {
6973
for (var file in downloadedFiles) {
7074
File(file.path).deleteSync();

lib/classes/database.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import '../models.dart';
66

77
class DatabaseManager {
88
bool _initalised = false;
9-
Future<Database> get _db {
9+
Future<Database> get _db async {
1010
if (!_initalised && (Platform.isLinux || Platform.isWindows)) {
1111
sqfliteFfiInit();
1212
databaseFactory = databaseFactoryFfiNoIsolate;
1313
}
14-
_initalised = true;
15-
return openDatabase("files.db", version: 2, onCreate: (db, version) async {
14+
final db = await openDatabase("files.db", version: 2,
15+
onCreate: (db, version) async {
1616
await db.execute(
1717
"create table downloaded (ID integer primary key autoincrement, name text not null, path text not null, type text, timeepoch int not null)");
1818
await db.execute(
@@ -26,7 +26,8 @@ class DatabaseManager {
2626
.execute("alter table uploaded rename column time to timeepoch");
2727
} on Exception catch (e) {
2828
//Some old android devices does not support 'alert table'
29-
//Workaround: Dropping table then recreating
29+
//
30+
//Workaround: Dropping table then recreating table
3031
//since database contains only file history that would not be a problem
3132
await FirebaseCrashlytics.instance.recordError(e, null,
3233
reason:
@@ -42,6 +43,8 @@ class DatabaseManager {
4243
throw UnsupportedError("Unsupported db version");
4344
}
4445
});
46+
_initalised = true;
47+
return db;
4548
}
4649

4750
///Insert a uploaded or downloaded file information

lib/classes/notifications.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,38 @@ AndroidNotificationDetails _androidDetails(int progress, Type type) {
2727
showProgress: true,
2828
maxProgress: 100,
2929
progress: progress,
30-
channelAction: AndroidNotificationChannelAction.update,
3130
category: AndroidNotificationCategory.progress);
3231
}
3332

3433
///Show or update download progress notification.
3534
///
3635
///Make sure [progress] within range 0 to 100
3736
Future<void> showDownload(int progress) => _localNotifications.show(
38-
0,
37+
Type.download.index,
3938
null,
4039
null,
4140
NotificationDetails(android: _androidDetails(progress, Type.download)));
4241

4342
///Show or update upload progress notification.
4443
///
4544
///Make sure [progress] within range 0 to 100
46-
Future<void> showUpload(int progress) => _localNotifications.show(1, null, null,
45+
Future<void> showUpload(int progress) => _localNotifications.show(
46+
Type.upload.index,
47+
null,
48+
null,
4749
NotificationDetails(android: _androidDetails(progress, Type.upload)));
4850

49-
Future<void> cancelDownload() => _localNotifications.cancel(0);
51+
Future<void> cancelDownload() async {
52+
//_localNotifications.cancel(Type.download.index);
53+
}
5054

51-
Future<void> cancelUpload() => _localNotifications.cancel(1);
55+
Future<void> cancelUpload() async {
56+
//_localNotifications.cancel(Type.upload.index);
57+
}
5258

59+
///Inıtalise local notifications.
60+
///
61+
///Call before using any method.
5362
Future<bool> initalise() async {
5463
final status = await _localNotifications.initialize(
5564
const InitializationSettings(

lib/classes/workers/worker_interface.dart

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:developer';
23
import 'dart:io';
34

45
import 'package:file_picker/file_picker.dart';
@@ -132,12 +133,12 @@ class IsolatedSender extends Sender {
132133
await _workManager.registerOneOffTask(MyTasks.send.name, MyTasks.send.name,
133134
inputData: map);
134135
if (progressNotification) {
135-
await notifications.showDownload(0);
136+
await notifications.showUpload(0);
136137
}
138+
await exitBlock.future;
137139
if (progressNotification) {
138-
await notifications.cancelDownload();
140+
await notifications.cancelUpload();
139141
}
140-
return exitBlock.future;
141142
}
142143

143144
@override
@@ -169,7 +170,8 @@ class IsolatedReceiver extends Receiver {
169170

170171
///Starts worker and runs [Receiver.listen]
171172
///
172-
///If called twice, it has no effect.
173+
///If necessary requests permission. Throws [NoStoragePermissionException]
174+
///if permission rejected by user.
173175
@override
174176
Future<int> listen() async {
175177
await initalize();
@@ -200,38 +202,44 @@ class IsolatedReceiver extends Receiver {
200202
}
201203

202204
Future<void> _portCallback(data) async {
203-
final type = messages.MessageType.values[data["type"]];
204-
switch (type) {
205-
case messages.MessageType.updatePercent:
206-
final message = messages.UpdatePercent.fromMap(data);
207-
if (progressNotification) {
208-
await notifications.showDownload((message.newPercent * 100).round());
209-
}
210-
super.onDownloadUpdatePercent?.call(message.newPercent);
211-
break;
212-
case messages.MessageType.filedropError:
213-
final message = messages.FiledropError.fromMap(data);
214-
if (progressNotification) {
215-
await notifications.cancelDownload();
216-
}
217-
super.onDownloadError?.call(message.exception);
218-
break;
219-
case messages.MessageType.fileDownloaded:
220-
final message = messages.FileDownloaded.fromMap(data);
221-
super.onFileDownloaded?.call(message.file);
222-
break;
223-
case messages.MessageType.allFilesDownloaded:
224-
final message = messages.AllFilesDownloaded.fromMap(data);
225-
if (progressNotification) {
226-
await notifications.cancelDownload();
227-
}
228-
super.onAllFilesDownloaded?.call(message.files.toList());
229-
break;
230-
case messages.MessageType.downloadStarted:
231-
final _ = messages.DownloadStarted.fromMap(data);
232-
super.onDownloadStart?.call();
233-
default:
234-
throw Error();
205+
try {
206+
final type = messages.MessageType.values[data["type"]];
207+
switch (type) {
208+
case messages.MessageType.updatePercent:
209+
final message = messages.UpdatePercent.fromMap(data);
210+
if (progressNotification) {
211+
await notifications
212+
.showDownload((message.newPercent * 100).round());
213+
}
214+
super.onDownloadUpdatePercent?.call(message.newPercent);
215+
break;
216+
case messages.MessageType.filedropError:
217+
final message = messages.FiledropError.fromMap(data);
218+
if (progressNotification) {
219+
await notifications.cancelDownload();
220+
}
221+
super.onDownloadError?.call(message.exception);
222+
break;
223+
case messages.MessageType.fileDownloaded:
224+
final message = messages.FileDownloaded.fromMap(data);
225+
super.onFileDownloaded?.call(message.file);
226+
break;
227+
case messages.MessageType.allFilesDownloaded:
228+
final message = messages.AllFilesDownloaded.fromMap(data);
229+
if (progressNotification) {
230+
await notifications.cancelDownload();
231+
}
232+
super.onAllFilesDownloaded?.call(message.files.toList());
233+
break;
234+
case messages.MessageType.downloadStarted:
235+
final _ = messages.DownloadStarted.fromMap(data);
236+
super.onDownloadStart?.call();
237+
default:
238+
throw Error();
239+
}
240+
} on Exception catch (e) {
241+
log("Interface error", name: "IsolatedReceiver", error: e);
242+
rethrow;
235243
}
236244
}
237245
}

lib/models.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ class DbFile {
8484
"timeepoch": timeEpoch,
8585
"fileStatus": fileStatus.index
8686
};
87+
@override
88+
int get hashCode =>
89+
path.hashCode ^ timeEpoch.hashCode ^ fileStatus.index.hashCode;
90+
91+
@override
92+
bool operator ==(Object other) {
93+
if (other is DbFile) {
94+
return other.hashCode == hashCode;
95+
} else {
96+
return false;
97+
}
98+
}
8799
}
88100

89101
class Device {

0 commit comments

Comments
 (0)