diff --git a/lib/core/features/action.dart b/lib/core/features/action.dart index 08d7a51..61c09bb 100644 --- a/lib/core/features/action.dart +++ b/lib/core/features/action.dart @@ -14,10 +14,15 @@ enum MUDActionTarget { class MUDAction { String content; + Automation? parent; MUDActionTarget target; - MUDAction(this.content, {this.target = MUDActionTarget.execute}); + MUDAction( + this.content, { + this.target = MUDActionTarget.execute, + this.parent, + }); - void invoke(GameStore store, Automation parent, List matches) { + void invoke(GameStore store, List matches) { debugPrint('MUDAction.invoke: ${this.content}, $matches'); var content = this.content; for (var i = 0; i < matches.length; i++) { @@ -30,14 +35,14 @@ class MUDAction { case MUDActionTarget.world: debugPrint('ActionSendTo.world: $content'); store.send(content); - if (!parent.isRemovedFromBuffer) { + if (parent != null && !parent!.isRemovedFromBuffer) { store.echoOwn(content); } break; case MUDActionTarget.execute: debugPrint('ActionSendTo.execute: $content'); store.execute(content); - if (!parent.isRemovedFromBuffer) { + if (parent != null && !parent!.isRemovedFromBuffer) { store.echoOwn(content); } break; @@ -84,7 +89,7 @@ class MUDAction { // variables from the store // TODO allow disabling this - for (final vari in store.variables.values) { + for (final vari in store.currentProfile.variables.values) { content = content.replaceAll('%${vari.name}', vari.value); } return content; @@ -95,11 +100,12 @@ class NativeMUDAction extends MUDAction { NativeMUDAction(this.customInvoke) : super('-- native code --', target: MUDActionTarget.script); - final void Function(GameStore store, Automation parent, List matches) + final void Function(GameStore store, List matches) customInvoke; @override - void invoke(GameStore store, Automation parent, List matches) { - customInvoke(store, parent, matches); + void invoke(GameStore store, List matches) { + customInvoke(store, matches); } } + diff --git a/lib/core/features/alias.dart b/lib/core/features/alias.dart index a2effa5..fb2b0f5 100644 --- a/lib/core/features/alias.dart +++ b/lib/core/features/alias.dart @@ -97,7 +97,7 @@ final builtInAliases = [ id: _key('help'), pattern: 'mudhelp', action: NativeMUDAction( - (store, parent, matches) { + (store, matches) { store.echo(BuiltinCommand.help()); }, ), diff --git a/lib/core/features/automation.dart b/lib/core/features/automation.dart index 1aabe94..3542854 100644 --- a/lib/core/features/automation.dart +++ b/lib/core/features/automation.dart @@ -102,7 +102,7 @@ class Automation { void invokeEffect(GameStore store, String line) { invokeCount++; - action.invoke(store, this, allMatches(line)); + action.invoke(store, allMatches(line)); } List allMatches(String str) { diff --git a/lib/core/features/game_button.dart b/lib/core/features/game_button.dart index 08ad21b..9371bf3 100644 --- a/lib/core/features/game_button.dart +++ b/lib/core/features/game_button.dart @@ -420,7 +420,7 @@ class _GameButtonState extends State with GameStoreStateMixin { void _callAction(MUDAction? action) { final act = action ?? data.getActionOrDefault(GameButtonInteraction.press); - act.invoke(store, parentAutomation, []); + act.invoke(store, []); } void _callCurrentDirection() { diff --git a/lib/core/keyboard_shortcuts.dart b/lib/core/features/keyboard_shortcuts.dart similarity index 99% rename from lib/core/keyboard_shortcuts.dart rename to lib/core/features/keyboard_shortcuts.dart index 25b259e..0559364 100644 --- a/lib/core/keyboard_shortcuts.dart +++ b/lib/core/features/keyboard_shortcuts.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'store.dart'; +import '../store.dart'; class KeyboardIntent extends Intent { const KeyboardIntent(this.key); diff --git a/lib/core/features/lua.dart b/lib/core/features/lua.dart index 7b4a007..68b4032 100644 --- a/lib/core/features/lua.dart +++ b/lib/core/features/lua.dart @@ -76,7 +76,7 @@ class LuaBindings { final name = ls.checkString(1)!; ls.pop(1); debugPrint("lua.getVariable $name"); - final vari = store.variables[name]; + final vari = store.currentProfile.variables[name]; if (vari != null) { ls.pushString(vari.value); } else { @@ -90,12 +90,15 @@ class LuaBindings { final value = ls.checkString(2)!; ls.pop(2); debugPrint("lua.setVariable $name, $value"); - if (store.variables[name] == null) { - store.variables[name] = Variable(name, value); + final profile = store.currentProfile; + if (profile.variables[name] == null) { + profile.variables[name] = Variable(name, value); } - store.variables[name]!.value = value; - store.currentProfile - .saveVariable(store.variables.values.toList(), store.variables[name]!); + profile.variables[name]!.value = value; + profile.saveVariable( + profile.variables.values.toList(), + profile.variables[name]!, + ); return 0; } } @@ -110,11 +113,11 @@ class LuaAliasBindings extends LuaAutomationBindings { @override List getGroup(String group) => - store.aliases.where((alias) => alias.group == group).toList(); + store.currentProfile.aliases.where((alias) => alias.group == group).toList(); @override Alias getSingle(String label) => - store.aliases.firstWhere((alias) => alias.label == label); + store.currentProfile.aliases.firstWhere((alias) => alias.label == label); @override Future saveGroup(List items, bool state) async { @@ -122,14 +125,14 @@ class LuaAliasBindings extends LuaAutomationBindings { i.enabled = state; return store.currentProfile.saveAlias(i); })); - return store.loadAliases(); + return store.currentProfile.getAliases(); } @override Future saveSingle(Alias item, bool state) async { item.enabled = state; await store.currentProfile.saveAlias(item); - return store.loadAliases(); + return store.currentProfile.getAliases(); } } @@ -143,11 +146,11 @@ class LuaTriggerBindings extends LuaAutomationBindings { @override List getGroup(String group) => - store.triggers.where((alias) => alias.group == group).toList(); + store.currentProfile.triggers.where((alias) => alias.group == group).toList(); @override Trigger getSingle(String label) => - store.triggers.firstWhere((alias) => alias.label == label); + store.currentProfile.triggers.firstWhere((alias) => alias.label == label); @override Future saveGroup(List items, bool state) async { @@ -155,14 +158,14 @@ class LuaTriggerBindings extends LuaAutomationBindings { i.enabled = state; return store.currentProfile.saveTrigger(i); })); - return store.loadTriggers(); + return store.currentProfile.getTriggers(); } @override Future saveSingle(Trigger item, bool state) async { item.enabled = state; await store.currentProfile.saveTrigger(item); - return store.loadTriggers(); + return store.currentProfile.getTriggers(); } } @@ -176,11 +179,11 @@ class LuaButtonSetBindings extends LuaAutomationBindings { @override List getGroup(String group) => - store.buttonSets.where((alias) => alias.group == group).toList(); + store.currentProfile.buttonSets.where((alias) => alias.group == group).toList(); @override GameButtonSetData getSingle(String label) => - store.buttonSets.firstWhere((alias) => alias.label == label); + store.currentProfile.buttonSets.firstWhere((alias) => alias.label == label); @override Future saveGroup(List items, bool state) async { @@ -188,14 +191,14 @@ class LuaButtonSetBindings extends LuaAutomationBindings { i.enabled = state; return store.currentProfile.saveButtonSet(i); })); - return store.loadTriggers(); + return store.currentProfile.getTriggers(); } @override Future saveSingle(GameButtonSetData item, bool state) async { item.enabled = state; await store.currentProfile.saveButtonSet(item); - return store.loadTriggers(); + return store.currentProfile.getTriggers(); } } diff --git a/lib/core/features/plugin.dart b/lib/core/features/plugin.dart new file mode 100644 index 0000000..bece2c8 --- /dev/null +++ b/lib/core/features/plugin.dart @@ -0,0 +1,213 @@ +import 'package:flutter/foundation.dart'; + +import '../storage.dart'; +import 'alias.dart'; +import 'game_button_set.dart'; +import 'settings.dart'; +import 'trigger.dart'; +import 'variable.dart'; + +class PluginBase extends ChangeNotifier { + final String id; + + final List triggers = []; + final List aliases = []; + final Map variables = {}; + final List buttonSets = []; + + PluginBase(this.id); + + Future load() async { + await Future.wait([ + getAliases(), + getTriggers(), + getVariables(), + getButtonSets(), + ]); + + notifyListeners(); + } + + Future> loadTriggers() async { + debugPrint('MUDProfile.loadTriggers: $id'); + final triggers = await ProfileStorage.listProfileFiles(id, 'triggers'); + final triggerFiles = >[]; + for (final trigger in triggers) { + debugPrint('MUDProfile.loadTriggers: $id/triggers/$trigger'); + final triggerFile = + await ProfileStorage.readProfileFile(id, 'triggers/$trigger'); + if (triggerFile != null) { + triggerFiles.add(triggerFile); + } + } + return triggerFiles.map((e) => Trigger.fromJson(e)).toList(); + } + + Future> loadAliases() async { + debugPrint('MUDProfile.loadAliases: $id'); + final aliases = await ProfileStorage.listProfileFiles(id, 'aliases'); + final aliasFiles = >[]; + for (final alias in aliases) { + debugPrint('MUDProfile.loadAliases: $id/aliases/$alias'); + final aliasFile = + await ProfileStorage.readProfileFile(id, 'aliases/$alias'); + if (aliasFile != null) { + aliasFiles.add(aliasFile); + } + } + return aliasFiles.map((e) => Alias.fromJson(e)).toList(); + } + + Future> loadVariables() async { + debugPrint('MUDProfile.loadVariables: $id'); + final vars = await ProfileStorage.readProfileFile(id, 'vars'); + if (vars == null) { + return []; + } + return (vars['vars'] as List) + .map((e) => Variable.fromJson(e)) + .toList(); + } + + Future> loadButtonSets() async { + debugPrint('MUDProfile.loadButtonSets: $id'); + final buttonSets = await ProfileStorage.listProfileFiles(id, 'button_sets'); + final buttonSetFiles = >[]; + for (final buttonSet in buttonSets) { + debugPrint('MUDProfile.loadButtonSets: $id/buttonSets/$buttonSet'); + final buttonSetFile = + await ProfileStorage.readProfileFile(id, 'button_sets/$buttonSet'); + if (buttonSetFile != null) { + buttonSetFiles.add(buttonSetFile); + } + } + return buttonSetFiles.map((e) => GameButtonSetData.fromJson(e)).toList(); + } + + + Future loadSettings() async { + debugPrint('MUDProfile.loadSettings: $id'); + final settings = await ProfileStorage.readProfileFile(id, 'settings'); + if (settings == null) { + return Settings.empty(); + } + return Settings.fromJson(settings); + } + + Future saveAlias(Alias alias) async { + debugPrint('MUDProfile.saveAlias: $id/aliases/${alias.id}'); + return ProfileStorage.writeProfileFile( + id, 'aliases/${alias.id}', alias.toJson()); + } + + Future deleteAlias(Alias alias) async { + debugPrint('MUDProfile.deleteAlias: $id/aliases/${alias.id}'); + return ProfileStorage.deleteProfileFile(id, 'aliases/${alias.id}'); + } + + Future saveTrigger(Trigger trigger) async { + debugPrint('MUDProfile.saveTrigger: $id/triggers/${trigger.id}'); + return ProfileStorage.writeProfileFile( + id, 'triggers/${trigger.id}', trigger.toJson()); + } + + Future deleteTrigger(Trigger trigger) async { + debugPrint('MUDProfile.deleteTrigger: $id/triggers/${trigger.id}'); + return ProfileStorage.deleteProfileFile(id, 'triggers/${trigger.id}'); + } + + Future saveButtonSet(GameButtonSetData buttonSet) async { + debugPrint('MUDProfile.saveButtonSet: $id/button_sets/${buttonSet.id}'); + return ProfileStorage.writeProfileFile( + id, 'button_sets/${buttonSet.id}', buttonSet.toJson()); + } + + Future saveSettings(Settings settings) async { + debugPrint('MUDProfile.saveSettings: $id'); + return ProfileStorage.writeProfileFile(id, 'settings', settings.toJson()); + } + + Future deleteButtonSet(GameButtonSetData buttonSet) async { + debugPrint('MUDProfile.deleteButtonSet: $id/button_sets/${buttonSet.id}'); + return ProfileStorage.deleteProfileFile(id, 'button_sets/${buttonSet.id}'); + } + + Future saveVariable(List current, Variable update) async { + debugPrint('MUDProfile.saveVariable: $id/vars'); + final existing = current.indexWhere( + (v) => v.name == update.name, + ); + if (existing >= 0) { + current[existing] = update; + } else { + current.add(update); + } + return ProfileStorage.writeProfileFile( + id, + 'vars', + {'vars': current.map((v) => v.toJson()).toList()}, + ); + } + + Future deleteVariable(List current, Variable update) async { + debugPrint('MUDProfile.deleteVariable: $id/vars'); + final existing = current.indexWhere( + (v) => v.name == update.name, + ); + if (existing >= 0) { + current.removeAt(existing); + } + return ProfileStorage.writeProfileFile( + id, + 'vars', + {'vars': current.map((v) => v.toJson()).toList()}, + ); + } + + Future getTriggers() async { + debugPrint('loadTriggers'); + final list = await loadTriggers(); + triggers.clear(); + triggers.addAll(list); + notifyListeners(); + debugPrint('Triggers: ${triggers.length}'); + } + + Future getAliases() async { + final list = await loadAliases(); + aliases.clear(); + aliases.addAll(list); + notifyListeners(); + debugPrint('Aliases: ${aliases.length}'); + } + + Future getVariables() async { + final list = await loadVariables(); + variables.clear(); + variables.addAll(Map.fromEntries(list.map((e) => MapEntry(e.name, e)))); + notifyListeners(); + debugPrint('Variables: ${variables.length}'); + } + + Future getButtonSets() async { + final list = await loadButtonSets(); + buttonSets.clear(); + buttonSets.addAll(list); + notifyListeners(); + debugPrint('ButtonSets: ${buttonSets.length}'); + } +} + +class Plugin extends PluginBase { + final String profileId; + + @override + String get id => _id; + + final String _id; + + Plugin(this.profileId, String id) + : _id = id, + super('$profileId/$id'); +} + diff --git a/lib/core/features/profile.dart b/lib/core/features/profile.dart index 9f04dde..5959ddb 100644 --- a/lib/core/features/profile.dart +++ b/lib/core/features/profile.dart @@ -2,18 +2,12 @@ import 'package:encrypt/encrypt.dart' as enc; import 'package:flutter/foundation.dart'; import '../consts.dart'; -import '../keyboard_shortcuts.dart'; import '../secrets.dart'; import '../storage.dart'; import '../string_utils.dart'; -import 'alias.dart'; -import 'game_button_set.dart'; -import 'settings.dart'; -import 'trigger.dart'; -import 'variable.dart'; +import 'plugin.dart'; -class MUDProfile { - String id; +class MUDProfile extends PluginBase { String name; String host; int port; @@ -23,7 +17,7 @@ class MUDProfile { AuthMethod authMethod; MUDProfile({ - required this.id, + required String id, required this.name, required this.host, required this.port, @@ -31,7 +25,7 @@ class MUDProfile { this.username = '', this.password = '', this.authMethod = AuthMethod.none, - }); + }) : super(id); factory MUDProfile.empty() => MUDProfile( id: uuid(), @@ -95,156 +89,6 @@ class MUDProfile { profile.id, profile.id, (profile.toJson())); } - Future> loadTriggers() async { - debugPrint('MUDProfile.loadTriggers: $id'); - final triggers = await ProfileStorage.listProfileFiles(id, 'triggers'); - final triggerFiles = >[]; - for (final trigger in triggers) { - debugPrint('MUDProfile.loadTriggers: $id/triggers/$trigger'); - final triggerFile = - await ProfileStorage.readProfileFile(id, 'triggers/$trigger'); - if (triggerFile != null) { - triggerFiles.add(triggerFile); - } - } - return triggerFiles.map((e) => Trigger.fromJson(e)).toList(); - } - - Future> loadAliases() async { - debugPrint('MUDProfile.loadAliases: $id'); - final aliases = await ProfileStorage.listProfileFiles(id, 'aliases'); - final aliasFiles = >[]; - for (final alias in aliases) { - debugPrint('MUDProfile.loadAliases: $id/aliases/$alias'); - final aliasFile = - await ProfileStorage.readProfileFile(id, 'aliases/$alias'); - if (aliasFile != null) { - aliasFiles.add(aliasFile); - } - } - return aliasFiles.map((e) => Alias.fromJson(e)).toList(); - } - - Future> loadVariables() async { - debugPrint('MUDProfile.loadVariables: $id'); - final vars = await ProfileStorage.readProfileFile(id, 'vars'); - if (vars == null) { - return []; - } - return (vars['vars'] as List) - .map((e) => Variable.fromJson(e)) - .toList(); - } - - Future> loadButtonSets() async { - debugPrint('MUDProfile.loadButtonSets: $id'); - final buttonSets = await ProfileStorage.listProfileFiles(id, 'button_sets'); - final buttonSetFiles = >[]; - for (final buttonSet in buttonSets) { - debugPrint('MUDProfile.loadButtonSets: $id/buttonSets/$buttonSet'); - final buttonSetFile = - await ProfileStorage.readProfileFile(id, 'button_sets/$buttonSet'); - if (buttonSetFile != null) { - buttonSetFiles.add(buttonSetFile); - } - } - return buttonSetFiles.map((e) => GameButtonSetData.fromJson(e)).toList(); - } - - Future loadKeyboardShortcuts() async { - debugPrint('MUDProfile.loadKeyboardShortcuts: $id'); - final shortcuts = - await ProfileStorage.readProfileFile(id, 'keyboard_shortcuts'); - if (shortcuts == null) { - return KeyboardShortcuts.empty(); - } - return KeyboardShortcuts.fromJson(shortcuts); - } - - Future loadSettings() async { - debugPrint('MUDProfile.loadSettings: $id'); - final settings = await ProfileStorage.readProfileFile(id, 'settings'); - if (settings == null) { - return Settings.empty(); - } - return Settings.fromJson(settings); - } - - Future saveAlias(Alias alias) async { - debugPrint('MUDProfile.saveAlias: $id/aliases/${alias.id}'); - return ProfileStorage.writeProfileFile( - id, 'aliases/${alias.id}', alias.toJson()); - } - - Future deleteAlias(Alias alias) async { - debugPrint('MUDProfile.deleteAlias: $id/aliases/${alias.id}'); - return ProfileStorage.deleteProfileFile(id, 'aliases/${alias.id}'); - } - - Future saveTrigger(Trigger trigger) async { - debugPrint('MUDProfile.saveTrigger: $id/triggers/${trigger.id}'); - return ProfileStorage.writeProfileFile( - id, 'triggers/${trigger.id}', trigger.toJson()); - } - - Future deleteTrigger(Trigger trigger) async { - debugPrint('MUDProfile.deleteTrigger: $id/triggers/${trigger.id}'); - return ProfileStorage.deleteProfileFile(id, 'triggers/${trigger.id}'); - } - - Future saveButtonSet(GameButtonSetData buttonSet) async { - debugPrint('MUDProfile.saveButtonSet: $id/button_sets/${buttonSet.id}'); - return ProfileStorage.writeProfileFile( - id, 'button_sets/${buttonSet.id}', buttonSet.toJson()); - } - - Future saveKeyboardShortcuts(KeyboardShortcuts shortcuts) async { - debugPrint('MUDProfile.saveKeyboardShortcuts: $id'); - return ProfileStorage.writeProfileFile( - id, 'keyboard_shortcuts', shortcuts.toJson()); - } - - Future saveSettings(Settings settings) async { - debugPrint('MUDProfile.saveSettings: $id'); - return ProfileStorage.writeProfileFile(id, 'settings', settings.toJson()); - } - - Future deleteButtonSet(GameButtonSetData buttonSet) async { - debugPrint('MUDProfile.deleteButtonSet: $id/button_sets/${buttonSet.id}'); - return ProfileStorage.deleteProfileFile(id, 'button_sets/${buttonSet.id}'); - } - - Future saveVariable(List current, Variable update) async { - debugPrint('MUDProfile.saveVariable: $id/vars'); - final existing = current.indexWhere( - (v) => v.name == update.name, - ); - if (existing >= 0) { - current[existing] = update; - } else { - current.add(update); - } - return ProfileStorage.writeProfileFile( - id, - 'vars', - {'vars': current.map((v) => v.toJson()).toList()}, - ); - } - - Future deleteVariable(List current, Variable update) async { - debugPrint('MUDProfile.deleteVariable: $id/vars'); - final existing = current.indexWhere( - (v) => v.name == update.name, - ); - if (existing >= 0) { - current.removeAt(existing); - } - return ProfileStorage.writeProfileFile( - id, - 'vars', - {'vars': current.map((v) => v.toJson()).toList()}, - ); - } static final encKey = enc.Key.fromUtf8(pwdKey); static final encrypter = enc.Encrypter(enc.AES(encKey, padding: null)); diff --git a/lib/core/routes.dart b/lib/core/routes.dart index f486fa6..11ebdcf 100644 --- a/lib/core/routes.dart +++ b/lib/core/routes.dart @@ -94,7 +94,7 @@ final routes = { shortcuts: store.keyboardShortcuts, onSave: (shortcuts) { store.keyboardShortcuts = shortcuts; - store.currentProfile.saveKeyboardShortcuts(shortcuts); + store.saveKeyboardShortcuts(shortcuts); }, ); }); diff --git a/lib/core/store.dart b/lib/core/store.dart index 653a339..8b85d8e 100644 --- a/lib/core/store.dart +++ b/lib/core/store.dart @@ -13,12 +13,9 @@ import 'color_utils.dart'; import 'consts.dart'; import 'features/action.dart'; import 'features/alias.dart'; -import 'features/game_button_set.dart'; +import 'features/keyboard_shortcuts.dart'; import 'features/profile.dart'; import 'features/settings.dart'; -import 'features/trigger.dart'; -import 'features/variable.dart'; -import 'keyboard_shortcuts.dart'; const maxLines = 2000; @@ -45,13 +42,8 @@ class GameStore extends ChangeNotifier { RegExp("(? triggers = []; - final List aliases = []; - final Map variables = {}; - final List buttonSets = []; KeyboardShortcuts keyboardShortcuts = KeyboardShortcuts.empty(); + Settings settings = Settings.empty(); MUDProfile get currentProfile => _currentProfile!; @@ -82,56 +74,30 @@ class GameStore extends ChangeNotifier { onData: onData, onError: onError, ); - await Future.wait([ - loadTriggers(), - loadAliases(), - loadVariables(), - loadButtonSets(), + await Future.wait(>[ + currentProfile.load(), loadKeyboardShortcuts(), - loadSettings(), ]); _client.connect(); + notifyListeners(); } - Future loadTriggers() async { - debugPrint('loadTriggers'); - final list = await currentProfile.loadTriggers(); - triggers.clear(); - triggers.addAll(list); - notifyListeners(); - debugPrint('Triggers: ${triggers.length}'); + Future getKeyboardShortcuts() async { + debugPrint('MUDProfile.loadKeyboardShortcuts: ${currentProfile.id}'); + // TODO use global storage (not profile specific) + final shortcuts = await ProfileStorage.readProfileFile( + currentProfile.id, 'keyboard_shortcuts'); + if (shortcuts == null) { + return KeyboardShortcuts.empty(); + } + return KeyboardShortcuts.fromJson(shortcuts); } - Future loadAliases() async { - final list = await currentProfile.loadAliases(); - aliases.clear(); - aliases.addAll(list); - notifyListeners(); - debugPrint('Aliases: ${aliases.length}'); - } - - Future loadVariables() async { - final list = await currentProfile.loadVariables(); - variables.clear(); - variables.addAll(Map.fromEntries(list.map((e) => MapEntry(e.name, e)))); - notifyListeners(); - debugPrint('Variables: ${variables.length}'); - } - - Future loadButtonSets() async { - final list = await currentProfile.loadButtonSets(); - buttonSets.clear(); - buttonSets.addAll(list); - notifyListeners(); - debugPrint('ButtonSets: ${buttonSets.length}'); - } - - Future loadKeyboardShortcuts() async { - final shortcuts = await currentProfile.loadKeyboardShortcuts(); - keyboardShortcuts = shortcuts; - notifyListeners(); - debugPrint('KeyboardShortcuts loaded'); + Future saveKeyboardShortcuts(KeyboardShortcuts shortcuts) async { + debugPrint('MUDProfile.saveKeyboardShortcuts: ${currentProfile.id}'); + return ProfileStorage.writeProfileFile( + currentProfile.id, 'keyboard_shortcuts', shortcuts.toJson()); } Future loadSettings() async { @@ -141,10 +107,17 @@ class GameStore extends ChangeNotifier { debugPrint('Settings loaded'); } + Future loadKeyboardShortcuts() async { + final shortcuts = await getKeyboardShortcuts(); + keyboardShortcuts = shortcuts; + notifyListeners(); + debugPrint('KeyboardShortcuts loaded'); + } + bool processTriggers(String line) { bool showLine = true; final str = ColorUtils.stripColor(line); - for (final trigger in triggers) { + for (final trigger in currentProfile.triggers) { if (!trigger.isAvailable) { continue; } @@ -164,7 +137,7 @@ class GameStore extends ChangeNotifier { bool processAliases(String line) { bool sendLine = true; final str = line; - for (final alias in [...builtInAliases, ...aliases]) { + for (final alias in [...builtInAliases, ...currentProfile.aliases]) { if (!alias.isAvailable) { continue; } diff --git a/lib/pages/alias_list_page.dart b/lib/pages/alias_list_page.dart index cfabf60..3350130 100644 --- a/lib/pages/alias_list_page.dart +++ b/lib/pages/alias_list_page.dart @@ -13,7 +13,7 @@ class AliasListPage extends StatelessWidget with GameStoreMixin { return GenericListPage( title: const Text('Aliases'), save: save, - items: storeOf(context).aliases, + items: storeOf(context).currentProfile.aliases, detailsPath: Paths.alias, displayName: (alias) => alias.pattern, searchTags: (alias) => [ @@ -45,7 +45,7 @@ class AliasListPage extends StatelessWidget with GameStoreMixin { switch (value) { case 'delete': store.currentProfile.deleteAlias(alias); - store.loadAliases(); + store.currentProfile.loadAliases(); break; } }, @@ -68,6 +68,6 @@ class AliasListPage extends StatelessWidget with GameStoreMixin { Future save(GameStore store, Alias updated) async { await store.currentProfile.saveAlias(updated); // TODO - stop re-loading all aliases, only replace the one that changed - await store.loadAliases(); + await store.currentProfile.loadAliases(); } } diff --git a/lib/pages/button_sets_list_page.dart b/lib/pages/button_sets_list_page.dart index ae51677..4d14d82 100644 --- a/lib/pages/button_sets_list_page.dart +++ b/lib/pages/button_sets_list_page.dart @@ -13,7 +13,7 @@ class ButtonSetListPage extends StatelessWidget with GameStoreMixin { return GenericListPage( title: const Text('Button Sets'), save: save, - items: storeOf(context).buttonSets, + items: storeOf(context).currentProfile.buttonSets, detailsPath: Paths.buttonSet, displayName: (buttonSet) => buttonSet.name, searchTags: (buttonSet) => [ @@ -40,7 +40,7 @@ class ButtonSetListPage extends StatelessWidget with GameStoreMixin { ); if (data != null) { store.currentProfile.saveButtonSet(data as GameButtonSetData); - store.loadButtonSets(); + store.currentProfile.loadButtonSets(); } break; } @@ -72,7 +72,7 @@ class ButtonSetListPage extends StatelessWidget with GameStoreMixin { switch (value) { case 'delete': store.currentProfile.deleteButtonSet(buttonSet); - store.loadButtonSets(); + store.currentProfile.loadButtonSets(); break; } }, @@ -95,7 +95,7 @@ class ButtonSetListPage extends StatelessWidget with GameStoreMixin { Future save(GameStore store, GameButtonSetData updated) async { await store.currentProfile.saveButtonSet(updated); // TODO - stop re-loading all triggers, only replace the one that changed - await store.loadButtonSets(); + await store.currentProfile.loadButtonSets(); } } diff --git a/lib/pages/home_page.dart b/lib/pages/home_page.dart index a5eb913..c5f3d37 100644 --- a/lib/pages/home_page.dart +++ b/lib/pages/home_page.dart @@ -9,7 +9,7 @@ import 'package:window_manager/window_manager.dart'; import '../core/consts.dart'; import '../core/features/game_button_set.dart'; -import '../core/keyboard_shortcuts.dart'; +import '../core/features/keyboard_shortcuts.dart'; import '../core/store.dart'; class HomePage extends StatefulWidget { @@ -85,9 +85,10 @@ class HomePageState extends State TextSpan( text: segment.text, style: consoleStyle.copyWith( - color: Color(segment.themedFgColor), - backgroundColor: - Color(segment.themedBgColor), + color: Color( + segment.themedFgColor), + backgroundColor: Color( + segment.themedBgColor), fontWeight: segment.bold ? FontWeight.w800 : null, @@ -114,12 +115,14 @@ class HomePageState extends State ), ), ), - for (final buttonSet - in store.buttonSets.where((b) => b.enabled)) - Padding( - padding: const EdgeInsets.all(8.0), - child: GameButtonSet(data: buttonSet), - ) + if (store.connected) + for (final buttonSet in store + .currentProfile.buttonSets + .where((b) => b.enabled)) + Padding( + padding: const EdgeInsets.all(8.0), + child: GameButtonSet(data: buttonSet), + ) ], ), ); diff --git a/lib/pages/keyboard_shortcuts_page.dart b/lib/pages/keyboard_shortcuts_page.dart index 1137386..4dc9daa 100644 --- a/lib/pages/keyboard_shortcuts_page.dart +++ b/lib/pages/keyboard_shortcuts_page.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; -import '../core/keyboard_shortcuts.dart'; +import '../core/features/keyboard_shortcuts.dart'; + class KeyboardShortcutsPage extends StatefulWidget { const KeyboardShortcutsPage({super.key, required this.shortcuts, required this.onSave}); diff --git a/lib/pages/trigger_list_page.dart b/lib/pages/trigger_list_page.dart index a08ae0d..0a0bbe4 100644 --- a/lib/pages/trigger_list_page.dart +++ b/lib/pages/trigger_list_page.dart @@ -13,7 +13,7 @@ class TriggerListPage extends StatelessWidget with GameStoreMixin { return GenericListPage( title: const Text('Triggers'), save: save, - items: storeOf(context).triggers, + items: storeOf(context).currentProfile.triggers, detailsPath: Paths.trigger, displayName: (trigger) => trigger.pattern, searchTags: (trigger) => [ @@ -45,8 +45,9 @@ class TriggerListPage extends StatelessWidget with GameStoreMixin { onSelected: (value) { switch (value) { case 'delete': + // TODO extract this to props store.currentProfile.deleteTrigger(trigger); - store.loadTriggers(); + store.currentProfile.loadTriggers(); break; } }, @@ -66,10 +67,11 @@ class TriggerListPage extends StatelessWidget with GameStoreMixin { ); } + // TODO extract this to props Future save(GameStore store, Trigger updated) async { await store.currentProfile.saveTrigger(updated); // TODO - stop re-loading all triggers, only replace the one that changed - await store.loadTriggers(); + await store.currentProfile.loadTriggers(); } } diff --git a/lib/pages/variable_list_page.dart b/lib/pages/variable_list_page.dart index 80cc949..f0c1387 100644 --- a/lib/pages/variable_list_page.dart +++ b/lib/pages/variable_list_page.dart @@ -13,10 +13,11 @@ class VariableListPage extends StatelessWidget with GameStoreMixin { appBar: AppBar( title: const Text('Variables'), ), + // TODO extract this to props body: GameStore.consumer( builder: (context, store, child) { debugPrint('Variable list rebuild'); - final variables = store.variables.values; + final variables = store.currentProfile.variables.values; return ListView.builder( itemCount: variables.length, itemBuilder: (context, item) { @@ -53,9 +54,11 @@ class VariableListPage extends StatelessWidget with GameStoreMixin { ); } + // TODO extract this to props Future save(GameStore store, Variable updated) async { await store.currentProfile - .saveVariable(store.variables.values.toList(), updated); - await store.loadVariables(); + .saveVariable(store.currentProfile.variables.values.toList(), updated); + await store.currentProfile.loadVariables(); } } +