2019-04-24 15:46:25 +00:00
|
|
|
import 'package:comunic/helpers/conversations_helper.dart';
|
2021-03-11 17:11:24 +00:00
|
|
|
import 'package:comunic/helpers/users_helper.dart';
|
|
|
|
import 'package:comunic/lists/users_list.dart';
|
2019-04-24 15:46:25 +00:00
|
|
|
import 'package:comunic/models/conversation.dart';
|
2020-05-05 11:21:37 +00:00
|
|
|
import 'package:comunic/ui/routes/main_route/main_route.dart';
|
2019-04-25 06:56:16 +00:00
|
|
|
import 'package:comunic/ui/screens/conversation_screen.dart';
|
2020-04-16 12:58:02 +00:00
|
|
|
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
|
2021-03-11 17:11:24 +00:00
|
|
|
import 'package:comunic/ui/widgets/conversation_image_widget.dart';
|
2021-03-10 16:54:41 +00:00
|
|
|
import 'package:comunic/ui/widgets/safe_state.dart';
|
2019-04-24 15:46:25 +00:00
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Single conversation route
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class ConversationRoute extends StatefulWidget {
|
|
|
|
final int conversationID;
|
|
|
|
|
|
|
|
const ConversationRoute({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.conversationID,
|
2019-04-24 15:46:25 +00:00
|
|
|
}) : assert(conversationID != null),
|
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _ConversationRouteState();
|
|
|
|
}
|
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
class _ConversationRouteState extends SafeState<ConversationRoute> {
|
2019-04-24 15:46:25 +00:00
|
|
|
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
2022-03-10 18:39:57 +00:00
|
|
|
Conversation? _conversation;
|
|
|
|
UsersList? _users;
|
|
|
|
String? _conversationName;
|
2019-04-24 15:46:25 +00:00
|
|
|
bool _error = false;
|
|
|
|
|
|
|
|
setError(bool err) => setState(() => _error = err);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
_loadConversation();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _loadConversation() async {
|
|
|
|
setError(false);
|
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
try {
|
|
|
|
_conversation = await _conversationsHelper
|
|
|
|
.getSingle(widget.conversationID, force: true);
|
2019-04-24 15:46:25 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
_users = await UsersHelper().getList(_conversation!.membersID);
|
2019-04-24 15:46:25 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
final conversationName =
|
2022-03-10 18:39:57 +00:00
|
|
|
ConversationsHelper.getConversationName(_conversation!, _users);
|
2019-04-24 15:46:25 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
if (!this.mounted) return null;
|
2019-05-01 15:10:23 +00:00
|
|
|
|
2021-03-10 16:54:41 +00:00
|
|
|
setState(() => _conversationName = conversationName);
|
|
|
|
} catch (e, s) {
|
|
|
|
print("Failed to get conversation name! $e => $s");
|
|
|
|
setError(true);
|
|
|
|
}
|
2019-04-24 15:46:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 16:41:51 +00:00
|
|
|
void _openSettings() =>
|
2022-03-10 18:39:57 +00:00
|
|
|
MainController.of(context)!.openConversationSettingsRoute(_conversation!);
|
2019-05-01 07:24:50 +00:00
|
|
|
|
2019-04-24 15:46:25 +00:00
|
|
|
Widget _buildRouteBody() {
|
|
|
|
//Handle errors
|
|
|
|
if (_error != null && _error)
|
|
|
|
return buildErrorCard(
|
|
|
|
tr("Could not get conversation information!"),
|
|
|
|
actions: <Widget>[
|
2021-03-13 14:14:54 +00:00
|
|
|
TextButton(
|
2019-04-24 15:46:25 +00:00
|
|
|
onPressed: _loadConversation,
|
|
|
|
child: Text(
|
2022-03-10 18:39:57 +00:00
|
|
|
tr("Try again")!.toUpperCase(),
|
2019-04-24 15:46:25 +00:00
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2019-04-25 06:56:16 +00:00
|
|
|
if (_conversationName == null || _conversation == null)
|
2019-04-24 15:46:25 +00:00
|
|
|
return buildCenteredProgressBar();
|
2019-04-25 06:56:16 +00:00
|
|
|
|
|
|
|
return ConversationScreen(
|
|
|
|
conversationID: widget.conversationID,
|
|
|
|
);
|
2019-04-24 15:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2021-03-11 17:11:24 +00:00
|
|
|
leading: isTablet(context)
|
|
|
|
? (_conversation == null || _users == null
|
|
|
|
? null
|
|
|
|
: ConversationImageWidget(
|
2022-03-10 18:39:57 +00:00
|
|
|
conversation: _conversation!, users: _users!))
|
2021-03-11 17:11:24 +00:00
|
|
|
: ComunicBackButton(),
|
2019-04-24 15:46:25 +00:00
|
|
|
title: Text(
|
2022-03-10 18:39:57 +00:00
|
|
|
_conversationName == null ? tr("Loading")! : _conversationName!,
|
2019-04-24 15:46:25 +00:00
|
|
|
),
|
2019-05-01 07:24:50 +00:00
|
|
|
actions: <Widget>[
|
2020-04-20 08:53:25 +00:00
|
|
|
// Start call (if possible)
|
|
|
|
_conversation == null ||
|
2022-03-10 18:39:57 +00:00
|
|
|
_conversation!.callCapabilities == CallCapabilities.NONE
|
2020-04-20 08:53:25 +00:00
|
|
|
? Container()
|
|
|
|
: IconButton(
|
|
|
|
icon: Icon(Icons.phone),
|
2022-03-10 18:39:57 +00:00
|
|
|
onPressed: () => MainController.of(context)!
|
2020-04-20 08:53:25 +00:00
|
|
|
.startCall(widget.conversationID),
|
|
|
|
),
|
|
|
|
|
|
|
|
// Edit conversation settings
|
2019-05-01 07:24:50 +00:00
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.settings),
|
|
|
|
onPressed: _openSettings,
|
|
|
|
)
|
|
|
|
],
|
2019-04-24 15:46:25 +00:00
|
|
|
),
|
|
|
|
body: _buildRouteBody(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|