fluffychat/lib/views/home_view_parts/contact_list.dart

155 lines
5.0 KiB
Dart
Raw Normal View History

2021-02-05 16:30:52 +01:00
import 'dart:async';
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
import 'package:fluffychat/components/avatar.dart';
import 'package:fluffychat/components/default_app_bar_search_field.dart';
import 'package:fluffychat/components/list_items/contact_list_tile.dart';
import 'package:fluffychat/components/matrix.dart';
2021-02-06 20:35:52 +01:00
import 'package:fluffychat/utils/fluffy_share.dart';
import 'package:flutter/material.dart';
2021-02-07 09:12:34 +01:00
import '../../app_config.dart';
import '../../utils/client_presence_extension.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
class ContactList extends StatefulWidget {
2021-02-05 16:30:52 +01:00
final Stream onAppBarButtonTap;
const ContactList({Key key, this.onAppBarButtonTap}) : super(key: key);
@override
_ContactListState createState() => _ContactListState();
}
class _ContactListState extends State<ContactList> {
String _searchQuery = '';
2021-02-05 16:30:52 +01:00
final ScrollController _scrollController = ScrollController();
StreamSubscription _onAppBarButtonTapSub;
2021-02-07 09:55:24 +01:00
StreamSubscription _onSync;
2021-02-05 16:30:52 +01:00
final GlobalKey<DefaultAppBarSearchFieldState> _searchField = GlobalKey();
@override
void initState() {
_onAppBarButtonTapSub =
widget.onAppBarButtonTap.where((i) => i == 0).listen((_) async {
await _scrollController.animateTo(
_scrollController.position.minScrollExtent,
duration: Duration(milliseconds: 200),
curve: Curves.ease,
);
WidgetsBinding.instance.addPostFrameCallback(
(_) => _searchField.currentState.requestFocus(),
);
});
super.initState();
}
@override
void dispose() {
2021-02-07 09:55:24 +01:00
_onSync?.cancel();
2021-02-05 16:30:52 +01:00
_onAppBarButtonTapSub?.cancel();
super.dispose();
}
2021-02-07 09:55:24 +01:00
DateTime _lastSetState = DateTime.now();
Timer _coolDown;
void _updateView() {
_lastSetState = DateTime.now();
setState(() => null);
}
@override
Widget build(BuildContext context) {
2021-02-07 09:55:24 +01:00
_onSync ??= Matrix.of(context).client.onSync.stream.listen((_) {
if (DateTime.now().millisecondsSinceEpoch -
_lastSetState.millisecondsSinceEpoch <
1000) {
_coolDown?.cancel();
_coolDown = Timer(Duration(seconds: 1), _updateView);
} else {
_updateView();
}
});
2021-02-05 16:30:52 +01:00
return ListView(
controller: _scrollController,
children: [
Padding(
padding: EdgeInsets.all(12),
child: DefaultAppBarSearchField(
key: _searchField,
hintText: L10n.of(context).search,
prefixIcon: Icon(Icons.search_outlined),
onChanged: (t) => setState(() => _searchQuery = t),
padding: EdgeInsets.zero,
),
),
2021-02-05 16:30:52 +01:00
ListTile(
leading: CircleAvatar(
backgroundColor: Theme.of(context).primaryColor,
foregroundColor: Colors.white,
child: Icon(Icons.add_outlined),
radius: Avatar.defaultSize / 2,
),
title: Text(L10n.of(context).addNewContact),
onTap: () =>
AdaptivePageLayout.of(context).pushNamed('/newprivatechat'),
),
2021-02-05 16:30:52 +01:00
Divider(height: 1),
2021-02-07 09:55:24 +01:00
Builder(builder: (context) {
final contactList = Matrix.of(context)
.client
.contactList
.where((p) =>
p.senderId.toLowerCase().contains(_searchQuery.toLowerCase()))
.toList();
if (contactList.isEmpty) {
return Column(
children: [
SizedBox(height: 32),
Icon(
Icons.people_outlined,
size: 80,
color: Colors.grey,
),
RaisedButton(
elevation: 7,
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.share_outlined, color: Colors.white),
SizedBox(width: 16),
Text(
L10n.of(context).inviteContact,
style: TextStyle(
color: Colors.white,
fontSize: 16,
),
2021-02-06 20:35:52 +01:00
),
2021-02-07 09:55:24 +01:00
],
),
onPressed: () => FluffyShare.share(
L10n.of(context).inviteText(
Matrix.of(context).client.userID,
'https://matrix.to/#/${Matrix.of(context).client.userID}'),
context),
),
],
);
}
return ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: EdgeInsets.only(bottom: 24),
itemCount: contactList.length,
itemBuilder: (context, i) =>
ContactListTile(contact: contactList[i]),
);
}),
2021-02-05 16:30:52 +01:00
],
);
}
}