fluffychat/lib/views/chat_list.dart

435 lines
16 KiB
Dart
Raw Normal View History

2020-01-02 15:10:21 +01:00
import 'dart:async';
2020-04-02 14:05:32 +02:00
import 'dart:io';
2020-01-02 15:10:21 +01:00
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
import 'package:fluffychat/components/list_items/presence_list_item.dart';
2020-02-22 20:05:04 +01:00
import 'package:fluffychat/components/list_items/public_room_list_item.dart';
import 'package:flutter/foundation.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
2020-04-02 14:05:32 +02:00
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:share/share.dart';
2020-01-01 19:10:13 +01:00
2020-02-16 20:11:39 +01:00
import '../components/theme_switcher.dart';
import '../components/adaptive_page_layout.dart';
import '../components/list_items/chat_list_item.dart';
import '../components/matrix.dart';
2020-05-07 07:52:40 +02:00
import '../l10n/l10n.dart';
import '../utils/app_route.dart';
import '../utils/url_launcher.dart';
import '../utils/client_presence_extension.dart';
import 'archive.dart';
import 'new_group.dart';
import 'new_private_chat.dart';
import 'settings.dart';
enum SelectMode { normal, share }
2020-01-17 11:37:02 +01:00
2020-01-01 19:10:13 +01:00
class ChatListView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.FIRST,
firstScaffold: ChatList(),
secondScaffold: Scaffold(
body: Center(
2020-01-03 13:51:18 +01:00
child: Image.asset("assets/logo.png", width: 100, height: 100),
2020-01-01 19:10:13 +01:00
),
),
);
}
}
class ChatList extends StatefulWidget {
final String activeChat;
const ChatList({this.activeChat, Key key}) : super(key: key);
2020-01-01 19:10:13 +01:00
@override
_ChatListState createState() => _ChatListState();
}
class _ChatListState extends State<ChatList> {
bool get searchMode => searchController.text?.isNotEmpty ?? 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-17 11:37:02 +01:00
SelectMode selectMode = SelectMode.normal;
2020-02-22 20:05:04 +01:00
Timer coolDown;
PublicRoomsResponse publicRoomsResponse;
bool loadingPublicRooms = false;
String searchServer;
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
}
sub ??= client.onSync.stream
.listen((s) => mounted ? setState(() => null) : null);
2020-01-02 15:10:21 +01:00
return true;
2020-01-01 19:10:13 +01:00
}
2020-01-02 22:55:12 +01:00
@override
void initState() {
2020-02-22 20:05:04 +01:00
searchController.addListener(() {
coolDown?.cancel();
if (searchController.text.isEmpty) {
setState(() {
loadingPublicRooms = false;
publicRoomsResponse = null;
});
return;
}
2020-02-22 20:05:04 +01:00
coolDown = Timer(Duration(seconds: 1), () async {
setState(() => loadingPublicRooms = true);
final newPublicRoomsResponse =
2020-04-27 13:36:39 +02:00
await SimpleDialogs(context).tryRequestWithErrorToast(
2020-02-22 20:05:04 +01:00
Matrix.of(context).client.requestPublicRooms(
limit: 30,
includeAllNetworks: true,
genericSearchTerm: searchController.text,
server: searchServer,
),
);
setState(() {
loadingPublicRooms = false;
if (newPublicRoomsResponse != false) {
publicRoomsResponse = newPublicRoomsResponse;
if (searchController.text.isNotEmpty &&
searchController.text.isValidMatrixId &&
searchController.text.sigil == "#") {
publicRoomsResponse.publicRooms.add(
PublicRoomEntry(
2020-02-23 09:52:43 +01:00
aliases: [searchController.text],
name: searchController.text,
roomId: searchController.text,
client: Matrix.of(context).client,
),
2020-02-22 20:05:04 +01:00
);
}
}
});
});
setState(() => null);
});
2020-04-02 14:05:32 +02:00
_initReceiveSharingINtent();
2020-01-02 22:55:12 +01:00
super.initState();
}
2020-01-19 19:28:12 +01:00
StreamSubscription _intentDataStreamSubscription;
2020-04-02 14:05:32 +02:00
StreamSubscription _intentFileStreamSubscription;
2020-01-19 19:28:12 +01:00
2020-04-02 14:05:32 +02:00
void _processIncomingSharedFiles(List<SharedMediaFile> files) {
if (files?.isEmpty ?? true) return;
if (Navigator.of(context).canPop()) {
Navigator.of(context).popUntil((r) => r.isFirst);
}
final File file = File(files.first.path);
Matrix.of(context).shareContent = {
"msgtype": "chat.fluffy.shared_file",
"file": MatrixFile(
bytes: file.readAsBytesSync(),
path: file.path,
2020-03-29 12:06:25 +02:00
),
2020-04-02 14:05:32 +02:00
};
}
void _processIncomingSharedText(String text) {
if (text == null) return;
if (Navigator.of(context).canPop()) {
Navigator.of(context).popUntil((r) => r.isFirst);
}
if (text.startsWith("https://matrix.to/#/")) {
UrlLauncher(context, text).openMatrixToUrl();
return;
}
Matrix.of(context).shareContent = {
"msgtype": "m.text",
"body": text,
};
}
void _initReceiveSharingINtent() {
if (kIsWeb) return;
// For sharing images coming from outside the app while the app is in the memory
_intentFileStreamSubscription = ReceiveSharingIntent.getMediaStream()
.listen(_processIncomingSharedFiles, onError: print);
// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then(_processIncomingSharedFiles);
// For sharing or opening urls/text coming from outside the app while the app is in the memory
_intentDataStreamSubscription = ReceiveSharingIntent.getTextStream()
.listen(_processIncomingSharedText, onError: print);
// For sharing or opening urls/text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then(_processIncomingSharedText);
2020-01-19 19:28:12 +01:00
}
void _drawerTapAction(Widget view) {
Navigator.of(context).pop();
Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
view,
),
(r) => r.isFirst,
);
}
void _setStatus(BuildContext context) async {
Navigator.of(context).pop();
final status = await SimpleDialogs(context).enterText(
multiLine: true,
2020-05-07 07:52:40 +02:00
titleText: L10n.of(context).setStatus,
labelText: L10n.of(context).setStatus,
hintText: L10n.of(context).statusExampleMessage,
);
if (status?.isEmpty ?? true) return;
2020-04-27 13:36:39 +02:00
await SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context).client.jsonRequest(
type: HTTPType.PUT,
action:
'/client/r0/presence/${Matrix.of(context).client.userID}/status',
data: {
"presence": "online",
"status_msg": status,
},
),
);
}
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-19 19:28:12 +01:00
_intentDataStreamSubscription?.cancel();
2020-04-02 14:05:32 +02:00
_intentFileStreamSubscription?.cancel();
2020-04-09 09:51:52 +02:00
_onShareContentChangedSub?.cancel();
2020-01-01 19:10:13 +01:00
super.dispose();
}
2020-04-09 09:51:52 +02:00
StreamSubscription _onShareContentChangedSub;
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2020-04-09 09:51:52 +02:00
_onShareContentChangedSub ??= Matrix.of(context)
.onShareContentChanged
.stream
.listen((c) => setState(() => null));
2020-01-17 11:37:02 +01:00
if (Matrix.of(context).shareContent != null) {
selectMode = SelectMode.share;
} else if (selectMode == SelectMode.share) {
setState(() => selectMode = SelectMode.normal);
}
2020-01-01 19:10:13 +01:00
return Scaffold(
2020-04-26 19:08:55 +02:00
drawer: selectMode == SelectMode.share
? null
: Drawer(
child: SafeArea(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
ListTile(
leading: Icon(Icons.edit),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).setStatus),
2020-04-26 19:08:55 +02:00
onTap: () => _setStatus(context),
),
Divider(height: 1),
ListTile(
leading: Icon(Icons.people_outline),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).createNewGroup),
2020-04-26 19:08:55 +02:00
onTap: () => _drawerTapAction(NewGroupView()),
),
ListTile(
leading: Icon(Icons.person_add),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).newPrivateChat),
2020-04-26 19:08:55 +02:00
onTap: () => _drawerTapAction(NewPrivateChatView()),
),
Divider(height: 1),
ListTile(
leading: Icon(Icons.archive),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).archive),
2020-04-26 19:08:55 +02:00
onTap: () => _drawerTapAction(
Archive(),
),
),
ListTile(
leading: Icon(Icons.settings),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).settings),
2020-04-26 19:08:55 +02:00
onTap: () => _drawerTapAction(
SettingsView(),
),
),
Divider(height: 1),
ListTile(
leading: Icon(Icons.share),
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).inviteContact),
2020-04-26 19:08:55 +02:00
onTap: () {
Navigator.of(context).pop();
2020-05-07 07:52:40 +02:00
Share.share(L10n.of(context).inviteText(
2020-04-26 19:08:55 +02:00
Matrix.of(context).client.userID,
"https://matrix.to/#/${Matrix.of(context).client.userID}"));
},
),
],
2020-01-02 22:55:12 +01:00
),
),
2020-04-26 19:08:55 +02:00
),
appBar: AppBar(
2020-04-26 19:08:55 +02:00
leading: selectMode != SelectMode.share
? null
: IconButton(
icon: Icon(Icons.close),
onPressed: () => Matrix.of(context).shareContent = null,
),
2020-04-26 18:43:20 +02:00
titleSpacing: 0,
2020-04-26 19:08:55 +02:00
title: selectMode == SelectMode.share
2020-05-07 07:52:40 +02:00
? Text(L10n.of(context).share)
2020-04-26 19:08:55 +02:00
: Container(
padding: EdgeInsets.all(8),
height: 42,
margin: EdgeInsets.only(right: 8),
decoration: BoxDecoration(
color: Theme.of(context).secondaryHeaderColor,
borderRadius: BorderRadius.circular(90),
),
child: TextField(
autocorrect: false,
controller: searchController,
decoration: InputDecoration(
suffixIcon: loadingPublicRooms
? Container(
alignment: Alignment.centerRight,
child: Container(
width: 20,
height: 20,
child: CircularProgressIndicator(),
),
)
: Icon(Icons.search),
contentPadding: EdgeInsets.all(9),
border: InputBorder.none,
2020-05-07 07:52:40 +02:00
hintText: L10n.of(context).searchForAChat,
2020-04-26 19:08:55 +02:00
),
),
),
2020-01-01 19:10:13 +01:00
),
2020-04-26 19:08:55 +02:00
floatingActionButton: (AdaptivePageLayout.columnMode(context) ||
selectMode == SelectMode.share)
2020-04-09 10:00:18 +02:00
? null
: SpeedDial(
child: Icon(Icons.add),
overlayColor: blackWhiteColor(context),
foregroundColor: Colors.white,
backgroundColor: Theme.of(context).primaryColor,
children: [
SpeedDialChild(
child: Icon(Icons.people_outline),
foregroundColor: Colors.white,
backgroundColor: Colors.blue,
2020-05-07 07:52:40 +02:00
label: L10n.of(context).createNewGroup,
2020-04-09 10:00:18 +02:00
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
onTap: () => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, NewGroupView()),
(r) => r.isFirst),
),
SpeedDialChild(
child: Icon(Icons.person_add),
foregroundColor: Colors.white,
backgroundColor: Colors.green,
2020-05-07 07:52:40 +02:00
label: L10n.of(context).newPrivateChat,
2020-04-09 10:00:18 +02:00
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
onTap: () => Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, NewPrivateChatView()),
(r) => r.isFirst),
),
],
),
2020-04-27 10:12:12 +02:00
body: FutureBuilder<bool>(
future: waitForFirstSync(context),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
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 && (!searchMode || publicRoomsResponse == null)) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
searchMode ? Icons.search : Icons.chat_bubble_outline,
size: 80,
color: Colors.grey,
),
Text(searchMode
2020-05-07 07:52:40 +02:00
? L10n.of(context).noRoomsFound
: L10n.of(context).startYourFirstChat),
2020-04-27 10:12:12 +02:00
],
),
);
}
final int publicRoomsCount =
(publicRoomsResponse?.publicRooms?.length ?? 0);
final int totalCount = rooms.length + publicRoomsCount;
return ListView.separated(
separatorBuilder: (BuildContext context, int i) =>
2020-04-27 15:29:02 +02:00
i == totalCount - publicRoomsCount
2020-04-27 10:12:12 +02:00
? Material(
elevation: 2,
child: ListTile(
2020-05-07 07:52:40 +02:00
title: Text(L10n.of(context).publicRooms),
2020-04-27 10:12:12 +02:00
),
)
2020-04-27 10:12:12 +02:00
: Container(),
itemCount: totalCount + 1,
itemBuilder: (BuildContext context, int i) {
if (i == 0) {
return Matrix.of(context).client.statusList.isEmpty
? Container()
: PreferredSize(
preferredSize: Size.fromHeight(89),
child: Container(
height: 81,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount:
Matrix.of(context).client.statusList.length,
itemBuilder: (BuildContext context, int i) =>
PresenceListItem(Matrix.of(context)
.client
.statusList[i]),
),
),
);
}
i--;
return i < rooms.length
? ChatListItem(
rooms[i],
activeChat: widget.activeChat == rooms[i].id,
)
: PublicRoomListItem(
publicRoomsResponse.publicRooms[i - rooms.length]);
});
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
2020-01-01 19:10:13 +01:00
),
);
}
}