diff --git a/lib/cwtch/cwtch.dart b/lib/cwtch/cwtch.dart new file mode 100644 index 00000000..64ad763e --- /dev/null +++ b/lib/cwtch/cwtch.dart @@ -0,0 +1,14 @@ +abstract class Cwtch { + void Start(String appDir, String torPath) ; + + void SelectProfile(String onion); + + String ACNEvents(); + String ContactEvents(); + + String GetProfiles(); + String GetContacts(String onion); + + int NumMessages(String profile, String handle); + String GetMessage(String profile, String handle, int index); +} \ No newline at end of file diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart new file mode 100644 index 00000000..01003a76 --- /dev/null +++ b/lib/cwtch/ffi.dart @@ -0,0 +1,115 @@ +import 'dart:ffi'; + +import 'package:ffi/ffi.dart'; +import 'package:flutter_app/cwtch/cwtch.dart'; + +///////////////////// +/// Cwtch API /// +///////////////////// + +typedef start_cwtch_function = Void Function(Pointer str, Int32 length); +typedef StartCwtchFn = void Function(Pointer dir, int len); + +typedef access_cwtch_eventbus_function = Void Function(); +typedef NextEventFn = void Function(); + +typedef get_json_blob_void_function = Pointer Function(); +typedef GetJsonBlobVoidFn = Pointer Function(); + +typedef get_json_blob_string_function = Pointer Function(Pointer str, Int32 length); +typedef GetJsonBlobStringFn = Pointer Function(Pointer str,int len); + +//func NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int) (n C.int) { +typedef get_int_from_str_str_function = Int32 Function(Pointer, Int32, Pointer, Int32); +typedef GetIntFromStrStrFn = int Function(Pointer, int, Pointer, int); + +//func GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, message_index C.int) *C.char { +typedef get_json_blob_from_str_str_int_function = Pointer Function(Pointer, Int32, Pointer, Int32, Int32); +typedef GetJsonBlobFromStrStrIntFn = Pointer Function(Pointer, int, Pointer, int, int); + +typedef acn_events_function = Pointer Function(); +typedef ACNEventsFn = Pointer Function(); + +class CwtchFfi implements Cwtch { + DynamicLibrary library; + + CwtchFfi() { + library = DynamicLibrary.open("libCwtch.so"); + } + + void Start(String appDir, String torPath) { + var startCwtchC = library.lookup>("StartCwtch"); + // ignore: non_constant_identifier_names + final StartCwtch = startCwtchC.asFunction(); + StartCwtch(Utf8.toUtf8(appDir), appDir.length); + } + + void SelectProfile(String onion) { + var selectProfileC = library.lookup>("SelectProfile"); + // ignore: non_constant_identifier_names + final SelectProfile = selectProfileC.asFunction(); + + SelectProfile(Utf8.toUtf8(onion), onion.length); + } + + String ACNEvents() { + var acnEventsC = library.lookup>( + "ACNEvents"); + // ignore: non_constant_identifier_names + final ACNEvents = acnEventsC.asFunction(); + + Pointer result = ACNEvents(); + String event = Utf8.fromUtf8(result); + return event; + } + + + String ContactEvents() { + var acnEventsC = library.lookup>( + "ContactEvents"); + // ignore: non_constant_identifier_names + final ContactEvents = acnEventsC.asFunction(); + + Pointer result = ContactEvents(); + String event = Utf8.fromUtf8(result); + return event; + } + + String GetProfiles() { + var getProfilesC = library.lookup>("GetProfiles"); + // ignore: non_constant_identifier_names + final GetProfiles = getProfilesC.asFunction(); + + Pointer jsonProfilesBytes = GetProfiles(); + String jsonProfiles = Utf8.fromUtf8(jsonProfilesBytes); + return jsonProfiles; + } + + String GetContacts(String onion) { + var getContactsC = library.lookup>("GetContacts"); + // ignore: non_constant_identifier_names + final GetContacts = getContactsC.asFunction(); + Pointer jsonContactBytes = GetContacts(Utf8.toUtf8(onion), onion.length); + String jsonContacts = Utf8.fromUtf8(jsonContactBytes); + return jsonContacts; + } + + int NumMessages(String profile, String handle) { + var numMessagesC = library.lookup>("NumMessages"); + // ignore: non_constant_identifier_names + final NumMessages = numMessagesC.asFunction(); + + int num = NumMessages(Utf8.toUtf8(profile), profile.length, Utf8.toUtf8(handle), handle.length); + return num; + } + + String GetMessage(String profile, String handle, int index) { + var getMessageC = library.lookup>("GetMessage"); + // ignore: non_constant_identifier_names + final GetMessage = getMessageC.asFunction(); + + Pointer jsonMessageBytes = GetMessage(Utf8.toUtf8(profile), profile.length, Utf8.toUtf8(handle), handle.length, index); + String jsonMessage = Utf8.fromUtf8(jsonMessageBytes); + return jsonMessage; + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 8d34ecb4..5af85287 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,30 +1,13 @@ -import 'dart:ffi'; -import 'package:ffi/ffi.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_app/cwtch/ffi.dart'; import 'package:provider/provider.dart'; +import 'cwtch/cwtch.dart'; import 'model.dart'; import 'views/profilemgrview.dart'; import 'package:flutter/services.dart'; import 'dart:io' show Platform; import 'package:path/path.dart' as path; import 'package:path_provider/path_provider.dart'; -import 'model.dart' as model; -import 'dart:collection'; -import 'dart:convert'; -import 'package:flutter/material.dart'; - -typedef start_cwtch_function = Void Function(Pointer str, Int32 length); -typedef StartCwtchFn = void Function(Pointer dir, int len); - -typedef access_cwtch_eventbus_function = Void Function(); -typedef NextEventFn = void Function(); - - -typedef get_json_blob_void_function = Pointer Function(); -typedef GetJsonBlobVoidFn = Pointer Function(); - -typedef get_json_blob_string_function = Pointer Function(Pointer str, Int32 length); -typedef GetJsonBlobStringFn = Pointer Function(Pointer str,int len); void main() => runApp(Flwtch()); @@ -38,7 +21,7 @@ class Flwtch extends StatefulWidget { class FlwtchState extends State { final TextStyle biggerFont = const TextStyle(fontSize: 18); - DynamicLibrary library; + Cwtch cwtch; AppModel appStatus; static const appInfoPlatform = const MethodChannel('test.flutter.dev/applicationInfo'); @@ -70,8 +53,10 @@ class FlwtchState extends State { @override Widget build(BuildContext context) { - library = DynamicLibrary.open("libCwtch.so"); - appStatus = AppModel(library: library); + + cwtch = CwtchFfi(); + + appStatus = AppModel(cwtch: cwtch); String home = ""; Map envVars = Platform.environment; @@ -85,13 +70,9 @@ class FlwtchState extends State { home = androidHomeDirectory; } - var startCwtchC = library.lookup>("StartCwtch"); - // ignore: non_constant_identifier_names - final StartCwtch = startCwtchC.asFunction(); - var cwtchDir = path.join(home, ".cwtch/dev/"); print("cwtchDir $cwtchDir"); - StartCwtch(Utf8.toUtf8(cwtchDir), cwtchDir.length); + cwtch.Start(cwtchDir, "tor"); return Provider( create: (_) => this, diff --git a/lib/model.dart b/lib/model.dart index dd9a2b16..8e69144a 100644 --- a/lib/model.dart +++ b/lib/model.dart @@ -3,6 +3,8 @@ import 'package:ffi/ffi.dart'; import 'dart:async'; import 'dart:collection'; +import 'cwtch/cwtch.dart'; + //////////////////// /// UI State /// //////////////////// @@ -28,22 +30,15 @@ class ContactModel { /// ACN /// ///////////// -typedef acn_events_function = Pointer Function(); -typedef ACNEventsFn = Pointer Function(); + class AppModel { - final DynamicLibrary library; - AppModel({this.library}); + final Cwtch cwtch; + AppModel({this.cwtch}); Stream contactEvents() async* { - var acnEventsC = library.lookup>( - "ContactEvents"); - // ignore: non_constant_identifier_names - final ContactEvents = acnEventsC.asFunction(); - while (true) { - Pointer result = ContactEvents(); - String event = Utf8.fromUtf8(result); + String event = cwtch.ContactEvents(); if (event != "") { print(event); yield event; @@ -53,16 +48,9 @@ class AppModel { } } - Stream torStatus() async* { - var acnEventsC = library.lookup>( - "ACNEvents"); - // ignore: non_constant_identifier_names - final ACNEvents = acnEventsC.asFunction(); - while (true) { - Pointer result = ACNEvents(); - String event = Utf8.fromUtf8(result); + String event = cwtch.ACNEvents(); if (event != "") { yield event; } else { @@ -71,27 +59,3 @@ class AppModel { } } } - -///////////////////// -/// Cwtch API /// -///////////////////// - -typedef start_cwtch_function = Void Function(Pointer str, Int32 length); -typedef StartCwtchFn = void Function(Pointer dir, int len); - -typedef access_cwtch_eventbus_function = Void Function(); -typedef NextEventFn = void Function(); - -typedef get_json_blob_void_function = Pointer Function(); -typedef GetJsonBlobVoidFn = Pointer Function(); - -typedef get_json_blob_string_function = Pointer Function(Pointer str, Int32 length); -typedef GetJsonBlobStringFn = Pointer Function(Pointer str,int len); - -//func NumMessages(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int) (n C.int) { -typedef get_int_from_str_str_function = Int32 Function(Pointer, Int32, Pointer, Int32); -typedef GetIntFromStrStrFn = int Function(Pointer, int, Pointer, int); - -//func GetMessage(profile_ptr *C.char, profile_len C.int, handle_ptr *C.char, handle_len C.int, message_index C.int) *C.char { -typedef get_json_blob_from_str_str_int_function = Pointer Function(Pointer, Int32, Pointer, Int32, Int32); -typedef GetJsonBlobFromStrStrIntFn = Pointer Function(Pointer, int, Pointer, int, int); diff --git a/lib/views/contactsview.dart b/lib/views/contactsview.dart index c24e4d0d..f9a07650 100644 --- a/lib/views/contactsview.dart +++ b/lib/views/contactsview.dart @@ -31,15 +31,10 @@ class _ContactsViewState extends State { Widget _buildContactList() { - var getContactsC = Provider.of(context).library.lookup>("GetContacts"); - // ignore: non_constant_identifier_names - final GetContacts = getContactsC.asFunction(); - return StreamBuilder( stream: Provider.of(context).appStatus.contactEvents(), builder: (BuildContext context, AsyncSnapshot snapshot) { - Pointer jsonContactBytes = GetContacts(Utf8.toUtf8(widget.profileOnion), widget.profileOnion.length); - String jsonContacts = Utf8.fromUtf8(jsonContactBytes); + String jsonContacts = Provider.of(context).cwtch.GetContacts(widget.profileOnion); print(jsonContacts); List contacts = jsonDecode(jsonContacts); diff --git a/lib/views/messageview.dart b/lib/views/messageview.dart index 3d3aae81..4aae7daf 100644 --- a/lib/views/messageview.dart +++ b/lib/views/messageview.dart @@ -29,32 +29,20 @@ class _MessageViewState extends State { } Widget _buildMessageView(BuildContext context) { - var numMessagesC = Provider.of(context).library.lookup>("NumMessages"); - // ignore: non_constant_identifier_names - final NumMessages = numMessagesC.asFunction(); - var getMessageC = Provider.of(context).library.lookup>("GetMessage"); - // ignore: non_constant_identifier_names - final GetMessage = getMessageC.asFunction(); - - _updateMessageCount(context, NumMessages); + _updateMessageCount(context); if (timer == null) { timer = Timer.periodic(Duration(seconds: 1), (Timer t) { print("tick"); - _updateMessageCount(context, NumMessages); + _updateMessageCount(context); }); } - return ProxyProvider0( update: (_, __) => MessageCounter(conversationNumMessages), child: ListView.builder( itemCount: conversationNumMessages, itemBuilder: (context, index) { - Pointer jsonMessageBytes = GetMessage( - Utf8.toUtf8(widget.profileOnion), widget.profileOnion.length, - Utf8.toUtf8(widget.conversationHandle), widget.conversationHandle.length, - index); - String jsonMessage = Utf8.fromUtf8(jsonMessageBytes); + String jsonMessage = Provider.of(context).cwtch.GetMessage(widget.profileOnion, widget.conversationHandle, index); //print(jsonMessage); dynamic messageWrapper = jsonDecode(jsonMessage); dynamic message = jsonDecode(messageWrapper['Message']); @@ -68,18 +56,13 @@ class _MessageViewState extends State { )); } - Future _updateMessageCount(BuildContext context, GetIntFromStrStrFn cfn) async { + Future _updateMessageCount(BuildContext context) async { if (!mounted) { if (timer != null && timer.isActive) timer.cancel(); return; } - setState(() { - conversationNumMessages = cfn( - Utf8.toUtf8(widget.profileOnion), widget.profileOnion.length, - Utf8.toUtf8(widget.conversationHandle), - widget.conversationHandle.length, - ); + conversationNumMessages = Provider.of(context).cwtch.NumMessages(widget.profileOnion, widget.conversationHandle); }); } } diff --git a/lib/views/profilemgrview.dart b/lib/views/profilemgrview.dart index dfcb09ac..6b4334ab 100644 --- a/lib/views/profilemgrview.dart +++ b/lib/views/profilemgrview.dart @@ -24,12 +24,8 @@ class _ProfileMgrViewState extends State { } if (_profiles.length < 1) { - var getProfilesC = Provider.of(context).library.lookup>("GetProfiles"); - // ignore: non_constant_identifier_names - final GetProfiles = getProfilesC.asFunction(); - Pointer jsonProfilesBytes = GetProfiles(); - String jsonProfiles = Utf8.fromUtf8(jsonProfilesBytes); + String jsonProfiles = Provider.of(context).cwtch.GetProfiles(); print(jsonProfiles); Map profiles = jsonDecode(jsonProfiles); @@ -84,11 +80,7 @@ class _ProfileMgrViewState extends State { subtitle: Text(profile.onion), onTap: () { setState(() { - var selectProfileC = Provider.of(context, listen:false).library.lookup>("SelectProfile"); - // ignore: non_constant_identifier_names - final SelectProfile = selectProfileC.asFunction(); - - SelectProfile(Utf8.toUtf8(profile.onion), profile.onion.length); + Provider.of(context, listen:false).cwtch.SelectProfile(profile.onion); _pushContactList(profile.onion); }); }, diff --git a/lib/widgets/torstatuslabel.dart b/lib/widgets/torstatuslabel.dart index 76c29602..daef03d5 100644 --- a/lib/widgets/torstatuslabel.dart +++ b/lib/widgets/torstatuslabel.dart @@ -31,13 +31,4 @@ class _TorStatusState extends State { }, )); } - - void _incrementCounter() { - setState(() { - var nextEventC = Provider.of(context).library.lookup>("NextEvent"); - // ignore: non_constant_identifier_names - final NextEvent = nextEventC.asFunction(); - NextEvent(); - }); - } }