fluffychat/lib/pages/add_story/invite_story_page.dart

138 lines
4.7 KiB
Dart
Raw Normal View History

2021-12-24 14:18:09 +01:00
//@dart=2.12
import 'package:flutter/material.dart';
2022-01-15 12:14:23 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-12-24 14:18:09 +01:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/client_stories_extension.dart';
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
class InviteStoryPage extends StatefulWidget {
final Room? storiesRoom;
const InviteStoryPage({
required this.storiesRoom,
Key? key,
}) : super(key: key);
@override
_InviteStoryPageState createState() => _InviteStoryPageState();
}
class _InviteStoryPageState extends State<InviteStoryPage> {
Set<String> _undecided = {};
final Set<String> _invite = {};
void _inviteAction() async {
2022-01-15 12:14:23 +01:00
final confirmed = await showOkCancelAlertDialog(
context: context,
message: L10n.of(context)!.storyPrivacyWarning,
okLabel: L10n.of(context)!.iUnderstand,
cancelLabel: L10n.of(context)!.cancel,
);
if (confirmed != OkCancelResult.ok) return;
2021-12-24 14:18:09 +01:00
final result = await showFutureLoadingDialog(
context: context,
future: () async {
final client = Matrix.of(context).client;
2021-12-27 08:48:06 +01:00
var room = await client.getStoriesRoom(context);
final inviteList = _invite.toList();
2021-12-24 14:18:09 +01:00
if (room == null) {
2021-12-27 08:48:06 +01:00
room = await client.createStoriesRoom(inviteList.take(10).toList());
if (inviteList.length > 10) {
inviteList.removeRange(0, 10);
} else {
inviteList.clear();
}
2021-12-27 08:48:47 +01:00
}
for (final userId in inviteList) {
room.invite(userId);
2021-12-24 14:18:09 +01:00
}
_undecided.removeAll(_invite);
2021-12-29 11:06:53 +01:00
_undecided.addAll(client.storiesBlockList);
2021-12-24 14:18:09 +01:00
await client.setStoriesBlockList(_undecided.toList());
},
);
if (result.error != null) return;
Navigator.of(context).pop<bool>(true);
}
Future<List<User>>? loadContacts;
@override
Widget build(BuildContext context) {
loadContacts ??= Matrix.of(context)
.client
.getUndecidedContactsForStories(widget.storiesRoom)
.then((contacts) {
return contacts;
});
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.close),
onPressed: () => Navigator.of(context).pop<bool>(false),
),
title: Text(L10n.of(context)!.whoCanSeeMyStories),
2022-01-15 13:18:25 +01:00
elevation: 0,
),
body: Column(
children: [
ListTile(
2021-12-28 19:30:19 +01:00
title: Text(L10n.of(context)!.whoCanSeeMyStoriesDesc),
leading: CircleAvatar(
backgroundColor: Theme.of(context).secondaryHeaderColor,
foregroundColor: Theme.of(context).colorScheme.secondary,
child: const Icon(Icons.lock),
),
),
2022-01-15 13:18:25 +01:00
const Divider(height: 1),
Expanded(
child: FutureBuilder<List<User>>(
future: loadContacts,
builder: (context, snapshot) {
final contacts = snapshot.data;
if (contacts == null) {
final error = snapshot.error;
if (error != null) {
return Center(
child: Text(error.toLocalizedString(context)));
}
return const Center(
child: CircularProgressIndicator.adaptive());
}
_undecided = contacts.map((u) => u.id).toSet();
return ListView.builder(
itemCount: contacts.length,
itemBuilder: (context, i) => SwitchListTile.adaptive(
value: _invite.contains(contacts[i].id),
onChanged: (b) => setState(() => b
? _invite.add(contacts[i].id)
: _invite.remove(contacts[i].id)),
secondary: Avatar(
mxContent: contacts[i].avatarUrl,
name: contacts[i].calcDisplayname(),
),
title: Text(contacts[i].calcDisplayname()),
),
);
}),
),
],
2021-12-24 14:18:09 +01:00
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _inviteAction,
label: Text(L10n.of(context)!.publish),
backgroundColor: Theme.of(context).colorScheme.surface,
foregroundColor: Theme.of(context).colorScheme.onSurface,
2021-12-31 09:12:47 +01:00
icon: const Icon(Icons.send_rounded),
2021-12-24 14:18:09 +01:00
),
);
}
}