Line data Source code
1 : import 'package:flutter/cupertino.dart';
2 : import 'package:flutter/widgets.dart';
3 :
4 : import 'profile.dart';
5 :
6 : class ProfileListState extends ChangeNotifier {
7 : List<ProfileInfoState> _profiles = [];
8 0 : int get num => _profiles.length;
9 :
10 0 : void add(
11 : String onion,
12 : String name,
13 : String privateName,
14 : String picture,
15 : String defaultPicture,
16 : String contactsJson,
17 : String serverJson,
18 : bool online,
19 : bool autostart,
20 : bool encrypted,
21 : bool appearOffline,
22 : ) {
23 0 : var idx = _profiles.indexWhere((element) => element.onion == onion);
24 0 : if (idx == -1) {
25 0 : _profiles.add(
26 0 : ProfileInfoState(
27 : onion: onion,
28 : nickname: name,
29 : privateName: privateName,
30 : imagePath: picture,
31 : defaultImagePath: defaultPicture,
32 : contactsJson: contactsJson,
33 : serversJson: serverJson,
34 : online: online,
35 : autostart: autostart,
36 : encrypted: encrypted,
37 : appearOffline: appearOffline,
38 : ),
39 : );
40 : } else {
41 0 : _profiles[idx].updateFrom(onion, name, picture, contactsJson, serverJson, online);
42 : }
43 0 : notifyListeners();
44 : }
45 :
46 0 : List<ProfileInfoState> get profiles => _profiles.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
47 :
48 0 : ProfileInfoState? getProfile(String onion) {
49 0 : int idx = _profiles.indexWhere((element) => element.onion == onion);
50 0 : return idx >= 0 ? _profiles[idx] : null;
51 : }
52 :
53 0 : void delete(String onion) {
54 0 : _profiles.removeWhere((element) => element.onion == onion);
55 0 : notifyListeners();
56 : }
57 :
58 0 : int generateUnreadCount(String selectedProfile) => _profiles.where((p) => p.onion != selectedProfile).fold(0, (i, p) => i + p.unreadMessages);
59 :
60 0 : int cacheMemUsage() {
61 0 : return _profiles.map((e) => e.cacheMemUsage()).fold(0, (previousValue, element) => previousValue + element);
62 : }
63 : }
|