2020-05-13 13:57:26 +03:00
|
|
|
part of '../main.dart';
|
|
|
|
|
2020-05-29 21:16:59 +03:00
|
|
|
enum DisplayMode {normal, fullscreen}
|
|
|
|
|
2020-05-13 13:57:26 +03:00
|
|
|
class AppSettings {
|
|
|
|
|
2020-05-28 23:23:13 +03:00
|
|
|
static const DEFAULT_HIVE_BOX = 'defaultSettingsBox';
|
|
|
|
|
|
|
|
static const AUTH_TOKEN_KEY = 'llt';
|
|
|
|
|
2020-05-13 13:57:26 +03:00
|
|
|
static final AppSettings _instance = AppSettings._internal();
|
|
|
|
|
|
|
|
factory AppSettings() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppSettings._internal();
|
|
|
|
|
|
|
|
String mobileAppDeviceName;
|
|
|
|
String _domain;
|
|
|
|
String _port;
|
|
|
|
String displayHostname;
|
|
|
|
String webSocketAPIEndpoint;
|
|
|
|
String httpWebHost;
|
2020-05-13 15:46:25 +03:00
|
|
|
String longLivedToken;
|
|
|
|
String tempToken;
|
2020-05-13 13:57:26 +03:00
|
|
|
String oauthUrl;
|
|
|
|
String webhookId;
|
|
|
|
double haVersion;
|
|
|
|
bool scrollBadges;
|
2020-06-22 22:30:51 +03:00
|
|
|
bool nextAlarmSensorCreated = false;
|
2020-05-29 21:16:59 +03:00
|
|
|
DisplayMode displayMode;
|
2020-05-13 15:46:25 +03:00
|
|
|
AppTheme appTheme;
|
|
|
|
final int defaultLocationUpdateIntervalMinutes = 20;
|
2020-07-07 18:37:44 +03:00
|
|
|
final int defaultActiveLocationUpdateIntervalSeconds = 900;
|
2020-05-13 15:46:25 +03:00
|
|
|
Duration locationUpdateInterval;
|
|
|
|
bool locationTrackingEnabled = false;
|
2020-05-13 13:57:26 +03:00
|
|
|
|
2020-05-13 15:46:25 +03:00
|
|
|
bool get isAuthenticated => longLivedToken != null;
|
|
|
|
bool get isTempAuthenticated => tempToken != null;
|
|
|
|
|
2020-05-29 21:16:59 +03:00
|
|
|
loadStartupSettings() async {
|
2020-05-13 15:46:25 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
appTheme = AppTheme.values[prefs.getInt('app-theme') ?? AppTheme.defaultTheme.index];
|
2020-05-29 21:16:59 +03:00
|
|
|
displayMode = DisplayMode.values[prefs.getInt('display-mode') ?? DisplayMode.normal.index];
|
2020-05-13 15:46:25 +03:00
|
|
|
}
|
2020-05-13 13:57:26 +03:00
|
|
|
|
2020-05-14 13:56:52 +03:00
|
|
|
Future load(bool full) async {
|
|
|
|
if (full) {
|
2020-05-28 23:23:13 +03:00
|
|
|
await Hive.openBox(DEFAULT_HIVE_BOX);
|
2020-05-14 13:56:52 +03:00
|
|
|
Logger.d('Loading settings...');
|
2020-05-13 13:57:26 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
_domain = prefs.getString('hassio-domain');
|
|
|
|
_port = prefs.getString('hassio-port');
|
|
|
|
webhookId = prefs.getString('app-webhook-id');
|
|
|
|
mobileAppDeviceName = prefs.getString('app-integration-device-name');
|
|
|
|
scrollBadges = prefs.getBool('scroll-badges') ?? true;
|
|
|
|
displayHostname = "$_domain:$_port";
|
2020-05-13 15:46:25 +03:00
|
|
|
webSocketAPIEndpoint =
|
2020-05-13 13:57:26 +03:00
|
|
|
"${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket";
|
|
|
|
httpWebHost =
|
|
|
|
"${prefs.getString('hassio-res-protocol')}://$_domain:$_port";
|
2020-05-13 15:46:25 +03:00
|
|
|
locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ??
|
|
|
|
defaultLocationUpdateIntervalMinutes);
|
|
|
|
locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
|
2020-06-22 22:30:51 +03:00
|
|
|
nextAlarmSensorCreated = prefs.getBool("next-alarm-sensor-created") ?? false;
|
2020-05-28 23:23:13 +03:00
|
|
|
longLivedToken = Hive.box(DEFAULT_HIVE_BOX).get(AUTH_TOKEN_KEY);
|
|
|
|
oauthUrl = "$httpWebHost/auth/authorize?client_id=${Uri.encodeComponent(
|
|
|
|
'https://ha-client.app')}&redirect_uri=${Uri
|
|
|
|
.encodeComponent(
|
|
|
|
'https://ha-client.app/service/auth_callback.html')}";
|
2020-05-13 13:57:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:58:14 +03:00
|
|
|
Future<dynamic> loadSingle(String key) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
return prefs.get('$key');
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:46:25 +03:00
|
|
|
Future save(Map<String, dynamic> settings) async {
|
|
|
|
if (settings != null && settings.isNotEmpty) {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
settings.forEach((k,v) async {
|
|
|
|
if (v is String) {
|
|
|
|
await prefs.setString(k, v);
|
|
|
|
} else if (v is bool) {
|
|
|
|
await prefs.setBool(k ,v);
|
|
|
|
} else if (v is int) {
|
|
|
|
await prefs.setInt(k ,v);
|
|
|
|
} else if (v is double) {
|
|
|
|
await prefs.setDouble(k, v);
|
|
|
|
} else {
|
|
|
|
Logger.e('Unknown setting type: <$k, $v>');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:57:26 +03:00
|
|
|
Future startAuth() {
|
|
|
|
return AuthManager().start(
|
|
|
|
oauthUrl: oauthUrl
|
|
|
|
).then((token) {
|
|
|
|
Logger.d("Token from AuthManager recived");
|
2020-05-13 15:46:25 +03:00
|
|
|
tempToken = token;
|
2020-05-13 13:57:26 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:46:25 +03:00
|
|
|
Future clearTokens() async {
|
|
|
|
longLivedToken = null;
|
|
|
|
tempToken = null;
|
2020-05-28 23:23:13 +03:00
|
|
|
Hive.box(DEFAULT_HIVE_BOX).delete(AUTH_TOKEN_KEY);
|
2020-05-13 15:46:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:26:20 +03:00
|
|
|
void saveLongLivedToken(token) {
|
2020-05-13 15:46:25 +03:00
|
|
|
longLivedToken = token;
|
|
|
|
tempToken = null;
|
2020-05-28 23:23:13 +03:00
|
|
|
Hive.box(DEFAULT_HIVE_BOX).put(AUTH_TOKEN_KEY, longLivedToken);
|
2020-05-13 15:46:25 +03:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:57:26 +03:00
|
|
|
bool isNotConfigured() {
|
|
|
|
return _domain == null && _port == null && webhookId == null && mobileAppDeviceName == null;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSomethingMissed() {
|
|
|
|
return (_domain == null) || (_port == null) || (_domain.isEmpty) || (_port.isEmpty);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|