relatica/lib/screens/contacts_screen.dart

125 wiersze
4.2 KiB
Dart
Czysty Zwykły widok Historia

2022-12-15 03:27:30 +00:00
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:logging/logging.dart';
2022-12-15 03:27:30 +00:00
import 'package:provider/provider.dart';
import '../controls/app_bottom_nav_bar.dart';
import '../controls/current_profile_button.dart';
import '../controls/linear_status_indicator.dart';
2023-03-14 03:49:52 +00:00
import '../controls/standard_app_drawer.dart';
import '../globals.dart';
2022-12-15 03:27:30 +00:00
import '../models/connection.dart';
import '../routes.dart';
2023-03-21 20:27:04 +00:00
import '../services/auth_service.dart';
2022-12-15 03:27:30 +00:00
import '../services/connections_manager.dart';
import '../services/network_status_service.dart';
2023-03-14 03:49:52 +00:00
import '../utils/active_profile_selector.dart';
2022-12-15 03:27:30 +00:00
2022-12-28 03:37:52 +00:00
class ContactsScreen extends StatefulWidget {
@override
State<ContactsScreen> createState() => _ContactsScreenState();
}
class _ContactsScreenState extends State<ContactsScreen> {
static final _logger = Logger('$ContactsScreen');
2022-12-28 03:37:52 +00:00
var filterText = '';
2022-12-15 03:27:30 +00:00
@override
Widget build(BuildContext context) {
final nss = getIt<NetworkStatusService>();
2023-03-21 20:27:04 +00:00
final activeProfile = context.watch<AccountsService>();
2023-03-12 01:50:31 +00:00
final manager = context
.watch<ActiveProfileSelector<ConnectionsManager>>()
.activeEntry
.value;
final allContacts = manager.getMyContacts();
final filterTextLC = filterText.toLowerCase();
final contacts = allContacts
2022-12-28 03:37:52 +00:00
.where((c) =>
filterText.isEmpty ||
c.name.toLowerCase().contains(filterTextLC) ||
c.handle.toLowerCase().contains(filterTextLC))
2022-12-28 03:37:52 +00:00
.toList();
2022-12-15 03:27:30 +00:00
contacts.sort((c1, c2) => c1.name.compareTo(c2.name));
_logger.finer(
() => '# Contacts: ${allContacts.length}, #filtered: ${contacts.length}',
);
2022-12-15 03:27:30 +00:00
late Widget body;
if (contacts.isEmpty) {
2023-03-15 15:45:21 +00:00
body = SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: Center(
child: Text('No contacts'),
));
2022-12-15 03:27:30 +00:00
} else {
body = ListView.separated(
physics: const AlwaysScrollableScrollPhysics(),
itemBuilder: (context, index) {
final contact = contacts[index];
return ListTile(
onTap: () {
context.pushNamed(ScreenPaths.userProfile,
params: {'id': contact.id});
},
title: Text(contact.name),
trailing: Text(contact.status.label()),
);
},
separatorBuilder: (context, index) => const Divider(),
itemCount: contacts.length);
}
2023-03-15 15:45:21 +00:00
2023-03-21 20:27:04 +00:00
return Scaffold(
drawer: StandardAppDrawer(skipPopDismiss: true),
body: SafeArea(
child: RefreshIndicator(
onRefresh: () async {
if (nss.connectionUpdateStatus.value) {
2023-03-15 15:45:21 +00:00
return;
2023-03-21 20:27:04 +00:00
}
manager.updateAllContacts();
return;
},
child: Column(
children: [
Row(
children: [
SizedBox(
width: 50.0, child: buildCurrentProfileButton(context)!),
Expanded(
2023-03-21 20:42:19 +00:00
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).backgroundColor,
),
borderRadius: BorderRadius.circular(5.0),
2023-03-21 20:27:04 +00:00
),
),
),
),
)
],
),
StandardLinearProgressIndicator(nss.connectionUpdateStatus),
Expanded(child: body),
],
2023-03-15 15:45:21 +00:00
),
),
2023-03-21 20:27:04 +00:00
),
bottomNavigationBar: AppBottomNavBar(
currentButton: NavBarButtons.contacts,
2022-12-15 03:27:30 +00:00
),
);
}
}