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 'dart:convert';
import 'package:outbag_app/main.dart';
const String wellKnownPath = "/.well-known/outbag/server";
const int defaultPort = 7223;

View file

@ -232,9 +232,9 @@ class Room {
id: map['id'],
serverTag: map['server'],
name: map['name'],
description: map['description']??'',
icon: RoomIcon(type: map['icon']??'Other'),
visibility: RoomVisibility(map['visibility']??0));
description: map['description'] ?? '',
icon: RoomIcon(type: map['icon'] ?? 'Other'),
visibility: RoomVisibility(map['visibility'] ?? 0));
}
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 {
final db = Localstore.instance;
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_svg/flutter_svg.dart';
import 'package:outbag_app/backend/request.dart';
@ -20,57 +22,49 @@ class _HomePageState extends State<HomePage> {
// wait for background room changes
Room.listen((_) async {
try {
final newRooms = await Room.listRooms();
setState(() {
rooms = newRooms;
});
} catch (_) {}
try {
final newRooms = await Room.listRooms();
setState(() {
rooms = newRooms;
});
} catch (_) {}
});
// load cached rooms
(() async {
try {
final newRooms = await Room.listRooms();
setState(() {
rooms = newRooms;
});
} catch (_) {}
try {
final newRooms = await Room.listRooms();
setState(() {
rooms = newRooms;
});
} catch (_) {}
})();
// fetch room list
(() async {
User user;
try {
user = await User.fromDisk();
} catch (_) {
// probably not logged in
return;
}
User user;
try {
user = await User.fromDisk();
} catch (_) {
// probably not logged in
return;
}
try {
final resp = await postWithCreadentials(
path: 'listLocalRooms',
try {
final resp = await postWithCreadentials(
path: 'listRooms',
credentials: user,
target: user.server,
body: {});
if (resp.res == Result.ok) {
final List<Map<String, dynamic>> list = resp.body['data'];
for (Map<String, dynamic> rawRoom in list) {
try {
Room(
id: rawRoom['room'],
serverTag: rawRoom['server'],
name: rawRoom['title'],
icon: RoomIcon(type: rawRoom['icon']),
visibility: RoomVisibility(rawRoom['visibility']))
.toDisk();
} catch (_) {
continue;
}
}
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();
}
} catch (_) {}
}
} catch (_) {}
})();
}
@ -103,19 +97,19 @@ class _HomePageState extends State<HomePage> {
},
menuChildren: [
MenuItemButton(
leadingIcon: const Icon(Icons.settings),
child: const Text('Settings'),
onPressed: () {
// show settings screen
Routemaster.of(context).push("/settings");
}),
leadingIcon: const Icon(Icons.settings),
child: const Text('Settings'),
onPressed: () {
// show settings screen
Routemaster.of(context).push("/settings");
}),
MenuItemButton(
leadingIcon: const Icon(Icons.info_rounded),
child: const Text('About'),
onPressed: () {
// show about screen
Routemaster.of(context).push("/about");
}),
leadingIcon: const Icon(Icons.info_rounded),
child: const Text('About'),
onPressed: () {
// show about screen
Routemaster.of(context).push("/about");
}),
],
)
],
@ -125,34 +119,31 @@ class _HomePageState extends State<HomePage> {
itemBuilder: (ctx, i) {
final room = rooms[i];
return Card(
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,
)
)
)
);
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,
))));
},
),
floatingActionButton: FloatingActionButton.extended(