fluffychat/lib/views/chat_list.dart

342 lines
14 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-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 12:46:38 +01:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-07-04 10:22:51 +02:00
import 'package:fluffychat/components/connection_status_header.dart';
2020-12-06 12:51:40 +01:00
import 'package:fluffychat/components/default_app_bar_search_field.dart';
import 'package:fluffychat/components/default_drawer.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-12-11 14:14:33 +01:00
import 'package:fluffychat/app_config.dart';
2020-09-26 20:27:15 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/foundation.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-04-02 14:05:32 +02:00
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import '../components/list_items/chat_list_item.dart';
import '../components/matrix.dart';
2020-09-04 12:56:25 +02:00
import '../utils/matrix_file_extension.dart';
import '../utils/url_launcher.dart';
2020-10-02 15:50:59 +02:00
enum SelectMode { normal, share, select }
2020-01-17 11:37:02 +01:00
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 22:55:12 +01:00
final TextEditingController searchController = TextEditingController();
2020-10-02 15:50:59 +02:00
final _selectedRoomIds = <String>{};
2020-01-01 19:10:13 +01:00
2020-05-09 09:30:03 +02:00
final ScrollController _scrollController = ScrollController();
2020-12-06 12:51:40 +01:00
bool _scrolledToTop = true;
2020-05-09 09:30:03 +02:00
2020-10-02 15:50:59 +02:00
void _toggleSelection(String roomId) =>
setState(() => _selectedRoomIds.contains(roomId)
? _selectedRoomIds.remove(roomId)
: _selectedRoomIds.add(roomId));
2020-05-09 07:17:55 +02:00
Future<void> waitForFirstSync(BuildContext context) async {
2020-05-13 15:58:59 +02:00
var 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
return true;
2020-01-01 19:10:13 +01:00
}
2020-01-02 22:55:12 +01:00
@override
void initState() {
2020-05-09 09:30:03 +02:00
_scrollController.addListener(() async {
if (_scrollController.position.pixels > 0 && _scrolledToTop) {
setState(() => _scrolledToTop = false);
} else if (_scrollController.position.pixels == 0 && !_scrolledToTop) {
setState(() => _scrolledToTop = true);
}
});
2020-09-04 12:56:25 +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;
2021-01-16 12:46:38 +01:00
AdaptivePageLayout.of(context).popUntilIsFirst();
2020-05-13 15:58:59 +02:00
final file = File(files.first.path);
2020-04-02 14:05:32 +02:00
Matrix.of(context).shareContent = {
2020-05-13 15:58:59 +02:00
'msgtype': 'chat.fluffy.shared_file',
'file': MatrixFile(
2020-04-02 14:05:32 +02:00
bytes: file.readAsBytesSync(),
2020-07-02 11:30:59 +02:00
name: file.path,
2020-09-04 12:56:25 +02:00
).detectFileType,
2020-04-02 14:05:32 +02:00
};
}
void _processIncomingSharedText(String text) {
if (text == null) return;
2021-01-16 12:46:38 +01:00
AdaptivePageLayout.of(context).popUntilIsFirst();
if (text.toLowerCase().startsWith(AppConfig.inviteLinkPrefix) ||
(text.toLowerCase().startsWith(AppConfig.schemePrefix) &&
!RegExp(r'\s').hasMatch(text))) {
2020-04-02 14:05:32 +02:00
UrlLauncher(context, text).openMatrixToUrl();
return;
}
Matrix.of(context).shareContent = {
2020-05-13 15:58:59 +02:00
'msgtype': 'm.text',
'body': text,
2020-04-02 14:05:32 +02:00
};
}
2020-09-04 12:56:25 +02:00
void _initReceiveSharingIntent() {
2020-09-26 20:27:15 +02:00
if (!PlatformInfos.isMobile) return;
2020-04-02 14:05:32 +02:00
// 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
}
2020-01-01 19:10:13 +01:00
@override
void dispose() {
2020-01-19 19:28:12 +01:00
_intentDataStreamSubscription?.cancel();
2020-04-02 14:05:32 +02:00
_intentFileStreamSubscription?.cancel();
2020-01-01 19:10:13 +01:00
super.dispose();
}
Future<void> _toggleUnread(BuildContext context) {
final room = Matrix.of(context).client.getRoomById(_selectedRoomIds.single);
2020-12-25 09:58:34 +01:00
return showFutureLoadingDialog(
context: context,
future: () => room.setUnread(!room.isUnread),
);
}
2020-10-02 15:50:59 +02:00
Future<void> _toggleFavouriteRoom(BuildContext context) {
final room = Matrix.of(context).client.getRoomById(_selectedRoomIds.single);
2020-12-25 09:58:34 +01:00
return showFutureLoadingDialog(
context: context,
future: () => room.setFavourite(!room.isFavourite),
2020-10-02 15:50:59 +02:00
);
}
Future<void> _toggleMuted(BuildContext context) {
final room = Matrix.of(context).client.getRoomById(_selectedRoomIds.single);
2020-12-25 09:58:34 +01:00
return showFutureLoadingDialog(
context: context,
future: () => room.setPushRuleState(
room.pushRuleState == PushRuleState.notify
? PushRuleState.mentions_only
: PushRuleState.notify),
2020-10-02 15:50:59 +02:00
);
}
Future<void> _archiveAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
final confirmed = await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).areYouSure,
) ==
OkCancelResult.ok;
2020-10-02 15:50:59 +02:00
if (!confirmed) return;
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
future: () => _archiveSelectedRooms(context),
);
2020-10-02 15:50:59 +02:00
setState(() => null);
}
Future<void> _archiveSelectedRooms(BuildContext context) async {
final client = Matrix.of(context).client;
while (_selectedRoomIds.isNotEmpty) {
final roomId = _selectedRoomIds.first;
await client.getRoomById(roomId).leave();
_selectedRoomIds.remove(roomId);
}
}
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2021-01-16 12:46:38 +01:00
return StreamBuilder(
stream: Matrix.of(context).onShareContentChanged.stream,
2020-05-09 07:17:55 +02:00
builder: (context, snapshot) {
2021-01-16 12:46:38 +01:00
final selectMode = Matrix.of(context).shareContent == null
? _selectedRoomIds.isEmpty
? SelectMode.normal
: SelectMode.select
: SelectMode.share;
if (selectMode == SelectMode.share) {
_selectedRoomIds.clear();
2020-05-12 11:58:47 +02:00
}
2021-01-16 12:46:38 +01:00
Room selectedRoom;
if (_selectedRoomIds.length == 1) {
selectedRoom =
Matrix.of(context).client.getRoomById(_selectedRoomIds.single);
}
return Scaffold(
drawer: selectMode != SelectMode.normal ? null : DefaultDrawer(),
appBar: AppBar(
centerTitle: false,
elevation: _scrolledToTop ? 0 : null,
leading: selectMode == SelectMode.share
? IconButton(
icon: Icon(Icons.close),
onPressed: () => Matrix.of(context).shareContent = null,
)
: selectMode == SelectMode.select
? IconButton(
icon: Icon(Icons.close),
onPressed: () => setState(_selectedRoomIds.clear),
)
: null,
titleSpacing: 0,
actions: selectMode != SelectMode.select
? null
: [
if (_selectedRoomIds.length == 1)
IconButton(
tooltip: L10n.of(context).toggleUnread,
icon: Icon(selectedRoom.isUnread
? Icons.mark_chat_read_outlined
: Icons.mark_chat_unread_outlined),
onPressed: () => _toggleUnread(context),
),
2021-01-16 12:46:38 +01:00
if (_selectedRoomIds.length == 1)
IconButton(
tooltip: L10n.of(context).toggleFavorite,
icon: Icon(Icons.push_pin_outlined),
onPressed: () => _toggleFavouriteRoom(context),
),
if (_selectedRoomIds.length == 1)
IconButton(
icon: Icon(
selectedRoom.pushRuleState == PushRuleState.notify
? Icons.notifications_off_outlined
: Icons.notifications_outlined),
tooltip: L10n.of(context).toggleMuted,
onPressed: () => _toggleMuted(context),
),
IconButton(
icon: Icon(Icons.archive_outlined),
tooltip: L10n.of(context).archive,
onPressed: () => _archiveAction(context),
2020-08-22 11:02:08 +02:00
),
],
2021-01-16 12:46:38 +01:00
title: selectMode == SelectMode.share
? Text(L10n.of(context).share)
: selectMode == SelectMode.select
? Text(_selectedRoomIds.length.toString())
: DefaultAppBarSearchField(
searchController: searchController,
hintText: L10n.of(context).searchForAChat,
onChanged: (_) => setState(() => null),
2021-01-16 21:50:01 +01:00
suffix: Icon(Icons.search_outlined),
2021-01-16 12:46:38 +01:00
),
),
floatingActionButton:
AdaptivePageLayout.of(context).columnMode(context)
? null
: FloatingActionButton(
child: Icon(Icons.add_outlined),
onPressed: () => AdaptivePageLayout.of(context)
.pushNamedAndRemoveUntilIsFirst('/newprivatechat'),
),
body: Column(
children: [
ConnectionStatusHeader(),
Expanded(
child: StreamBuilder(
stream: Matrix.of(context)
.client
.onSync
.stream
.where((s) => s.hasRoomUpdate),
builder: (context, snapshot) {
return FutureBuilder<void>(
future: waitForFirstSync(context),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
var rooms = List<Room>.from(
Matrix.of(context).client.rooms);
rooms.removeWhere((Room room) =>
room.lastEvent == null ||
(searchMode &&
!room.displayname.toLowerCase().contains(
searchController.text.toLowerCase() ??
'')));
if (rooms.isEmpty && (!searchMode)) {
2021-01-16 16:03:25 +01:00
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
searchMode
? Icons.search_outlined
: Icons.maps_ugc_outlined,
size: 80,
color: Colors.grey,
),
Text(
searchMode
? L10n.of(context).noRoomsFound
: L10n.of(context).startYourFirstChat,
style: TextStyle(
2021-01-16 12:46:38 +01:00
color: Colors.grey,
2021-01-16 16:03:25 +01:00
fontSize: 16,
2021-01-16 12:46:38 +01:00
),
2021-01-16 16:03:25 +01:00
),
],
2021-01-16 12:46:38 +01:00
);
}
final totalCount = rooms.length;
2021-01-16 22:18:22 +01:00
return ListView.builder(
2021-01-16 12:46:38 +01:00
controller: _scrollController,
itemCount: totalCount,
itemBuilder: (BuildContext context, int i) =>
ChatListItem(
rooms[i],
selected:
_selectedRoomIds.contains(rooms[i].id),
onTap: selectMode == SelectMode.select
? () => _toggleSelection(rooms[i].id)
: null,
onLongPress: selectMode != SelectMode.share
? () => _toggleSelection(rooms[i].id)
: null,
activeChat: widget.activeChat == rooms[i].id,
),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
);
}),
),
],
),
);
2020-05-09 07:17:55 +02:00
});
2020-01-01 19:10:13 +01:00
}
}