2019-04-24 15:46:25 +00:00
|
|
|
import 'package:comunic/helpers/conversations_helper.dart';
|
|
|
|
import 'package:comunic/models/conversation.dart';
|
2019-05-01 07:24:50 +00:00
|
|
|
import 'package:comunic/ui/routes/update_conversation_route.dart';
|
2019-04-25 06:56:16 +00:00
|
|
|
import 'package:comunic/ui/screens/conversation_screen.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({
|
|
|
|
Key key,
|
|
|
|
@required this.conversationID,
|
|
|
|
}) : assert(conversationID != null),
|
|
|
|
super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _ConversationRouteState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ConversationRouteState extends State<ConversationRoute> {
|
|
|
|
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
|
|
|
Conversation _conversation;
|
|
|
|
String _conversationName;
|
|
|
|
bool _error = false;
|
|
|
|
|
|
|
|
setError(bool err) => setState(() => _error = err);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
_loadConversation();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _loadConversation() async {
|
|
|
|
setError(false);
|
|
|
|
|
2019-04-25 06:56:16 +00:00
|
|
|
_conversation = await _conversationsHelper.getSingle(widget.conversationID);
|
2019-04-24 15:46:25 +00:00
|
|
|
|
|
|
|
if (_conversation == null) return setError(true);
|
|
|
|
|
|
|
|
final conversationName =
|
|
|
|
await ConversationsHelper.getConversationNameAsync(_conversation);
|
|
|
|
|
2019-04-25 06:56:16 +00:00
|
|
|
if (conversationName == null) return setError(true);
|
2019-04-24 15:46:25 +00:00
|
|
|
|
|
|
|
setState(() {
|
|
|
|
_conversationName = conversationName;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-01 07:24:50 +00:00
|
|
|
void _openSettings() {
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (b) {
|
|
|
|
return UpdateConversationRoute(
|
|
|
|
conversationID: widget.conversationID,
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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>[
|
|
|
|
FlatButton(
|
|
|
|
onPressed: _loadConversation,
|
|
|
|
child: Text(
|
|
|
|
tr("Try again").toUpperCase(),
|
|
|
|
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(
|
|
|
|
title: Text(
|
|
|
|
_conversationName == null ? tr("Loading") : _conversationName,
|
|
|
|
),
|
2019-05-01 07:24:50 +00:00
|
|
|
actions: <Widget>[
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.settings),
|
|
|
|
onPressed: _openSettings,
|
|
|
|
)
|
|
|
|
],
|
2019-04-24 15:46:25 +00:00
|
|
|
),
|
|
|
|
body: _buildRouteBody(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|