diff --git a/lib/backend/resolve_url.dart b/lib/backend/resolve_url.dart index 53d2f04..700ffe4 100644 --- a/lib/backend/resolve_url.dart +++ b/lib/backend/resolve_url.dart @@ -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; diff --git a/lib/backend/room.dart b/lib/backend/room.dart index dd478f3..f084c3a 100644 --- a/lib/backend/room.dart +++ b/lib/backend/room.dart @@ -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 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 toDisk() async { final db = Localstore.instance; await db.collection('rooms').doc('$id@$serverTag').set(toMap()); diff --git a/lib/screens/home.dart b/lib/screens/home.dart index c751a0b..38ed29d 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -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 { // 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> list = resp.body['data']; - for (Map 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 list = resp.body['data'].map((json){ + return Room.fromJSON(json); + }).toList(); + for (Room r in list) { + await r.toDisk(); } - } catch (_) {} + } + } catch (_) {} })(); } @@ -103,19 +97,19 @@ class _HomePageState extends State { }, 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 { 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(