1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/widgets/tablet_mode/conversations/conversation_window.dart

186 lines
5.3 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-09 17:45:07 +00:00
enum _Actions { START_CALL, 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.conversationID == _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: [
FlatButton(
textColor: Colors.white,
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: Icon(_hasNewMessages ? Icons.trip_origin : Icons.comment),
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: <Widget>[
PopupMenuButton<_Actions>(
itemBuilder: (c) => [
2020-05-09 17:45:07 +00:00
// Start a new call
PopupMenuItem(
child: Text(tr("Start call")),
value: _Actions.START_CALL,
enabled: _conversation.callCapabilities != CallCapabilities.NONE,
),
// Show the list of members
PopupMenuItem(
child: Text(tr("Members")),
value: _Actions.OPEN_MEMBERS,
2020-05-09 12:21:02 +00:00
),
// 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-09 17:45:07 +00:00
case _Actions.START_CALL:
MainController.of(context).startCall(_convID);
break;
case _Actions.OPEN_MEMBERS:
_openMembersList();
break;
2020-05-09 12:21:02 +00:00
case _Actions.OPEN_SETTINGS:
_openConversationSettings();
break;
}
}
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();
}
2020-05-09 06:17:52 +00:00
}