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:
parent
1f0d70848f
commit
b06c28dc87
@ -25,7 +25,8 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
final UsersHelper _usersHelper = UsersHelper();
|
||||
ConversationsList _list;
|
||||
LoadErrorLevel _error = LoadErrorLevel.NONE;
|
||||
bool _loading = true;
|
||||
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
|
||||
GlobalKey<RefreshIndicatorState>();
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
@ -35,14 +36,11 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
|
||||
@override
|
||||
void setState(fn) {
|
||||
if(mounted)
|
||||
super.setState(fn);
|
||||
if (mounted) super.setState(fn);
|
||||
}
|
||||
|
||||
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
|
||||
Future<void> _loadConversations() async {
|
||||
await _loadConversationsList(true);
|
||||
@ -50,14 +48,12 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
}
|
||||
|
||||
void _gotLoadingError() {
|
||||
setLoading(false);
|
||||
setError(_list == null ? LoadErrorLevel.MAJOR : LoadErrorLevel.MINOR);
|
||||
}
|
||||
|
||||
/// Load the list of conversations
|
||||
Future<void> _loadConversationsList(bool cached) async {
|
||||
setError(LoadErrorLevel.NONE);
|
||||
setLoading(true);
|
||||
|
||||
//Get the list of conversations
|
||||
var list;
|
||||
@ -73,13 +69,10 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
|
||||
if (list.users == null) return _gotLoadingError();
|
||||
|
||||
|
||||
//Save list
|
||||
setState(() {
|
||||
_list = list;
|
||||
});
|
||||
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
/// Build an error card
|
||||
@ -88,7 +81,7 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
tr("Could not retrieve the list of conversations!"),
|
||||
actions: <Widget>[
|
||||
FlatButton(
|
||||
onPressed: () => _loadConversationsList(false),
|
||||
onPressed: () => _refreshIndicatorKey.currentState.show(),
|
||||
child: Text(
|
||||
tr("Retry").toUpperCase(),
|
||||
style: TextStyle(
|
||||
@ -177,7 +170,11 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
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(
|
||||
@ -193,6 +190,7 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
itemCount: _list.length,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@ -205,20 +203,6 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
|
||||
// Loading indicator
|
||||
Positioned(
|
||||
top: 8.0,
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
child: Container(
|
||||
child: !_loading
|
||||
? null
|
||||
: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user