Line data Source code
1 : import 'package:flutter/cupertino.dart'; 2 : 3 : import 'contact.dart'; 4 : 5 : class RemoteServerInfoState extends ChangeNotifier { 6 : final String onion; 7 : final int identifier; 8 : String _status; 9 : String description; 10 : List<ContactInfoState> _groups = []; 11 : 12 : double syncProgress = 0; 13 : DateTime lastPreSyncMessagTime = new DateTime(2020); 14 : 15 0 : RemoteServerInfoState(this.onion, this.identifier, this.description, this._status, {lastPreSyncMessageTime, mostRecentMessageTime}) { 16 0 : if (_status == "Authenticated" || _status == "Synced") { 17 0 : this.lastPreSyncMessagTime = lastPreSyncMessageTime; 18 0 : updateSyncProgressFor(mostRecentMessageTime); 19 : } 20 : } 21 : 22 0 : void updateDescription(String newDescription) { 23 0 : this.description = newDescription; 24 0 : notifyListeners(); 25 : } 26 : 27 0 : void clearGroups() { 28 0 : _groups = []; 29 : } 30 : 31 0 : void addGroup(ContactInfoState group) { 32 0 : _groups.add(group); 33 0 : notifyListeners(); 34 : } 35 : 36 0 : String get status => _status; 37 0 : set status(String newStatus) { 38 0 : _status = newStatus; 39 0 : if (status == "Authenticated") { 40 : // syncing, set lastPreSyncMessageTime 41 0 : _groups.forEach((g) { 42 0 : if (g.lastMessageReceivedTime.isAfter(lastPreSyncMessagTime)) { 43 0 : lastPreSyncMessagTime = g.lastMessageReceivedTime; 44 : } 45 : }); 46 : } 47 0 : notifyListeners(); 48 : } 49 : 50 : // updateSyncProgressFor point takes a message's time, and updates the server sync progress, 51 : // based on that point in time between the precalculated lastPreSyncMessagTime and Now 52 0 : void updateSyncProgressFor(DateTime point) { 53 0 : var range = lastPreSyncMessagTime.toUtc().difference(DateTime.now().toUtc()); 54 0 : var pointFromStart = lastPreSyncMessagTime.toUtc().difference(point.toUtc()); 55 0 : if (!pointFromStart.isNegative) { 56 : // ! is Negative cus all the duration's we're calculating incidently are negative 57 : // this message is from before we think we should be syncing with the server 58 : // Can be because of a new server or a full resync, either way, use this (oldest message) as our lastPreSyncMessageTime 59 0 : this.lastPreSyncMessagTime = point; 60 0 : pointFromStart = lastPreSyncMessagTime.toUtc().difference(point.toUtc()); 61 : } 62 0 : syncProgress = pointFromStart.inSeconds / range.inSeconds; 63 0 : notifyListeners(); 64 : } 65 : 66 0 : List<ContactInfoState> get groups => _groups.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier 67 : }