FurryChat/lib/views/chat_list.dart

260 lines
8.7 KiB
Dart
Raw Normal View History

2020-01-02 14:10:21 +00:00
import 'dart:async';
2020-01-01 18:10:13 +00: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';
2020-01-20 12:46:39 +00:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-01 18:10:13 +00:00
import 'package:fluffychat/utils/app_route.dart';
2020-01-19 18:28:12 +00:00
import 'package:fluffychat/utils/url_launcher.dart';
2020-01-05 11:27:03 +00:00
import 'package:fluffychat/views/archive.dart';
2020-01-01 18:10:13 +00:00
import 'package:fluffychat/views/settings.dart';
import 'package:flutter/foundation.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
2020-01-19 18:28:12 +00:00
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
2020-01-01 18:10:13 +00:00
2020-01-17 10:37:02 +00:00
enum SelectMode { normal, multi_select, share }
2020-01-01 18:10:13 +00:00
class ChatListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.FIRST,
firstScaffold: ChatList(),
secondScaffold: Scaffold(
body: Center(
2020-01-03 12:51:18 +00:00
child: Image.asset("assets/logo.png", width: 100, height: 100),
2020-01-01 18:10:13 +00: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 21:55:12 +00:00
bool searchMode = false;
2020-01-02 14:10:21 +00:00
StreamSubscription sub;
2020-01-02 21:55:12 +00:00
final TextEditingController searchController = TextEditingController();
2020-01-17 10:37:02 +00:00
SelectMode selectMode = SelectMode.normal;
2020-01-01 18:10:13 +00:00
2020-01-02 14:10:21 +00:00
Future<bool> waitForFirstSync(BuildContext context) async {
2020-01-01 18:10:13 +00:00
Client client = Matrix.of(context).client;
2020-01-02 21:31:39 +00:00
if (client.prevBatch?.isEmpty ?? true) {
2020-01-02 14:10:21 +00:00
await client.onFirstSync.stream.first;
2020-01-02 21:31:39 +00:00
}
sub ??= client.onSync.stream
.listen((s) => mounted ? setState(() => null) : null);
2020-01-02 14:10:21 +00:00
return true;
2020-01-01 18:10:13 +00:00
}
2020-01-02 21:55:12 +00:00
@override
void initState() {
searchController.addListener(
() => setState(() => null),
);
if (kIsWeb) {
getSharedData();
}
2020-01-02 21:55:12 +00:00
super.initState();
}
2020-01-19 18:28:12 +00:00
StreamSubscription _intentDataStreamSubscription;
void processSharedText(String text) {
if (text?.isEmpty ?? true) return;
2020-01-19 18:28:12 +00:00
if (text.startsWith("https://matrix.to/#/")) {
UrlLauncher(context, text).openMatrixToUrl();
} else {
setState(() => Matrix.of(context).shareContent = {
"msgtype": "m.text",
"body": text,
});
}
}
void getSharedData() {
// For sharing or opening urls/text coming from outside the app while the app is in the memory
_intentDataStreamSubscription = ReceiveSharingIntent.getTextStream()
.listen(processSharedText, onError: (err) {
print("getLinkStream error: $err");
});
// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then(processSharedText);
}
2020-01-01 18:10:13 +00:00
@override
void dispose() {
2020-01-02 14:10:21 +00:00
sub?.cancel();
2020-01-02 21:55:12 +00:00
searchController.removeListener(
() => setState(() => null),
);
2020-01-19 18:28:12 +00:00
_intentDataStreamSubscription?.cancel();
2020-01-01 18:10:13 +00:00
super.dispose();
}
@override
Widget build(BuildContext context) {
2020-01-17 10:37:02 +00:00
if (Matrix.of(context).shareContent != null) {
selectMode = SelectMode.share;
} else if (selectMode == SelectMode.share) {
setState(() => selectMode = SelectMode.normal);
}
2020-01-01 18:10:13 +00:00
return Scaffold(
appBar: AppBar(
2020-01-02 21:55:12 +00:00
title: searchMode
? TextField(
autofocus: true,
autocorrect: false,
controller: searchController,
decoration: InputDecoration(
border: InputBorder.none,
2020-01-20 12:46:39 +00:00
hintText: I18n.of(context).searchForAChat,
2020-01-02 21:55:12 +00:00
),
)
: Text(
2020-01-20 12:46:39 +00:00
selectMode == SelectMode.share
? I18n.of(context).share
: I18n.of(context).fluffychat,
2020-01-01 18:10:13 +00:00
),
2020-01-02 21:55:12 +00:00
leading: searchMode
? IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => setState(() => searchMode = false),
)
: null,
2020-01-03 11:37:16 +00:00
automaticallyImplyLeading: false,
2020-01-02 21:55:12 +00:00
actions: searchMode
? null
: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () => setState(() => searchMode = true),
),
2020-01-17 10:37:02 +00:00
if (selectMode == SelectMode.share)
IconButton(
icon: Icon(Icons.close),
onPressed: () {
Matrix.of(context).shareContent = null;
setState(() => selectMode = SelectMode.normal);
},
),
if (selectMode == SelectMode.normal)
PopupMenuButton(
onSelected: (String choice) {
switch (choice) {
case "settings":
Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
SettingsView(),
),
(r) => r.isFirst,
);
break;
case "archive":
Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
Archive(),
),
(r) => r.isFirst,
);
break;
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
2020-01-20 12:46:39 +00:00
PopupMenuItem<String>(
2020-01-17 10:37:02 +00:00
value: "archive",
2020-01-20 12:46:39 +00:00
child: Text(I18n.of(context).archive),
2020-01-17 10:37:02 +00:00
),
2020-01-20 12:46:39 +00:00
PopupMenuItem<String>(
2020-01-17 10:37:02 +00:00
value: "settings",
2020-01-20 12:46:39 +00:00
child: Text(I18n.of(context).settings),
2020-01-17 10:37:02 +00:00
),
],
),
2020-01-02 21:55:12 +00:00
],
2020-01-01 18:10:13 +00:00
),
floatingActionButton: SpeedDial(
child: Icon(Icons.add),
2020-01-03 10:57:00 +00:00
backgroundColor: Theme.of(context).primaryColor,
2020-01-01 18:10:13 +00:00
children: [
SpeedDialChild(
child: Icon(Icons.people_outline),
backgroundColor: Colors.blue,
2020-01-20 12:46:39 +00:00
label: I18n.of(context).createNewGroup,
2020-01-01 18:10:13 +00:00
labelStyle: TextStyle(fontSize: 18.0),
onTap: () => showDialog(
context: context,
builder: (BuildContext innerContext) => NewGroupDialog(),
),
),
SpeedDialChild(
2020-01-02 21:55:12 +00:00
child: Icon(Icons.person_add),
2020-01-01 18:10:13 +00:00
backgroundColor: Colors.green,
2020-01-20 12:46:39 +00:00
label: I18n.of(context).newPrivateChat,
2020-01-01 18:10:13 +00:00
labelStyle: TextStyle(fontSize: 18.0),
onTap: () => showDialog(
context: context,
builder: (BuildContext innerContext) => NewPrivateChatDialog()),
),
],
),
2020-01-02 14:10:21 +00:00
body: FutureBuilder<bool>(
future: waitForFirstSync(context),
2020-01-01 18:10:13 +00:00
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
2020-01-02 21:55:12 +00: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
2020-01-20 12:46:39 +00:00
? I18n.of(context).noRoomsFound
: I18n.of(context).startYourFirstChat),
2020-01-02 21:55:12 +00:00
],
),
);
}
2020-01-03 13:47:42 +00:00
return ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
Divider(indent: 70, height: 1),
2020-01-01 18:10:13 +00:00
itemCount: rooms.length,
itemBuilder: (BuildContext context, int i) => ChatListItem(
rooms[i],
activeChat: widget.activeChat == rooms[i].id,
),
);
2020-01-02 21:31:39 +00:00
} else {
2020-01-01 18:10:13 +00:00
return Center(
child: CircularProgressIndicator(),
);
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
},
),
);
}
}