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

Automatically refresh the list of memberships

This commit is contained in:
Pierre HUBERT 2020-05-06 18:16:57 +02:00
parent 6961694fd2
commit f31323fe23
2 changed files with 11 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
abstract class SafeState<T extends StatefulWidget> extends State<T> { abstract class SafeState<T extends StatefulWidget> extends State<T> {
final _subscriptions = List<StreamSubscription>(); final _subscriptions = List<StreamSubscription>();
final _timers = List<Timer>();
bool _unmounted = false; bool _unmounted = false;
@ -19,6 +20,9 @@ abstract class SafeState<T extends StatefulWidget> extends State<T> {
// Close subscriptions // Close subscriptions
_subscriptions.forEach((f) => f.cancel()); _subscriptions.forEach((f) => f.cancel());
// Stop intervals
_timers.forEach((f) => f.cancel());
super.dispose(); super.dispose();
} }
@ -52,4 +56,10 @@ abstract class SafeState<T extends StatefulWidget> extends State<T> {
if (!_unmounted) cb(); if (!_unmounted) cb();
}); });
} }
/// Safely mimic the setInterval javascript function
void setInterval(int secs, void Function(Timer) cb) {
final timer = Timer.periodic(Duration(seconds: secs), cb);
_timers.add(timer);
}
} }

View File

@ -28,7 +28,6 @@ class MembershipsPanel extends StatefulWidget {
const _MembershipIconsWidth = 30.0; const _MembershipIconsWidth = 30.0;
/// TODO : add auto-refresh
class _MembershipsPanelState extends SafeState<MembershipsPanel> { class _MembershipsPanelState extends SafeState<MembershipsPanel> {
final _refreshKey = GlobalKey<RefreshIndicatorState>(); final _refreshKey = GlobalKey<RefreshIndicatorState>();
MembershipList _membershipList; MembershipList _membershipList;
@ -48,13 +47,13 @@ class _MembershipsPanelState extends SafeState<MembershipsPanel> {
}); });
} catch (e, s) { } catch (e, s) {
print("Could not load the list of memberships! $e\n$s"); print("Could not load the list of memberships! $e\n$s");
setTimeout(5, _refresh);
} }
} }
@override @override
void initState() { void initState() {
super.initState(); super.initState();
setInterval(20, (t) => _refresh());
_refresh(); _refresh();
} }