293 lines
8.3 KiB
Dart
293 lines
8.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:aslan/chatvibe_data/sources/local/file_cache_manager.dart';
|
|
import 'package:aslan/chatvibe_data/sources/repositories/config_repository_imp.dart';
|
|
import 'package:aslan/chatvibe_data/sources/repositories/room_repository_imp.dart';
|
|
import 'package:aslan/chatvibe_data/models/country_mode.dart';
|
|
import 'package:aslan/chatvibe_domain/models/res/at_banner_leaderboard_res.dart';
|
|
import 'package:aslan/chatvibe_domain/models/res/country_res.dart';
|
|
import 'package:aslan/chatvibe_domain/models/res/gift_res.dart';
|
|
import 'package:aslan/chatvibe_domain/models/res/at_index_banner_res.dart';
|
|
import 'package:aslan/chatvibe_domain/models/res/at_room_emoji_res.dart';
|
|
|
|
import '../chatvibe_data/models/enum/at_banner_type.dart';
|
|
import '../chatvibe_domain/models/res/room_res.dart';
|
|
|
|
class AppGeneralManager extends ChangeNotifier {
|
|
List<CountryMode> countryModeList = [];
|
|
Map<String, List<Country>> _countryMap = {};
|
|
Map<String, Country> _countryByNameMap = {};
|
|
|
|
///礼物
|
|
List<ChatVibeGiftRes> giftResList = [];
|
|
Map<String, ChatVibeGiftRes> _giftByIdMap = {};
|
|
|
|
Map<String, List<ChatVibeGiftRes>> giftByTab = {};
|
|
List<String> giftTabs = [];
|
|
Map<String, List<ChatVibeGiftRes>> activityGiftByTab = {};
|
|
|
|
///选中的国家
|
|
Country? selectCountryInfo;
|
|
|
|
List<ChatVibeGiftRes> activityList = [];
|
|
|
|
AppGeneralManager() {
|
|
loadCountry();
|
|
giftList(); // 使用默认参数
|
|
giftActivityList();
|
|
discoveryRoom();
|
|
// giftBackpack();
|
|
// configLevel();
|
|
}
|
|
|
|
///加载国家列表
|
|
void loadCountry() async {
|
|
if (countryModeList.isNotEmpty) {
|
|
resetCountryState();
|
|
notifyListeners();
|
|
return;
|
|
}
|
|
while (countryModeList.isEmpty) {
|
|
await Future.delayed(Duration(milliseconds: 550));
|
|
var result = await ConfigRepositoryImp().loadCountry();
|
|
if (result.openCountry!.isNotEmpty) {
|
|
result.openCountry?.forEach((c) {
|
|
String prefix = c.aliasName?.substring(0, 1) ?? "";
|
|
if (prefix.isNotEmpty) {
|
|
List<Country> countryList = _countryMap[prefix] ?? [];
|
|
countryList.add(c.copyWith());
|
|
_countryMap[prefix] = countryList;
|
|
}
|
|
_countryByNameMap[c.countryName!] = c;
|
|
});
|
|
}
|
|
_countryMap.forEach((k, v) {
|
|
countryModeList.add(CountryMode(k, v));
|
|
});
|
|
|
|
///排序
|
|
countryModeList.sort((a, b) => a.prefix.compareTo(b.prefix));
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
///国家国家名称获取国家
|
|
Country? getCountryByName(String countryName) {
|
|
return _countryByNameMap[countryName];
|
|
}
|
|
|
|
/// 只暴露国旗地址给 Selector 使用,避免 AppGeneralManager 其它数据更新时刷新整块 UI。
|
|
String getCountryFlagByName(String? countryName) {
|
|
if (countryName == null || countryName.isEmpty) return "";
|
|
return _countryByNameMap[countryName]?.nationalFlag ?? "";
|
|
}
|
|
|
|
///选择国家
|
|
Country? selectCountry(int subIndex, int cIndex) {
|
|
var selectCm = countryModeList[subIndex].prefixCountrys[cIndex];
|
|
if (selectCountryInfo != null) {
|
|
selectCountryInfo = null;
|
|
} else {
|
|
selectCountryInfo = selectCm;
|
|
}
|
|
return selectCountryInfo;
|
|
}
|
|
|
|
void setCurrentCountry(Country? countryInfo) {
|
|
selectCountryInfo = countryInfo;
|
|
notifyListeners();
|
|
}
|
|
|
|
void resetCountryState() {
|
|
selectCountryInfo = null;
|
|
}
|
|
|
|
void openDrawer(BuildContext context) {
|
|
Scaffold.of(context).openDrawer();
|
|
}
|
|
|
|
void closeDrawer(BuildContext context) {
|
|
Scaffold.of(context).closeDrawer();
|
|
}
|
|
|
|
void giftList({bool includeCustomized = true}) async {
|
|
try {
|
|
giftResList = await ChatRoomRepository().giftList();
|
|
giftByTab.clear();
|
|
giftTabs.clear();
|
|
final apiGiftTabs = <String>[];
|
|
for (var gift in giftResList) {
|
|
final giftTab = gift.giftTab;
|
|
if (giftTab == null || giftTab.isEmpty) {
|
|
continue;
|
|
}
|
|
if (!apiGiftTabs.contains(giftTab)) {
|
|
apiGiftTabs.add(giftTab);
|
|
}
|
|
var gmap = giftByTab[giftTab];
|
|
gmap ??= [];
|
|
gmap.add(gift);
|
|
giftByTab[giftTab] = gmap;
|
|
var gAllMap = giftByTab["ALL"];
|
|
gAllMap ??= [];
|
|
if (!_isSpecialGiftTab(giftTab)) {
|
|
gAllMap.add(gift);
|
|
}
|
|
giftByTab["ALL"] = gAllMap;
|
|
if (gift.id != null) {
|
|
_giftByIdMap[gift.id!] = gift;
|
|
}
|
|
}
|
|
|
|
if (includeCustomized) {
|
|
for (final giftTab in apiGiftTabs) {
|
|
if (_isCustomizedGiftTab(giftTab) &&
|
|
(giftByTab[giftTab]?.isNotEmpty ?? false)) {
|
|
///定制礼物需要一个占位符,显示规则的
|
|
giftByTab[giftTab]?.insert(0, ChatVibeGiftRes(id: "-1000"));
|
|
}
|
|
}
|
|
} else {
|
|
giftByTab.remove("CUSTOMIZED");
|
|
giftByTab.remove("EXCLUSIVE");
|
|
apiGiftTabs.removeWhere(_isCustomizedGiftTab);
|
|
}
|
|
|
|
if (giftByTab["ALL"]?.isNotEmpty ?? false) {
|
|
giftTabs.add("ALL");
|
|
}
|
|
giftTabs.addAll(
|
|
apiGiftTabs.where((giftTab) => _isSpecialGiftTab(giftTab)),
|
|
);
|
|
|
|
notifyListeners();
|
|
|
|
///先去预下载
|
|
downLoad(giftResList);
|
|
} catch (e) {}
|
|
}
|
|
|
|
bool _isSpecialGiftTab(String giftTab) {
|
|
return giftTab == "NATIONAL_FLAG" ||
|
|
giftTab == "ACTIVITY" ||
|
|
giftTab == "LUCKY_GIFT" ||
|
|
giftTab == "CP" ||
|
|
_isCustomizedGiftTab(giftTab) ||
|
|
giftTab == "MAGIC";
|
|
}
|
|
|
|
bool _isCustomizedGiftTab(String giftTab) {
|
|
return giftTab == "CUSTOMIZED" || giftTab == "EXCLUSIVE";
|
|
}
|
|
|
|
void giftActivityList() async {
|
|
try {
|
|
activityList.clear();
|
|
activityList = await ChatRoomRepository().giftActivityList();
|
|
activityGiftByTab.clear();
|
|
for (var gift in activityList) {
|
|
var gmap = activityGiftByTab["${gift.activityId}"];
|
|
gmap ??= [];
|
|
gmap.add(gift);
|
|
activityGiftByTab["${gift.activityId}"] = gmap;
|
|
}
|
|
notifyListeners();
|
|
} catch (e) {}
|
|
}
|
|
|
|
///礼物背包
|
|
void giftBackpack() async {
|
|
try {
|
|
await ChatRoomRepository().giftBackpack();
|
|
} catch (e) {}
|
|
}
|
|
|
|
///礼物id获取礼物
|
|
ChatVibeGiftRes? getGiftById(String id) {
|
|
return _giftByIdMap[id];
|
|
}
|
|
|
|
void downLoad(List<ChatVibeGiftRes> giftResList) {
|
|
for (var gift in giftResList) {
|
|
final sourceUrl = gift.giftSourceUrl;
|
|
if (sourceUrl != null && sourceUrl.isNotEmpty) {
|
|
FileCacheManager.getInstance().getFile(url: sourceUrl);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<ATIndexBannerRes> homeBanners = [];
|
|
List<ATIndexBannerRes> exploreBanners = [];
|
|
List<ATIndexBannerRes> roomBanners = [];
|
|
List<ATIndexBannerRes> roomListBanners = [];
|
|
List<ATIndexBannerRes> gameBanners = [];
|
|
|
|
///首页banner
|
|
void loadMainBanner() async {
|
|
try {
|
|
var banners = await ConfigRepositoryImp().getBanner();
|
|
homeBanners.clear();
|
|
roomBanners.clear();
|
|
roomListBanners.clear();
|
|
gameBanners.clear();
|
|
exploreBanners.clear();
|
|
for (var v in banners) {
|
|
List<String> displayPositions = (v.displayPosition ?? "").split(',');
|
|
if (displayPositions.contains(ATBannerType.EXPLORE_PAGE.name)) {
|
|
exploreBanners.add(v);
|
|
}
|
|
if (displayPositions.contains(ATBannerType.ROOM.name)) {
|
|
roomBanners.add(v);
|
|
}
|
|
if (displayPositions.contains(ATBannerType.ROOM_LIST.name)) {
|
|
roomListBanners.add(v);
|
|
}
|
|
|
|
if (displayPositions.contains(ATBannerType.HOME_ALERT.name)) {
|
|
homeBanners.add(v);
|
|
}
|
|
if (displayPositions.contains(ATBannerType.GAME.name)) {
|
|
gameBanners.add(v);
|
|
}
|
|
}
|
|
notifyListeners();
|
|
} catch (e) {}
|
|
}
|
|
|
|
Map<String, List<Emojis>> emojiByTab = {};
|
|
|
|
///emoji表情
|
|
void emojiAll() async {
|
|
var roomEmojis = await ChatRoomRepository().emojiAll();
|
|
for (var value in roomEmojis) {
|
|
emojiByTab[value.groupName!] = value.emojis ?? [];
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
///加载等级资源
|
|
void configLevel() {
|
|
ConfigRepositoryImp().configLevel();
|
|
}
|
|
|
|
ATBannerLeaderboardRes? appLeaderResult;
|
|
|
|
///查询榜单前三名
|
|
void appLeaderboard() async {
|
|
try {
|
|
appLeaderResult = await ChatRoomRepository().appLeaderboard();
|
|
notifyListeners();
|
|
} catch (e) {}
|
|
}
|
|
|
|
ChatVibeRoomRes? hotRoom;
|
|
|
|
void discoveryRoom() {
|
|
ChatRoomRepository()
|
|
.discovery(allRegion: true)
|
|
.then((values) {
|
|
hotRoom = values.first;
|
|
})
|
|
.catchError((e) {});
|
|
}
|
|
}
|