flutter_app/lib/model.dart

285 lines
6.5 KiB
Dart
Raw Normal View History

2021-01-21 20:37:35 +00:00
import 'dart:convert';
2021-02-10 01:36:12 +00:00
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'dart:collection';
import 'cwtch/cwtch.dart';
////////////////////
/// UI State ///
////////////////////
2021-02-10 01:36:12 +00:00
//todo: delete
class ProfileModel {
String onion;
String nickname;
String creationDate;
2021-01-21 20:37:35 +00:00
String imagePath;
HashMap<String, ContactModel> contacts;
}
2021-02-10 01:36:12 +00:00
//todo: delete
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-03-16 23:33:03 +00:00
ContactModel({this.onion, this.nickname, this.status, this.isInvitation, this.isBlocked, this.imagePath});
}
2021-02-10 01:36:12 +00:00
//todo: delete
2021-01-22 07:56:30 +00:00
class DanMessageModel {
2021-02-10 01:36:12 +00:00
// ignore: non_constant_identifier_names
2021-01-14 23:36:09 +00:00
String Timestamp;
2021-02-10 01:36:12 +00:00
// ignore: non_constant_identifier_names
2021-01-14 23:36:09 +00:00
bool Acknowledged;
2021-02-10 01:36:12 +00:00
// ignore: non_constant_identifier_names
2021-01-14 23:36:09 +00:00
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'];
2021-03-10 17:40:14 +00:00
Map<String, dynamic> toJson() => {
2021-01-21 20:37:35 +00:00
'o': o,
'd': d,
};
}
///////////////////
/// Providers ///
///////////////////
class ProfileListState extends ChangeNotifier {
2021-03-12 12:31:21 +00:00
List<ProfileInfoState> _profiles = [];
int get num => _profiles.length;
2021-03-12 12:31:21 +00:00
void addAll(Iterable<ProfileInfoState> newProfiles) {
_profiles.addAll(newProfiles);
notifyListeners();
}
2021-03-12 12:31:21 +00:00
void add(ProfileInfoState newProfile) {
2021-03-16 23:33:03 +00:00
print("ProfileListState: adding " + newProfile.onion + " and notifying");
2021-03-12 12:31:21 +00:00
_profiles.add(newProfile);
notifyListeners();
}
2021-03-16 23:33:03 +00:00
List<ProfileInfoState> get profiles => _profiles.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
2021-03-12 12:31:21 +00:00
ProfileInfoState getProfile(String onion) {
int idx = _profiles.indexWhere((element) => element.onion == onion);
return idx >= 0 ? _profiles[idx] : null;
}
}
class ContactListState extends ChangeNotifier {
2021-03-12 12:31:21 +00:00
List<ContactInfoState> _contacts = [];
int get num => _contacts.length;
void addAll(Iterable<ContactInfoState> newContacts) {
_contacts.addAll(newContacts);
notifyListeners();
}
2021-03-12 12:31:21 +00:00
void add(ContactInfoState newContact) {
_contacts.add(newContact);
notifyListeners();
}
void updateUnreadMessages(String forOnion, int newVal) {
2021-03-16 23:33:03 +00:00
_contacts.sort((ContactInfoState a, ContactInfoState b) {
return b.unreadMessages - a.unreadMessages;
});
//<todo> if(changed) {
notifyListeners();
//} </todo>
}
2021-03-16 23:33:03 +00:00
List<ContactInfoState> get contacts => _contacts.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
2021-03-12 12:31:21 +00:00
ContactInfoState getContact(String onion) {
int idx = _contacts.indexWhere((element) => element.onion == onion);
return idx >= 0 ? _contacts[idx] : null;
}
}
class ProfileInfoState extends ChangeNotifier {
2021-03-12 12:31:21 +00:00
ContactListState _contacts = ContactListState();
final String onion;
String _nickname = "";
String _imagePath = "";
int _unreadMessages = 0;
2021-03-16 23:33:03 +00:00
ProfileInfoState({
this.onion,
nickname = "",
imagePath = "",
unreadMessages = 0,
contactsJson = "",
}) {
this._nickname = nickname;
this._imagePath = imagePath;
this._unreadMessages = unreadMessages;
2021-03-12 12:31:21 +00:00
if (contactsJson != null && contactsJson != "" && contactsJson != "null") {
print("decoding " + contactsJson);
List<dynamic> contacts = jsonDecode(contactsJson);
2021-03-16 23:33:03 +00:00
this._contacts.addAll(contacts.map((contact) {
return ContactInfoState(
profileOnion: this.onion,
onion: contact["onion"],
nickname: contact["name"],
status: contact["status"],
imagePath: contact["picture"],
2021-03-17 22:54:41 +00:00
numMessages: contact["numMessages"],
numUnread: contact["numUnread"],
2021-03-16 23:33:03 +00:00
);
}));
2021-03-12 12:31:21 +00:00
}
}
String get nickname => this._nickname;
set nickname(String newValue) {
2021-02-23 02:55:10 +00:00
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();
}
2021-03-12 12:31:21 +00:00
ContactListState get contactList => this._contacts;
@override
void dispose() {
super.dispose();
print("profileinfostate.dispose()");
}
}
class ContactInfoState extends ChangeNotifier {
final String profileOnion;
final String onion;
String _nickname;
bool _isGroup;
bool _isInvitation;
bool _isBlocked;
String _status;
String _imagePath;
2021-03-17 22:54:41 +00:00
int _totalMessages = 0;
int _unreadMessages = 0;
2021-03-10 17:40:14 +00:00
ContactInfoState({
this.profileOnion,
this.onion,
nickname = "",
isGroup = false,
isInvitation = false,
isBlocked = false,
status = "",
imagePath = "",
2021-03-17 22:54:41 +00:00
numMessages = 0,
numUnread = 0,
2021-03-10 17:40:14 +00:00
}) {
this._nickname = nickname;
this._isGroup = isGroup;
this._isInvitation = isInvitation;
this._isBlocked = isBlocked;
this._status = status;
this._imagePath = imagePath;
2021-03-17 22:54:41 +00:00
this._totalMessages = numMessages;
this._unreadMessages = numUnread;
}
get nickname => this._nickname;
set nickname(String newVal) {
this._nickname = newVal;
notifyListeners();
}
2021-02-10 01:36:12 +00:00
get isInvitation => this._isInvitation;
set isInvitation(bool newVal) {
this._isInvitation = newVal;
notifyListeners();
}
get status => this._status;
set status(String newVal) {
this._status = newVal;
notifyListeners();
}
get unreadMessages => this._unreadMessages;
set unreadMessages(int newVal) {
this._unreadMessages = newVal;
notifyListeners();
}
2021-03-12 12:31:21 +00:00
2021-03-17 22:54:41 +00:00
get totalMessages => this._totalMessages;
set totalMessages(int newVal) {
this._totalMessages = newVal;
notifyListeners();
}
2021-03-12 12:31:21 +00:00
get imagePath => this._imagePath;
set imagePath(String newVal) {
this._imagePath = 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));
}
}
}
}