diff --git a/lib/backend/room.dart b/lib/backend/room.dart index 0039a67..799edcf 100644 --- a/lib/backend/room.dart +++ b/lib/backend/room.dart @@ -263,16 +263,25 @@ class Room { await db.collection('rooms').doc('$id@$serverTag').set(toMap()); } - static Future fromDisk({required String id, required String serverTag}) async { + Future removeDisk() async { + final db = Localstore.instance; + // NOTE: Because categories, products and entries + // are supposed to be stored in the room object + // (they are just not read by Room.fromDisk) + // deleting the document from the collection + // should also get rid of affiliated collections + await db.collection('rooms').doc('$id@$serverTag').delete(); + } + + static Future fromDisk( + {required String id, required String serverTag}) async { final db = Localstore.instance; final raw = await db.collection('rooms').doc('$id@$serverTag').get(); return Room.fromMap(raw!); } } -class ShoppingListItem { - -} +class ShoppingListItem {} class RoomMember { final String id; diff --git a/lib/screens/room/about.dart b/lib/screens/room/about.dart index fb41519..8e2ffd7 100644 --- a/lib/screens/room/about.dart +++ b/lib/screens/room/about.dart @@ -149,8 +149,7 @@ class _AboutRoomPageState extends State { scaffMgr.hideCurrentSnackBar(); scaffMgr.showSnackBar(snackBar); } - } catch (e) { - print(e); + } catch (_) { // network error final snackBar = SnackBar( behavior: SnackBarBehavior.floating, @@ -235,14 +234,116 @@ class _AboutRoomPageState extends State { ], )), - Padding( - padding: const EdgeInsets.all(8), - child: FilledButton.tonal( - child: const Text('Leave Room'), - onPressed: () { - // TODO: show confirm dialog - }, - )) + ...(widget.info != null) + ? [ + Padding( + padding: const EdgeInsets.all(8), + child: FilledButton.tonal( + child: Text(((widget.info?.isOwner)!) + ? 'Delete Room' + : 'Leave Room'), + onPressed: () { + // show confirm dialog + showDialog( + context: context, + builder: (ctx) => AlertDialog( + title: Text(((widget.info?.isOwner)!) + ? 'Delete Room' + : 'Leave Room'), + content: Text( + 'Do you really want to ${((widget.info?.isOwner)!) ? "delete" : "leave"} the room?'), + actions: [ + TextButton( + onPressed: () { + // close popup + Navigator.of(ctx).pop(); + }, + child: const Text('Cancel'), + ), + FilledButton( + onPressed: () async { + // send request + final scaffMgr = + ScaffoldMessenger.of(ctx); + final nav = Navigator.of(ctx); + final rmaster = Routemaster.of(ctx); + + User user; + try { + user = await User.fromDisk(); + } catch (_) { + // probably not logged in + nav.pop(); + return; + } + + try { + final resp = await postWithCreadentials( + path: ((widget.info?.isOwner)!) + ? 'deleteRoom' + : 'leaveRoom', + target: user.server, + body: { + 'room': widget.room?.id, + 'server': + (widget.room?.serverTag)!, + }, + credentials: user); + if (resp.res == Result.ok) { + // try delete room from disk + try { + await widget.room?.removeDisk(); + } catch (_) {} + + // go back home + rmaster.replace('/'); + } else { + // server error + final snackBar = SnackBar( + behavior: SnackBarBehavior.floating, + content: + Text(errorAsString(resp.body)), + action: SnackBarAction( + label: 'Dismiss', + onPressed: () { + scaffMgr.hideCurrentSnackBar(); + }, + ), + ); + + scaffMgr.hideCurrentSnackBar(); + scaffMgr.showSnackBar(snackBar); + } + } catch (_) { + // network error + final snackBar = SnackBar( + behavior: SnackBarBehavior.floating, + content: const Text('Network error'), + action: SnackBarAction( + label: 'Dismiss', + onPressed: () { + scaffMgr.hideCurrentSnackBar(); + }, + ), + ); + + scaffMgr.hideCurrentSnackBar(); + scaffMgr.showSnackBar(snackBar); + } + + // close popup + nav.pop(); + }, + child: Text(((widget.info?.isOwner)!) + ? 'Delete' + : 'Leave'), + ) + ], + )); + }, + )) + ] + : [], ])); } }