195 lines
6.0 KiB
Dart
195 lines
6.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:yumi/ui_kit/components/sc_tts.dart';
|
|
import 'package:yumi/shared/tools/sc_loading_manager.dart';
|
|
import 'package:yumi/modules/room/voice_room_route.dart';
|
|
import 'package:yumi/services/audio/rtc_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/business_logic/models/res/sc_edit_room_info_res.dart';
|
|
|
|
import 'package:yumi/shared/business_logic/models/res/my_room_res.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/room_res.dart';
|
|
import 'package:yumi/shared/business_logic/models/res/sc_room_contribute_level_res.dart';
|
|
import 'package:yumi/shared/data_sources/sources/local/user_manager.dart';
|
|
|
|
final SocialChatRoomManager socialChatRoomManager = SocialChatRoomManager();
|
|
|
|
SocialChatRoomManager resolveSocialChatRoomManager(BuildContext? context) {
|
|
if (context != null && context.mounted) {
|
|
try {
|
|
final manager = Provider.of<SocialChatRoomManager?>(
|
|
context,
|
|
listen: false,
|
|
);
|
|
if (manager != null) {
|
|
return manager;
|
|
}
|
|
} catch (_) {
|
|
// A stale route context can be outside the provider tree after logout.
|
|
}
|
|
}
|
|
return socialChatRoomManager;
|
|
}
|
|
|
|
class SocialChatRoomManager extends ChangeNotifier {
|
|
MyRoomRes? myRoom;
|
|
SCRoomContributeLevelRes? roomContributeLevelRes;
|
|
String? _roomContributeLevelRoomId;
|
|
bool isMyRoomLoading = false;
|
|
bool hasLoadedMyRoom = false;
|
|
|
|
Future<void> fetchMyRoomData() async {
|
|
if (isMyRoomLoading) {
|
|
return;
|
|
}
|
|
isMyRoomLoading = true;
|
|
if (!hasLoadedMyRoom) {
|
|
myRoom = null;
|
|
}
|
|
notifyListeners();
|
|
try {
|
|
myRoom = await SCAccountRepository().myProfile();
|
|
} catch (_) {
|
|
if (!hasLoadedMyRoom) {
|
|
myRoom = null;
|
|
}
|
|
} finally {
|
|
isMyRoomLoading = false;
|
|
hasLoadedMyRoom = true;
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void updateMyRoomInfo(SCEditRoomInfoRes roomInfo) {
|
|
if (myRoom != null && myRoom!.id == roomInfo.id) {
|
|
if ((roomInfo.roomCover ?? "").trim().isNotEmpty) {
|
|
myRoom?.setRoomCover = roomInfo.roomCover!;
|
|
}
|
|
if ((roomInfo.roomBackground ?? "").trim().isNotEmpty) {
|
|
myRoom?.setRoomBackground = roomInfo.roomBackground!;
|
|
}
|
|
if (roomInfo.roomName != null) {
|
|
myRoom?.setRoomName = roomInfo.roomName!;
|
|
}
|
|
if (roomInfo.roomDesc != null) {
|
|
myRoom?.setRoomDesc = roomInfo.roomDesc!;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
Future<void> createNewRoom(BuildContext context, {String? customName}) async {
|
|
if (isMyRoomLoading) {
|
|
return;
|
|
}
|
|
isMyRoomLoading = true;
|
|
notifyListeners();
|
|
SCLoadingManager.show(context: context);
|
|
try {
|
|
// 差异化:添加自定义名称参数(暂未使用,为未来扩展预留)
|
|
if (customName != null) {
|
|
// TODO: 实现自定义房间名称
|
|
}
|
|
final createdRoom = await SCAccountRepository().createRoom();
|
|
try {
|
|
final profileRoom = await SCAccountRepository().myProfile();
|
|
myRoom =
|
|
_hasUsableRoomIdentity(profileRoom)
|
|
? profileRoom
|
|
: _myRoomFromCreatedRoom(createdRoom);
|
|
} catch (_) {
|
|
myRoom = _myRoomFromCreatedRoom(createdRoom);
|
|
}
|
|
final draftRoom = myRoom;
|
|
if (!_hasUsableRoomIdentity(draftRoom)) {
|
|
throw Exception("Room data is not ready");
|
|
}
|
|
hasLoadedMyRoom = true;
|
|
isMyRoomLoading = false;
|
|
if (!context.mounted) {
|
|
return;
|
|
}
|
|
notifyListeners();
|
|
SCLoadingManager.hide();
|
|
await VoiceRoomRoute.openRoomEdit(
|
|
context,
|
|
needRestCurrentRoomInfo: true,
|
|
setupPreviewData: RoomEntryPreviewData.fromMyRoom(
|
|
draftRoom!,
|
|
ownerProfile: AccountStorage().getCurrentUser()?.userProfile,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
isMyRoomLoading = false;
|
|
if (context.mounted) {
|
|
SCTts.show(e.toString());
|
|
}
|
|
} finally {
|
|
notifyListeners();
|
|
SCLoadingManager.hide();
|
|
}
|
|
}
|
|
|
|
MyRoomRes _myRoomFromCreatedRoom(SocialChatRoomRes room) {
|
|
return MyRoomRes(
|
|
id: room.id,
|
|
roomAccount: room.roomAccount,
|
|
userId: room.userId,
|
|
roomCover: room.roomCover,
|
|
roomBackground: room.roomBackground,
|
|
roomName: room.roomName,
|
|
roomDesc: room.roomDesc,
|
|
event: room.event,
|
|
sysOrigin: room.sysOrigin,
|
|
countryCode: room.countryCode,
|
|
countryName: room.countryName,
|
|
nationalFlag: room.nationalFlag,
|
|
setting: room.extValues?.roomSetting,
|
|
);
|
|
}
|
|
|
|
bool _hasUsableRoomIdentity(MyRoomRes? room) {
|
|
return (room?.id ?? "").trim().isNotEmpty &&
|
|
(room?.roomAccount ?? "").trim().isNotEmpty;
|
|
}
|
|
|
|
void clearContributionLevelData({bool notify = true}) {
|
|
_roomContributeLevelRoomId = null;
|
|
if (roomContributeLevelRes == null) {
|
|
return;
|
|
}
|
|
roomContributeLevelRes = null;
|
|
if (notify) {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void fetchContributionLevelData(String roomId, {bool forceRefresh = false}) {
|
|
final resolvedRoomId = roomId.trim();
|
|
if (resolvedRoomId.isEmpty) {
|
|
clearContributionLevelData();
|
|
return;
|
|
}
|
|
|
|
final shouldClear =
|
|
forceRefresh || _roomContributeLevelRoomId != resolvedRoomId;
|
|
_roomContributeLevelRoomId = resolvedRoomId;
|
|
if (shouldClear && roomContributeLevelRes != null) {
|
|
roomContributeLevelRes = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
SCChatRoomRepository()
|
|
.roomContributionActivity(resolvedRoomId)
|
|
.then((value) {
|
|
if (_roomContributeLevelRoomId != resolvedRoomId) {
|
|
return;
|
|
}
|
|
roomContributeLevelRes = value;
|
|
notifyListeners();
|
|
})
|
|
.catchError((_) {});
|
|
}
|
|
}
|