ServerCache, Peer Name Updates + Profile Event Updates
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2021-06-01 12:33:19 -07:00
parent 564867d245
commit 9a05200af2
7 changed files with 57 additions and 67 deletions

View File

@ -1,4 +1,5 @@
import 'dart:convert';
import 'package:cwtch/models/servers.dart';
import 'package:cwtch/notification_manager.dart';
import 'package:provider/provider.dart';
@ -49,9 +50,17 @@ class CwtchNotifier {
));
break;
case "GroupCreated":
// Retrieve Server Status from Cache...
String status = "";
ServerInfoState? serverInfoState = profileCN.getProfile(data["ProfileOnion"])?.serverList.getServer(data["GroupServer"]);
if (serverInfoState != null) {
status = serverInfoState.status;
}
if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(data["GroupID"]) == null) {
profileCN.getProfile(data["ProfileOnion"])?.contactList.add(ContactInfoState(data["ProfileOnion"], data["GroupID"],
isInvitation: false, imagePath: data["PicturePath"], nickname: data["GroupName"], server: data["GroupServer"], isGroup: true, lastMessageTime: DateTime.now()));
isInvitation: false, imagePath: data["PicturePath"], nickname: data["GroupName"], status: status, server: data["GroupServer"], isGroup: true, lastMessageTime: DateTime.now()));
profileCN.getProfile(data["ProfileOnion"])?.contactList.updateLastMessageTime(data["GroupID"], DateTime.now());
}
break;
@ -175,9 +184,23 @@ class CwtchNotifier {
String inviteJson = new String.fromCharCodes(base64Decode(invite.substring(5)));
dynamic groupInvite = jsonDecode(inviteJson);
print("new group invite: $groupInvite");
// Retrieve Server Status from Cache...
String status = "";
ServerInfoState? serverInfoState = profileCN.getProfile(data["ProfileOnion"])?.serverList.getServer(groupInvite["ServerHost"]);
if (serverInfoState != null) {
status = serverInfoState.status;
}
if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(groupInvite["GroupID"]) == null) {
profileCN.getProfile(data["ProfileOnion"])?.contactList.add(ContactInfoState(data["ProfileOnion"], groupInvite["GroupID"],
isInvitation: true, imagePath: data["PicturePath"], nickname: groupInvite["GroupName"], server: groupInvite["ServerHost"], isGroup: true, lastMessageTime: DateTime.now()));
isInvitation: true,
imagePath: data["PicturePath"],
nickname: groupInvite["GroupName"],
server: groupInvite["ServerHost"],
status: status,
isGroup: true,
lastMessageTime: DateTime.now()));
profileCN.getProfile(data["ProfileOnion"])?.contactList.updateLastMessageTime(groupInvite["GroupID"], DateTime.now());
}
}
@ -189,6 +212,8 @@ class CwtchNotifier {
break;
case "ServerStateChange":
print("server state change: $data");
// Update the Server Cache
profileCN.getProfile(data["ProfileOnion"])?.updateServerStatusCache(data["GroupServer"], data["ConnectionState"]);
profileCN.getProfile(data["ProfileOnion"])?.contactList.contacts.forEach((contact) {
if (contact.isGroup == true && contact.server == data["GroupServer"]) {
contact.status = data["ConnectionState"];
@ -205,6 +230,16 @@ class CwtchNotifier {
print("unhandled set group attribute event: $type $data");
}
break;
case "NewRetValMessageFromPeer":
if (data["Path"] == "name") {
// Update locally on the UI...
if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(data["RemotePeer"]) != null) {
profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(data["RemotePeer"])!.nickname = data["Data"];
}
} else {
print("unhandled peer attribute event: $type $data");
}
break;
default:
print("unhandled event: $type $data");
}

View File

@ -43,7 +43,6 @@ class FlwtchState extends State<Flwtch> {
String selectedConversation = "";
var columns = [1]; // default or 'single column' mode
//var columns = [1, 1, 2];
late AppModel appStatus;
late ProfileListState profs;
@override
@ -69,8 +68,6 @@ class FlwtchState extends State<Flwtch> {
cwtchInit = true;
});
});
appStatus = AppModel(cwtch: cwtch);
}
ChangeNotifierProvider<TorStatus> getTorStatusProvider() => ChangeNotifierProvider.value(value: globalTorStatus);

View File

@ -190,9 +190,16 @@ class ProfileInfoState extends ChangeNotifier {
// TODO Keys...
return ServerInfoState(onion: server["onion"], status: server["status"]);
}));
notifyListeners();
}
}
//
void updateServerStatusCache(String server, String status) {
this._servers.updateServerCache(server, status);
notifyListeners();
}
// Getters and Setters for Online Status
bool get isOnline => this._online;
set isOnline(bool newValue) {
@ -470,37 +477,3 @@ class MessageState extends ChangeNotifier {
});
}
}
/////////////
/// ACN ///
/////////////
class AppModel {
final Cwtch cwtch;
AppModel({required this.cwtch});
Stream<String> contactEvents() async* {
while (true) {
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) {
String event = await cwtch.ACNEvents();
if (event != "") {
yield event;
} else {
print("TOR TEST TEST FAIL TEST FAIL 123");
await Future.delayed(Duration(seconds: 1));
}
}
}
}

View File

@ -14,6 +14,16 @@ class ServerListState extends ChangeNotifier {
return idx >= 0 ? _servers[idx] : null;
}
void updateServerCache(String onion, String status) {
int idx = _servers.indexWhere((element) => element.onion == onion);
if (idx >= 0) {
_servers[idx] = ServerInfoState(onion: onion, status: status);
} else {
print("Tried to update server cache without a starting state...this is probably an error");
}
notifyListeners();
}
List<ServerInfoState> get servers => _servers.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
}

View File

@ -93,7 +93,8 @@ class _ContactRowState extends State<ContactRow> {
MaterialPageRoute<void>(
builder: (BuildContext builderContext) {
// assert we have an actual profile...
var profile = Provider.of<FlwtchState>(builderContext, listen: false).profs.getProfile(profileOnion)!;
// We need to listen for updates to the profile in order to update things like invitation message bubbles.
var profile = Provider.of<FlwtchState>(builderContext).profs.getProfile(profileOnion)!;
return MultiProvider(
providers: [
ChangeNotifierProvider.value(value: profile),

View File

@ -40,6 +40,7 @@ class _MessageListState extends State<MessageList> {
create: (x) => MessageState(
context: itemBuilderContext,
profileOnion: Provider.of<ProfileInfoState>(outerContext, listen: false).onion,
// We don't want to listen for updates to the contact handle...
contactHandle: Provider.of<ContactInfoState>(x, listen: false).onion,
messageIndex: trueIndex,
),

View File

@ -1,27 +0,0 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../main.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.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! : AppLocalizations.of(context)!.loadingTor,
style: Theme.of(context).textTheme.headline4,
);
},
));
}
}