Fixed bug where room list was not processed properly

This was caused by a wrong type.

To fix this I updated the type and implemented Room.fromJSON
which should make fetching rooms easier in the future
This commit is contained in:
Jakob Meier 2023-03-20 20:45:49 +01:00
parent cfd54e3bb5
commit f2cbfaf924
No known key found for this signature in database
GPG key ID: 66BDC7E6A01A6152
3 changed files with 82 additions and 83 deletions

View file

@ -2,8 +2,6 @@ import 'package:localstore/localstore.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'package:outbag_app/main.dart';
const String wellKnownPath = "/.well-known/outbag/server"; const String wellKnownPath = "/.well-known/outbag/server";
const int defaultPort = 7223; const int defaultPort = 7223;

View file

@ -232,9 +232,9 @@ class Room {
id: map['id'], id: map['id'],
serverTag: map['server'], serverTag: map['server'],
name: map['name'], name: map['name'],
description: map['description']??'', description: map['description'] ?? '',
icon: RoomIcon(type: map['icon']??'Other'), icon: RoomIcon(type: map['icon'] ?? 'Other'),
visibility: RoomVisibility(map['visibility']??0)); visibility: RoomVisibility(map['visibility'] ?? 0));
} }
Map<String, dynamic> toMap() { Map<String, dynamic> toMap() {
@ -248,6 +248,16 @@ class Room {
}; };
} }
factory Room.fromJSON(dynamic json) {
return Room(
id: json['name'],
serverTag: json['server'],
name: json['title'],
description: json['description'],
icon: RoomIcon(type: json['icon']),
visibility: RoomVisibility(json['visibility']));
}
Future<void> toDisk() async { Future<void> toDisk() async {
final db = Localstore.instance; final db = Localstore.instance;
await db.collection('rooms').doc('$id@$serverTag').set(toMap()); await db.collection('rooms').doc('$id@$serverTag').set(toMap());

View file

@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:outbag_app/backend/request.dart'; import 'package:outbag_app/backend/request.dart';
@ -20,57 +22,49 @@ class _HomePageState extends State<HomePage> {
// wait for background room changes // wait for background room changes
Room.listen((_) async { Room.listen((_) async {
try { try {
final newRooms = await Room.listRooms(); final newRooms = await Room.listRooms();
setState(() { setState(() {
rooms = newRooms; rooms = newRooms;
}); });
} catch (_) {} } catch (_) {}
}); });
// load cached rooms // load cached rooms
(() async { (() async {
try { try {
final newRooms = await Room.listRooms(); final newRooms = await Room.listRooms();
setState(() { setState(() {
rooms = newRooms; rooms = newRooms;
}); });
} catch (_) {} } catch (_) {}
})(); })();
// fetch room list // fetch room list
(() async { (() async {
User user; User user;
try { try {
user = await User.fromDisk(); user = await User.fromDisk();
} catch (_) { } catch (_) {
// probably not logged in // probably not logged in
return; return;
} }
try { try {
final resp = await postWithCreadentials( final resp = await postWithCreadentials(
path: 'listLocalRooms', path: 'listRooms',
credentials: user, credentials: user,
target: user.server, target: user.server,
body: {}); body: {});
if (resp.res == Result.ok) { if (resp.res == Result.ok) {
final List<Map<String, dynamic>> list = resp.body['data']; final List<Room> list = resp.body['data'].map<Room>((json){
for (Map<String, dynamic> rawRoom in list) { return Room.fromJSON(json);
try { }).toList();
Room( for (Room r in list) {
id: rawRoom['room'], await r.toDisk();
serverTag: rawRoom['server'],
name: rawRoom['title'],
icon: RoomIcon(type: rawRoom['icon']),
visibility: RoomVisibility(rawRoom['visibility']))
.toDisk();
} catch (_) {
continue;
}
}
} }
} catch (_) {} }
} catch (_) {}
})(); })();
} }
@ -103,19 +97,19 @@ class _HomePageState extends State<HomePage> {
}, },
menuChildren: [ menuChildren: [
MenuItemButton( MenuItemButton(
leadingIcon: const Icon(Icons.settings), leadingIcon: const Icon(Icons.settings),
child: const Text('Settings'), child: const Text('Settings'),
onPressed: () { onPressed: () {
// show settings screen // show settings screen
Routemaster.of(context).push("/settings"); Routemaster.of(context).push("/settings");
}), }),
MenuItemButton( MenuItemButton(
leadingIcon: const Icon(Icons.info_rounded), leadingIcon: const Icon(Icons.info_rounded),
child: const Text('About'), child: const Text('About'),
onPressed: () { onPressed: () {
// show about screen // show about screen
Routemaster.of(context).push("/about"); Routemaster.of(context).push("/about");
}), }),
], ],
) )
], ],
@ -125,34 +119,31 @@ class _HomePageState extends State<HomePage> {
itemBuilder: (ctx, i) { itemBuilder: (ctx, i) {
final room = rooms[i]; final room = rooms[i];
return Card( return Card(
margin: const EdgeInsets.all(8.0), margin: const EdgeInsets.all(8.0),
clipBehavior: Clip.antiAliasWithSaveLayer, clipBehavior: Clip.antiAliasWithSaveLayer,
semanticContainer: true, semanticContainer: true,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
// open room // open room
Routemaster.of(context).push("/r/${room.serverTag}/${room.id}"); Routemaster.of(context)
}, .push("/r/${room.serverTag}/${room.id}");
onLongPress: () { },
// open bottom sheet onLongPress: () {
// NOTE: feature yet to be confirmed // open bottom sheet
}, // NOTE: feature yet to be confirmed
child: Container( },
padding: const EdgeInsets.fromLTRB(10, 5, 5, 10), child: Container(
child: ListTile( padding: const EdgeInsets.fromLTRB(10, 5, 5, 10),
title: Text(room.name), child: ListTile(
visualDensity: const VisualDensity(vertical: 3), title: Text(room.name),
subtitle: Text(room.description), visualDensity: const VisualDensity(vertical: 3),
leading: AspectRatio( subtitle: Text(room.description),
aspectRatio: 1 / 1, leading: AspectRatio(
child: SvgPicture.asset("${room.icon?.img}"), aspectRatio: 1 / 1,
), child: SvgPicture.asset("${room.icon?.img}"),
hoverColor: Colors.transparent, ),
hoverColor: Colors.transparent,
) ))));
)
)
);
}, },
), ),
floatingActionButton: FloatingActionButton.extended( floatingActionButton: FloatingActionButton.extended(