2019-04-23 12:35:41 +00:00
|
|
|
import 'package:comunic/enums/load_error_level.dart';
|
|
|
|
import 'package:comunic/helpers/conversations_helper.dart';
|
2019-04-23 15:29:58 +00:00
|
|
|
import 'package:comunic/helpers/users_helper.dart';
|
|
|
|
import 'package:comunic/lists/conversations_list.dart';
|
2019-04-24 15:46:25 +00:00
|
|
|
import 'package:comunic/ui/routes/conversation_route.dart';
|
2019-04-27 14:23:08 +00:00
|
|
|
import 'package:comunic/ui/routes/create_conversation_route.dart';
|
2019-04-23 12:35:41 +00:00
|
|
|
import 'package:comunic/ui/tiles/conversation_tile.dart';
|
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Conversations screen
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2019-04-24 15:46:25 +00:00
|
|
|
class ConversationsListScreen extends StatefulWidget {
|
2019-04-23 12:35:41 +00:00
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _ConversationScreenState();
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:46:25 +00:00
|
|
|
class _ConversationScreenState extends State<ConversationsListScreen> {
|
2019-04-23 12:35:41 +00:00
|
|
|
final ConversationsHelper _conversationsHelper = ConversationsHelper();
|
2019-04-23 15:29:58 +00:00
|
|
|
final UsersHelper _usersHelper = UsersHelper();
|
|
|
|
ConversationsList _list;
|
2019-04-23 12:35:41 +00:00
|
|
|
LoadErrorLevel _error = LoadErrorLevel.NONE;
|
|
|
|
bool _loading = true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
_loadConversations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setError(LoadErrorLevel err) => setState(() => _error = err);
|
|
|
|
|
|
|
|
void setLoading(bool loading) => setState(() => _loading = loading);
|
|
|
|
|
2019-04-24 13:03:36 +00:00
|
|
|
/// Get the list of conversations, once from the cache, once from the server
|
|
|
|
Future<void> _loadConversations() async {
|
|
|
|
await _loadConversationsList(true);
|
|
|
|
await _loadConversationsList(false);
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:29:58 +00:00
|
|
|
void _gotLoadingError() {
|
2019-04-23 12:35:41 +00:00
|
|
|
setLoading(false);
|
|
|
|
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load the list of conversations
|
2019-04-24 13:03:36 +00:00
|
|
|
Future<void> _loadConversationsList(bool cached) async {
|
2019-04-23 12:35:41 +00:00
|
|
|
setError(LoadErrorLevel.NONE);
|
|
|
|
setLoading(true);
|
|
|
|
|
2019-04-24 13:03:36 +00:00
|
|
|
//Get the list of conversations
|
|
|
|
var list;
|
2019-04-27 14:23:08 +00:00
|
|
|
if (cached)
|
2019-04-24 13:03:36 +00:00
|
|
|
list = await _conversationsHelper.getCachedList();
|
|
|
|
else
|
|
|
|
list = await _conversationsHelper.downloadList();
|
|
|
|
|
2019-04-23 15:29:58 +00:00
|
|
|
if (list == null) return _gotLoadingError();
|
2019-04-23 12:35:41 +00:00
|
|
|
|
2019-04-23 15:29:58 +00:00
|
|
|
//Get information about the members of the conversations
|
2019-04-24 09:33:58 +00:00
|
|
|
list.users = await _usersHelper.getUsersInfo(list.allUsersID);
|
2019-04-24 13:03:36 +00:00
|
|
|
if (list.users == null) return _gotLoadingError();
|
2019-04-23 12:35:41 +00:00
|
|
|
|
|
|
|
//Save list
|
2019-04-23 15:29:58 +00:00
|
|
|
setState(() {
|
|
|
|
_list = list;
|
|
|
|
});
|
2019-04-23 12:35:41 +00:00
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build an error card
|
|
|
|
Widget _buildErrorCard() {
|
|
|
|
return buildErrorCard(
|
|
|
|
tr("Could not retrieve the list of conversations!"),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
2019-04-25 09:47:48 +00:00
|
|
|
onPressed: () => _loadConversationsList(false),
|
2019-04-23 12:35:41 +00:00
|
|
|
child: Text(
|
|
|
|
tr("Retry").toUpperCase(),
|
|
|
|
style: TextStyle(
|
|
|
|
color: Colors.white,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-24 15:46:25 +00:00
|
|
|
/// Open a conversation
|
2019-04-27 14:23:08 +00:00
|
|
|
void _openConversation(BuildContext context, int conversationId) {
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (c) {
|
|
|
|
return ConversationRoute(
|
|
|
|
conversationID: conversationId,
|
|
|
|
);
|
2019-04-24 15:46:25 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-04-27 14:23:08 +00:00
|
|
|
/// Create a new conversation
|
|
|
|
void _createConversation(BuildContext context) {
|
|
|
|
Navigator.of(context)
|
|
|
|
.push(MaterialPageRoute(builder: (c) => CreateConversationRoute()));
|
|
|
|
}
|
|
|
|
|
2019-04-23 12:35:41 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (_error == LoadErrorLevel.MAJOR) return _buildErrorCard();
|
|
|
|
if (_list == null) return buildCenteredProgressBar();
|
|
|
|
|
|
|
|
// Show the list of conversations
|
2019-04-24 13:03:36 +00:00
|
|
|
return Stack(
|
|
|
|
children: <Widget>[
|
|
|
|
Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null,
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
2019-04-24 15:46:25 +00:00
|
|
|
controller: ScrollController(),
|
2019-04-24 13:03:36 +00:00
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return ConversationTile(
|
|
|
|
conversation: _list.elementAt(index),
|
|
|
|
usersList: _list.users,
|
2019-04-27 14:23:08 +00:00
|
|
|
onOpen: (c) {
|
|
|
|
_openConversation(context, c.id);
|
|
|
|
},
|
2019-04-24 13:03:36 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
itemCount: _list.length,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2019-04-27 14:23:08 +00:00
|
|
|
|
|
|
|
// Add conversation button
|
|
|
|
Positioned(
|
|
|
|
right: 20.0,
|
|
|
|
bottom: 20.0,
|
|
|
|
child: FloatingActionButton(
|
|
|
|
onPressed: () => _createConversation(context),
|
|
|
|
child: Icon(Icons.add),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
// Loading indicator
|
2019-04-24 13:03:36 +00:00
|
|
|
Positioned(
|
|
|
|
top: 8.0,
|
|
|
|
left: 0.0,
|
|
|
|
right: 0.0,
|
|
|
|
child: Container(
|
|
|
|
child: !_loading
|
|
|
|
? null
|
|
|
|
: Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2019-04-23 12:35:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|