Added ability to delete or leave room.

NOTE: Only the room owner can delete a room,
but they are not able to leave the room.

BUG: Deleting or leaving the room deletes the room from disk,
but for an unknown reason, the homescreen does not update
and still shows the room.
Opening and closing the room seems to cause the homescreen to
update, removing the room.
This commit is contained in:
Jakob Meier 2023-03-23 08:58:34 +01:00
parent c54e4f519f
commit c466a19fc9
No known key found for this signature in database
GPG key ID: 66BDC7E6A01A6152
2 changed files with 124 additions and 14 deletions

View file

@ -263,16 +263,25 @@ class Room {
await db.collection('rooms').doc('$id@$serverTag').set(toMap());
}
static Future<Room> fromDisk({required String id, required String serverTag}) async {
Future<void> 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<Room> 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;

View file

@ -149,8 +149,7 @@ class _AboutRoomPageState extends State<AboutRoomPage> {
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<AboutRoomPage> {
],
)),
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'),
)
],
));
},
))
]
: [],
]));
}
}