OpenContacts/lib/widgets/friends_list.dart

132 lines
4.2 KiB
Dart
Raw Normal View History

2023-04-30 09:43:59 -04:00
import 'dart:async';
2023-05-01 13:13:40 -04:00
import 'package:contacts_plus_plus/api_client.dart';
import 'package:contacts_plus_plus/apis/friend_api.dart';
import 'package:contacts_plus_plus/models/friend.dart';
import 'package:contacts_plus_plus/widgets/expanding_input_fab.dart';
import 'package:contacts_plus_plus/widgets/friend_list_tile.dart';
import 'package:contacts_plus_plus/widgets/settings_page.dart';
2023-04-29 13:18:46 -04:00
import 'package:flutter/material.dart';
class FriendsList extends StatefulWidget {
const FriendsList({super.key});
2023-04-29 13:18:46 -04:00
@override
State<FriendsList> createState() => _FriendsListState();
2023-04-29 13:18:46 -04:00
}
class _FriendsListState extends State<FriendsList> {
2023-05-03 12:43:06 -04:00
Future<List<Friend>>? _friendsFuture;
2023-04-30 07:39:09 -04:00
ClientHolder? _clientHolder;
2023-04-30 09:43:59 -04:00
Timer? _debouncer;
2023-05-03 12:43:06 -04:00
String _searchFilter = "";
2023-04-30 09:43:59 -04:00
@override
void dispose() {
_debouncer?.cancel();
super.dispose();
}
2023-04-29 15:26:12 -04:00
@override
2023-04-30 07:39:09 -04:00
void didChangeDependencies() {
super.didChangeDependencies();
final clientHolder = ClientHolder.of(context);
if (_clientHolder != clientHolder) {
_clientHolder = clientHolder;
_refreshFriendsList();
}
2023-04-29 15:26:12 -04:00
}
void _refreshFriendsList() {
2023-05-03 12:43:06 -04:00
_friendsFuture = FriendApi.getFriendsList(_clientHolder!.client).then((Iterable<Friend> value) =>
2023-04-29 15:26:12 -04:00
value.toList()
..sort((a, b) {
if (a.userStatus.onlineStatus == b.userStatus.onlineStatus) {
return a.userStatus.lastStatusChange.compareTo(b.userStatus.lastStatusChange);
} else {
2023-05-03 12:43:06 -04:00
return a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus);
2023-04-29 15:26:12 -04:00
}
},
),
2023-05-03 12:43:06 -04:00
);
2023-04-29 15:26:12 -04:00
}
2023-04-29 13:18:46 -04:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-04-30 09:43:59 -04:00
title: const Text("Contacts++"),
actions: [
IconButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => const SettingsPage()));
},
icon: const Icon(Icons.settings),
)
],
2023-04-29 13:18:46 -04:00
),
2023-04-30 09:43:59 -04:00
body: Stack(
children: [
RefreshIndicator(
onRefresh: () async {
_refreshFriendsList();
2023-05-03 12:43:06 -04:00
await _friendsFuture;
2023-04-30 09:43:59 -04:00
},
child: FutureBuilder(
2023-05-03 12:43:06 -04:00
future: _friendsFuture,
2023-04-30 09:43:59 -04:00
builder: (context, snapshot) {
if (snapshot.hasData) {
2023-05-03 12:43:06 -04:00
var data = (snapshot.data as List<Friend>);
if (_searchFilter.isNotEmpty) {
data = data.where((element) => element.username.contains(_searchFilter)).toList();
data.sort((a, b) => a.username.length.compareTo(b.username.length));
}
2023-04-30 09:43:59 -04:00
return ListView.builder(
itemCount: data.length,
2023-05-03 12:43:06 -04:00
itemBuilder: (context, index) => FriendListTile(friend: data[index]),
2023-04-30 09:43:59 -04:00
);
} else if (snapshot.hasError) {
2023-04-30 17:14:29 -04:00
FlutterError.reportError(FlutterErrorDetails(exception: snapshot.error!, stack: snapshot.stackTrace));
2023-04-30 09:43:59 -04:00
return Center(
child: Padding(
padding: const EdgeInsets.all(64),
child: Text(
"Something went wrong: ${snapshot.error}",
softWrap: true,
style: Theme
.of(context)
.textTheme
.labelMedium,
2023-04-30 07:39:09 -04:00
),
),
2023-04-29 15:26:12 -04:00
);
2023-04-30 09:43:59 -04:00
} else {
2023-04-30 17:14:29 -04:00
return const SizedBox.shrink();
2023-04-30 09:43:59 -04:00
}
}
),
),
Align(
alignment: Alignment.bottomCenter,
child: ExpandingInputFab(
onInputChanged: (String text) {
2023-04-30 17:14:29 -04:00
setState(() {
2023-05-03 12:43:06 -04:00
_searchFilter = text;
2023-04-30 09:43:59 -04:00
});
},
onExpansionChanged: (expanded) {
if (_debouncer?.isActive ?? false) _debouncer?.cancel();
if (!expanded) {
setState(() {
2023-05-03 12:43:06 -04:00
_searchFilter = "";
2023-04-30 09:43:59 -04:00
});
}
},
),
),
],
2023-04-29 13:18:46 -04:00
),
);
}
2023-04-30 09:43:59 -04:00
}