334 lines
10 KiB
Dart
334 lines
10 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:yumi/app_localizations.dart';
|
|
import 'package:yumi/ui_kit/components/sc_tts.dart';
|
|
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
|
|
import 'package:yumi/shared/data_sources/sources/repositories/sc_room_repository_imp.dart';
|
|
import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart';
|
|
import 'package:yumi/shared/data_sources/sources/repositories/sc_vip_repository_imp.dart';
|
|
import 'package:yumi/shared/business_logic/models/req/sc_user_profile_cmd.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/country_res.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/login_res.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/room_user_card_res.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/sc_user_identity_res.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/sc_vip_res.dart';
|
|
|
|
class SocialChatUserProfileManager extends ChangeNotifier {
|
|
SCUserProfileCmd? editUser;
|
|
|
|
void updateUserAvatar(String avatar) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setUserAvatar = avatar;
|
|
notifyListeners();
|
|
}
|
|
|
|
void updateUserSex(num sex) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setUserSex = sex;
|
|
}
|
|
|
|
void updateUserNickname(String nickname) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setUserNickname = nickname;
|
|
}
|
|
|
|
void updateBornYear(num bornYear) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setBornYear = bornYear;
|
|
}
|
|
|
|
void updateBornMonth(num bornMonth) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setBornMonth = bornMonth;
|
|
}
|
|
|
|
void updateBornDay(num bornDay) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setBornDay = bornDay;
|
|
}
|
|
|
|
void updateAge(num age) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setAge = age;
|
|
}
|
|
|
|
void setCountry(Country country) {
|
|
editUser ??= SCUserProfileCmd();
|
|
editUser!.setCountryCode = country.alphaTwo!;
|
|
editUser!.setCountryId = country.id!;
|
|
editUser!.setCountryName = country.countryName!;
|
|
}
|
|
|
|
void resetEditUserData() {
|
|
editUser = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
SocialChatUserProfile? get currentUserProfile =>
|
|
AccountStorage().getCurrentUser()?.userProfile;
|
|
|
|
void syncCurrentUserProfile(SocialChatUserProfile? profile) {
|
|
if (profile == null) {
|
|
return;
|
|
}
|
|
final currentUser = AccountStorage().getCurrentUser();
|
|
if (currentUser != null) {
|
|
currentUser.setUserProfile(profile);
|
|
AccountStorage().setCurrentUser(currentUser);
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future fetchUserProfileData({
|
|
bool loadGuardCount = true,
|
|
bool refreshFamilyData = false,
|
|
}) async {
|
|
var us = AccountStorage().getCurrentUser();
|
|
String userId = us?.userProfile?.id ?? "";
|
|
if (userId.isEmpty) {
|
|
return;
|
|
}
|
|
var userInfo = await SCAccountRepository().loadUserInfo(userId);
|
|
userInfo = await _withCurrentVipLevel(userInfo, userId);
|
|
syncCurrentUserProfile(userInfo);
|
|
}
|
|
|
|
SCUserIdentityRes? userIdentity;
|
|
|
|
void getUserIdentity() async {
|
|
if (kDebugMode) {
|
|
final profile = AccountStorage().getCurrentUser()?.userProfile;
|
|
debugPrint(
|
|
'[SCUserIdentity] request start userId=${profile?.id}, account=${profile?.account}',
|
|
);
|
|
}
|
|
try {
|
|
userIdentity = await SCAccountRepository().userIdentity();
|
|
if (kDebugMode) {
|
|
debugPrint(
|
|
'[SCUserIdentity] manager stored admin=${userIdentity?.admin}, '
|
|
'manager=${userIdentity?.manager}, yumiManager=${userIdentity?.yumiManager}',
|
|
);
|
|
}
|
|
notifyListeners();
|
|
} catch (error) {
|
|
if (kDebugMode) {
|
|
debugPrint('[SCUserIdentity] request failed error=$error');
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 当前正在查看的个人详情资料,不用于承载登录用户自己的 Me 页数据。
|
|
SocialChatUserProfile? userProfile;
|
|
|
|
void getUserInfoById(String userId) async {
|
|
userProfile = null;
|
|
final loadedProfile = await SCAccountRepository().loadUserInfo(userId);
|
|
userProfile = await _withCurrentVipLevel(loadedProfile, userId);
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
///房间资料卡
|
|
RoomUserCardRes? userCardInfo;
|
|
|
|
// List<UserCountGuardRes>? userCountGuardResList;
|
|
|
|
void roomUserCard(String roomId, String userId) async {
|
|
userCardInfo = null;
|
|
userCardInfo = await SCChatRoomRepository().roomUserCard(roomId, userId);
|
|
userCardInfo = await _withCurrentVipLevelForCard(userCardInfo, userId);
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<RoomUserCardRes?> _withCurrentVipLevelForCard(
|
|
RoomUserCardRes? card,
|
|
String userId,
|
|
) async {
|
|
final profile = card?.userProfile;
|
|
if (card == null || profile == null) {
|
|
return card;
|
|
}
|
|
final mergedProfile = await _withCurrentVipLevel(profile, userId);
|
|
if (mergedProfile == profile) {
|
|
return card;
|
|
}
|
|
return card.copyWith(userProfile: mergedProfile);
|
|
}
|
|
|
|
Future<SocialChatUserProfile> _withCurrentVipLevel(
|
|
SocialChatUserProfile profile,
|
|
String userId,
|
|
) async {
|
|
if (!_isCurrentUserId(userId)) {
|
|
return profile;
|
|
}
|
|
|
|
final profileVipLevel = profile.vipLevel?.trim();
|
|
if (profileVipLevel != null &&
|
|
profileVipLevel.isNotEmpty &&
|
|
profile.getHeaddress() != null) {
|
|
return profile;
|
|
}
|
|
|
|
final vipStatus = await _loadCurrentVipStatus();
|
|
final currentVipLevel = currentUserProfile?.vipLevel?.trim();
|
|
final resolvedVipLevel =
|
|
profileVipLevel != null && profileVipLevel.isNotEmpty
|
|
? profileVipLevel
|
|
: currentVipLevel != null && currentVipLevel.isNotEmpty
|
|
? currentVipLevel
|
|
: _vipLevelFromStatus(vipStatus);
|
|
|
|
var nextProfile = profile;
|
|
if (resolvedVipLevel != null && resolvedVipLevel.isNotEmpty) {
|
|
nextProfile = nextProfile.copyWith(vipLevel: resolvedVipLevel);
|
|
}
|
|
|
|
return socialChatProfileWithAvatarFrameResource(
|
|
nextProfile,
|
|
_avatarFramePropsFromVipStatus(vipStatus),
|
|
expireTime: _expireTimeFromVipStatus(vipStatus),
|
|
);
|
|
}
|
|
|
|
bool _isCurrentUserId(String userId) {
|
|
final currentUserId = AccountStorage().getCurrentUser()?.userProfile?.id;
|
|
return currentUserId != null &&
|
|
currentUserId.isNotEmpty &&
|
|
currentUserId == userId;
|
|
}
|
|
|
|
Future<SCVipStatusRes?> _loadCurrentVipStatus() async {
|
|
final repository = SCVipRepositoryImp();
|
|
try {
|
|
final home = await repository.vipHome();
|
|
final state = home.state;
|
|
if (state != null && state.levelInt > 0) {
|
|
return state;
|
|
}
|
|
} catch (_) {}
|
|
|
|
try {
|
|
return repository.vipStatus();
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
PropsResources? _avatarFramePropsFromVipStatus(SCVipStatusRes? status) {
|
|
if (status?.isActive != true) {
|
|
return null;
|
|
}
|
|
final resource = status?.avatarFrame;
|
|
return socialChatAvatarFrameProps(
|
|
id: resource?.resourceId,
|
|
name: resource?.name,
|
|
sourceUrl: resource?.sourceResourceUrl,
|
|
cover: resource?.coverUrl ?? resource?.cover,
|
|
code: "VIP_AVATAR_FRAME",
|
|
);
|
|
}
|
|
|
|
String? _expireTimeFromVipStatus(SCVipStatusRes? status) {
|
|
final expireAt = status?.expireAt?.trim();
|
|
if (expireAt == null || expireAt.isEmpty) {
|
|
return null;
|
|
}
|
|
final normalized =
|
|
expireAt.contains("T") ? expireAt : expireAt.replaceFirst(" ", "T");
|
|
return DateTime.tryParse(normalized)?.millisecondsSinceEpoch.toString();
|
|
}
|
|
|
|
String? _vipLevelFromStatus(SCVipStatusRes? status) {
|
|
if (status == null) {
|
|
return null;
|
|
}
|
|
if (status.active == false) {
|
|
return '0';
|
|
}
|
|
final level = status.levelInt;
|
|
return level > 0 ? level.toString() : '0';
|
|
}
|
|
|
|
void followUser(String userId) async {
|
|
var result = await SCAccountRepository().followUser(userId);
|
|
if (result) {
|
|
if (userCardInfo != null) {
|
|
userCardInfo = userCardInfo?.copyWith(follow: !userCardInfo!.follow!);
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|
|
|
|
double myBalance = 0.0;
|
|
|
|
void balance() async {
|
|
myBalance = await SCAccountRepository().balance();
|
|
notifyListeners();
|
|
}
|
|
|
|
updateBalance(double m) {
|
|
myBalance = m;
|
|
notifyListeners();
|
|
}
|
|
|
|
void inviteHostOpt(BuildContext ct, String id, String status) async {
|
|
final successMessage = SCAppLocalizations.of(ct)!.operationSuccessful;
|
|
try {
|
|
await SCAccountRepository().inviteHost(id, status);
|
|
getUserIdentity();
|
|
SCTts.show(successMessage);
|
|
} catch (e) {}
|
|
}
|
|
|
|
void inviteAgentOpt(BuildContext ct, String id, String status) async {
|
|
final successMessage = SCAppLocalizations.of(ct)!.operationSuccessful;
|
|
try {
|
|
await SCAccountRepository().inviteAgent(id, status);
|
|
getUserIdentity();
|
|
SCTts.show(successMessage);
|
|
} catch (e) {}
|
|
}
|
|
|
|
void inviteBDOpt(BuildContext ct, String id, String status) async {
|
|
final successMessage = SCAppLocalizations.of(ct)!.operationSuccessful;
|
|
try {
|
|
await SCAccountRepository().inviteBD(id, status);
|
|
getUserIdentity();
|
|
SCTts.show(successMessage);
|
|
} catch (e) {}
|
|
}
|
|
|
|
void inviteBDLeader(BuildContext ct, String id, String status) async {
|
|
final successMessage = SCAppLocalizations.of(ct)!.operationSuccessful;
|
|
try {
|
|
await SCAccountRepository().inviteBDLeader(id, status);
|
|
getUserIdentity();
|
|
SCTts.show(successMessage);
|
|
} catch (e) {}
|
|
}
|
|
|
|
void inviteRechargeAgent(BuildContext ct, String id, String status) async {
|
|
final successMessage = SCAppLocalizations.of(ct)!.operationSuccessful;
|
|
try {
|
|
await SCAccountRepository().inviteRechargeAgent(id, status);
|
|
getUserIdentity();
|
|
SCTts.show(successMessage);
|
|
} catch (e) {}
|
|
}
|
|
|
|
void cpRlationshipProcessApply(
|
|
BuildContext ct,
|
|
String id,
|
|
bool status,
|
|
) async {
|
|
final successMessage = SCAppLocalizations.of(ct)!.operationSuccessful;
|
|
try {
|
|
await SCAccountRepository().cpRelationshipProcessApply(id, status);
|
|
fetchUserProfileData(loadGuardCount: false);
|
|
SCTts.show(successMessage);
|
|
} catch (e) {}
|
|
}
|
|
}
|