453 lines
14 KiB
Dart
453 lines
14 KiB
Dart
|
import 'dart:ffi';
|
||
|
import 'package:ffi/ffi.dart';
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'dart:io' show Platform;
|
||
|
import 'package:path/path.dart' as path;
|
||
|
import 'model.dart' as model;
|
||
|
import 'dart:collection';
|
||
|
import 'dart:convert';
|
||
|
import 'package:flutter/material.dart';
|
||
|
|
||
|
typedef start_cwtch_function = Void Function(Pointer<Utf8> str, Int32 length);
|
||
|
typedef StartCwtchFn = void Function(Pointer<Utf8> dir, int len);
|
||
|
|
||
|
typedef access_cwtch_eventbus_function = Void Function();
|
||
|
typedef NextEventFn = void Function();
|
||
|
|
||
|
|
||
|
typedef get_json_blob_void_function = Pointer<Utf8> Function();
|
||
|
typedef GetJsonBlobVoidFn = Pointer<Utf8> Function();
|
||
|
|
||
|
typedef get_json_blob_string_function = Pointer<Utf8> Function(Pointer<Utf8> str, Int32 length);
|
||
|
typedef GetJsonBlobStringFn = Pointer<Utf8> Function(Pointer<Utf8> str,int len);
|
||
|
|
||
|
|
||
|
class ProfileModel {
|
||
|
String onion;
|
||
|
String nickname;
|
||
|
String creationDate;
|
||
|
HashMap<String, ContactModel> contacts;
|
||
|
}
|
||
|
|
||
|
class ContactModel {
|
||
|
String onion;
|
||
|
String nickname;
|
||
|
bool isGroup;
|
||
|
bool isBlocked;
|
||
|
String status;
|
||
|
List<MessageModel> messages;
|
||
|
|
||
|
ContactModel({this.onion, this.nickname, this.status});
|
||
|
|
||
|
}
|
||
|
|
||
|
class MessageModel {
|
||
|
String from;
|
||
|
String message;
|
||
|
String timestamp;
|
||
|
}
|
||
|
|
||
|
|
||
|
void main() => runApp(MyApp());
|
||
|
|
||
|
|
||
|
class MyApp extends StatelessWidget {
|
||
|
// This widget is the root of your application.
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
|
||
|
var library = DynamicLibrary.open("libCwtch.so");
|
||
|
|
||
|
var startCwtchC = library.lookup<NativeFunction<start_cwtch_function>>("StartCwtch");
|
||
|
final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
|
||
|
|
||
|
var cwtchDir = "/home/sarah/.cwtch/dev/";
|
||
|
StartCwtch(Utf8.toUtf8(cwtchDir), cwtchDir.length);
|
||
|
|
||
|
|
||
|
return MaterialApp(
|
||
|
title: 'Flutter Demo',
|
||
|
theme: ThemeData(
|
||
|
// This is the theme of your application.
|
||
|
//
|
||
|
// Try running your application with "flutter run". You'll see the
|
||
|
// application has a blue toolbar. Then, without quitting the app, try
|
||
|
// changing the primarySwatch below to Colors.green and then invoke
|
||
|
// "hot reload" (press "r" in the console where you ran "flutter run",
|
||
|
// or simply save your changes to "hot reload" in a Flutter IDE).
|
||
|
// Notice that the counter didn't reset back to zero; the application
|
||
|
// is not restarted.
|
||
|
primarySwatch: Colors.blue,
|
||
|
// This makes the visual density adapt to the platform that you run
|
||
|
// the app on. For desktop platforms, the controls will be smaller and
|
||
|
// closer together (more dense) than on mobile platforms.
|
||
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||
|
primaryColor: Color(0xFF4B3557),
|
||
|
canvasColor: Color(0xFFB09CBC),
|
||
|
accentColor: Color(0xFFD01972),
|
||
|
),
|
||
|
home:RandomWords(title: 'Cwtch App', library:library, appStatus: model.AppModel(library: library)),
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
class RandomWords extends StatefulWidget {
|
||
|
|
||
|
RandomWords({Key key, this.title, this.library, this.appStatus}) : super(key: key);
|
||
|
|
||
|
// This widget is the home page of your application. It is stateful, meaning
|
||
|
// that it has a State object (defined below) that contains fields that affect
|
||
|
// how it looks.
|
||
|
|
||
|
// This class is the configuration for the state. It holds the values (in this
|
||
|
// case the title) provided by the parent (in this case the App widget) and
|
||
|
// used by the build method of the State. Fields in a Widget subclass are
|
||
|
// always marked "final".
|
||
|
|
||
|
final String title;
|
||
|
final DynamicLibrary library;
|
||
|
final model.AppModel appStatus;
|
||
|
|
||
|
@override
|
||
|
_RandomWordsState createState() => _RandomWordsState();
|
||
|
}
|
||
|
|
||
|
class _RandomWordsState extends State<RandomWords> {
|
||
|
HashMap<String, ProfileModel> _profiles;
|
||
|
String currentlySelectedProfile = "";
|
||
|
String currentlySelectedConversation = "";
|
||
|
|
||
|
// example stuff
|
||
|
final TextStyle _biggerFont = const TextStyle(fontSize: 18);
|
||
|
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
|
||
|
if (_profiles == null) {
|
||
|
_profiles = new HashMap<String, ProfileModel>();
|
||
|
}
|
||
|
|
||
|
if (_profiles.length < 1) {
|
||
|
|
||
|
var getProfilesC = widget.library.lookup<NativeFunction<get_json_blob_void_function>>("GetProfiles");
|
||
|
final GetProfiles = getProfilesC.asFunction<GetJsonBlobVoidFn>();
|
||
|
|
||
|
|
||
|
|
||
|
Pointer<Utf8> jsonProfilesBytes = GetProfiles();
|
||
|
String jsonProfiles = Utf8.fromUtf8(jsonProfilesBytes);
|
||
|
print(jsonProfiles);
|
||
|
|
||
|
Map<String, dynamic> profiles = jsonDecode(jsonProfiles);
|
||
|
|
||
|
profiles.forEach((onion,nick) {
|
||
|
ProfileModel profile1 = new ProfileModel();
|
||
|
profile1.onion = onion;
|
||
|
profile1.nickname = nick;
|
||
|
profile1.creationDate = "4 jan 2020";
|
||
|
|
||
|
profile1.contacts = new HashMap<String, ContactModel>();
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
_profiles.putIfAbsent(profile1.onion, () => profile1);
|
||
|
});
|
||
|
|
||
|
// ProfileModel profile1 = new ProfileModel();
|
||
|
// profile1.onion = "xyzonion";
|
||
|
// profile1.nickname = "Erinn";
|
||
|
// profile1.creationDate = "4 jan 2020";
|
||
|
// profile1.contacts = new HashMap<String, ContactModel>();
|
||
|
// ProfileModel profile2 = new ProfileModel();
|
||
|
// profile2.onion = "baphonion";
|
||
|
// profile2.nickname = "Baphomet";
|
||
|
// profile2.creationDate = "6 jan 2020";
|
||
|
// profile2.contacts = new HashMap<String, ContactModel>();
|
||
|
//
|
||
|
// ContactModel contactErinn = new ContactModel();
|
||
|
// contactErinn.onion = profile1.onion;
|
||
|
// contactErinn.nickname = profile1.nickname;
|
||
|
// MessageModel m0 = new MessageModel();
|
||
|
// m0.from = contactErinn.onion;
|
||
|
// m0.message = "hello baphomet!!";
|
||
|
// m0.timestamp = "7 jan 2020";
|
||
|
// MessageModel m1 = new MessageModel();
|
||
|
// m1.from = profile2.onion;
|
||
|
// m1.message = "well hi there! can't believe this worked!";
|
||
|
// m1.timestamp = "7 jan 2020";
|
||
|
// contactErinn.messages = <MessageModel>[];
|
||
|
// contactErinn.messages.add(m0);
|
||
|
// contactErinn.messages.add(m1);
|
||
|
|
||
|
// profile2.contacts.putIfAbsent(contactErinn.onion, () => contactErinn);
|
||
|
// _profiles.putIfAbsent(profile1.onion, () => profile1);
|
||
|
// _profiles.putIfAbsent(profile2.onion, () => profile2);
|
||
|
}
|
||
|
|
||
|
return Scaffold (
|
||
|
appBar: AppBar(
|
||
|
title: Text('Profiles'),
|
||
|
actions: [
|
||
|
IconButton(icon: Icon(Icons.list)),
|
||
|
],
|
||
|
),
|
||
|
body: _buildProfileManager(),//_buildSuggestions(),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
void _pushContactList() {
|
||
|
Navigator.of(context).push(
|
||
|
MaterialPageRoute<void>(
|
||
|
builder: (BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: Text('Contacts for ' + currentlySelectedProfile),
|
||
|
),
|
||
|
body: _buildContactList(),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Widget _buildContactList() {
|
||
|
|
||
|
var getContactsC = widget.library.lookup<NativeFunction<get_json_blob_string_function>>("GetContacts");
|
||
|
final GetContacts = getContactsC.asFunction<GetJsonBlobStringFn>();
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
return StreamBuilder<String>(
|
||
|
stream: widget.appStatus.contactEvents(),
|
||
|
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||
|
|
||
|
|
||
|
|
||
|
Pointer<Utf8> jsonContactBytes = GetContacts(Utf8.toUtf8(currentlySelectedProfile), currentlySelectedProfile.length);
|
||
|
String jsonContacts = Utf8.fromUtf8(jsonContactBytes);
|
||
|
print(jsonContacts);
|
||
|
List<dynamic> contacts = jsonDecode(jsonContacts);
|
||
|
|
||
|
Map<String, ContactModel> _contacts = new HashMap<String, ContactModel>();
|
||
|
contacts.forEach((onion) {
|
||
|
_contacts.putIfAbsent(onion['onion'], () => ContactModel(onion: onion['onion'], nickname: onion['name'], status: onion['status']));
|
||
|
});
|
||
|
|
||
|
// Map<String, ContactModel> _contacts = _profiles[currentlySelectedProfile].contacts;
|
||
|
final tiles = _contacts.values.map(
|
||
|
(ContactModel contact) {
|
||
|
return ListTile(
|
||
|
title: Text(
|
||
|
contact.nickname,
|
||
|
style: _biggerFont,
|
||
|
),
|
||
|
subtitle: Text(contact.status),
|
||
|
onTap: () {
|
||
|
setState(() {
|
||
|
|
||
|
currentlySelectedConversation = contact.onion;
|
||
|
_pushMessageView();
|
||
|
});
|
||
|
},
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
|
||
|
final divided = ListTile.divideTiles(
|
||
|
context: context,
|
||
|
tiles: tiles,
|
||
|
).toList();
|
||
|
|
||
|
return ListView(children: divided);
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Widget _pushMessageView() {
|
||
|
Navigator.of(context).push(
|
||
|
MaterialPageRoute<void>(
|
||
|
builder: (BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: Text('Convo: ' + currentlySelectedConversation),
|
||
|
),
|
||
|
body: _buildMessageView(),
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
String getNick(String profile, String contact) {
|
||
|
return contact == profile ? "me" : _profiles[profile].contacts[contact].nickname;
|
||
|
}
|
||
|
|
||
|
Widget _buildMessageView() {
|
||
|
List<MessageModel> _contacts = _profiles[currentlySelectedProfile].contacts[currentlySelectedConversation].messages;
|
||
|
final tiles = _contacts.map(
|
||
|
(MessageModel m) {
|
||
|
return ListTile(
|
||
|
title: Text(
|
||
|
getNick(currentlySelectedProfile, m.from),
|
||
|
),
|
||
|
subtitle: Text(
|
||
|
m.message,
|
||
|
style: _biggerFont,
|
||
|
),
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
|
||
|
final divided = ListTile.divideTiles(
|
||
|
context: context,
|
||
|
tiles: tiles,
|
||
|
).toList();
|
||
|
|
||
|
return ListView(children: divided);
|
||
|
}
|
||
|
|
||
|
Widget _buildProfileManager() {
|
||
|
final tiles = _profiles.values.map(
|
||
|
(ProfileModel profile) {
|
||
|
return ListTile(
|
||
|
title: Text(
|
||
|
profile.nickname,
|
||
|
style: _biggerFont,
|
||
|
),
|
||
|
subtitle: Text(profile.onion),
|
||
|
onTap: () {
|
||
|
setState(() {
|
||
|
|
||
|
var selectProfileC = widget.library.lookup<NativeFunction<get_json_blob_string_function>>("SelectProfile");
|
||
|
final SelectProfile = selectProfileC.asFunction<GetJsonBlobStringFn>();
|
||
|
|
||
|
SelectProfile(Utf8.toUtf8(profile.onion), profile.onion.length);
|
||
|
|
||
|
currentlySelectedConversation = "";
|
||
|
currentlySelectedProfile = profile.onion;
|
||
|
_pushContactList();
|
||
|
});
|
||
|
},
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
|
||
|
final divided = ListTile.divideTiles(
|
||
|
context: context,
|
||
|
tiles: tiles,
|
||
|
).toList();
|
||
|
|
||
|
return ListView(children: divided);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
class MyHomePage extends StatefulWidget {
|
||
|
MyHomePage({Key key, this.title, this.library, this.appStatus}) : super(key: key);
|
||
|
|
||
|
// This widget is the home page of your application. It is stateful, meaning
|
||
|
// that it has a State object (defined below) that contains fields that affect
|
||
|
// how it looks.
|
||
|
|
||
|
// This class is the configuration for the state. It holds the values (in this
|
||
|
// case the title) provided by the parent (in this case the App widget) and
|
||
|
// used by the build method of the State. Fields in a Widget subclass are
|
||
|
// always marked "final".
|
||
|
|
||
|
final String title;
|
||
|
final DynamicLibrary library;
|
||
|
final model.AppModel appStatus;
|
||
|
|
||
|
@override
|
||
|
_MyHomePageState createState() => _MyHomePageState();
|
||
|
}
|
||
|
|
||
|
|
||
|
class _MyHomePageState extends State<MyHomePage> {
|
||
|
int _counter = 0;
|
||
|
|
||
|
void _incrementCounter() {
|
||
|
setState(() {
|
||
|
|
||
|
|
||
|
var nextEventC = widget.library.lookup<NativeFunction<access_cwtch_eventbus_function>>("NextEvent");
|
||
|
final NextEvent = nextEventC.asFunction<NextEventFn>();
|
||
|
|
||
|
|
||
|
// This call to setState tells the Flutter framework that something has
|
||
|
// changed in this State, which causes it to rerun the build method below
|
||
|
// so that the display can reflect the updated values. If we changed
|
||
|
// _counter without calling setState(), then the build method would not be
|
||
|
// called again, and so nothing would appear to happen.
|
||
|
NextEvent();
|
||
|
_counter = 1;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
// This method is rerun every time setState is called, for instance as done
|
||
|
// by the _incrementCounter method above.
|
||
|
//
|
||
|
// The Flutter framework has been optimized to make rerunning build methods
|
||
|
// fast, so that you can just rebuild anything that needs updating rather
|
||
|
// than having to individually change instances of widgets.
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
// Here we take the value from the MyHomePage object that was created by
|
||
|
// the App.build method, and use it to set our appbar title.
|
||
|
title: Text(widget.title),
|
||
|
),
|
||
|
body: Center(
|
||
|
// Center is a layout widget. It takes a single child and positions it
|
||
|
// in the middle of the parent.
|
||
|
child: Column(
|
||
|
// Column is also a layout widget. It takes a list of children and
|
||
|
// arranges them vertically. By default, it sizes itself to fit its
|
||
|
// children horizontally, and tries to be as tall as its parent.
|
||
|
//
|
||
|
// Invoke "debug painting" (press "p" in the console, choose the
|
||
|
// "Toggle Debug Paint" action from the Flutter Inspector in Android
|
||
|
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
|
||
|
// to see the wireframe for each widget.
|
||
|
//
|
||
|
// Column has various properties to control how it sizes itself and
|
||
|
// how it positions its children. Here we use mainAxisAlignment to
|
||
|
// center the children vertically; the main axis here is the vertical
|
||
|
// axis because Columns are vertical (the cross axis would be
|
||
|
// horizontal).
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: <Widget>[
|
||
|
StreamBuilder<String>(
|
||
|
stream: widget.appStatus.torStatus(),
|
||
|
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||
|
return Text(
|
||
|
snapshot.hasData ?
|
||
|
snapshot.data : "Tor not yet Connected",
|
||
|
style: Theme
|
||
|
.of(context)
|
||
|
.textTheme
|
||
|
.headline4,
|
||
|
);
|
||
|
},
|
||
|
),
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
floatingActionButton: FloatingActionButton(
|
||
|
onPressed: _incrementCounter,
|
||
|
tooltip: 'Increment',
|
||
|
child: Icon(Icons.add),
|
||
|
), // This trailing comma makes auto-formatting nicer for build methods.
|
||
|
);
|
||
|
}
|
||
|
}
|