2022-04-14 22:34:17 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2022-01-18 21:26:52 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
|
|
|
|
import 'profile.dart';
|
|
|
|
|
|
|
|
class ProfileListState extends ChangeNotifier {
|
|
|
|
List<ProfileInfoState> _profiles = [];
|
|
|
|
int get num => _profiles.length;
|
|
|
|
|
2023-09-14 01:38:08 +00:00
|
|
|
void add(String onion, String name, String picture, String defaultPicture, String contactsJson, String serverJson, bool online, bool autostart, bool encrypted, bool appearOffline) {
|
2022-01-18 21:26:52 +00:00
|
|
|
var idx = _profiles.indexWhere((element) => element.onion == onion);
|
|
|
|
if (idx == -1) {
|
2022-02-07 22:26:14 +00:00
|
|
|
_profiles.add(ProfileInfoState(
|
2022-12-06 20:11:55 +00:00
|
|
|
onion: onion,
|
|
|
|
nickname: name,
|
|
|
|
imagePath: picture,
|
|
|
|
defaultImagePath: defaultPicture,
|
|
|
|
contactsJson: contactsJson,
|
|
|
|
serversJson: serverJson,
|
|
|
|
online: online,
|
|
|
|
autostart: autostart,
|
2023-09-14 01:38:08 +00:00
|
|
|
encrypted: encrypted,
|
|
|
|
appearOffline: appearOffline));
|
2022-01-18 21:26:52 +00:00
|
|
|
} else {
|
|
|
|
_profiles[idx].updateFrom(onion, name, picture, contactsJson, serverJson, online);
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<ProfileInfoState> get profiles => _profiles.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
|
|
|
|
|
|
|
|
ProfileInfoState? getProfile(String onion) {
|
|
|
|
int idx = _profiles.indexWhere((element) => element.onion == onion);
|
|
|
|
return idx >= 0 ? _profiles[idx] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
void delete(String onion) {
|
|
|
|
_profiles.removeWhere((element) => element.onion == onion);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2022-01-25 22:37:51 +00:00
|
|
|
|
2022-02-05 00:57:31 +00:00
|
|
|
int generateUnreadCount(String selectedProfile) => _profiles.where((p) => p.onion != selectedProfile).fold(0, (i, p) => i + p.unreadMessages);
|
2022-04-14 22:34:17 +00:00
|
|
|
|
|
|
|
int cacheMemUsage() {
|
|
|
|
return _profiles.map((e) => e.cacheMemUsage()).fold(0, (previousValue, element) => previousValue + element);
|
|
|
|
}
|
2022-01-19 21:58:52 +00:00
|
|
|
}
|