Extract join room with snackbars and fix refreshing
This commit is contained in:
parent
ba93340461
commit
d0ff5a0616
2 changed files with 93 additions and 98 deletions
87
lib/helpers/extensions/join_room_with_snackbars.dart
Normal file
87
lib/helpers/extensions/join_room_with_snackbars.dart
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:flutter/material.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:matrix/matrix.dart";
|
||||
import "package:nexus/controllers/key_controller.dart";
|
||||
import "package:nexus/controllers/spaces_controller.dart";
|
||||
|
||||
extension JoinRoomWithSnackbars on Client {
|
||||
Future<void> joinRoomWithSnackBars(
|
||||
BuildContext context,
|
||||
String roomAlias,
|
||||
WidgetRef ref,
|
||||
) async {
|
||||
final parsed = roomAlias.parseIdentifierIntoParts();
|
||||
final alias = parsed?.primaryIdentifier ?? roomAlias;
|
||||
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
|
||||
final snackbar = scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("Joining room $alias."),
|
||||
duration: Duration(days: 999),
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
final id = await joinRoom(alias, via: parsed?.via.toList());
|
||||
|
||||
snackbar.close();
|
||||
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text("Room $alias successfully joined."),
|
||||
action: SnackBarAction(
|
||||
label: "Open",
|
||||
onPressed: () async {
|
||||
final spaces = await ref.refresh(
|
||||
SpacesController.provider.future,
|
||||
);
|
||||
final space = spaces.firstWhereOrNull((space) => space.id == id);
|
||||
|
||||
await ref
|
||||
.watch(
|
||||
KeyController.provider(KeyController.spaceKey).notifier,
|
||||
)
|
||||
.set(
|
||||
space?.id ??
|
||||
spaces
|
||||
.firstWhere(
|
||||
(space) =>
|
||||
space.children.firstWhereOrNull(
|
||||
(child) => child.roomData.id == id,
|
||||
) !=
|
||||
null,
|
||||
)
|
||||
.id,
|
||||
);
|
||||
|
||||
if (space == null) {
|
||||
await ref
|
||||
.watch(
|
||||
KeyController.provider(KeyController.roomKey).notifier,
|
||||
)
|
||||
.set(id);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
snackbar.close();
|
||||
if (context.mounted) {
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.errorContainer,
|
||||
content: Text(
|
||||
error.toString(),
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onErrorContainer,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue