import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; import 'package:provider/provider.dart'; import 'package:yumi/app/constants/sc_room_msg_type.dart'; import 'package:yumi/modules/room/background/room_background_history.dart'; import 'package:yumi/services/audio/rtc_manager.dart'; import 'package:yumi/services/audio/rtm_manager.dart'; import 'package:yumi/services/room/rc_room_manager.dart'; import 'package:yumi/shared/data_sources/sources/repositories/sc_general_repository_imp.dart'; import 'package:yumi/shared/data_sources/sources/repositories/sc_user_repository_impl.dart'; import 'package:yumi/ui_kit/widgets/room/room_msg_item.dart'; class RoomBackgroundApplyService { static Future apply(BuildContext context, String sourcePath) async { final normalizedSource = sourcePath.trim(); if (normalizedSource.isEmpty) { throw StateError("Room background source is empty"); } final rtcProvider = context.read(); final rtmProvider = context.read(); final roomManager = context.read(); final roomProfile = rtcProvider.currenRoom?.roomProfile?.roomProfile; final roomId = (roomProfile?.id ?? "").trim(); final roomAccount = (roomProfile?.roomAccount ?? "").trim(); if (roomId.isEmpty) { throw StateError("Room id is empty"); } final uploadedUrl = _isNetworkImage(normalizedSource) ? normalizedSource : await _uploadSource(normalizedSource); if (uploadedUrl.trim().isEmpty) { throw StateError("Uploaded background URL is empty"); } final roomInfo = await SCAccountRepository().updateRoomBackground( roomId, uploadedUrl, ); final roomBackground = _preferNonEmpty( roomInfo.roomBackground, uploadedUrl, ); rtcProvider.updateCurrentRoomBasicInfo(roomBackground: roomBackground); roomManager.updateMyRoomInfo( roomInfo.copyWith( id: roomInfo.id ?? roomId, roomBackground: roomBackground, ), ); if (roomAccount.isNotEmpty) { rtmProvider.dispatchMessage( Msg( groupId: roomAccount, msg: roomInfo.id ?? roomId, type: SCRoomMsgType.roomSettingUpdate, ), addLocal: false, ); } await RoomBackgroundHistory.add(roomBackground); return roomBackground; } static bool _isNetworkImage(String value) { return value.startsWith("http://") || value.startsWith("https://"); } static Future _uploadSource(String sourcePath) async { final sourceFile = _isAssetPath(sourcePath) ? await _copyAssetToTemporaryFile(sourcePath) : File(sourcePath); if (!sourceFile.existsSync()) { throw StateError("Room background source file does not exist"); } final uploadedUrl = await SCGeneralRepositoryImp().upload(sourceFile); return uploadedUrl; } static bool _isAssetPath(String value) { return value.startsWith("assets/") || value.startsWith("sc_images/"); } static Future _copyAssetToTemporaryFile(String assetPath) async { final bytes = await rootBundle.load(assetPath); final directory = await getTemporaryDirectory(); final fileName = assetPath.split("/").last; final file = File("${directory.path}/room_background_$fileName"); await file.writeAsBytes( bytes.buffer.asUint8List(bytes.offsetInBytes, bytes.lengthInBytes), flush: true, ); return file; } static String _preferNonEmpty(String? primary, String fallback) { if ((primary ?? "").trim().isNotEmpty) { return primary!.trim(); } return fallback.trim(); } }