flutter_app/lib/model.dart

203 lines
4.5 KiB
Dart
Raw Normal View History

2021-01-21 20:37:35 +00:00
import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'dart:collection';
import 'cwtch/cwtch.dart';
import 'main.dart';
////////////////////
/// UI State ///
////////////////////
class ProfileModel {
String onion;
String nickname;
String creationDate;
2021-01-21 20:37:35 +00:00
String imagePath;
HashMap<String, ContactModel> contacts;
}
class ContactModel {
String onion;
String nickname;
bool isGroup;
2021-01-21 20:37:35 +00:00
bool isInvitation;
bool isBlocked;
String status;
2021-01-21 20:37:35 +00:00
String imagePath;
2021-01-21 20:37:35 +00:00
ContactModel({this.onion, this.nickname, this.status, this.isInvitation, this.isBlocked, this.imagePath});
}
2021-01-22 07:56:30 +00:00
class DanMessageModel {
2021-01-14 23:36:09 +00:00
String Timestamp;
bool Acknowledged;
String Message;
}
2021-01-21 20:37:35 +00:00
class ChatMessage {
final int o;
final String d;
ChatMessage({this.o, this.d});
ChatMessage.fromJson(Map<String, dynamic> json)
: o = json['o'],
d = json['d'];
Map<String, dynamic> toJson() =>
{
'o': o,
'd': d,
};
}
///////////////////
/// Providers ///
///////////////////
class ProfileListState extends ChangeNotifier {
List<ProfileInfoState> _onions = [];
int get num => _onions.length;
void addAll(Iterable<ProfileInfoState> newOnions) {
_onions.addAll(newOnions);
notifyListeners();
}
void add(ProfileInfoState newOnion) {
print("ProfileListState: adding " + newOnion.onion +" and notifying");
_onions.add(newOnion);
notifyListeners();
}
List<ProfileInfoState> get onions => _onions.sublist(0);//todo: copy?? dont want caller able to bypass changenotifier
}
class ContactListState extends ChangeNotifier {
List<ContactInfoState> _onions = [];
int get num => _onions.length;
void addAll(Iterable<ContactInfoState> newOnions) {
_onions.addAll(newOnions);
notifyListeners();
}
void add(ContactInfoState newOnion) {
_onions.add(newOnion);
notifyListeners();
}
void updateUnreadMessages(String forOnion, int newVal) {
_onions.sort((ContactInfoState a, ContactInfoState b) { return b.unreadMessages - a.unreadMessages; });
//<todo> if(changed) {
notifyListeners();
//} </todo>
}
List<ContactInfoState> get onions => _onions.sublist(0);//todo: copy?? dont want caller able to bypass changenotifier
}
class ProfileInfoState extends ChangeNotifier {
final String onion;
String _nickname = "";
String _imagePath = "";
int _unreadMessages = 0;
ProfileInfoState({this.onion, nickname = "", imagePath = "", unreadMessages = 0,}){
this._nickname = nickname;
this._imagePath = imagePath;
this._unreadMessages = unreadMessages;
}
String get nickname => this._nickname;
set nickname(String newValue) {
this.nickname = newValue;
notifyListeners();
}
String get imagePath => this._imagePath;
set imagePath(String newVal) {
this._imagePath = newVal;
notifyListeners();
}
int get unreadMessages => this._unreadMessages;
set unreadMessages(int newVal) {
this._unreadMessages = newVal;
notifyListeners();
}
}
class ContactInfoState extends ChangeNotifier {
final String profileOnion;
final String onion;
String _nickname;
bool _isGroup;
bool _isInvitation;
bool _isBlocked;
String _status;
String _imagePath;
int _unreadMessages = 0;
ContactInfoState({this.profileOnion, this.onion, nickname = "", isGroup = false, isInvitation = false, isBlocked = false, status = "", imagePath = "",}) {
this._nickname = nickname;
this._isGroup = isGroup;
this._isInvitation = isInvitation;
this._isBlocked = isBlocked;
this._status = status;
this._imagePath = imagePath;
}
get nickname => this._nickname;
set nickname(String newVal) {
this._nickname = newVal;
notifyListeners();
}
get unreadMessages => this._unreadMessages;
set unreadMessages(int newVal) {
this._unreadMessages = newVal;
notifyListeners();
}
}
/////////////
/// ACN ///
/////////////
class AppModel {
final Cwtch cwtch;
AppModel({this.cwtch});
Stream<String> contactEvents() async* {
while (true) {
2021-01-14 23:36:09 +00:00
String event = await cwtch.ContactEvents();
if (event != "") {
print(event);
yield event;
} else {
print("TEST TEST FAIL TEST FAIL 123");
await Future.delayed(Duration(seconds: 1));
}
}
}
Stream<String> torStatus() async* {
while (true) {
2021-01-14 23:36:09 +00:00
String event = await cwtch.ACNEvents();
if (event != "") {
yield event;
} else {
print("TOR TEST TEST FAIL TEST FAIL 123");
await Future.delayed(Duration(seconds: 1));
}
}
}
}