From f31323fe2359c08004fce5ab5a9744e8e2088b37 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 6 May 2020 18:16:57 +0200 Subject: [PATCH] Automatically refresh the list of memberships --- lib/ui/widgets/safe_state.dart | 10 ++++++++++ lib/ui/widgets/tablet_mode/memberships_panel.dart | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/ui/widgets/safe_state.dart b/lib/ui/widgets/safe_state.dart index e32887b..ce32488 100644 --- a/lib/ui/widgets/safe_state.dart +++ b/lib/ui/widgets/safe_state.dart @@ -9,6 +9,7 @@ import 'package:flutter/material.dart'; abstract class SafeState extends State { final _subscriptions = List(); + final _timers = List(); bool _unmounted = false; @@ -19,6 +20,9 @@ abstract class SafeState extends State { // Close subscriptions _subscriptions.forEach((f) => f.cancel()); + // Stop intervals + _timers.forEach((f) => f.cancel()); + super.dispose(); } @@ -52,4 +56,10 @@ abstract class SafeState extends State { 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); + } } diff --git a/lib/ui/widgets/tablet_mode/memberships_panel.dart b/lib/ui/widgets/tablet_mode/memberships_panel.dart index 539b33b..2ef5794 100644 --- a/lib/ui/widgets/tablet_mode/memberships_panel.dart +++ b/lib/ui/widgets/tablet_mode/memberships_panel.dart @@ -28,7 +28,6 @@ class MembershipsPanel extends StatefulWidget { const _MembershipIconsWidth = 30.0; -/// TODO : add auto-refresh class _MembershipsPanelState extends SafeState { final _refreshKey = GlobalKey(); MembershipList _membershipList; @@ -48,13 +47,13 @@ class _MembershipsPanelState extends SafeState { }); } catch (e, s) { print("Could not load the list of memberships! $e\n$s"); - setTimeout(5, _refresh); } } @override void initState() { super.initState(); + setInterval(20, (t) => _refresh()); _refresh(); }