1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 06:19:22 +00:00
comunicmobile/lib/ui/widgets/tablet_mode/conversations/conversation_window.dart

194 lines
5.6 KiB
Dart
Raw Normal View History

2020-05-09 07:51:37 +00:00
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/events_helper.dart';
2020-05-09 07:30:46 +00:00
import 'package:comunic/models/conversation.dart';
import 'package:comunic/ui/dialogs/screen_dialog.dart';
2020-05-09 17:45:07 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2020-05-09 12:21:02 +00:00
import 'package:comunic/ui/routes/update_conversation_route.dart';
import 'package:comunic/ui/screens/conversation_members_screen.dart';
2020-05-09 07:51:37 +00:00
import 'package:comunic/ui/screens/conversation_screen.dart';
2020-05-09 07:30:46 +00:00
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/ui/widgets/tablet_mode/conversations/conversation_window_container.dart';
import 'package:comunic/utils/account_utils.dart';
2020-05-09 07:30:46 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
2020-05-09 06:17:52 +00:00
import 'package:flutter/material.dart';
/// Single conversation window
///
/// @author Pierre Hubert
2020-05-11 16:27:00 +00:00
enum _Actions { OPEN_FULL_SCREEN, OPEN_MEMBERS, OPEN_SETTINGS }
2020-05-09 06:17:52 +00:00
class ConversationWindow extends StatefulWidget {
final int convID;
2020-05-09 07:30:46 +00:00
final Function() onClose;
2020-05-09 06:17:52 +00:00
const ConversationWindow({
Key key,
@required this.convID,
2020-05-09 07:30:46 +00:00
@required this.onClose,
2020-05-09 06:17:52 +00:00
}) : assert(convID != null),
2020-05-09 07:30:46 +00:00
assert(onClose != null),
2020-05-09 06:17:52 +00:00
super(key: key);
@override
_ConversationWindowState createState() => _ConversationWindowState();
}
2020-05-09 07:30:46 +00:00
class _ConversationWindowState extends SafeState<ConversationWindow> {
Conversation _conversation;
String _convTitle;
2020-05-09 07:51:37 +00:00
bool _error = false;
2020-05-09 08:20:06 +00:00
bool _collapsed = false;
bool _hasNewMessages = false;
2020-05-09 07:51:37 +00:00
int get _convID => widget.convID;
2020-05-09 08:20:06 +00:00
final _convKey = UniqueKey();
2020-05-09 07:51:37 +00:00
void _setError(bool e) => setState(() => _error = e);
void _toggleVisibility() => setState(() {
_collapsed = !_collapsed;
if (!_collapsed) _hasNewMessages = false;
});
2020-05-09 08:20:06 +00:00
2020-05-09 07:51:37 +00:00
Future<void> _refresh() async {
try {
_setError(false);
final conversation =
await ConversationsHelper().getSingle(_convID, force: true);
assert(conversation != null);
final name =
await ConversationsHelper.getConversationNameAsync(conversation);
assert(name != null);
setState(() {
_conversation = conversation;
_convTitle = name;
});
} catch (e, s) {
_setError(true);
print("Could not refresh the list of conversations! $e\n$s");
showSimpleSnack(context, tr("Could not load conversation information!"));
}
}
@override
void initState() {
super.initState();
_refresh();
listen<NewConversationMessageEvent>((e) {
if (e.msg.convID == _convID && _collapsed && e.msg.userID != userID())
setState(() => _hasNewMessages = true);
});
2020-05-09 07:51:37 +00:00
}
2020-05-09 07:30:46 +00:00
2020-05-09 06:17:52 +00:00
@override
Widget build(BuildContext context) {
2020-05-09 07:51:37 +00:00
// In case of error
if (_error)
return ConversationWindowContainer(
icon: Icon(Icons.error),
2020-05-09 07:51:37 +00:00
title: Text(tr("Error")),
onClose: widget.onClose,
2020-05-09 08:20:44 +00:00
onToggleCollapse: _toggleVisibility,
2020-05-09 08:20:06 +00:00
isCollapsed: _collapsed,
2020-05-09 07:51:37 +00:00
body: buildErrorCard(tr("Could not load conversation information!"),
actions: [
2021-03-13 14:42:19 +00:00
ElevatedButton(
2020-05-09 07:51:37 +00:00
onPressed: _refresh,
child: Text(tr("Try again").toUpperCase()),
)
]),
);
// If it is still loading
if (_conversation == null)
return ConversationWindowContainer(
icon: Icon(Icons.message),
2020-05-09 07:51:37 +00:00
title: Text(tr("Loading...")),
onClose: widget.onClose,
2020-05-09 08:20:44 +00:00
onToggleCollapse: _toggleVisibility,
2020-05-09 08:20:06 +00:00
isCollapsed: _collapsed,
2020-05-09 07:51:37 +00:00
body: buildCenteredProgressBar(),
);
2020-05-09 07:30:46 +00:00
return ConversationWindowContainer(
icon: _hasNewMessages ? Icon(Icons.trip_origin) : null,
appBarBgColor: _hasNewMessages ? Colors.green : null,
2020-05-09 07:51:37 +00:00
title: Text(_convTitle),
2020-05-09 07:30:46 +00:00
onClose: widget.onClose,
2020-05-09 08:20:44 +00:00
onToggleCollapse: _toggleVisibility,
2020-05-09 08:20:06 +00:00
isCollapsed: _collapsed,
action: (_conversation.callCapabilities != CallCapabilities.NONE
? [IconButton(icon: Icon(Icons.call), onPressed: _startCall)]
: [])
..addAll(<Widget>[
PopupMenuButton<_Actions>(
itemBuilder: (c) => [
2020-05-11 16:27:00 +00:00
// Show in full screen
PopupMenuItem(
child: Text(tr("Open in full screen")),
value: _Actions.OPEN_FULL_SCREEN,
),
// Show the list of members
PopupMenuItem(
child: Text(tr("Members")),
value: _Actions.OPEN_MEMBERS,
),
// Show conversation settings
PopupMenuItem(
child: Text(tr("Settings")),
value: _Actions.OPEN_SETTINGS,
)
],
onSelected: _menuCallback,
),
]),
2020-05-09 07:51:37 +00:00
body: ConversationScreen(
2020-05-09 08:20:06 +00:00
key: _convKey,
2020-05-09 07:51:37 +00:00
conversationID: _convID,
),
2020-05-09 07:30:46 +00:00
);
2020-05-09 06:17:52 +00:00
}
void _menuCallback(_Actions value) {
switch (value) {
2020-05-11 16:27:00 +00:00
case _Actions.OPEN_FULL_SCREEN:
_openFullScreen();
break;
case _Actions.OPEN_MEMBERS:
_openMembersList();
break;
2020-05-09 12:21:02 +00:00
case _Actions.OPEN_SETTINGS:
_openConversationSettings();
break;
}
}
2020-05-11 16:27:00 +00:00
void _openFullScreen() {
MainController.of(context)
.openConversation(_conversation, fullScreen: true);
2020-05-11 16:27:00 +00:00
widget.onClose();
}
void _openMembersList() {
showScreenDialog(context, ConversationMembersScreen(convID: _convID));
}
2020-05-09 12:21:02 +00:00
void _openConversationSettings() async {
await showScreenDialog(
context, UpdateConversationRoute(conversationID: _convID));
_refresh();
}
void _startCall() => MainController.of(context).startCall(_convID);
2020-05-09 06:17:52 +00:00
}