2023-03-17 21:08:29 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-22 21:17:03 +01:00
|
|
|
import 'package:outbag_app/backend/request.dart';
|
|
|
|
import 'package:outbag_app/backend/room.dart';
|
2023-03-25 17:18:46 +01:00
|
|
|
import 'package:outbag_app/backend/user.dart';
|
2023-03-23 14:04:48 +01:00
|
|
|
import 'package:outbag_app/screens/room/pages/about.dart';
|
|
|
|
import 'package:outbag_app/screens/room/pages/categories.dart';
|
|
|
|
import 'package:outbag_app/screens/room/pages/products.dart';
|
|
|
|
import 'package:outbag_app/screens/room/pages/list.dart';
|
2023-03-23 14:48:53 +01:00
|
|
|
import 'package:outbag_app/tools/fetch_wrapper.dart';
|
2023-03-25 17:18:46 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2023-03-17 21:08:29 +01:00
|
|
|
|
|
|
|
class RoomPage extends StatefulWidget {
|
|
|
|
final String server;
|
|
|
|
final String tag;
|
|
|
|
|
2023-03-22 21:17:03 +01:00
|
|
|
const RoomPage(this.server, this.tag, {super.key});
|
2023-03-17 21:08:29 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _RoomPageState();
|
|
|
|
}
|
|
|
|
|
2023-03-22 21:17:03 +01:00
|
|
|
class _RoomPageState extends State<RoomPage> {
|
|
|
|
RoomInfo? info;
|
|
|
|
Room? room;
|
|
|
|
|
|
|
|
final PageController _ctr = PageController(initialPage: 0);
|
|
|
|
int page = 0;
|
|
|
|
|
|
|
|
// fetch room information
|
|
|
|
void fetchInfo() async {
|
2023-03-23 14:48:53 +01:00
|
|
|
final sm = ScaffoldMessenger.of(context);
|
2023-03-25 17:18:46 +01:00
|
|
|
final user = context.read<User>();
|
2023-03-23 10:19:14 +01:00
|
|
|
|
2023-03-22 21:17:03 +01:00
|
|
|
try {
|
|
|
|
final diskRoom =
|
2023-03-23 14:48:53 +01:00
|
|
|
await Room.fromDisk(serverTag: widget.server, id: widget.tag);
|
2023-03-22 21:17:03 +01:00
|
|
|
setState(() {
|
2023-03-23 14:48:53 +01:00
|
|
|
room = diskRoom;
|
2023-03-22 21:17:03 +01:00
|
|
|
});
|
|
|
|
} catch (_) {}
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
doNetworkRequest(sm,
|
2023-03-25 17:18:46 +01:00
|
|
|
req: () => postWithCreadentials(
|
2023-03-23 14:48:53 +01:00
|
|
|
path: 'getRoomInfo',
|
2023-03-25 17:18:46 +01:00
|
|
|
credentials: user,
|
|
|
|
target: user.server,
|
2023-03-23 14:48:53 +01:00
|
|
|
body: {'room': widget.tag, 'server': widget.server}),
|
|
|
|
onOK: (body) async {
|
|
|
|
final info = RoomInfo.fromJSON(body['data']);
|
|
|
|
final room = Room.fromJSON(body['data']);
|
2023-03-23 10:19:14 +01:00
|
|
|
|
|
|
|
room.toDisk();
|
|
|
|
|
2023-03-22 21:17:03 +01:00
|
|
|
setState(() {
|
2023-03-23 14:48:53 +01:00
|
|
|
this.info = info;
|
2023-03-22 21:17:03 +01:00
|
|
|
});
|
2023-03-23 14:48:53 +01:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onNetworkErr: () {
|
|
|
|
// user offline
|
|
|
|
if (room == null) {
|
|
|
|
// no room data available
|
|
|
|
// NOTE: close room?
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
onServerErr: (json) {
|
|
|
|
// user no longer in room
|
|
|
|
// TODO: close room
|
|
|
|
return true;
|
|
|
|
});
|
2023-03-22 21:17:03 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 21:08:29 +01:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-03-22 21:17:03 +01:00
|
|
|
|
|
|
|
_ctr.addListener(() {
|
2023-03-23 14:48:53 +01:00
|
|
|
setState(() {
|
|
|
|
page = _ctr.page?.toInt() ?? _ctr.initialPage;
|
|
|
|
});
|
2023-03-22 21:17:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Room.listen((_) async {
|
2023-03-23 14:48:53 +01:00
|
|
|
// rooms changed on disk
|
|
|
|
// probably this one,
|
|
|
|
// because it is currently open
|
|
|
|
// NOTE: might be a different room
|
|
|
|
// (if a background listener is implemented at some point,
|
|
|
|
// checking if this room changed might improve performance)
|
|
|
|
try {
|
|
|
|
final r = await Room.fromDisk(serverTag: widget.server, id: widget.tag);
|
|
|
|
setState(() {
|
|
|
|
room = r;
|
|
|
|
});
|
|
|
|
} catch (_) {}
|
2023-03-22 21:17:03 +01:00
|
|
|
});
|
2023-03-17 21:08:29 +01:00
|
|
|
|
2023-03-24 16:33:37 +01:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => fetchInfo());
|
2023-03-23 14:48:53 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 21:08:29 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-03-22 21:17:03 +01:00
|
|
|
title: Text(room?.name ?? 'Unknown Room'),
|
2023-03-17 21:08:29 +01:00
|
|
|
),
|
2023-03-22 21:17:03 +01:00
|
|
|
body: PageView(
|
|
|
|
controller: _ctr,
|
|
|
|
children: [
|
|
|
|
ShoppingListPage(room, info),
|
|
|
|
RoomProductsPage(room, info),
|
|
|
|
RoomCategoriesPage(room, info),
|
|
|
|
AboutRoomPage(room, info)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
bottomNavigationBar: NavigationBar(
|
|
|
|
onDestinationSelected: (int index) {
|
|
|
|
_ctr.animateToPage(index,
|
2023-03-23 14:48:53 +01:00
|
|
|
curve: Curves.easeInOut,
|
|
|
|
duration: const Duration(milliseconds: 300));
|
2023-03-22 21:17:03 +01:00
|
|
|
},
|
|
|
|
selectedIndex: page,
|
|
|
|
destinations: const [
|
|
|
|
NavigationDestination(
|
2023-03-23 14:48:53 +01:00
|
|
|
icon: Icon(Icons.list),
|
|
|
|
label: "List",
|
|
|
|
tooltip: 'View shopping list'),
|
2023-03-22 21:17:03 +01:00
|
|
|
NavigationDestination(
|
2023-03-23 14:48:53 +01:00
|
|
|
icon: Icon(Icons.inventory_2),
|
|
|
|
label: "Products",
|
|
|
|
tooltip: 'View saved items'),
|
2023-03-22 21:17:03 +01:00
|
|
|
NavigationDestination(
|
2023-03-23 14:48:53 +01:00
|
|
|
icon: Icon(Icons.category),
|
|
|
|
label: "Categories",
|
|
|
|
tooltip: 'View categories'),
|
2023-03-22 21:17:03 +01:00
|
|
|
NavigationDestination(
|
2023-03-23 14:48:53 +01:00
|
|
|
icon: Icon(Icons.info_rounded),
|
|
|
|
label: "About",
|
|
|
|
tooltip: 'View room info'),
|
2023-03-22 21:17:03 +01:00
|
|
|
],
|
|
|
|
),
|
2023-03-17 21:08:29 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|