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

Commit

Permalink
refactor: updating receive logic for running from isolate
Browse files Browse the repository at this point in the history
Have to update ui from callback instead passing animationcontroller for supporting isolates
  • Loading branch information
iamalper committed Nov 27, 2023
1 parent fe04914 commit 79eef8e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ios/Runner/GeneratedPluginRegistrant.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
@import url_launcher_ios;
#endif

#if __has_include(<workmanager/WorkmanagerPlugin.h>)
#import <workmanager/WorkmanagerPlugin.h>
#else
@import workmanager;
#endif

@implementation GeneratedPluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
Expand All @@ -107,6 +113,7 @@ + (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
[SqflitePlugin registerWithRegistrar:[registry registrarForPlugin:@"SqflitePlugin"]];
[Sqlite3FlutterLibsPlugin registerWithRegistrar:[registry registrarForPlugin:@"Sqlite3FlutterLibsPlugin"]];
[URLLauncherPlugin registerWithRegistrar:[registry registrarForPlugin:@"URLLauncherPlugin"]];
[WorkmanagerPlugin registerWithRegistrar:[registry registrarForPlugin:@"WorkmanagerPlugin"]];
}

@end
15 changes: 13 additions & 2 deletions lib/classes/receiver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ class Receiver {
final bool saveToTemp;

///If [downloadAnimC] is set, progress will be sent to it.
@Deprecated(
"Prefer downloadUpdatePercent() because it allows updating UI from isolates")
final AnimationController? downloadAnimC;

///[onDownloadUpdatePercent] will be called for each saved chunk in download operation.
///
///Use for animating progress.
final void Function(double percent)? onDownloadUpdatePercent;

///[port] listened for incoming connections. Should not set except testing or
///other devices will require manual port setting.
final int? port;
Expand All @@ -62,11 +69,12 @@ class Receiver {

///Listen and receive files from other devices.
///
///Set [downloadAnimC], [onDownloadStart], [onFileDownloaded], [onAllFilesDownloaded] for animating download progess.
///Set [onDownloadUpdatePercent], [onDownloadStart], [onFileDownloaded], [onAllFilesDownloaded] for animating download progess.
///
///Call [listen] for start listening.
Receiver(
{this.downloadAnimC,
this.onDownloadUpdatePercent,
this.useDb = true,
this.saveToTemp = false,
this.port,
Expand Down Expand Up @@ -157,9 +165,12 @@ class Receiver {
}
final totalLengh = request.contentLength!;
final fileWriter = file.openWrite();
var downloadPercent = 0.0;
await for (var bytes in mime.timeout(const Duration(seconds: 10))) {
fileWriter.add(bytes);
downloadAnimC?.value += bytes.length / totalLengh;
downloadPercent += bytes.length / totalLengh;
downloadAnimC?.value = downloadPercent;
onDownloadUpdatePercent?.call(downloadPercent);
}
await fileWriter.flush();
await fileWriter.close();
Expand Down
29 changes: 29 additions & 0 deletions lib/classes/worker.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dart:developer';
import 'dart:ui';
import 'package:workmanager/workmanager.dart';

enum MyTasks { receive }

enum IsolatePorts { receive }

final _workManager = Workmanager();

@pragma("vm:entry-point")
void _callBack() {
_workManager.executeTask((taskName, inputData) async {
final task =
MyTasks.values.singleWhere((element) => element.name == taskName);
switch (task) {
case MyTasks.receive:
final sendPort =
IsolateNameServer.lookupPortByName(IsolatePorts.receive.name);
return true;
}
});
}

Future<void> initalize() async {
throw UnimplementedError();
// ignore: dead_code
await _workManager.initialize(_callBack);
}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.1.0"
workmanager:
dependency: "direct main"
description:
name: workmanager
sha256: ed13530cccd28c5c9959ad42d657cd0666274ca74c56dea0ca183ddd527d3a00
url: "https://pub.dev"
source: hosted
version: "0.5.2"
xdg_directories:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 1.0.0+1

environment:
sdk: '>=2.19.6 <3.0.0'
sdk: '>=3.2.0 <4.0.0'

dependencies:
network_discovery: ^1.0.0
Expand Down Expand Up @@ -39,6 +39,7 @@ dependencies:
flutter_riverpod: ^2.4.0
cupertino_icons: ^1.0.6
num_remap: ^1.0.1
workmanager: ^0.5.2

dev_dependencies:
flutter_test:
Expand Down

1 comment on commit 79eef8e

@iamalper
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#7

Please sign in to comment.