together_mobile/lib/models/apply_list_model.dart

59 lines
1.3 KiB
Dart
Raw Normal View History

2023-07-27 18:17:52 +08:00
import 'package:flutter/material.dart';
class Apply {
int? relation;
String? applicant, recipient, hello, createdAt;
String? groupChatId;
Map<String, dynamic>? setting;
Apply.fromJson(Map<String, dynamic> json) {
relation = json['relation'];
applicant = json['applicant'];
recipient = json['recipient'];
hello = json['hello'];
groupChatId = json['groupChatId'];
setting = json['setting'];
}
}
class ApplyList extends ChangeNotifier {
List<Apply> applyList = [];
List<String> applicantIds = [];
int count = 0;
void init(dynamic json) {
for (var element in json) {
Apply apply = Apply.fromJson(element);
applyList.add(apply);
applicantIds.add(element['applicant']);
count += 1;
}
}
void removeAt(int index) {
applyList.removeAt(index);
2023-08-11 23:02:31 +08:00
applicantIds.removeAt(index);
2023-07-27 18:17:52 +08:00
count--;
2023-08-11 23:02:31 +08:00
notifyListeners();
2023-07-27 18:17:52 +08:00
}
void addJson(Map<String, dynamic> json) {
Apply apply = Apply.fromJson(json);
2023-08-11 23:02:31 +08:00
int index = applicantIds.indexOf(json['applicant']);
if (index != -1) {
applyList.removeAt(index);
count--;
}
2023-07-27 18:17:52 +08:00
applyList.add(apply);
2023-08-11 23:02:31 +08:00
applicantIds.add(json['applicant']);
2023-07-27 18:17:52 +08:00
count++;
2023-08-11 23:02:31 +08:00
notifyListeners();
2023-07-27 18:17:52 +08:00
}
2023-09-09 16:48:47 +08:00
void clear() {
applyList.clear();
applicantIds.clear();
2023-07-27 18:17:52 +08:00
count = 0;
}
}