2018-09-25 22:47:06 +03:00
|
|
|
part of 'main.dart';
|
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
class Logger {
|
2018-09-25 22:47:06 +03:00
|
|
|
|
|
|
|
static List<String> _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;
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
static void e(String message) {
|
2018-10-27 01:24:23 +03:00
|
|
|
_writeToLog("Error", message);
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
static void w(String message) {
|
2018-10-27 01:24:23 +03:00
|
|
|
_writeToLog("Warning", message);
|
|
|
|
}
|
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
static void d(String message) {
|
2018-10-27 01:24:23 +03:00
|
|
|
_writeToLog("Debug", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _writeToLog(String level, String message) {
|
2018-09-25 22:47:06 +03:00
|
|
|
if (isInDebugMode) {
|
|
|
|
debugPrint('$message');
|
|
|
|
}
|
2018-10-27 01:24:23 +03:00
|
|
|
DateTime t = DateTime.now();
|
|
|
|
_log.add("${formatDate(t, ["mm","dd"," ","HH",":","nn",":","ss"])} [$level] : $message");
|
|
|
|
if (_log.length > 100) {
|
2018-09-25 22:47:06 +03:00
|
|
|
_log.removeAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-19 21:43:52 +03:00
|
|
|
class HAError {
|
|
|
|
String message;
|
|
|
|
final List<HAErrorAction> 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.FULL_RELOAD, this.url});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class HAErrorActionType {
|
|
|
|
static const FULL_RELOAD = 0;
|
|
|
|
static const QUICK_RELOAD = 1;
|
|
|
|
static const LOGOUT = 2;
|
|
|
|
static const URL = 3;
|
|
|
|
static const OPEN_CONNECTION_SETTINGS = 4;
|
|
|
|
}
|
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
class HAUtils {
|
2018-09-25 22:47:06 +03:00
|
|
|
static void launchURL(String url) async {
|
2019-03-13 17:05:15 +02:00
|
|
|
if (await urlLauncher.canLaunch(url)) {
|
|
|
|
await urlLauncher.launch(url);
|
2018-09-25 22:47:06 +03:00
|
|
|
} else {
|
2018-12-15 14:09:37 +02:00
|
|
|
Logger.e( "Could not launch $url");
|
2018-09-25 22:47:06 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 17:05:15 +02:00
|
|
|
|
|
|
|
static void launchURLInCustomTab(BuildContext context, String url) async {
|
|
|
|
try {
|
|
|
|
await launch(
|
|
|
|
"$url",
|
|
|
|
option: new CustomTabsOption(
|
|
|
|
toolbarColor: Theme.of(context).primaryColor,
|
|
|
|
enableDefaultShare: true,
|
|
|
|
enableUrlBarHiding: true,
|
|
|
|
showPageTitle: true,
|
|
|
|
animation: new CustomTabsAnimation.slideIn()
|
|
|
|
// or user defined animation.
|
|
|
|
/*animation: new CustomTabsAnimation(
|
|
|
|
startEnter: 'slide_up',
|
|
|
|
startExit: 'android:anim/fade_out',
|
|
|
|
endEnter: 'android:anim/fade_in',
|
|
|
|
endExit: 'slide_down',
|
|
|
|
)*/,
|
|
|
|
extraCustomTabs: <String>[
|
|
|
|
// ref. https://play.google.com/store/apps/details?id=org.mozilla.firefox
|
|
|
|
'org.mozilla.firefox',
|
|
|
|
// ref. https://play.google.com/store/apps/details?id=com.microsoft.emmx
|
|
|
|
'com.microsoft.emmx',
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
Logger.w("Can't open custom tab: ${e.toString()}");
|
|
|
|
Logger.w("Launching in default browser");
|
|
|
|
HAUtils.launchURL(url);
|
|
|
|
}
|
|
|
|
}
|
2018-09-25 22:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class StateChangedEvent {
|
|
|
|
String entityId;
|
2018-09-29 13:49:25 +03:00
|
|
|
String newState;
|
2018-12-15 14:09:37 +02:00
|
|
|
bool needToRebuildUI;
|
2018-09-25 22:47:06 +03:00
|
|
|
|
2018-12-15 14:09:37 +02:00
|
|
|
StateChangedEvent({
|
|
|
|
this.entityId,
|
|
|
|
this.newState,
|
|
|
|
this.needToRebuildUI: false
|
|
|
|
});
|
2018-09-25 22:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class SettingsChangedEvent {
|
|
|
|
bool reconnect;
|
|
|
|
|
|
|
|
SettingsChangedEvent(this.reconnect);
|
2018-09-29 13:49:25 +03:00
|
|
|
}
|
|
|
|
|
2018-10-07 02:17:14 +03:00
|
|
|
class RefreshDataFinishedEvent {
|
|
|
|
RefreshDataFinishedEvent();
|
|
|
|
}
|
|
|
|
|
2019-03-20 19:01:30 +02:00
|
|
|
class ReloadUIEvent {
|
|
|
|
ReloadUIEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
class StartAuthEvent {
|
|
|
|
String oauthUrl;
|
2019-04-05 14:07:03 +03:00
|
|
|
bool showButton;
|
2019-03-20 19:01:30 +02:00
|
|
|
|
2019-04-05 14:07:03 +03:00
|
|
|
StartAuthEvent(this.oauthUrl, this.showButton);
|
2019-03-20 19:01:30 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 13:49:25 +03:00
|
|
|
class ServiceCallEvent {
|
|
|
|
String domain;
|
|
|
|
String service;
|
|
|
|
String entityId;
|
2018-10-16 17:35:13 +03:00
|
|
|
Map<String, dynamic> additionalParams;
|
2018-09-29 13:49:25 +03:00
|
|
|
|
|
|
|
ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams);
|
2018-09-29 16:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class ShowEntityPageEvent {
|
|
|
|
Entity entity;
|
|
|
|
|
|
|
|
ShowEntityPageEvent(this.entity);
|
2018-10-07 09:55:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class ShowErrorEvent {
|
2019-04-19 21:43:52 +03:00
|
|
|
final HAError error;
|
2018-10-07 09:55:37 +03:00
|
|
|
|
2019-04-19 21:43:52 +03:00
|
|
|
ShowErrorEvent(this.error);
|
2018-09-25 22:47:06 +03:00
|
|
|
}
|