actions-test/lib/screens/room/main.dart

158 lines
4.2 KiB
Dart
Raw Normal View History

2023-03-17 21:08:29 +01:00
import 'package:flutter/material.dart';
import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/backend/room.dart';
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';
import 'package:outbag_app/tools/fetch_wrapper.dart';
2023-03-17 21:08:29 +01:00
import 'package:routemaster/routemaster.dart';
class RoomPage extends StatefulWidget {
final String server;
final String tag;
const RoomPage(this.server, this.tag, {super.key});
2023-03-17 21:08:29 +01:00
@override
State<StatefulWidget> createState() => _RoomPageState();
}
class _RoomPageState extends State<RoomPage> {
RoomInfo? info;
Room? room;
final PageController _ctr = PageController(initialPage: 0);
int page = 0;
// fetch room information
void fetchInfo() async {
final sm = ScaffoldMessenger.of(context);
try {
final diskRoom =
await Room.fromDisk(serverTag: widget.server, id: widget.tag);
setState(() {
room = diskRoom;
});
} catch (_) {}
doNetworkRequest(sm,
req: (user) => postWithCreadentials(
path: 'getRoomInfo',
credentials: user!,
target: (user.server),
body: {'room': widget.tag, 'server': widget.server}),
onOK: (body) async {
final info = RoomInfo.fromJSON(body['data']);
final room = Room.fromJSON(body['data']);
room.toDisk();
setState(() {
this.info = info;
});
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-17 21:08:29 +01:00
@override
void initState() {
super.initState();
_ctr.addListener(() {
setState(() {
page = _ctr.page?.toInt() ?? _ctr.initialPage;
});
});
Room.listen((_) async {
// 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-17 21:08:29 +01:00
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// schedule info-get
fetchInfo();
}
2023-03-17 21:08:29 +01:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(room?.name ?? 'Unknown Room'),
2023-03-17 21:08:29 +01:00
leading: IconButton(
onPressed: () {
// go back
2023-03-17 21:08:29 +01:00
Routemaster.of(context).history.back();
},
icon: const Icon(Icons.arrow_back),
tooltip: "Go back",
),
),
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,
curve: Curves.easeInOut,
duration: const Duration(milliseconds: 300));
},
selectedIndex: page,
destinations: const [
NavigationDestination(
icon: Icon(Icons.list),
label: "List",
tooltip: 'View shopping list'),
NavigationDestination(
icon: Icon(Icons.inventory_2),
label: "Products",
tooltip: 'View saved items'),
NavigationDestination(
icon: Icon(Icons.category),
label: "Categories",
tooltip: 'View categories'),
NavigationDestination(
icon: Icon(Icons.info_rounded),
label: "About",
tooltip: 'View room info'),
],
),
2023-03-17 21:08:29 +01:00
);
}
}