GraphView: Dispose the Graph correctly

This commit is contained in:
Vishesh Handa
2020-09-28 00:48:18 +02:00
parent dc4e18156c
commit 48b9b4d642
2 changed files with 23 additions and 5 deletions

View File

@ -174,8 +174,7 @@ class Graph extends ChangeNotifier {
bool shouldStop = _updateGraphPositions(this); bool shouldStop = _updateGraphPositions(this);
// print("shouldStop $shouldStop"); // print("shouldStop $shouldStop");
if (shouldStop) { if (shouldStop) {
layoutTimer.cancel(); stopLayout();
layoutTimer = null;
} }
}); });
@ -187,6 +186,11 @@ class Graph extends ChangeNotifier {
} }
});*/ });*/
} }
void stopLayout() {
layoutTimer.cancel();
layoutTimer = null;
}
} }
class GraphNodeLayout { class GraphNodeLayout {

View File

@ -20,15 +20,29 @@ class _GraphViewScreenState extends State<GraphViewScreen> {
var rootFolder = Provider.of<NotesFolderFS>(context); var rootFolder = Provider.of<NotesFolderFS>(context);
setState(() { setState(() {
graph = Graph.fromFolder(rootFolder); graph = Graph.fromFolder(rootFolder);
graph.addListener(() { graph.addListener(_setState);
setState(() {});
});
}); });
return Container(width: 2500, height: 2500); return Container(width: 2500, height: 2500);
} }
return SafeArea(child: GraphView(graph)); return SafeArea(child: GraphView(graph));
} }
@override
void dispose() {
if (graph != null) {
graph.stopLayout();
graph.removeListener(_setState);
}
super.dispose();
}
void _setState() {
if (!mounted) return;
setState(() {});
}
} }
class GraphView extends StatefulWidget { class GraphView extends StatefulWidget {