1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00

Use RefreshIndicator for conversations list

This commit is contained in:
Pierre HUBERT 2019-05-04 09:21:40 +02:00
parent 1f0d70848f
commit b06c28dc87

View File

@ -25,7 +25,8 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
final UsersHelper _usersHelper = UsersHelper(); final UsersHelper _usersHelper = UsersHelper();
ConversationsList _list; ConversationsList _list;
LoadErrorLevel _error = LoadErrorLevel.NONE; LoadErrorLevel _error = LoadErrorLevel.NONE;
bool _loading = true; final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
@override @override
void didChangeDependencies() { void didChangeDependencies() {
@ -35,14 +36,11 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
@override @override
void setState(fn) { void setState(fn) {
if(mounted) if (mounted) super.setState(fn);
super.setState(fn);
} }
void setError(LoadErrorLevel err) => setState(() => _error = err); void setError(LoadErrorLevel err) => setState(() => _error = err);
void setLoading(bool loading) => setState(() => _loading = loading);
/// Get the list of conversations, once from the cache, once from the server /// Get the list of conversations, once from the cache, once from the server
Future<void> _loadConversations() async { Future<void> _loadConversations() async {
await _loadConversationsList(true); await _loadConversationsList(true);
@ -50,14 +48,12 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
} }
void _gotLoadingError() { void _gotLoadingError() {
setLoading(false);
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR); setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
} }
/// Load the list of conversations /// Load the list of conversations
Future<void> _loadConversationsList(bool cached) async { Future<void> _loadConversationsList(bool cached) async {
setError(LoadErrorLevel.NONE); setError(LoadErrorLevel.NONE);
setLoading(true);
//Get the list of conversations //Get the list of conversations
var list; var list;
@ -73,13 +69,10 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
if (list.users == null) return _gotLoadingError(); if (list.users == null) return _gotLoadingError();
//Save list //Save list
setState(() { setState(() {
_list = list; _list = list;
}); });
setLoading(false);
} }
/// Build an error card /// Build an error card
@ -88,7 +81,7 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
tr("Could not retrieve the list of conversations!"), tr("Could not retrieve the list of conversations!"),
actions: <Widget>[ actions: <Widget>[
FlatButton( FlatButton(
onPressed: () => _loadConversationsList(false), onPressed: () => _refreshIndicatorKey.currentState.show(),
child: Text( child: Text(
tr("Retry").toUpperCase(), tr("Retry").toUpperCase(),
style: TextStyle( style: TextStyle(
@ -177,20 +170,25 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null, child: _error == LoadErrorLevel.MINOR ? _buildErrorCard() : null,
), ),
Expanded( Expanded(
child: ListView.builder( child: RefreshIndicator(
controller: ScrollController(), onRefresh: () => _loadConversationsList(false),
itemBuilder: (context, index) { key: _refreshIndicatorKey,
return ConversationTile( child: ListView.builder(
conversation: _list.elementAt(index), physics: AlwaysScrollableScrollPhysics(),
usersList: _list.users, controller: ScrollController(),
onOpen: (c) { itemBuilder: (context, index) {
_openConversation(context, c.id); return ConversationTile(
}, conversation: _list.elementAt(index),
onRequestUpdate: _updateConversation, usersList: _list.users,
onRequestDelete: _requestDeleteConversation, onOpen: (c) {
); _openConversation(context, c.id);
}, },
itemCount: _list.length, onRequestUpdate: _updateConversation,
onRequestDelete: _requestDeleteConversation,
);
},
itemCount: _list.length,
),
), ),
), ),
], ],
@ -205,20 +203,6 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
child: Icon(Icons.add), child: Icon(Icons.add),
), ),
), ),
// Loading indicator
Positioned(
top: 8.0,
left: 0.0,
right: 0.0,
child: Container(
child: !_loading
? null
: Center(
child: CircularProgressIndicator(),
),
),
)
], ],
); );
} }