2023-04-21 13:54:31 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-12-07 02:33:51 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-04-21 13:54:31 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:result_monad/result_monad.dart';
|
|
|
|
|
|
|
|
import '../controls/responsive_max_width.dart';
|
|
|
|
import '../controls/status_and_refresh_button.dart';
|
|
|
|
import '../globals.dart';
|
2024-12-07 02:33:51 +00:00
|
|
|
import '../models/auth/profile.dart';
|
2023-04-21 13:54:31 +00:00
|
|
|
import '../models/connection.dart';
|
|
|
|
import '../models/exec_error.dart';
|
2024-08-28 15:23:44 +00:00
|
|
|
import '../models/timeline_grouping_list_data.dart';
|
2024-12-10 11:54:45 +00:00
|
|
|
import '../riverpod_controllers/account_services.dart';
|
2024-12-07 02:33:51 +00:00
|
|
|
import '../riverpod_controllers/circles_repo_services.dart';
|
|
|
|
import '../riverpod_controllers/connection_manager_services.dart';
|
2024-12-13 04:57:57 +00:00
|
|
|
import '../riverpod_controllers/networking/network_status_services.dart';
|
2023-04-21 13:54:31 +00:00
|
|
|
import '../routes.dart';
|
|
|
|
import '../utils/snackbar_builder.dart';
|
|
|
|
|
2024-12-07 02:33:51 +00:00
|
|
|
class CircleAddUsersScreen extends ConsumerStatefulWidget {
|
2023-11-15 21:05:45 +00:00
|
|
|
final String circleId;
|
2023-04-21 13:54:31 +00:00
|
|
|
|
2023-11-15 21:05:45 +00:00
|
|
|
const CircleAddUsersScreen({super.key, required this.circleId});
|
2023-04-21 13:54:31 +00:00
|
|
|
|
|
|
|
@override
|
2024-12-07 02:33:51 +00:00
|
|
|
ConsumerState<CircleAddUsersScreen> createState() =>
|
|
|
|
_CircleAddUsersScreenState();
|
2023-04-21 13:54:31 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 02:33:51 +00:00
|
|
|
class _CircleAddUsersScreenState extends ConsumerState<CircleAddUsersScreen> {
|
2023-11-15 21:05:45 +00:00
|
|
|
static final _logger = Logger('$CircleAddUsersScreen');
|
2023-04-21 13:54:31 +00:00
|
|
|
var filterText = '';
|
2024-08-28 15:23:44 +00:00
|
|
|
late TimelineGroupingListData circleData;
|
2023-04-21 13:54:31 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-12-07 02:33:51 +00:00
|
|
|
|
2024-12-10 11:54:45 +00:00
|
|
|
final profile = ref.read(activeProfileProvider);
|
2024-12-07 02:33:51 +00:00
|
|
|
circleData = ref
|
|
|
|
.read(timelineGroupingListProvider(profile, GroupingType.circle))
|
2024-08-28 15:57:10 +00:00
|
|
|
.first;
|
2023-04-21 13:54:31 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 21:05:45 +00:00
|
|
|
Future<void> addUserToCircle(
|
2024-12-07 02:33:51 +00:00
|
|
|
Profile profile,
|
2023-04-21 13:54:31 +00:00
|
|
|
Connection connection,
|
|
|
|
) async {
|
2023-11-15 21:05:45 +00:00
|
|
|
final messageBase = '${connection.name} from ${circleData.name}';
|
2023-04-21 13:54:31 +00:00
|
|
|
final confirm = await showYesNoDialog(context, 'Add $messageBase?');
|
|
|
|
if (context.mounted && confirm == true) {
|
2024-12-07 02:33:51 +00:00
|
|
|
final message = await ref
|
|
|
|
.read(circlesProvider(profile).notifier)
|
|
|
|
.addConnectionToCircle(circleData, connection)
|
2023-04-21 13:54:31 +00:00
|
|
|
.withResult((p0) => setState(() {}))
|
|
|
|
.fold(
|
|
|
|
onSuccess: (_) => 'Added $messageBase',
|
|
|
|
onError: (error) => 'Error adding $messageBase: $error',
|
|
|
|
);
|
2024-07-26 14:15:24 +00:00
|
|
|
if (mounted) {
|
|
|
|
buildSnackbar(context, message);
|
|
|
|
}
|
2023-04-21 13:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
_logger.finer('Build');
|
2024-12-10 11:54:45 +00:00
|
|
|
final profile = ref.watch(activeProfileProvider);
|
2024-12-13 04:57:57 +00:00
|
|
|
final loading = ref.watch(connectionsLoadingProvider(profile));
|
2024-12-07 02:33:51 +00:00
|
|
|
final circleMembers = ref
|
|
|
|
.read(circlesProvider(profile).notifier)
|
2023-11-15 21:05:45 +00:00
|
|
|
.getCircleMembers(circleData)
|
2023-04-21 13:54:31 +00:00
|
|
|
.withError((e) => logError(e, _logger))
|
|
|
|
.getValueOrElse(() => [])
|
|
|
|
.toSet();
|
2024-12-07 02:33:51 +00:00
|
|
|
|
|
|
|
final allContacts = switch (ref.watch(myContactsProvider(profile))) {
|
|
|
|
AsyncData(:final value) => value,
|
|
|
|
_ => []
|
|
|
|
};
|
2023-04-21 13:54:31 +00:00
|
|
|
final filterTextLC = filterText.toLowerCase();
|
|
|
|
final contacts = allContacts
|
2023-11-15 21:05:45 +00:00
|
|
|
.where((c) => !circleMembers.contains(c))
|
2023-04-21 13:54:31 +00:00
|
|
|
.where((c) =>
|
|
|
|
filterText.isEmpty ||
|
|
|
|
c.name.toLowerCase().contains(filterTextLC) ||
|
|
|
|
c.handle.toLowerCase().contains(filterTextLC))
|
|
|
|
.toList();
|
|
|
|
contacts.sort((c1, c2) => c1.name.compareTo(c2.name));
|
|
|
|
_logger.finer(
|
|
|
|
() =>
|
2023-11-15 21:05:45 +00:00
|
|
|
'# in circle: ${circleMembers.length} # Contacts: ${allContacts.length}, #filtered: ${contacts.length}',
|
2023-04-21 13:54:31 +00:00
|
|
|
);
|
|
|
|
late Widget body;
|
|
|
|
if (contacts.isEmpty) {
|
|
|
|
body = const SingleChildScrollView(
|
|
|
|
physics: AlwaysScrollableScrollPhysics(),
|
|
|
|
child: Center(
|
|
|
|
child: Text('No contacts'),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
body = ListView.separated(
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final contact = contacts[index];
|
|
|
|
return ListTile(
|
|
|
|
onTap: () {
|
|
|
|
context.pushNamed(ScreenPaths.userProfile,
|
2023-10-31 01:44:16 +00:00
|
|
|
pathParameters: {'id': contact.id});
|
2023-04-21 13:54:31 +00:00
|
|
|
},
|
|
|
|
title: Text(
|
|
|
|
'${contact.name} (${contact.handle})',
|
|
|
|
softWrap: true,
|
|
|
|
),
|
|
|
|
subtitle: Text(
|
|
|
|
'Last Status: ${contact.lastStatus?.toIso8601String() ?? "Unknown"}',
|
|
|
|
softWrap: true,
|
|
|
|
),
|
|
|
|
trailing: IconButton(
|
2023-11-15 21:05:45 +00:00
|
|
|
onPressed: () async =>
|
2024-12-07 02:33:51 +00:00
|
|
|
await addUserToCircle(profile, contact),
|
2023-04-21 13:54:31 +00:00
|
|
|
icon: const Icon(Icons.add)),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
separatorBuilder: (context, index) => const Divider(),
|
|
|
|
itemCount: contacts.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Add Users'),
|
|
|
|
),
|
|
|
|
body: SafeArea(
|
|
|
|
child: RefreshIndicator(
|
|
|
|
onRefresh: () async {
|
2024-12-13 04:57:57 +00:00
|
|
|
if (loading) {
|
2023-04-21 13:54:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-12-07 02:33:51 +00:00
|
|
|
ref
|
|
|
|
.read(circlesProvider(profile).notifier)
|
|
|
|
.refreshCircleMemberships(circleData);
|
2023-04-21 13:54:31 +00:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
child: ResponsiveMaxWidth(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Text(
|
2023-11-15 21:05:45 +00:00
|
|
|
'Circle: ${circleData.name}',
|
2023-04-21 13:54:31 +00:00
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
softWrap: true,
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: TextField(
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
filterText = value.toLowerCase();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'Filter By Name',
|
|
|
|
alignLabelWithHint: true,
|
|
|
|
border: OutlineInputBorder(
|
|
|
|
borderSide: BorderSide(
|
|
|
|
color: Theme.of(context).highlightColor,
|
|
|
|
),
|
|
|
|
borderRadius: BorderRadius.circular(5.0),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
2024-12-13 04:57:57 +00:00
|
|
|
child: StatusAndRefreshButton3(
|
|
|
|
executing: loading,
|
2024-12-07 02:33:51 +00:00
|
|
|
refreshFunction: () async => ref
|
|
|
|
.read(circlesProvider(profile).notifier)
|
|
|
|
.refreshCircleMemberships(circleData),
|
2023-04-21 13:54:31 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2024-12-13 04:57:57 +00:00
|
|
|
if (loading) const LinearProgressIndicator(),
|
2023-04-21 13:54:31 +00:00
|
|
|
Expanded(child: body),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|