messages view and some basic refactoring
This commit is contained in:
parent
6c310c4735
commit
5394482967
473
lib/main.dart
473
lib/main.dart
|
@ -1,452 +1,61 @@
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
import 'package:ffi/ffi.dart';
|
import 'package:ffi/ffi.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'dart:io' show Platform;
|
import 'package:provider/provider.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'model.dart';
|
||||||
import 'model.dart' as model;
|
import 'views/profilemgrview.dart';
|
||||||
import 'dart:collection';
|
|
||||||
import 'dart:convert';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
typedef start_cwtch_function = Void Function(Pointer<Utf8> str, Int32 length);
|
void main() => runApp(Flwtch());
|
||||||
typedef StartCwtchFn = void Function(Pointer<Utf8> dir, int len);
|
|
||||||
|
|
||||||
typedef access_cwtch_eventbus_function = Void Function();
|
class Flwtch extends StatefulWidget {
|
||||||
typedef NextEventFn = void Function();
|
final Key flwtch = GlobalKey();
|
||||||
|
|
||||||
|
@override
|
||||||
typedef get_json_blob_void_function = Pointer<Utf8> Function();
|
FlwtchState createState() => FlwtchState();
|
||||||
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 {
|
class FlwtchState extends State<Flwtch> {
|
||||||
String onion;
|
final TextStyle biggerFont = const TextStyle(fontSize: 18);
|
||||||
String nickname;
|
|
||||||
bool isGroup;
|
|
||||||
bool isBlocked;
|
|
||||||
String status;
|
|
||||||
List<MessageModel> messages;
|
|
||||||
|
|
||||||
ContactModel({this.onion, this.nickname, this.status});
|
DynamicLibrary library;
|
||||||
|
AppModel appStatus;
|
||||||
}
|
|
||||||
|
|
||||||
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
library = DynamicLibrary.open("libCwtch.so");
|
||||||
var library = DynamicLibrary.open("libCwtch.so");
|
appStatus = AppModel(library: library);
|
||||||
|
|
||||||
var startCwtchC = library.lookup<NativeFunction<start_cwtch_function>>("StartCwtch");
|
var startCwtchC = library.lookup<NativeFunction<start_cwtch_function>>("StartCwtch");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
|
final StartCwtch = startCwtchC.asFunction<StartCwtchFn>();
|
||||||
|
|
||||||
var cwtchDir = "/home/sarah/.cwtch/dev/";
|
var cwtchDir = "/home/erinn/.cwtch/dev/";
|
||||||
StartCwtch(Utf8.toUtf8(cwtchDir), cwtchDir.length);
|
StartCwtch(Utf8.toUtf8(cwtchDir), cwtchDir.length);
|
||||||
|
|
||||||
|
return Provider<FlwtchState>(
|
||||||
return MaterialApp(
|
create: (_) => this,
|
||||||
title: 'Flutter Demo',
|
child: MaterialApp(
|
||||||
theme: ThemeData(
|
title: 'Flutter Demo',
|
||||||
// This is the theme of your application.
|
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
|
// Try running your application with "flutter run". You'll see the
|
||||||
// changing the primarySwatch below to Colors.green and then invoke
|
// application has a blue toolbar. Then, without quitting the app, try
|
||||||
// "hot reload" (press "r" in the console where you ran "flutter run",
|
// changing the primarySwatch below to Colors.green and then invoke
|
||||||
// or simply save your changes to "hot reload" in a Flutter IDE).
|
// "hot reload" (press "r" in the console where you ran "flutter run",
|
||||||
// Notice that the counter didn't reset back to zero; the application
|
// or simply save your changes to "hot reload" in a Flutter IDE).
|
||||||
// is not restarted.
|
// Notice that the counter didn't reset back to zero; the application
|
||||||
primarySwatch: Colors.blue,
|
// is not restarted.
|
||||||
// This makes the visual density adapt to the platform that you run
|
primarySwatch: Colors.blue,
|
||||||
// the app on. For desktop platforms, the controls will be smaller and
|
// This makes the visual density adapt to the platform that you run
|
||||||
// closer together (more dense) than on mobile platforms.
|
// the app on. For desktop platforms, the controls will be smaller and
|
||||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
// closer together (more dense) than on mobile platforms.
|
||||||
primaryColor: Color(0xFF4B3557),
|
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||||
canvasColor: Color(0xFFB09CBC),
|
primaryColor: Color(0xFF4B3557),
|
||||||
accentColor: Color(0xFFD01972),
|
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(
|
home: ProfileMgrView(),
|
||||||
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.
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,21 +1,44 @@
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
import 'package:ffi/ffi.dart';
|
import 'package:ffi/ffi.dart';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:collection';
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
/// UI State ///
|
||||||
|
////////////////////
|
||||||
|
|
||||||
|
class ProfileModel {
|
||||||
|
String onion;
|
||||||
|
String nickname;
|
||||||
|
String creationDate;
|
||||||
|
HashMap<String, ContactModel> contacts;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ContactModel {
|
||||||
|
String onion;
|
||||||
|
String nickname;
|
||||||
|
bool isGroup;
|
||||||
|
bool isBlocked;
|
||||||
|
String status;
|
||||||
|
|
||||||
|
ContactModel({this.onion, this.nickname, this.status});
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
/// ACN ///
|
||||||
|
/////////////
|
||||||
|
|
||||||
typedef acn_events_function = Pointer<Utf8> Function();
|
typedef acn_events_function = Pointer<Utf8> Function();
|
||||||
typedef ACNEventsFn = Pointer<Utf8> Function();
|
typedef ACNEventsFn = Pointer<Utf8> Function();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AppModel {
|
class AppModel {
|
||||||
|
|
||||||
final DynamicLibrary library;
|
final DynamicLibrary library;
|
||||||
AppModel({this.library});
|
AppModel({this.library});
|
||||||
|
|
||||||
Stream<String> contactEvents() async* {
|
Stream<String> contactEvents() async* {
|
||||||
var acnEventsC = library.lookup<NativeFunction<acn_events_function>>(
|
var acnEventsC = library.lookup<NativeFunction<acn_events_function>>(
|
||||||
"ContactEvents");
|
"ContactEvents");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
final ContactEvents = acnEventsC.asFunction<ACNEventsFn>();
|
final ContactEvents = acnEventsC.asFunction<ACNEventsFn>();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -34,6 +57,7 @@ class AppModel {
|
||||||
Stream<String> torStatus() async* {
|
Stream<String> torStatus() async* {
|
||||||
var acnEventsC = library.lookup<NativeFunction<acn_events_function>>(
|
var acnEventsC = library.lookup<NativeFunction<acn_events_function>>(
|
||||||
"ACNEvents");
|
"ACNEvents");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
final ACNEvents = acnEventsC.asFunction<ACNEventsFn>();
|
final ACNEvents = acnEventsC.asFunction<ACNEventsFn>();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -46,4 +70,28 @@ class AppModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////////////////////
|
||||||
|
/// Cwtch API ///
|
||||||
|
/////////////////////
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
//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<Utf8>, Int32, Pointer<Utf8>, Int32);
|
||||||
|
typedef GetIntFromStrStrFn = int Function(Pointer<Utf8>, int, Pointer<Utf8>, 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<Utf8> Function(Pointer<Utf8>, Int32, Pointer<Utf8>, Int32, Int32);
|
||||||
|
typedef GetJsonBlobFromStrStrIntFn = Pointer<Utf8> Function(Pointer<Utf8>, int, Pointer<Utf8>, int, int);
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
import 'dart:collection';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
|
import 'package:ffi/ffi.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import '../main.dart';
|
||||||
|
import 'messageview.dart';
|
||||||
|
import '../model.dart';
|
||||||
|
|
||||||
|
class ContactsView extends StatefulWidget {
|
||||||
|
const ContactsView({Key key, this.flwtch, this.profileOnion}) : super(key: key);
|
||||||
|
final GlobalKey<FlwtchState> flwtch;
|
||||||
|
final String profileOnion;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_ContactsViewState createState() => _ContactsViewState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ContactsViewState extends State<ContactsView> {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Text('Contacts for ' + widget.profileOnion),
|
||||||
|
),
|
||||||
|
body: _buildContactList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Widget _buildContactList() {
|
||||||
|
var getContactsC = Provider.of<FlwtchState>(context).library.lookup<NativeFunction<get_json_blob_string_function>>("GetContacts");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
|
final GetContacts = getContactsC.asFunction<GetJsonBlobStringFn>();
|
||||||
|
|
||||||
|
return StreamBuilder<String>(
|
||||||
|
stream: Provider.of<FlwtchState>(context).appStatus.contactEvents(),
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||||||
|
Pointer<Utf8> jsonContactBytes = GetContacts(Utf8.toUtf8(widget.profileOnion), widget.profileOnion.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: Provider.of<FlwtchState>(context).biggerFont,
|
||||||
|
),
|
||||||
|
subtitle: Text(contact.status),
|
||||||
|
onTap: () {
|
||||||
|
setState(() {
|
||||||
|
_pushMessageView(contact.onion);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final divided = ListTile.divideTiles(
|
||||||
|
context: context,
|
||||||
|
tiles: tiles,
|
||||||
|
).toList();
|
||||||
|
|
||||||
|
return ListView(children: divided);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _pushMessageView(String handle) {
|
||||||
|
Navigator.of(context).push(
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return Provider(
|
||||||
|
create: (_) => Provider.of<FlwtchState>(context),
|
||||||
|
child: Scaffold(
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Text('Convo: ' + widget.profileOnion),
|
||||||
|
),
|
||||||
|
body: MessageView(profileOnion: widget.profileOnion, conversationHandle: handle),
|
||||||
|
));
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,80 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
|
import 'package:ffi/ffi.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../main.dart';
|
||||||
|
import '../model.dart';
|
||||||
|
|
||||||
|
class MessageView extends StatefulWidget {
|
||||||
|
const MessageView({Key key, this.flwtch, this.profileOnion, this.conversationHandle}) : super(key: key);
|
||||||
|
final GlobalKey<FlwtchState> flwtch;
|
||||||
|
final String profileOnion;
|
||||||
|
final String conversationHandle;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_MessageViewState createState() => _MessageViewState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _MessageViewState extends State<MessageView> {
|
||||||
|
int conversationNumMessages = 0;
|
||||||
|
Timer timer;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return _buildMessageView(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildMessageView(BuildContext context) {
|
||||||
|
var numMessagesC = Provider.of<FlwtchState>(context).library.lookup<NativeFunction<get_int_from_str_str_function>>("NumMessages");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
|
final NumMessages = numMessagesC.asFunction<GetIntFromStrStrFn>();
|
||||||
|
var getMessageC = Provider.of<FlwtchState>(context).library.lookup<NativeFunction<get_json_blob_from_str_str_int_function>>("GetMessage");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
|
final GetMessage = getMessageC.asFunction<GetJsonBlobFromStrStrIntFn>();
|
||||||
|
|
||||||
|
_updateMessageCount(context, NumMessages);
|
||||||
|
timer = Timer.periodic(Duration(seconds: 1), (Timer t) => _updateMessageCount(context, NumMessages));
|
||||||
|
|
||||||
|
|
||||||
|
return ProxyProvider0(
|
||||||
|
update: (_, __) => MessageCounter(conversationNumMessages),
|
||||||
|
child: ListView.builder(
|
||||||
|
itemCount: conversationNumMessages,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
Pointer<Utf8> jsonMessageBytes = GetMessage(
|
||||||
|
Utf8.toUtf8(widget.profileOnion), widget.profileOnion.length,
|
||||||
|
Utf8.toUtf8(widget.conversationHandle), widget.conversationHandle.length,
|
||||||
|
index);
|
||||||
|
String jsonMessage = Utf8.fromUtf8(jsonMessageBytes);
|
||||||
|
//print(jsonMessage);
|
||||||
|
dynamic messageWrapper = jsonDecode(jsonMessage);
|
||||||
|
dynamic message = jsonDecode(messageWrapper['Message']);
|
||||||
|
|
||||||
|
return ListTile(
|
||||||
|
title: Text(message['d']),
|
||||||
|
subtitle: Row(
|
||||||
|
children: [Text(messageWrapper['Timestamp']), messageWrapper['Acknowledged'] ? Icon(Icons.check_circle_outline) : Icon(Icons.hourglass_bottom_outlined)],
|
||||||
|
));
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future _updateMessageCount(BuildContext context, GetIntFromStrStrFn cfn) async {
|
||||||
|
setState(() {
|
||||||
|
conversationNumMessages = cfn(
|
||||||
|
Utf8.toUtf8(widget.profileOnion), widget.profileOnion.length,
|
||||||
|
Utf8.toUtf8(widget.conversationHandle),
|
||||||
|
widget.conversationHandle.length,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MessageCounter {
|
||||||
|
MessageCounter(this.x);
|
||||||
|
int x = 0;
|
||||||
|
}
|
|
@ -0,0 +1,107 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
import 'package:ffi/ffi.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
import '../main.dart';
|
||||||
|
import '../model.dart';
|
||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'contactsview.dart';
|
||||||
|
|
||||||
|
class ProfileMgrView extends StatefulWidget {
|
||||||
|
@override
|
||||||
|
_ProfileMgrViewState createState() => _ProfileMgrViewState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ProfileMgrViewState extends State<ProfileMgrView> {
|
||||||
|
HashMap<String, ProfileModel> _profiles;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
if (_profiles == null) {
|
||||||
|
_profiles = new HashMap<String, ProfileModel>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_profiles.length < 1) {
|
||||||
|
var getProfilesC = Provider.of<FlwtchState>(context).library.lookup<NativeFunction<get_json_blob_void_function>>("GetProfiles");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Scaffold (
|
||||||
|
appBar: AppBar(
|
||||||
|
title: Text('Profiles'),
|
||||||
|
actions: [
|
||||||
|
IconButton(icon: Icon(Icons.list), onPressed: () { },),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
body: _buildProfileManager(),//_buildSuggestions(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _pushContactList(String profile) {
|
||||||
|
Navigator.of(context).push(
|
||||||
|
MaterialPageRoute<void>(
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return Provider(
|
||||||
|
create: (_) => Provider.of<FlwtchState>(context),
|
||||||
|
child: ContactsView(profileOnion: profile),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
String getNick(String profile, String contact) {
|
||||||
|
return contact == profile ? "me" : _profiles[profile].contacts[contact].nickname;
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildProfileManager() {
|
||||||
|
final tiles = _profiles.values.map(
|
||||||
|
(ProfileModel profile) {
|
||||||
|
return ListTile(
|
||||||
|
title: Text(
|
||||||
|
profile.nickname,
|
||||||
|
style: Provider.of<FlwtchState>(context).biggerFont,
|
||||||
|
),
|
||||||
|
subtitle: Text(profile.onion),
|
||||||
|
onTap: () {
|
||||||
|
setState(() {
|
||||||
|
var selectProfileC = Provider.of<FlwtchState>(context).library.lookup<NativeFunction<get_json_blob_string_function>>("SelectProfile");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
|
final SelectProfile = selectProfileC.asFunction<GetJsonBlobStringFn>();
|
||||||
|
|
||||||
|
SelectProfile(Utf8.toUtf8(profile.onion), profile.onion.length);
|
||||||
|
_pushContactList(profile.onion);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final divided = ListTile.divideTiles(
|
||||||
|
context: context,
|
||||||
|
tiles: tiles,
|
||||||
|
).toList();
|
||||||
|
|
||||||
|
return ListView(children: divided);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../main.dart';
|
||||||
|
import '../model.dart';
|
||||||
|
|
||||||
|
class TorStatusLabel extends StatefulWidget {
|
||||||
|
@override
|
||||||
|
_TorStatusState createState() => _TorStatusState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TorStatusState extends State<TorStatusLabel> {
|
||||||
|
String status = "";
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Builder(
|
||||||
|
builder: (context2) => StreamBuilder<String>(
|
||||||
|
stream: Provider.of<FlwtchState>(context).appStatus.torStatus(),
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
|
||||||
|
return Text(
|
||||||
|
snapshot.hasData ?
|
||||||
|
snapshot.data : "Tor not yet Connected",
|
||||||
|
style: Theme
|
||||||
|
.of(context)
|
||||||
|
.textTheme
|
||||||
|
.headline4,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
void _incrementCounter() {
|
||||||
|
setState(() {
|
||||||
|
var nextEventC = Provider.of<FlwtchState>(context).library.lookup<NativeFunction<access_cwtch_eventbus_function>>("NextEvent");
|
||||||
|
// ignore: non_constant_identifier_names
|
||||||
|
final NextEvent = nextEventC.asFunction<NextEventFn>();
|
||||||
|
NextEvent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
15
pubspec.lock
15
pubspec.lock
|
@ -88,6 +88,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0-nullsafety.6"
|
version: "1.3.0-nullsafety.6"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.0.4"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -95,6 +102,13 @@ packages:
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.8.0-nullsafety.3"
|
version: "1.8.0-nullsafety.3"
|
||||||
|
provider:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "4.3.2+3"
|
||||||
sky_engine:
|
sky_engine:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description: flutter
|
description: flutter
|
||||||
|
@ -158,3 +172,4 @@ packages:
|
||||||
version: "2.1.0-nullsafety.5"
|
version: "2.1.0-nullsafety.5"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=2.12.0-0.0 <3.0.0"
|
dart: ">=2.12.0-0.0 <3.0.0"
|
||||||
|
flutter: ">=1.16.0"
|
||||||
|
|
|
@ -23,6 +23,7 @@ environment:
|
||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
provider: "4.3.2+3"
|
||||||
|
|
||||||
|
|
||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
|
|
Loading…
Reference in New Issue