app/lib/ui/party_room/party_room_home_ui_model.dart

210 lines
5.9 KiB
Dart
Raw Normal View History

2024-01-13 13:54:32 +08:00
import 'package:fixnum/fixnum.dart';
2024-01-07 14:57:49 +08:00
import 'package:starcitizen_doctor/base/ui_model.dart';
2024-01-14 20:47:15 +08:00
import 'package:starcitizen_doctor/common/conf.dart';
import 'package:starcitizen_doctor/common/grpc/party_room_server.dart';
2024-01-07 18:52:06 +08:00
import 'package:starcitizen_doctor/generated/grpc/party_room_server/index.pb.dart';
2024-01-14 20:47:15 +08:00
import 'package:starcitizen_doctor/global_ui_model.dart';
2024-01-13 17:29:41 +08:00
import 'package:starcitizen_doctor/ui/party_room/dialogs/party_room_create_dialog_ui_model.dart';
import 'dialogs/party_room_create_dialog_ui.dart';
2024-01-14 20:47:15 +08:00
import 'party_room_chat_ui_model.dart';
2024-01-07 14:57:49 +08:00
class PartyRoomHomeUIModel extends BaseUIModel {
String? pingServerMessage;
2024-01-07 18:52:06 +08:00
Map<String?, RoomType>? roomTypes;
RoomType? selectedRoomType;
RoomSubtype? selectedRoomSubType;
final roomStatus = <RoomStatus, String>{
RoomStatus.All: "全部",
RoomStatus.Open: "开启中",
RoomStatus.Full: "已满员",
RoomStatus.Closed: "已封闭",
RoomStatus.WillOffline: "房主离线",
RoomStatus.Offline: "已离线",
};
RoomStatus selectedStatus = RoomStatus.All;
final roomSorts = <RoomSortType, String>{
RoomSortType.Default: "默认",
RoomSortType.MostPlayerNumber: "最多玩家",
RoomSortType.MinimumPlayerNumber: "最少玩家",
RoomSortType.RecentlyCreated: "最近创建",
RoomSortType.OldestCreated: "最久创建",
};
RoomSortType selectedSortType = RoomSortType.Default;
2024-01-13 17:29:41 +08:00
int pageNum = 0;
2024-01-13 13:54:32 +08:00
List<RoomData>? rooms;
2024-01-14 20:47:15 +08:00
final pageCtrl = PageController();
@override
BaseUIModel? onCreateChildUIModel(modelKey) {
switch (modelKey) {
case "chat":
return PartyRoomChatUIModel(this);
}
return null;
2024-01-13 13:54:32 +08:00
}
2024-01-07 14:57:49 +08:00
@override
Future loadData() async {
2024-01-29 20:37:15 +08:00
// if (pingServerMessage != "") {
// pingServerMessage = null;
// notifyListeners();
// await _pingServer();
// }
// await _loadPage();
}
// @override
// reloadData() async {
// pageNum = 0;
// rooms = null;
// notifyListeners();
// _touchUser();
// return super.reloadData();
// }
// _loadPage() async {
// final r = await handleError(() => PartyRoomGrpcServer.getRoomList(
// RoomListPageReqData(
// pageNum: Int64.tryParseInt("$pageNum"),
// typeID: selectedRoomType?.id ?? "",
// subTypeID: selectedRoomSubType?.id ?? "",
// status: selectedStatus)));
// if (r == null) return;
// if (r.pageData.hasNext) {
// pageNum++;
// } else {
// pageNum = -1;
// }
// rooms = r.rooms;
// notifyListeners();
// }
//
// _pingServer() async {
// try {
// final r = await PartyRoomGrpcServer.pingServer();
// dPrint(
// "[PartyRoomHomeUIModel] Connected! serverVersion ==> ${r.serverVersion}");
// pingServerMessage = "";
// notifyListeners();
// } catch (e) {
// pingServerMessage = "服务器连接失败,请稍后重试。\n$e";
// notifyListeners();
// return;
// }
// }
//
// Future<void> _loadTypes() async {
// final r = await handleError(() => PartyRoomGrpcServer.getRoomTypes());
// if (r == null) return;
// selectedRoomType =
// RoomType(id: "", name: "全部", desc: "查看所有类型的房间,寻找一起玩的伙伴。");
// selectedRoomSubType = RoomSubtype(id: "", name: "全部");
// roomTypes = {"": selectedRoomType!};
// for (var element in r.roomTypes) {
// roomTypes![element.id] = element;
// }
// notifyListeners();
// }
2024-01-07 18:52:06 +08:00
Map<String, RoomSubtype>? getCurRoomSubTypes() {
if (selectedRoomType?.subTypes == null) return null;
Map<String, RoomSubtype> types = {};
for (var element in selectedRoomType!.subTypes) {
types[element.id] = element;
}
if (types.isEmpty) return null;
2024-01-13 20:00:06 +08:00
final allSubType = RoomSubtype(id: "", name: "全部");
2024-01-07 18:52:06 +08:00
selectedRoomSubType ??= allSubType;
return {"all": allSubType}..addAll(types);
}
void onChangeRoomType(RoomType? value) {
selectedRoomType = value;
selectedRoomSubType = null;
2024-01-13 17:29:41 +08:00
reloadData();
2024-01-07 18:52:06 +08:00
notifyListeners();
}
void onChangeRoomStatus(RoomStatus? value) {
if (value == null) return;
selectedStatus = value;
2024-01-13 17:29:41 +08:00
reloadData();
2024-01-07 18:52:06 +08:00
notifyListeners();
}
void onChangeRoomSort(RoomSortType? value) {
if (value == null) return;
selectedSortType = value;
2024-01-13 17:29:41 +08:00
reloadData();
2024-01-07 18:52:06 +08:00
notifyListeners();
}
void onChangeRoomSubType(RoomSubtype? value) {
if (value == null) return;
selectedRoomSubType = value;
2024-01-13 17:29:41 +08:00
reloadData();
2024-01-07 18:52:06 +08:00
notifyListeners();
}
2024-01-13 17:29:41 +08:00
onCreateRoom() async {
final room = await showDialog(
context: context!,
dismissWithEsc: false,
builder: (BuildContext context) {
return BaseUIContainer(
uiCreate: () => PartyRoomCreateDialogUI(),
modelCreate: () =>
PartyRoomCreateDialogUIModel(Map.from(roomTypes!)));
},
);
2024-01-13 20:00:06 +08:00
if (room == null) return;
2024-01-13 17:29:41 +08:00
dPrint(room);
2024-01-13 20:00:06 +08:00
reloadData();
}
onRefreshRoom() {
reloadData();
2024-01-13 17:29:41 +08:00
}
2024-01-14 20:47:15 +08:00
2024-01-29 20:37:15 +08:00
// Future<void> _touchUser() async {
// if (getCreatedChildUIModel<PartyRoomChatUIModel>("chat")?.selectRoom ==
// null) {
// final userName = await globalUIModel.getRunningGameUser();
// if (userName == null) return;
// // 检测用户已加入的房间
// final room = await handleError(() =>
// PartyRoomGrpcServer.touchUserRoom(userName, AppConf.deviceUUID));
// dPrint("touch room == ${room?.toProto3Json()}");
// if (room == null || room.id == "") return;
// onTapRoom(room);
// }
// }
2024-01-14 20:47:15 +08:00
onTapRoom(RoomData item) {
getCreatedChildUIModel<PartyRoomChatUIModel>("chat", create: true)
?.setRoom(item);
notifyListeners();
pageCtrl.animateToPage(1,
duration: const Duration(milliseconds: 100), curve: Curves.easeInExpo);
}
void checkUIInit() {
if (getCreatedChildUIModel<PartyRoomChatUIModel>("chat")?.selectRoom !=
null) {
pageCtrl.jumpToPage(1);
}
}
2024-01-07 14:57:49 +08:00
}