Files
tongxunruanjian/mobile-next/openim_common/lib/src/controller/cache_controller.dart
T
编码工程师andmultica-agent 0a2fbac92a feat(mobile-next): 导入官方 openim-flutter-demo 固定基线 3.8.3-patch.3 并完成双构建
- 上游: github.com/OpenIMSDK/openim-flutter-demo tag 3.8.3-patch.3
  commit b3dfdb1e8aaeaaf6f0793e10cadd20d5c184a31f
- 并行目录 mobile-next/ 原样导入(含 LICENSE),不覆盖现网 mobile/
- 锁定 Flutter 3.24.5 / Dart 3.5.4 / JDK 17 / Gradle 7.6.3 / AGP 7.3.1
- 实测 Android debug 与 release 构建均成功(见 docs/mobile-next-baseline-import.md)
- 本提交可单独回退:git revert 1db1229(重写前)

Co-authored-by: multica-agent <github@multica.ai>
2026-08-19 01:10:39 +08:00

126 lines
3.5 KiB
Dart

import 'dart:convert';
import 'package:flutter_openim_sdk/flutter_openim_sdk.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:openim_common/openim_common.dart';
import 'package:uuid/uuid.dart';
class CacheController extends GetxController {
final favoriteList = <EmojiInfo>[].obs;
final callRecordList = <CallRecords>[].obs;
Box? favoriteBox;
Box? callRecordBox;
bool _isInitFavoriteList = false;
bool _isInitCallRecords = false;
String get userID => DataSp.getLoginCertificate()!.userID;
void addFavoriteFromUrl(String? url, int? width, int? height) {
var emoji = EmojiInfo(url: url, width: width, height: height);
favoriteList.insert(0, emoji);
final list = favoriteList.value;
favoriteBox?.put(userID, list);
}
void addFavoriteFromPath(String path, int width, int height) async {
var result = await LoadingView.singleton.wrap(
asyncFunction: () => OpenIM.iMManager.uploadFile(
id: const Uuid().v4(),
filePath: path,
fileName: path,
),
);
if (result is String) {
final url = jsonDecode(result)['url'];
Logger.print('url:$url');
var emoji = EmojiInfo(url: url, width: width, height: height);
Logger.print('addFavoriteFromPath :$url');
favoriteList.insert(0, emoji);
favoriteBox?.put(userID, favoriteList.value);
}
}
void delFavorite(String url) {
favoriteList.removeWhere((element) => element.url == url);
favoriteBox?.put(userID, favoriteList.value);
}
void delFavoriteList(List<String> urlList) {
for (final url in urlList) {
favoriteList.removeWhere((element) => element.url == url);
}
favoriteBox?.put(userID, favoriteList.value);
}
initFavoriteEmoji() {
if (!_isInitFavoriteList) {
_isInitFavoriteList = true;
var list = favoriteBox?.get(userID, defaultValue: <EmojiInfo>[]);
if (list != null) {
favoriteList.assignAll((list as List).cast());
}
}
}
List<String> get urlList => favoriteList.map((e) => e.url!).toList();
initCallRecords() {
if (!_isInitCallRecords) {
_isInitCallRecords = true;
var list = callRecordBox?.get(userID, defaultValue: <CallRecords>[]);
if (list != null) {
callRecordList.assignAll((list as List).cast());
}
}
}
void resetCache() {
if (_isInitCallRecords) {
callRecordList.value = [];
final list = callRecordBox?.get(userID, defaultValue: <CallRecords>[]);
if (list != null) {
callRecordList.assignAll((list as List).cast());
}
}
if (_isInitFavoriteList) {
favoriteList.value = [];
final list = favoriteBox?.get(userID, defaultValue: <CallRecords>[]);
if (list != null) {
favoriteList.assignAll((list as List).cast());
}
}
}
addCallRecords(CallRecords records) {
callRecordList.insert(0, records);
callRecordBox?.put(userID, callRecordList.value);
}
deleteCallRecords(CallRecords records) async {
callRecordList.removeWhere((element) => element.userID == records.userID && element.date == records.date);
await callRecordBox?.put(userID, callRecordList.value);
}
@override
void onClose() {
_isInitFavoriteList = false;
_isInitCallRecords = false;
Hive.close();
super.onClose();
}
@override
void onInit() async {
Hive.registerAdapter(EmojiInfoAdapter());
Hive.registerAdapter(CallRecordsAdapter());
favoriteBox = await Hive.openBox<List>('favoriteEmoji');
callRecordBox = await Hive.openBox<List>('callRecords');
super.onInit();
}
}