2023-03-20 20:45:49 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-03-17 21:08:29 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-18 20:27:20 +01:00
|
|
|
import 'package:flutter_svg/flutter_svg.dart';
|
|
|
|
import 'package:outbag_app/backend/request.dart';
|
|
|
|
import 'package:outbag_app/backend/user.dart';
|
|
|
|
import 'package:routemaster/routemaster.dart';
|
|
|
|
import '../backend/room.dart';
|
2023-03-17 21:08:29 +01:00
|
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
|
|
State<HomePage> createState() => _HomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomePageState extends State<HomePage> {
|
2023-03-18 20:27:20 +01:00
|
|
|
List<Room> rooms = [];
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
// wait for background room changes
|
|
|
|
Room.listen((_) async {
|
2023-03-20 20:45:49 +01:00
|
|
|
try {
|
|
|
|
final newRooms = await Room.listRooms();
|
|
|
|
setState(() {
|
|
|
|
rooms = newRooms;
|
|
|
|
});
|
|
|
|
} catch (_) {}
|
2023-03-18 20:27:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// load cached rooms
|
|
|
|
(() async {
|
2023-03-20 20:45:49 +01:00
|
|
|
try {
|
|
|
|
final newRooms = await Room.listRooms();
|
|
|
|
setState(() {
|
|
|
|
rooms = newRooms;
|
|
|
|
});
|
|
|
|
} catch (_) {}
|
2023-03-18 20:27:20 +01:00
|
|
|
})();
|
|
|
|
|
|
|
|
// fetch room list
|
|
|
|
(() async {
|
2023-03-20 20:45:49 +01:00
|
|
|
User user;
|
|
|
|
try {
|
|
|
|
user = await User.fromDisk();
|
|
|
|
} catch (_) {
|
|
|
|
// probably not logged in
|
|
|
|
return;
|
|
|
|
}
|
2023-03-18 20:27:20 +01:00
|
|
|
|
2023-03-20 20:45:49 +01:00
|
|
|
try {
|
|
|
|
final resp = await postWithCreadentials(
|
|
|
|
path: 'listRooms',
|
2023-03-18 20:27:20 +01:00
|
|
|
credentials: user,
|
|
|
|
target: user.server,
|
|
|
|
body: {});
|
2023-03-20 20:45:49 +01:00
|
|
|
if (resp.res == Result.ok) {
|
|
|
|
final List<Room> list = resp.body['data'].map<Room>((json){
|
|
|
|
return Room.fromJSON(json);
|
|
|
|
}).toList();
|
|
|
|
for (Room r in list) {
|
|
|
|
await r.toDisk();
|
2023-03-18 20:27:20 +01:00
|
|
|
}
|
2023-03-20 20:45:49 +01:00
|
|
|
}
|
|
|
|
} catch (_) {}
|
2023-03-18 20:27:20 +01:00
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2023-03-17 21:08:29 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-03-18 20:27:20 +01:00
|
|
|
return Scaffold(
|
2023-03-17 21:08:29 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text("Outbag"),
|
2023-03-18 20:27:20 +01:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.search),
|
|
|
|
tooltip: "Search",
|
|
|
|
onPressed: () {
|
|
|
|
// show searchbar
|
|
|
|
// NOTE: location currently unknown
|
|
|
|
},
|
|
|
|
),
|
|
|
|
MenuAnchor(
|
|
|
|
builder: (ctx, controller, child) {
|
|
|
|
return IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (controller.isOpen) {
|
|
|
|
controller.close();
|
|
|
|
} else {
|
|
|
|
controller.open();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.more_vert),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
menuChildren: [
|
|
|
|
MenuItemButton(
|
2023-03-20 20:45:49 +01:00
|
|
|
leadingIcon: const Icon(Icons.settings),
|
|
|
|
child: const Text('Settings'),
|
|
|
|
onPressed: () {
|
|
|
|
// show settings screen
|
|
|
|
Routemaster.of(context).push("/settings");
|
|
|
|
}),
|
2023-03-18 20:27:20 +01:00
|
|
|
MenuItemButton(
|
2023-03-20 20:45:49 +01:00
|
|
|
leadingIcon: const Icon(Icons.info_rounded),
|
|
|
|
child: const Text('About'),
|
|
|
|
onPressed: () {
|
|
|
|
// show about screen
|
|
|
|
Routemaster.of(context).push("/about");
|
|
|
|
}),
|
2023-03-18 20:27:20 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
2023-03-17 21:08:29 +01:00
|
|
|
),
|
2023-03-18 20:27:20 +01:00
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: rooms.length,
|
|
|
|
itemBuilder: (ctx, i) {
|
|
|
|
final room = rooms[i];
|
|
|
|
return Card(
|
2023-03-20 20:45:49 +01:00
|
|
|
margin: const EdgeInsets.all(8.0),
|
|
|
|
clipBehavior: Clip.antiAliasWithSaveLayer,
|
|
|
|
semanticContainer: true,
|
|
|
|
child: InkWell(
|
|
|
|
onTap: () {
|
|
|
|
// open room
|
|
|
|
Routemaster.of(context)
|
|
|
|
.push("/r/${room.serverTag}/${room.id}");
|
|
|
|
},
|
|
|
|
onLongPress: () {
|
|
|
|
// open bottom sheet
|
|
|
|
// NOTE: feature yet to be confirmed
|
|
|
|
},
|
|
|
|
child: Container(
|
|
|
|
padding: const EdgeInsets.fromLTRB(10, 5, 5, 10),
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(room.name),
|
|
|
|
visualDensity: const VisualDensity(vertical: 3),
|
|
|
|
subtitle: Text(room.description),
|
|
|
|
leading: AspectRatio(
|
|
|
|
aspectRatio: 1 / 1,
|
|
|
|
child: SvgPicture.asset("${room.icon?.img}"),
|
|
|
|
),
|
|
|
|
hoverColor: Colors.transparent,
|
|
|
|
))));
|
2023-03-18 20:27:20 +01:00
|
|
|
},
|
2023-03-17 21:08:29 +01:00
|
|
|
),
|
|
|
|
floatingActionButton: FloatingActionButton.extended(
|
2023-03-20 21:19:25 +01:00
|
|
|
label: const Text('Add Room'),
|
2023-03-17 21:08:29 +01:00
|
|
|
icon: const Icon(Icons.add),
|
2023-03-18 20:27:20 +01:00
|
|
|
onPressed: () {
|
|
|
|
// create new room
|
2023-03-20 21:19:25 +01:00
|
|
|
Routemaster.of(context).push("/add-room");
|
2023-03-17 21:08:29 +01:00
|
|
|
},
|
2023-03-20 21:19:25 +01:00
|
|
|
tooltip: 'Add new Room',
|
2023-03-17 21:08:29 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|