actions-test/lib/screens/room/main.dart
2023-03-23 10:19:14 +01:00

158 lines
4.2 KiB
Dart

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';
import 'package:outbag_app/screens/room/about.dart';
import 'package:outbag_app/screens/room/categories.dart';
import 'package:outbag_app/screens/room/products.dart';
import 'package:outbag_app/screens/room/shopping_list.dart';
import 'package:routemaster/routemaster.dart';
class RoomPage extends StatefulWidget {
final String server;
final String tag;
const RoomPage(this.server, this.tag, {super.key});
@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 {
bool foundData = false;
try {
final diskRoom =
await Room.fromDisk(serverTag: widget.server, id: widget.tag);
foundData = true;
setState(() {
room = diskRoom;
});
} catch (_) {}
// fetch additional data from web
User user;
try {
user = await User.fromDisk();
} catch (_) {
// probably not logged in
return;
}
try {
final resp = await postWithCreadentials(
path: 'getRoomInfo',
credentials: user,
target: user.server,
body: {'room': widget.tag, 'server': widget.server});
if (resp.res == Result.ok) {
final info = RoomInfo.fromJSON(resp.body['data']);
final room = Room.fromJSON(resp.body['data']);
room.toDisk();
foundData = true;
setState(() {
this.info = info;
});
}
} catch (_) {}
if (!foundData) {
// no room data available
// TODO: close room
// or show snackbar
// BUG: there is currently no way of implementing this,
// because there is no context available here
}
}
@override
void initState() {
super.initState();
// schedule info-get
fetchInfo();
_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 (_) {}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(room?.name ?? 'Unknown Room'),
leading: IconButton(
onPressed: () {
// go back
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'),
],
),
);
}
}