1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00
comunicmobile/lib/ui/screens/conversations_list_screen.dart

208 lines
6.4 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/events_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversations_list.dart';
import 'package:comunic/models/conversation.dart';
2020-04-17 13:26:37 +00:00
import 'package:comunic/ui/routes/main_route.dart';
import 'package:comunic/ui/routes/update_conversation_route.dart';
import 'package:comunic/ui/screens/create_conversation_screen.dart';
2019-04-23 12:35:41 +00:00
import 'package:comunic/ui/tiles/conversation_tile.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
2019-04-23 12:35:41 +00:00
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();
}
class _ConversationScreenState extends SafeState<ConversationsListScreen> {
2019-04-23 12:35:41 +00:00
final ConversationsHelper _conversationsHelper = ConversationsHelper();
final UsersHelper _usersHelper = UsersHelper();
ConversationsList _list;
2019-04-23 12:35:41 +00:00
LoadErrorLevel _error = LoadErrorLevel.NONE;
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
2019-04-23 12:35:41 +00:00
@override
void initState() {
super.initState();
this.listen<NewNumberUnreadConversations>(
(d) => _refreshIndicatorKey.currentState.show());
2019-04-23 12:35:41 +00:00
}
2019-05-01 15:10:23 +00:00
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadConversations();
2019-05-01 15:10:23 +00:00
}
2019-04-23 12:35:41 +00:00
void setError(LoadErrorLevel err) => setState(() => _error = err);
/// Get the list of conversations, once from the cache, once from the server
Future<void> _loadConversations() async {
await _loadConversationsList(true);
await _loadConversationsList(false);
}
void _gotLoadingError() {
2019-04-23 12:35:41 +00:00
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
}
/// Load the list of conversations
Future<void> _loadConversationsList(bool cached) async {
2019-04-23 12:35:41 +00:00
setError(LoadErrorLevel.NONE);
//Get the list of conversations
var list;
2019-04-27 14:23:08 +00:00
if (cached)
list = await _conversationsHelper.getCachedList();
else
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
2019-04-24 09:33:58 +00:00
list.users = await _usersHelper.getUsersInfo(list.allUsersID);
2019-05-01 15:10:23 +00:00
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
}
/// Build an error card
Widget _buildErrorCard() {
return buildErrorCard(
tr("Could not retrieve the list of conversations!"),
actions: <Widget>[
FlatButton(
onPressed: () => _refreshIndicatorKey.currentState.show(),
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
void _openConversation(int conversationId) {
2020-04-17 13:26:37 +00:00
MainController.of(context).openConversation(conversationId);
2019-04-24 15:46:25 +00:00
}
2019-04-27 14:23:08 +00:00
/// Create a new conversation
void _createConversation() {
2020-04-17 13:26:37 +00:00
MainController.of(context).push(CreateConversationScreen());
2019-04-27 14:23:08 +00:00
}
/// Handle conversations updated requests
void _updateConversation(Conversation conversation) {
2020-04-17 13:26:37 +00:00
MainController.of(context).push(
UpdateConversationRoute(
conversationID: conversation.id,
),
);
}
/// Handle conversation deletion request
Future<void> _requestDeleteConversation(Conversation conversation) async {
final result = await showDialog<bool>(
context: context,
builder: (c) {
return AlertDialog(
title: Text(tr("Delete conversation")),
content: Text(tr(
"Do you really want to remove this conversation from your list of conversations ? If you are the owner of this conversation, it will be completely deleted!")),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text(tr("cancel").toUpperCase()),
),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text(
tr("delete").toUpperCase(),
style: TextStyle(color: Colors.red),
),
)
],
);
},
);
if (result == null || !result) return;
// Request the conversation to be deleted now
if (!await _conversationsHelper.deleteConversation(conversation.id))
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(tr("Could not delete the conversation!"))));
// Reload the list of conversations
_loadConversationsList(false);
}
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
return Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null,
),
Expanded(
child: RefreshIndicator(
onRefresh: () => _loadConversationsList(false),
key: _refreshIndicatorKey,
child: ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
controller: ScrollController(),
itemBuilder: (context, index) {
return ConversationTile(
conversation: _list.elementAt(index),
usersList: _list.users,
onOpen: (c) {
_openConversation(c.id);
},
onRequestUpdate: _updateConversation,
onRequestDelete: _requestDeleteConversation,
);
},
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(),
2019-04-27 14:23:08 +00:00
child: Icon(Icons.add),
),
),
],
2019-04-23 12:35:41 +00:00
);
}
}