1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 14:59:22 +00:00
comunicmobile/lib/ui/screens/conversations_screen.dart

97 lines
2.7 KiB
Dart
Raw Normal View History

2019-04-23 12:35:41 +00:00
import 'package:comunic/enums/load_error_level.dart';
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversations_list.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
class ConversationsScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _ConversationScreenState();
}
class _ConversationScreenState extends State<ConversationsScreen> {
final ConversationsHelper _conversationsHelper = ConversationsHelper();
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);
void _gotLoadingError() {
2019-04-23 12:35:41 +00:00
setLoading(false);
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
}
/// Load the list of conversations
Future<void> _loadConversations() async {
setError(LoadErrorLevel.NONE);
setLoading(true);
//Process the list of conversations
2019-04-23 12:35:41 +00:00
final list = await _conversationsHelper.downloadList();
if (list == null) return _gotLoadingError();
2019-04-23 12:35:41 +00:00
//Get information about the members of the conversations
list.users = await _usersHelper.downloadInfo(list.allUsersID);
if(list.users == null) return _gotLoadingError();
2019-04-23 12:35:41 +00:00
//Save list
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(
onPressed: _loadConversations,
child: Text(
tr("Retry").toUpperCase(),
style: TextStyle(
color: Colors.white,
),
),
)
],
);
}
@override
Widget build(BuildContext context) {
if (_error == LoadErrorLevel.MAJOR) return _buildErrorCard();
if (_list == null) return buildCenteredProgressBar();
// Show the list of conversations
return ListView.builder(
itemBuilder: (context, index) {
return ConversationTile(
conversation: _list.elementAt(index),
usersList: _list.users,
2019-04-23 12:35:41 +00:00
);
},
itemCount: _list.length,
);
}
}