fluffychat/lib/views/chat_list.dart

190 lines
5.9 KiB
Dart
Raw Normal View History

2020-01-02 15:10:21 +01:00
import 'dart:async';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/adaptive_page_layout.dart';
import 'package:fluffychat/components/dialogs/new_group_dialog.dart';
import 'package:fluffychat/components/dialogs/new_private_chat_dialog.dart';
import 'package:fluffychat/components/list_items/chat_list_item.dart';
import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/settings.dart';
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
class ChatListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.FIRST,
firstScaffold: ChatList(),
secondScaffold: Scaffold(
body: Center(
2020-01-03 11:57:00 +01:00
child: Icon(Icons.chat,
size: 100, color: Theme.of(context).primaryColor),
2020-01-01 19:10:13 +01:00
),
),
);
}
}
class ChatList extends StatefulWidget {
final String activeChat;
const ChatList({this.activeChat, Key key}) : super(key: key);
@override
_ChatListState createState() => _ChatListState();
}
class _ChatListState extends State<ChatList> {
2020-01-02 22:55:12 +01:00
bool searchMode = false;
2020-01-02 15:10:21 +01:00
StreamSubscription sub;
2020-01-02 22:55:12 +01:00
final TextEditingController searchController = TextEditingController();
2020-01-01 19:10:13 +01:00
2020-01-02 15:10:21 +01:00
Future<bool> waitForFirstSync(BuildContext context) async {
2020-01-01 19:10:13 +01:00
Client client = Matrix.of(context).client;
2020-01-02 22:31:39 +01:00
if (client.prevBatch?.isEmpty ?? true) {
2020-01-02 15:10:21 +01:00
await client.onFirstSync.stream.first;
2020-01-02 22:31:39 +01:00
}
2020-01-02 15:10:21 +01:00
sub ??= client.onSync.stream.listen((s) => setState(() => null));
return true;
2020-01-01 19:10:13 +01:00
}
2020-01-02 22:55:12 +01:00
@override
void initState() {
searchController.addListener(
() => setState(() => null),
);
super.initState();
}
2020-01-01 19:10:13 +01:00
@override
void dispose() {
2020-01-02 15:10:21 +01:00
sub?.cancel();
2020-01-02 22:55:12 +01:00
searchController.removeListener(
() => setState(() => null),
);
2020-01-01 19:10:13 +01:00
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2020-01-02 22:55:12 +01:00
title: searchMode
? TextField(
autofocus: true,
autocorrect: false,
controller: searchController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search for a chat",
),
)
: Text(
"FluffyChat",
2020-01-01 19:10:13 +01:00
),
2020-01-02 22:55:12 +01:00
leading: searchMode
? IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => setState(() => searchMode = false),
)
: null,
actions: searchMode
? null
: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () => setState(() => searchMode = true),
),
PopupMenuButton(
onSelected: (String choice) {
switch (choice) {
case "settings":
Navigator.of(context).push(
AppRoute.defaultRoute(
context,
SettingsView(),
),
);
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
const PopupMenuItem<String>(
value: "settings",
child: Text('Settings'),
),
],
),
],
2020-01-01 19:10:13 +01:00
),
floatingActionButton: SpeedDial(
child: Icon(Icons.add),
2020-01-03 11:57:00 +01:00
backgroundColor: Theme.of(context).primaryColor,
2020-01-01 19:10:13 +01:00
children: [
SpeedDialChild(
child: Icon(Icons.people_outline),
backgroundColor: Colors.blue,
label: 'Create new group',
labelStyle: TextStyle(fontSize: 18.0),
onTap: () => showDialog(
context: context,
builder: (BuildContext innerContext) => NewGroupDialog(),
),
),
SpeedDialChild(
2020-01-02 22:55:12 +01:00
child: Icon(Icons.person_add),
2020-01-01 19:10:13 +01:00
backgroundColor: Colors.green,
label: 'New private chat',
labelStyle: TextStyle(fontSize: 18.0),
onTap: () => showDialog(
context: context,
builder: (BuildContext innerContext) => NewPrivateChatDialog()),
),
],
),
2020-01-02 15:10:21 +01:00
body: FutureBuilder<bool>(
future: waitForFirstSync(context),
2020-01-01 19:10:13 +01:00
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
2020-01-02 22:55:12 +01:00
List<Room> rooms = List<Room>.from(Matrix.of(context).client.rooms);
rooms.removeWhere((Room room) =>
searchMode &&
!room.displayname
.toLowerCase()
.contains(searchController.text.toLowerCase() ?? ""));
if (rooms.isEmpty) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
searchMode ? Icons.search : Icons.add,
size: 80,
color: Colors.grey,
),
Text(searchMode
? "No rooms found..."
: "Start your first chat :-)"),
],
),
);
}
2020-01-01 19:10:13 +01:00
return ListView.builder(
itemCount: rooms.length,
itemBuilder: (BuildContext context, int i) => ChatListItem(
rooms[i],
activeChat: widget.activeChat == rooms[i].id,
),
);
2020-01-02 22:31:39 +01:00
} else {
2020-01-01 19:10:13 +01:00
return Center(
child: CircularProgressIndicator(),
);
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
},
),
);
}
}