Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
baumths committed May 2, 2024
1 parent a34e1c7 commit 5bdce26
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions example/lib/src/examples/restorable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@ import 'package:flutter_fancy_tree_view/flutter_fancy_tree_view.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../settings/controller.dart';
const String expandedIdsKey = 'fftv.restorableTreeController.expandedNodeIds';

class MyTreeController extends TreeController<Node> {
MyTreeController({
class RestorableTreeController extends TreeController<Node> {
RestorableTreeController({
required super.roots,
required this.expandedIds,
}) : super(childrenProvider: (Node node) => node.children);

final Set<String> expandedIds;
late final Set<String> expandedNodeIds = <String>{};

void restoreExpandedNodeIds(SharedPreferences prefs) {
final List<String>? expandedIds = prefs.getStringList(expandedIdsKey);

if (expandedIds != null) {
expandedNodeIds.addAll(expandedIds);
}
}

void persistExpandedNodeIds(SharedPreferences prefs) {
prefs.setStringList(expandedIdsKey, expandedNodeIds.toList());
}

@override
bool getExpansionState(Node node) => expandedIds.contains(node.id);
bool getExpansionState(Node node) => expandedNodeIds.contains(node.id);

@override
void setExpansionState(Node node, bool expanded) {
expanded ? expandedIds.add(node.id) : expandedIds.remove(node.id);
expanded ? expandedNodeIds.add(node.id) : expandedNodeIds.remove(node.id);
}
}

Expand All @@ -27,15 +38,6 @@ class Node {

final String id;
final List<Node> children;

@override
int get hashCode => id.hashCode;

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Node && other.id == id;
}
}

class RestorableTreeView extends StatefulWidget {
Expand All @@ -46,10 +48,8 @@ class RestorableTreeView extends StatefulWidget {
}

class _RestorableTreeViewState extends State<RestorableTreeView> {
static const String expandedIdsKey = 'fftv.restorableTreeView.expandedIds';

late final MyTreeController treeController;
late final SharedPreferences prefs = context.read<SettingsController>().prefs;
late final RestorableTreeController treeController;
late final SharedPreferences prefs = context.read<SharedPreferences>();

final Node root = Node(id: '0', children: [
Node(id: '1', children: [
Expand All @@ -73,20 +73,13 @@ class _RestorableTreeViewState extends State<RestorableTreeView> {
@override
void initState() {
super.initState();
treeController = MyTreeController(
roots: <Node>[root],
// Restore expanded ids
expandedIds: <String>{...?prefs.getStringList(expandedIdsKey)},
);
treeController = RestorableTreeController(roots: <Node>[root]);
treeController.restoreExpandedNodeIds(prefs);
}

@override
void dispose() {
// Persist expanded ids
prefs.setStringList(
expandedIdsKey,
treeController.expandedIds.toList(),
);
treeController.persistExpandedNodeIds(prefs);
treeController.dispose();
super.dispose();
}
Expand Down

0 comments on commit 5bdce26

Please sign in to comment.