Skip to content

Instantly share code, notes, and snippets.

@wess
Created Jun 4, 2021
Embed
What would you like to do?
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