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

@ -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';
@ -50,24 +52,16 @@ class _HomePageState extends State<HomePage> {
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 (_) {}
@ -131,7 +125,8 @@ class _HomePageState extends State<HomePage> {
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: () { onLongPress: () {
// open bottom sheet // open bottom sheet
@ -148,11 +143,7 @@ class _HomePageState extends State<HomePage> {
child: SvgPicture.asset("${room.icon?.img}"), child: SvgPicture.asset("${room.icon?.img}"),
), ),
hoverColor: Colors.transparent, hoverColor: Colors.transparent,
))));
)
)
)
);
}, },
), ),
floatingActionButton: FloatingActionButton.extended( floatingActionButton: FloatingActionButton.extended(