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:
parent
c54e4f519f
commit
c466a19fc9
2 changed files with 124 additions and 14 deletions
|
@ -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;
|
||||
|
|
|
@ -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'),
|
||||
)
|
||||
],
|
||||
));
|
||||
},
|
||||
))
|
||||
]
|
||||
: [],
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue