From 12ec4c243ff654d2d5bc2025fabc338697492467 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 3 Aug 2020 01:04:14 +0200 Subject: [PATCH] Graph Layout: Minor refactor --- lib/main_graph.dart | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/main_graph.dart b/lib/main_graph.dart index ac761eb4..10a88f02 100644 --- a/lib/main_graph.dart +++ b/lib/main_graph.dart @@ -36,17 +36,7 @@ class _MyExampleWidgetState extends State { graph.edges = edges; graph.assignRandomPositions(400, 650); - - const interval = Duration(milliseconds: 25); - bool shouldStop = false; - var timer = Timer.periodic(interval, (Timer t) { - if (shouldStop) { - return; - } - shouldStop = updateGraphPositions(graph); - }); - - Timer(const Duration(seconds: 5), () => timer.cancel()); + graph.startLayout(); } @override @@ -230,6 +220,30 @@ class Graph extends ChangeNotifier { notifyListeners(); } + + Timer layoutTimer; + + void startLayout() { + if (layoutTimer != null) { + return; + } + + const interval = Duration(milliseconds: 25); + layoutTimer = Timer.periodic(interval, (Timer t) { + bool shouldStop = updateGraphPositions(this); + if (shouldStop) { + layoutTimer.cancel(); + layoutTimer = null; + } + }); + + Timer(const Duration(seconds: 5), () { + if (layoutTimer != null) { + layoutTimer.cancel(); + layoutTimer = null; + } + }); + } } void main() => runApp(MyApp());