together_mobile/lib/models/contact_model.dart

134 lines
3.4 KiB
Dart
Raw Normal View History

2023-07-27 18:17:52 +08:00
import 'package:flutter/material.dart';
class FriendSetting {
String friendRemark = '';
String friendGroup = '';
FriendSetting.fromJson(Map json) {
2023-07-27 18:17:52 +08:00
friendRemark = json['friendRemark'] ?? '';
friendGroup = json['friendGroup'] ?? '我的好友';
2023-07-27 18:17:52 +08:00
}
}
class GroupChatSetting {
String groupChatRemark = '';
String myRemark = '';
GroupChatSetting.fromJson(Map<String, dynamic> json) {
groupChatRemark = json['groupChatRemark'] ?? '';
myRemark = json['myRemark'] ?? '';
}
}
class Contact extends ChangeNotifier {
Map<String, FriendSetting> friends = {};
List<String> friendGroups = [];
String defaultGroup = '';
2023-07-27 18:17:52 +08:00
Map<String, GroupChatSetting> groupChats = {};
void init(Map json) {
defaultGroup = json['defaultGroup'];
json['friends'].forEach((key, value) {
2023-07-27 18:17:52 +08:00
friends[key] = FriendSetting.fromJson(value);
});
// json['groupChats'].forEach((key, value) {
// groupChats[key] = GroupChatSetting.fromJson(value);
// });
// for (String i in json['friendGroups']) {
// friendGroups.add(i);
// }
friendGroups = List.from(json['friendGroups']);
}
2023-07-27 18:17:52 +08:00
Map<String, FriendSetting> filterGroupFriends(String groupName) {
Map<String, FriendSetting> groupFriends = {};
friends.forEach((key, value) {
if (value.friendGroup == groupName) {
groupFriends[key] = value;
}
2023-07-27 18:17:52 +08:00
});
return groupFriends;
}
2023-07-27 18:17:52 +08:00
void addFriend(String friendId, Map friendSetting) {
2023-08-11 23:02:31 +08:00
friends.addAll({friendId: FriendSetting.fromJson(friendSetting)});
notifyListeners();
}
void changeFriendRemark(String friendId, String remark) {
friends[friendId]!.friendRemark = remark;
}
void changeFriendGroup(String friendId, String group) {
friends[friendId]!.friendGroup = group;
}
void removeFriend(String friendId) {
friends.remove(friendId);
notifyListeners();
}
}
class FriendAccountProfile {
String id = '';
String username = '';
String email = '';
String nickname = '';
String gender = '';
String birthday = '';
String location = '';
String status = '';
String sign = '';
String avatar = '';
String baseAvatarPath = '';
FriendAccountProfile.fromJson(Map<String, dynamic> json) {
id = json['id'];
username = json['username'];
email = json['email'];
nickname = json['nickname'] ?? '';
birthday = json['birthday'] ?? '';
location = json['location'] ?? '';
status = json['status'] ?? '';
sign = json['sign'] ?? '';
avatar = json['avatar'] ?? '';
}
}
class ContactAccountProfile extends ChangeNotifier {
Map<String, FriendAccountProfile> friends = {};
void init(Map json) {
json['friends'].forEach((key, value) {
friends[key] = FriendAccountProfile.fromJson(value);
});
2023-07-27 18:17:52 +08:00
}
(bool, String) getIdBy(String condition, String filterValue) {
String friendId = '';
switch (condition) {
case 'username':
for (var key in friends.keys) {
if (friends[key]!.username == filterValue) {
friendId = key;
break;
}
}
break;
}
return friendId.isNotEmpty ? (true, friendId) : (false, '');
}
2023-08-11 23:02:31 +08:00
void addAccountProfile(String friendId, Map<String, dynamic> json) {
friends.addAll({friendId: FriendAccountProfile.fromJson(json)});
notifyListeners();
}
void removeFriend(String friendId) {
friends.remove(friendId);
2023-08-11 23:02:31 +08:00
notifyListeners();
}
2023-07-27 18:17:52 +08:00
}