Created
June 4, 2021 18:16
-
-
Save wess/d707a5c30657bb398412efc91da9e400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:bastion/providers/data.dart'; | |
| import 'package:fluentui_system_icons/fluentui_system_icons.dart'; | |
| import 'package:flutter/foundation.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:hive/hive.dart'; | |
| import 'package:window_size/window_size.dart'; | |
| enum NavOption { | |
| converter, | |
| profiles, | |
| settings, | |
| } | |
| extension NavOptionExt on NavOption { | |
| String get name { | |
| switch(this) { | |
| case NavOption.converter: | |
| return 'Converter'; | |
| case NavOption.profiles: | |
| return 'Profiles'; | |
| case NavOption.settings: | |
| return 'Settings'; | |
| default: | |
| throw Exception("No name for unknown route"); | |
| } | |
| } | |
| IconData get icon { | |
| switch(this) { | |
| case NavOption.converter: | |
| return FluentIcons.arrow_swap_24_regular; | |
| case NavOption.profiles: | |
| return FluentIcons.people_team_32_regular; | |
| case NavOption.settings: | |
| return FluentIcons.settings_28_regular; | |
| default: | |
| throw Exception("No icon for unknown route"); | |
| } | |
| } | |
| static NavOption load(String name) { | |
| switch(name) { | |
| case 'Converter': | |
| return NavOption.converter; | |
| case 'Profiles': | |
| return NavOption.profiles; | |
| case 'Settings': | |
| return NavOption.settings; | |
| default: | |
| throw Exception("No route of that name found"); | |
| } | |
| } | |
| } | |
| class NavigationProvider with ChangeNotifier { | |
| static List<NavOption> routes = [ | |
| NavOption.converter, | |
| NavOption.profiles, | |
| NavOption.settings, | |
| ]; | |
| final _box = Hive.box(DataBox.navigation.name); | |
| // State. | |
| int _current = 0; | |
| int get current => _current; | |
| bool _showingDrawer = false; | |
| bool get showingDrawer => _showingDrawer; | |
| // Methods. | |
| NavigationProvider() { | |
| String? cached = _box.get('current'); | |
| final current = cached == null ? NavigationProvider.routes[_current] : NavOptionExt.load(cached); | |
| _current = routes.indexOf(current); | |
| setWindowTitle('Bastion : ${current.name}'); | |
| } | |
| void move(NavOption to) { | |
| final incoming = NavigationProvider.routes.indexOf(to); | |
| if(_current == incoming) { | |
| return; | |
| } | |
| _showingDrawer = false; | |
| _current = incoming; | |
| _box.put('current', to.name); | |
| setWindowTitle('Bastion : ${to.name}'); | |
| notifyListeners(); | |
| } | |
| void showDrawer(bool show) { | |
| _showingDrawer = show; | |
| notifyListeners(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment