clean up cwtchNotifier calls and delete test code and add comments

This commit is contained in:
Dan Ballard 2021-02-09 15:47:12 -08:00
parent cbab08f072
commit 6b0238b3e7
4 changed files with 16 additions and 68 deletions

View File

@ -1,5 +1,7 @@
import '../model.dart';
// Class that handles libcwtch-go events (received either via ffi with an isolate or gomobile over a method channel from kotlin)
// Takes Notifiers and triggers them on appropriate events
class CwtchNotifier {
ProfileListState profileCN;

View File

@ -47,22 +47,14 @@ typedef GetJsonBlobFromStrStrIntIntFn = Pointer<Utf8> Function(Pointer<Utf8>, in
typedef acn_events_function = Pointer<Utf8> Function();
typedef ACNEventsFn = Pointer<Utf8> Function();
typedef repaint_event_function = Int8 Function();
typedef RepaintEventFn = int Function();
class CwtchFfi implements Cwtch {
DynamicLibrary library;
Stream<bool> repaintProfileStream;
ProfileListState profileCN;
CwtchNotifier cwtchNotifier;
Isolate cwtchIsolate;
CwtchFfi(ProfileListState profs) {
CwtchFfi(CwtchNotifier _cwtchNotifier) {
library = DynamicLibrary.open("libCwtch.so");
profileCN = profs;
cwtchNotifier = new CwtchNotifier(profs);
cwtchNotifier = _cwtchNotifier;
}
Future<void> Start() async {
@ -81,54 +73,32 @@ class CwtchFfi implements Cwtch {
final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
StartCwtch(Utf8.toUtf8(cwtchDir), cwtchDir.length, Utf8.toUtf8(""), 0);
// Test non timer getting events from libcwtch-go
// Spawn an isolate to listen to events from libcwtch-go and then dispatch them when received on main thread to cwtchNotifier
var _receivePort = ReceivePort();
cwtchIsolate = await Isolate.spawn(_checkAppbusEvents, _receivePort.sendPort);
_receivePort.listen((message) {
var env = jsonDecode(message);
cwtchNotifier.handleMessage(env["EventType"], env["Data"]);
});
//repaintProfileStream = pollRepaintProfileEvents();
}
// Called on object being disposed to (presumably on app close) to close the isolate that's listening to libcwtch-go events
@override
void dispose() {
if (cwtchIsolate != null) {
cwtchIsolate.kill();
}
}
/*
Stream<bool> GetRepaintProfileStream() {
return repaintProfileStream;
}
Stream<bool> pollRepaintProfileEvents() async* {
var getProfileRepaintEventC = library.lookup<NativeFunction<repaint_event_function>>("c_GetProfileRepaintEvent");
final GetProfileRepaintEvent = getProfileRepaintEventC.asFunction<RepaintEventFn>();
while (true) {
GetProfileRepaintEvent();
yield true;
}
}*/
// Test reader of a potential method for libcwtch-go to "push" methods into dart
// statis so it can be an isolate
// Entry point for an isolate to listen to a stream of events pulled from libcwtch-go and return them on the sendPort
static void _checkAppbusEvents(SendPort sendPort) async {
var stream = pollAppbusEvents();
await for (var value in stream) {
//print("_checkAppbuss events: $value");
sendPort.send(value);
}
}
// Steam of appbus events. should block in libcwtch-go GetAppbusEvent
// currently statis so the isolate tester can use it, shouldn't need to be static
// for other uses
// Steam of appbus events. Call blocks in libcwtch-go GetAppbusEvent. Static so the isolate can use it
static Stream<String> pollAppbusEvents() async* {
var library = DynamicLibrary.open("libCwtch.so");
var getAppbusEventC = library.lookup<NativeFunction<acn_events_function>>("c_GetAppBusEvent");

View File

@ -30,17 +30,15 @@ class CwtchGomobile implements Cwtch {
Future<String> androidLibraryDir;
Future<Directory> androidHomeDirectory;
ProfileListState profileCN;
CwtchNotifier cwtchNotifier;
CwtchGomobile(ProfileListState profs) {
CwtchGomobile(CwtchNotifier _cwtchNotifier) {
print("gomobile.dart: CwtchGomobile()");
profileCN = profs;
cwtchNotifier = new CwtchNotifier(profs);
cwtchNotifier = _cwtchNotifier;
androidHomeDirectory = getApplicationDocumentsDirectory();
androidLibraryDir = appInfoPlatform.invokeMethod('getNativeLibDir');
// Method channel to receive libcwtch-go events via Kotlin and dispatch them to _handleAppbusEvent (sends to cwtchNotifier)
final appbusEventChannel = MethodChannel(appbusEventChannelName);
appbusEventChannel.setMethodCallHandler(this._handleAppbusEvent);
}
@ -53,6 +51,7 @@ class CwtchGomobile implements Cwtch {
cwtchPlatform.invokeMethod("Start", {"appDir": cwtchDir, "torPath": torPath});
}
// Handle libcwtch-go events (received via kotlin) and dispatch to the cwtchNotifier
Future<void> _handleAppbusEvent(MethodCall call) async {
final String json = call.arguments;
var obj = jsonDecode(json);

View File

@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_app/views/triplecolview.dart';
import 'package:provider/provider.dart';
import 'cwtch/cwtch.dart';
import 'cwtch/cwtchNotifier.dart';
import 'model.dart';
import 'views/profilemgrview.dart';
import 'views/splashView.dart';
@ -36,11 +37,12 @@ class FlwtchState extends State<Flwtch> {
super.initState();
cwtchInit = false;
profs = ProfileListState();
var cwtchNotifier = new CwtchNotifier(profs);
if (Platform.isAndroid) {
cwtch = CwtchGomobile(profs);
cwtch = CwtchGomobile(cwtchNotifier);
} else {
cwtch = CwtchFfi(profs);
cwtch = CwtchFfi(cwtchNotifier);
}
cwtch.Start().then((val) {
@ -50,31 +52,6 @@ class FlwtchState extends State<Flwtch> {
});
appStatus = AppModel(cwtch: cwtch);
/* // Timing issue? Start may not have inited cwtch yet when we ask for getProfiles...
}
void loadProfiles() {
cwtch.GetProfiles().then((profilesJson) {
if (profilesJson != null) {
setState(() {
jsonDecode(profilesJson).forEach((profile) {
ProfileModel profile1 = new ProfileModel();
profile1.onion = profile['onion'];
profile1.nickname = profile['name'];
profile1.creationDate = "4 jan 2020";
profile1.contacts = new HashMap<String, ContactModel>();
profile1.imagePath = profile['imagePath'];
profiles.putIfAbsent(profile1.onion, () => profile1);
});
});
}
});
}
ChangeNotifierProvider<OpaqueTheme> getOpaqueProvider() {
return ChangeNotifierProvider(create: (context) => OpaqueTheme(Opaque.dark)); */
}
ChangeNotifierProvider<OpaqueTheme> getOpaqueProvider() => ChangeNotifierProvider(create: (context) => OpaqueTheme(Opaque.dark));