From c6aceed623f86d5e6482e3192c2e7482c8ccf4df Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Thu, 5 Sep 2019 00:17:08 +0300 Subject: [PATCH] utils separated --- lib/main.dart | 4 +- lib/types/event_bus_events.dart | 82 +++++++++++++++ lib/types/ha_error.dart | 45 +++++++++ lib/utils.class.dart | 171 -------------------------------- lib/utils/logger.dart | 46 +++++++++ 5 files changed, 176 insertions(+), 172 deletions(-) create mode 100644 lib/types/event_bus_events.dart create mode 100644 lib/types/ha_error.dart delete mode 100644 lib/utils.class.dart create mode 100644 lib/utils/logger.dart diff --git a/lib/main.dart b/lib/main.dart index 4e41479..bc9377e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -95,7 +95,6 @@ part 'pages/panel.page.dart'; part 'home_assistant.class.dart'; part 'pages/log.page.dart'; part 'pages/entity.page.dart'; -part 'utils.class.dart'; part 'mdi.class.dart'; part 'entity_collection.class.dart'; part 'managers/auth_manager.class.dart'; @@ -114,6 +113,9 @@ part 'ui_widgets/card_widget.dart'; part 'ui_widgets/card_header_widget.dart'; part 'panels/config_panel_widget.dart'; part 'panels/widgets/link_to_web_config.dart'; +part 'utils/logger.dart'; +part 'types/ha_error.dart'; +part 'types/event_bus_events.dart'; EventBus eventBus = new EventBus(); diff --git a/lib/types/event_bus_events.dart b/lib/types/event_bus_events.dart new file mode 100644 index 0000000..50068fb --- /dev/null +++ b/lib/types/event_bus_events.dart @@ -0,0 +1,82 @@ +part of '../main.dart'; + +class StateChangedEvent { + String entityId; + String newState; + bool needToRebuildUI; + + StateChangedEvent({ + this.entityId, + this.newState, + this.needToRebuildUI: false + }); +} + +class SettingsChangedEvent { + bool reconnect; + + SettingsChangedEvent(this.reconnect); +} + +class RefreshDataFinishedEvent { + RefreshDataFinishedEvent(); +} + +class ReloadUIEvent { + ReloadUIEvent(); +} + +class StartAuthEvent { + String oauthUrl; + bool showButton; + + StartAuthEvent(this.oauthUrl, this.showButton); +} + +class ServiceCallEvent { + String domain; + String service; + String entityId; + Map additionalParams; + + ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams); +} + +class ShowPopupDialogEvent { + final String title; + final String body; + final String positiveText; + final String negativeText; + final onPositive; + final onNegative; + + ShowPopupDialogEvent({this.title, this.body, this.positiveText: "Ok", this.negativeText: "Cancel", this.onPositive, this.onNegative}); +} + +class ShowPopupMessageEvent { + final String title; + final String body; + final String buttonText; + final onButtonClick; + + ShowPopupMessageEvent({this.title, this.body, this.buttonText: "Ok", this.onButtonClick}); +} + +class ShowEntityPageEvent { + Entity entity; + + ShowEntityPageEvent(this.entity); +} + +class ShowPageEvent { + final String path; + final bool goBackFirst; + + ShowPageEvent({@required this.path, this.goBackFirst: false}); +} + +class ShowErrorEvent { + final HAError error; + + ShowErrorEvent(this.error); +} \ No newline at end of file diff --git a/lib/types/ha_error.dart b/lib/types/ha_error.dart new file mode 100644 index 0000000..d0a4118 --- /dev/null +++ b/lib/types/ha_error.dart @@ -0,0 +1,45 @@ +part of '../main.dart'; + +class HAError { + String message; + final List actions; + + HAError(this.message, {this.actions: const [HAErrorAction.tryAgain()]}); + + HAError.unableToConnect({this.actions = const [HAErrorAction.tryAgain()]}) { + this.message = "Unable to connect to Home Assistant"; + } + + HAError.disconnected({this.actions = const [HAErrorAction.reconnect()]}) { + this.message = "Disconnected"; + } + + HAError.checkConnectionSettings({this.actions = const [HAErrorAction.reload(), HAErrorAction(title: "Settings", type: HAErrorActionType.OPEN_CONNECTION_SETTINGS)]}) { + this.message = "Check connection settings"; + } +} + +class HAErrorAction { + final String title; + final int type; + final String url; + + const HAErrorAction({@required this.title, this.type: HAErrorActionType.FULL_RELOAD, this.url}); + + const HAErrorAction.tryAgain({this.title = "Try again", this.type = HAErrorActionType.FULL_RELOAD, this.url}); + + const HAErrorAction.reconnect({this.title = "Reconnect", this.type = HAErrorActionType.FULL_RELOAD, this.url}); + + const HAErrorAction.reload({this.title = "Reload", this.type = HAErrorActionType.FULL_RELOAD, this.url}); + + const HAErrorAction.loginAgain({this.title = "Login again", this.type = HAErrorActionType.RELOGIN, this.url}); + +} + +class HAErrorActionType { + static const FULL_RELOAD = 0; + static const QUICK_RELOAD = 1; + static const URL = 3; + static const OPEN_CONNECTION_SETTINGS = 4; + static const RELOGIN = 5; +} \ No newline at end of file diff --git a/lib/utils.class.dart b/lib/utils.class.dart deleted file mode 100644 index 1e35372..0000000 --- a/lib/utils.class.dart +++ /dev/null @@ -1,171 +0,0 @@ -part of 'main.dart'; - -class Logger { - - static List _log = []; - - static String getLog() { - String res = ''; - _log.forEach((line) { - res += "$line\n"; - }); - return res; - } - - static bool get isInDebugMode { - bool inDebugMode = false; - - assert(inDebugMode = true); - - return inDebugMode; - } - - static void e(String message) { - _writeToLog("Error", message); - } - - static void w(String message) { - _writeToLog("Warning", message); - } - - static void d(String message) { - _writeToLog("Debug", message); - } - - static void _writeToLog(String level, String message) { - if (isInDebugMode) { - debugPrint('$message'); - } - DateTime t = DateTime.now(); - _log.add("${formatDate(t, ["mm","dd"," ","HH",":","nn",":","ss"])} [$level] : $message"); - if (_log.length > 100) { - _log.removeAt(0); - } - } - -} - -class HAError { - String message; - final List actions; - - HAError(this.message, {this.actions: const [HAErrorAction.tryAgain()]}); - - HAError.unableToConnect({this.actions = const [HAErrorAction.tryAgain()]}) { - this.message = "Unable to connect to Home Assistant"; - } - - HAError.disconnected({this.actions = const [HAErrorAction.reconnect()]}) { - this.message = "Disconnected"; - } - - HAError.checkConnectionSettings({this.actions = const [HAErrorAction.reload(), HAErrorAction(title: "Settings", type: HAErrorActionType.OPEN_CONNECTION_SETTINGS)]}) { - this.message = "Check connection settings"; - } -} - -class HAErrorAction { - final String title; - final int type; - final String url; - - const HAErrorAction({@required this.title, this.type: HAErrorActionType.FULL_RELOAD, this.url}); - - const HAErrorAction.tryAgain({this.title = "Try again", this.type = HAErrorActionType.FULL_RELOAD, this.url}); - - const HAErrorAction.reconnect({this.title = "Reconnect", this.type = HAErrorActionType.FULL_RELOAD, this.url}); - - const HAErrorAction.reload({this.title = "Reload", this.type = HAErrorActionType.FULL_RELOAD, this.url}); - - const HAErrorAction.loginAgain({this.title = "Login again", this.type = HAErrorActionType.RELOGIN, this.url}); - -} - -class HAErrorActionType { - static const FULL_RELOAD = 0; - static const QUICK_RELOAD = 1; - static const URL = 3; - static const OPEN_CONNECTION_SETTINGS = 4; - static const RELOGIN = 5; -} - -class StateChangedEvent { - String entityId; - String newState; - bool needToRebuildUI; - - StateChangedEvent({ - this.entityId, - this.newState, - this.needToRebuildUI: false - }); -} - -class SettingsChangedEvent { - bool reconnect; - - SettingsChangedEvent(this.reconnect); -} - -class RefreshDataFinishedEvent { - RefreshDataFinishedEvent(); -} - -class ReloadUIEvent { - ReloadUIEvent(); -} - -class StartAuthEvent { - String oauthUrl; - bool showButton; - - StartAuthEvent(this.oauthUrl, this.showButton); -} - -class ServiceCallEvent { - String domain; - String service; - String entityId; - Map additionalParams; - - ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams); -} - -class ShowPopupDialogEvent { - final String title; - final String body; - final String positiveText; - final String negativeText; - final onPositive; - final onNegative; - - ShowPopupDialogEvent({this.title, this.body, this.positiveText: "Ok", this.negativeText: "Cancel", this.onPositive, this.onNegative}); -} - -class ShowPopupMessageEvent { - final String title; - final String body; - final String buttonText; - final onButtonClick; - - ShowPopupMessageEvent({this.title, this.body, this.buttonText: "Ok", this.onButtonClick}); -} - -class ShowEntityPageEvent { - Entity entity; - - ShowEntityPageEvent(this.entity); -} - -class ShowPageEvent { - final String path; - final bool goBackFirst; - - ShowPageEvent({@required this.path, this.goBackFirst: false}); -} - -class ShowErrorEvent { - final HAError error; - - ShowErrorEvent(this.error); -} \ No newline at end of file diff --git a/lib/utils/logger.dart b/lib/utils/logger.dart new file mode 100644 index 0000000..717afe2 --- /dev/null +++ b/lib/utils/logger.dart @@ -0,0 +1,46 @@ +part of '../main.dart'; + +class Logger { + + static List _log = []; + + static String getLog() { + String res = ''; + _log.forEach((line) { + res += "$line\n"; + }); + return res; + } + + static bool get isInDebugMode { + bool inDebugMode = false; + + assert(inDebugMode = true); + + return inDebugMode; + } + + static void e(String message) { + _writeToLog("Error", message); + } + + static void w(String message) { + _writeToLog("Warning", message); + } + + static void d(String message) { + _writeToLog("Debug", message); + } + + static void _writeToLog(String level, String message) { + if (isInDebugMode) { + debugPrint('$message'); + } + DateTime t = DateTime.now(); + _log.add("${formatDate(t, ["mm","dd"," ","HH",":","nn",":","ss"])} [$level] : $message"); + if (_log.length > 100) { + _log.removeAt(0); + } + } + +} \ No newline at end of file