2018-09-10 00:34:52 +03:00
|
|
|
import 'dart:convert';
|
2018-09-15 01:46:15 +03:00
|
|
|
import 'dart:async';
|
2018-09-15 12:56:42 +03:00
|
|
|
import 'package:flutter/rendering.dart';
|
2018-09-10 00:34:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2018-09-10 03:06:35 +03:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2018-09-12 00:32:04 +03:00
|
|
|
import 'package:web_socket_channel/io.dart';
|
2018-09-15 01:46:15 +03:00
|
|
|
import 'package:progress_indicators/progress_indicators.dart';
|
2018-09-16 18:02:12 +03:00
|
|
|
import 'package:event_bus/event_bus.dart';
|
2018-09-16 19:24:26 +03:00
|
|
|
import 'package:flutter/widgets.dart';
|
2018-09-23 02:04:44 +03:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2018-09-24 22:12:56 +03:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2018-09-25 22:11:31 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2018-09-29 16:19:01 +03:00
|
|
|
import 'package:date_format/date_format.dart';
|
2018-09-10 03:06:35 +03:00
|
|
|
|
2018-10-01 21:57:54 +03:00
|
|
|
part 'entity_class/entity.class.dart';
|
|
|
|
part 'entity_class/button_entity.class.dart';
|
|
|
|
part 'entity_class/datetime_entity.class.dart';
|
|
|
|
part 'entity_class/select_entity.class.dart';
|
|
|
|
part 'entity_class/slider_entity.class.dart';
|
|
|
|
part 'entity_class/switch_entity.class.dart';
|
|
|
|
part 'entity_class/text_entity.class.dart';
|
|
|
|
|
2018-09-25 22:47:06 +03:00
|
|
|
part 'settings.page.dart';
|
2018-09-27 14:51:57 +03:00
|
|
|
part 'home_assistant.class.dart';
|
2018-09-25 22:47:06 +03:00
|
|
|
part 'log.page.dart';
|
2018-09-29 16:19:01 +03:00
|
|
|
part 'entity.page.dart';
|
2018-09-25 22:47:06 +03:00
|
|
|
part 'utils.class.dart';
|
|
|
|
part 'mdi.class.dart';
|
2018-09-26 22:16:50 +03:00
|
|
|
part 'entity_collection.class.dart';
|
2018-09-28 10:15:25 +03:00
|
|
|
part 'ui_builder_class.dart';
|
|
|
|
part 'view_class.dart';
|
|
|
|
part 'card_class.dart';
|
|
|
|
part 'badge_class.dart';
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-16 18:02:12 +03:00
|
|
|
EventBus eventBus = new EventBus();
|
2018-09-18 23:21:05 +03:00
|
|
|
const String appName = "HA Client";
|
2018-10-03 15:26:46 +03:00
|
|
|
const appVersion = "0.2.4";
|
2018-09-16 18:02:12 +03:00
|
|
|
|
2018-09-23 02:04:44 +03:00
|
|
|
String homeAssistantWebHost;
|
|
|
|
|
2018-09-24 20:07:22 +03:00
|
|
|
void main() {
|
|
|
|
FlutterError.onError = (errorDetails) {
|
|
|
|
TheLogger.log("Error", "${errorDetails.exception}");
|
|
|
|
if (TheLogger.isInDebugMode) {
|
|
|
|
FlutterError.dumpErrorToConsole(errorDetails);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
runZoned(() {
|
2018-09-25 22:47:06 +03:00
|
|
|
runApp(new HAClientApp());
|
2018-09-24 20:07:22 +03:00
|
|
|
}, onError: (error, stack) {
|
|
|
|
TheLogger.log("Global error", "$error");
|
2018-09-27 14:51:57 +03:00
|
|
|
if (TheLogger.isInDebugMode) {
|
|
|
|
debugPrint("$stack");
|
|
|
|
}
|
2018-09-24 20:07:22 +03:00
|
|
|
});
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-25 22:47:06 +03:00
|
|
|
class HAClientApp extends StatelessWidget {
|
2018-09-10 00:34:52 +03:00
|
|
|
// This widget is the root of your application.
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return new MaterialApp(
|
2018-09-17 01:03:34 +03:00
|
|
|
title: appName,
|
2018-09-10 00:34:52 +03:00
|
|
|
theme: new ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
2018-09-10 03:06:35 +03:00
|
|
|
initialRoute: "/",
|
|
|
|
routes: {
|
2018-10-02 00:41:40 +03:00
|
|
|
"/": (context) => MainPage(title: 'HA Client'),
|
2018-09-24 00:05:57 +03:00
|
|
|
"/connection-settings": (context) => ConnectionSettingsPage(title: "Connection Settings"),
|
|
|
|
"/log-view": (context) => LogViewPage(title: "Log")
|
2018-09-10 03:06:35 +03:00
|
|
|
},
|
2018-09-10 00:34:52 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 03:06:35 +03:00
|
|
|
class MainPage extends StatefulWidget {
|
|
|
|
MainPage({Key key, this.title}) : super(key: key);
|
2018-09-10 00:34:52 +03:00
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
2018-09-10 03:06:35 +03:00
|
|
|
_MainPageState createState() => new _MainPageState();
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 19:24:26 +03:00
|
|
|
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
2018-09-27 14:51:57 +03:00
|
|
|
HomeAssistant _homeAssistant;
|
|
|
|
EntityCollection _entities;
|
|
|
|
//Map _instanceConfig;
|
2018-10-03 14:36:23 +03:00
|
|
|
String _apiEndpoint;
|
|
|
|
String _apiPassword;
|
|
|
|
String _authType;
|
2018-09-16 14:58:21 +03:00
|
|
|
int _uiViewsCount = 0;
|
2018-09-17 00:28:19 +03:00
|
|
|
String _instanceHost;
|
2018-09-20 23:21:03 +03:00
|
|
|
int _errorCodeToBeShown = 0;
|
|
|
|
String _lastErrorMessage = "";
|
|
|
|
StreamSubscription _stateSubscription;
|
2018-09-21 00:39:49 +03:00
|
|
|
StreamSubscription _settingsSubscription;
|
2018-09-29 13:49:25 +03:00
|
|
|
StreamSubscription _serviceCallSubscription;
|
2018-09-29 16:19:01 +03:00
|
|
|
StreamSubscription _showEntityPageSubscription;
|
2018-09-20 23:21:03 +03:00
|
|
|
bool _isLoading = true;
|
2018-10-03 15:25:01 +03:00
|
|
|
bool _settingsLoaded = false;
|
2018-09-29 13:49:25 +03:00
|
|
|
|
2018-09-23 16:41:50 +03:00
|
|
|
Map<String, Color> _badgeColors = {
|
|
|
|
"default": Color.fromRGBO(223, 76, 30, 1.0),
|
|
|
|
"binary_sensor": Color.fromRGBO(3, 155, 229, 1.0)
|
|
|
|
};
|
2018-09-10 03:06:35 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-10-03 15:25:01 +03:00
|
|
|
_settingsLoaded = false;
|
2018-09-16 19:24:26 +03:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2018-10-03 14:36:23 +03:00
|
|
|
|
|
|
|
_homeAssistant = HomeAssistant();
|
|
|
|
|
2018-09-21 00:39:49 +03:00
|
|
|
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
|
2018-09-24 00:05:57 +03:00
|
|
|
TheLogger.log("Debug","Settings change event: reconnect=${event.reconnect}");
|
2018-10-03 14:36:23 +03:00
|
|
|
if (event.reconnect) {
|
|
|
|
_homeAssistant.closeConnection();
|
|
|
|
_initConnection().then((b){
|
|
|
|
setState(() {
|
|
|
|
_homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType);
|
|
|
|
_errorCodeToBeShown = 10;
|
|
|
|
_lastErrorMessage = "Connection settings was changed.";
|
|
|
|
});
|
|
|
|
}, onError: (_) {
|
|
|
|
setState(() {
|
|
|
|
_lastErrorMessage = _;
|
|
|
|
_errorCodeToBeShown = 5;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_initConnection().then((_){
|
|
|
|
_createConnection();
|
|
|
|
}, onError: (_) {
|
2018-09-20 23:21:03 +03:00
|
|
|
setState(() {
|
2018-10-03 14:36:23 +03:00
|
|
|
_lastErrorMessage = _;
|
|
|
|
_errorCodeToBeShown = 5;
|
2018-09-20 23:21:03 +03:00
|
|
|
});
|
|
|
|
});
|
2018-09-10 03:06:35 +03:00
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-16 19:24:26 +03:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
2018-09-24 00:05:57 +03:00
|
|
|
TheLogger.log("Debug","$state");
|
2018-10-03 15:25:01 +03:00
|
|
|
if (state == AppLifecycleState.resumed && _settingsLoaded) {
|
2018-09-16 19:24:26 +03:00
|
|
|
_refreshData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:21:03 +03:00
|
|
|
_initConnection() async {
|
2018-09-10 03:06:35 +03:00
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2018-09-17 00:28:19 +03:00
|
|
|
String domain = prefs.getString('hassio-domain');
|
|
|
|
String port = prefs.getString('hassio-port');
|
2018-09-17 22:20:36 +03:00
|
|
|
_instanceHost = "$domain:$port";
|
2018-10-03 14:36:23 +03:00
|
|
|
_apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket";
|
2018-09-23 02:04:44 +03:00
|
|
|
homeAssistantWebHost = "${prefs.getString('hassio-res-protocol')}://$domain:$port";
|
2018-10-03 14:36:23 +03:00
|
|
|
_apiPassword = prefs.getString('hassio-password');
|
|
|
|
_authType = prefs.getString('hassio-auth-type');
|
|
|
|
if ((domain == null) || (port == null) || (_apiPassword == null) ||
|
|
|
|
(domain.length == 0) || (port.length == 0) || (_apiPassword.length == 0)) {
|
|
|
|
throw("Check connection settings");
|
2018-10-03 15:25:01 +03:00
|
|
|
} else {
|
|
|
|
_settingsLoaded = true;
|
2018-09-20 23:21:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:36:23 +03:00
|
|
|
_createConnection() {
|
2018-09-17 01:03:34 +03:00
|
|
|
_refreshData();
|
2018-10-03 14:36:23 +03:00
|
|
|
if (_stateSubscription == null) {
|
|
|
|
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
|
|
|
|
setState(() {
|
|
|
|
if (event.localChange) {
|
|
|
|
_entities
|
|
|
|
.get(event.entityId)
|
|
|
|
.state = event.newState;
|
|
|
|
}
|
|
|
|
});
|
2018-09-16 18:02:12 +03:00
|
|
|
});
|
2018-10-03 14:36:23 +03:00
|
|
|
}
|
|
|
|
if (_serviceCallSubscription == null) {
|
|
|
|
_serviceCallSubscription =
|
|
|
|
eventBus.on<ServiceCallEvent>().listen((event) {
|
|
|
|
_callService(event.domain, event.service, event.entityId,
|
|
|
|
event.additionalParams);
|
|
|
|
});
|
|
|
|
}
|
2018-09-29 16:19:01 +03:00
|
|
|
|
2018-10-03 14:36:23 +03:00
|
|
|
if (_showEntityPageSubscription == null) {
|
|
|
|
_showEntityPageSubscription =
|
|
|
|
eventBus.on<ShowEntityPageEvent>().listen((event) {
|
|
|
|
_showEntityPage(event.entity);
|
|
|
|
});
|
|
|
|
}
|
2018-09-12 00:32:04 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 01:46:15 +03:00
|
|
|
_refreshData() async {
|
2018-10-03 14:36:23 +03:00
|
|
|
_homeAssistant.updateConnectionSettings(_apiEndpoint, _apiPassword, _authType);
|
2018-09-12 00:32:04 +03:00
|
|
|
setState(() {
|
2018-09-20 23:21:03 +03:00
|
|
|
_isLoading = true;
|
2018-09-12 00:32:04 +03:00
|
|
|
});
|
2018-09-20 23:21:03 +03:00
|
|
|
_errorCodeToBeShown = 0;
|
2018-10-03 14:36:23 +03:00
|
|
|
_lastErrorMessage = "";
|
|
|
|
await _homeAssistant.fetch().then((result) {
|
|
|
|
setState(() {
|
|
|
|
//_instanceConfig = _homeAssistant.instanceConfig;
|
|
|
|
_entities = _homeAssistant.entities;
|
|
|
|
_uiViewsCount = _homeAssistant.viewsCount;
|
|
|
|
_isLoading = false;
|
2018-09-12 00:32:04 +03:00
|
|
|
});
|
2018-10-03 14:36:23 +03:00
|
|
|
}).catchError((e) {
|
|
|
|
_setErrorState(e);
|
|
|
|
});
|
2018-09-11 01:09:21 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 00:01:53 +03:00
|
|
|
_setErrorState(e) {
|
|
|
|
setState(() {
|
|
|
|
_errorCodeToBeShown = e["errorCode"] != null ? e["errorCode"] : 99;
|
|
|
|
_lastErrorMessage = e["errorMessage"] ?? "Unknown error";
|
|
|
|
_isLoading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-29 11:52:17 +03:00
|
|
|
void _callService(String domain, String service, String entityId, Map<String, String> additionalParams) {
|
2018-10-02 22:48:47 +03:00
|
|
|
_homeAssistant.callService(domain, service, entityId, additionalParams).catchError((e) => _setErrorState(e));
|
2018-09-21 00:01:53 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 16:19:01 +03:00
|
|
|
void _showEntityPage(Entity entity) {
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(
|
|
|
|
builder: (context) => EntityViewPage(entity: entity),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
List<Widget> _buildViews() {
|
|
|
|
List<Widget> result = [];
|
2018-09-28 10:15:25 +03:00
|
|
|
if ((_entities != null) && (!_homeAssistant.uiBuilder.isEmpty)) {
|
|
|
|
_homeAssistant.uiBuilder.views.forEach((viewId, view) {
|
2018-09-21 23:05:12 +03:00
|
|
|
result.add(
|
|
|
|
RefreshIndicator(
|
|
|
|
color: Colors.amber,
|
|
|
|
child: ListView(
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
2018-09-28 10:15:25 +03:00
|
|
|
children: _buildSingleView(view),
|
2018-09-21 23:05:12 +03:00
|
|
|
),
|
|
|
|
onRefresh: () => _refreshData(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2018-09-15 12:56:42 +03:00
|
|
|
}
|
2018-09-16 00:06:07 +03:00
|
|
|
return result;
|
|
|
|
}
|
2018-09-15 12:56:42 +03:00
|
|
|
|
2018-09-28 10:15:25 +03:00
|
|
|
List<Widget> _buildSingleView(View view) {
|
2018-09-21 23:05:12 +03:00
|
|
|
List<Widget> result = [];
|
2018-09-28 10:15:25 +03:00
|
|
|
if (view.isThereBadges) {
|
2018-09-23 00:35:16 +03:00
|
|
|
result.add(
|
2018-09-28 11:18:37 +03:00
|
|
|
Wrap(
|
|
|
|
alignment: WrapAlignment.center,
|
2018-09-28 13:33:15 +03:00
|
|
|
spacing: 10.0,
|
|
|
|
runSpacing: 1.0,
|
2018-09-28 11:18:37 +03:00
|
|
|
children: _buildBadges(view.badges),
|
|
|
|
)
|
2018-09-23 00:35:16 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2018-09-28 10:15:25 +03:00
|
|
|
view.cards.forEach((id, card) {
|
|
|
|
if (card.entities.isNotEmpty) {
|
|
|
|
result.add(_buildCard(card));
|
2018-09-21 23:05:12 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-28 10:15:25 +03:00
|
|
|
List<Widget> _buildBadges( Map<String, Badge> badges) {
|
2018-09-23 00:35:16 +03:00
|
|
|
List<Widget> result = [];
|
2018-09-28 10:15:25 +03:00
|
|
|
badges.forEach((id, badge) {
|
|
|
|
var badgeEntity = _entities.get(id);
|
|
|
|
if (badgeEntity != null) {
|
2018-09-23 00:35:16 +03:00
|
|
|
result.add(
|
2018-09-28 10:15:25 +03:00
|
|
|
_buildSingleBadge(badgeEntity)
|
2018-09-23 00:35:16 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:51:57 +03:00
|
|
|
Widget _buildSingleBadge(Entity data) {
|
2018-09-23 16:07:41 +03:00
|
|
|
double iconSize = 26.0;
|
2018-09-23 00:35:16 +03:00
|
|
|
Widget badgeIcon;
|
2018-09-23 02:39:45 +03:00
|
|
|
String badgeTextValue;
|
2018-09-27 14:51:57 +03:00
|
|
|
Color iconColor = _badgeColors[data.domain] ?? _badgeColors["default"];
|
|
|
|
switch (data.domain) {
|
2018-09-23 00:35:16 +03:00
|
|
|
case "sun": {
|
2018-09-27 14:51:57 +03:00
|
|
|
badgeIcon = data.state == "below_horizon" ?
|
2018-09-23 16:07:41 +03:00
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconCode(0xf0dc),
|
|
|
|
size: iconSize,
|
|
|
|
) :
|
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconCode(0xf5a8),
|
|
|
|
size: iconSize,
|
|
|
|
);
|
2018-09-23 00:35:16 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "sensor": {
|
2018-09-27 14:51:57 +03:00
|
|
|
badgeTextValue = data.unitOfMeasurement;
|
2018-09-23 00:35:16 +03:00
|
|
|
badgeIcon = Center(
|
|
|
|
child: Text(
|
2018-09-28 13:33:15 +03:00
|
|
|
"${data.state == 'unknown' ? '-' : data.state}",
|
2018-09-23 02:04:44 +03:00
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
softWrap: false,
|
2018-09-23 00:35:16 +03:00
|
|
|
textAlign: TextAlign.center,
|
2018-09-28 13:33:15 +03:00
|
|
|
style: TextStyle(fontSize: 17.0),
|
2018-09-23 00:35:16 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-23 02:39:45 +03:00
|
|
|
case "device_tracker": {
|
2018-09-28 13:33:15 +03:00
|
|
|
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(data, iconSize,Colors.black);
|
2018-09-27 14:51:57 +03:00
|
|
|
badgeTextValue = data.state;
|
2018-09-23 02:39:45 +03:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 00:35:16 +03:00
|
|
|
default: {
|
2018-09-28 13:33:15 +03:00
|
|
|
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(data, iconSize,Colors.black);
|
2018-09-23 00:35:16 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 02:39:45 +03:00
|
|
|
Widget badgeText;
|
2018-09-28 13:33:15 +03:00
|
|
|
if (badgeTextValue == null || badgeTextValue.length == 0) {
|
2018-09-23 02:39:45 +03:00
|
|
|
badgeText = Container(width: 0.0, height: 0.0);
|
|
|
|
} else {
|
|
|
|
badgeText = Container(
|
|
|
|
padding: EdgeInsets.fromLTRB(6.0, 2.0, 6.0, 2.0),
|
|
|
|
child: Text("$badgeTextValue",
|
2018-09-28 13:33:15 +03:00
|
|
|
style: TextStyle(fontSize: 12.0, color: Colors.white),
|
2018-09-23 02:39:45 +03:00
|
|
|
textAlign: TextAlign.center, softWrap: false, overflow: TextOverflow.fade),
|
|
|
|
decoration: new BoxDecoration(
|
|
|
|
// Circle shape
|
|
|
|
//shape: BoxShape.circle,
|
2018-09-23 16:41:50 +03:00
|
|
|
color: iconColor,
|
2018-09-23 02:39:45 +03:00
|
|
|
borderRadius: BorderRadius.circular(9.0),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-09-23 00:35:16 +03:00
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
|
|
|
|
width: 50.0,
|
|
|
|
height: 50.0,
|
|
|
|
decoration: new BoxDecoration(
|
|
|
|
// Circle shape
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
color: Colors.white,
|
|
|
|
// The border you want
|
|
|
|
border: new Border.all(
|
|
|
|
width: 2.0,
|
2018-09-23 16:41:50 +03:00
|
|
|
color: iconColor,
|
2018-09-23 00:35:16 +03:00
|
|
|
),
|
|
|
|
),
|
2018-09-23 02:39:45 +03:00
|
|
|
child: Stack(
|
|
|
|
overflow: Overflow.visible,
|
|
|
|
children: <Widget>[
|
|
|
|
Positioned(
|
|
|
|
width: 46.0,
|
|
|
|
height: 46.0,
|
|
|
|
top: 0.0,
|
|
|
|
left: 0.0,
|
|
|
|
child: badgeIcon,
|
|
|
|
),
|
|
|
|
Positioned(
|
|
|
|
//width: 50.0,
|
|
|
|
bottom: -9.0,
|
2018-09-28 13:33:15 +03:00
|
|
|
left: -10.0,
|
|
|
|
right: -10.0,
|
2018-09-23 02:39:45 +03:00
|
|
|
child: Center(
|
|
|
|
child: badgeText,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2018-09-23 00:35:16 +03:00
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 60.0,
|
|
|
|
child: Text(
|
2018-09-27 14:51:57 +03:00
|
|
|
"${data.displayName}",
|
2018-09-23 00:35:16 +03:00
|
|
|
textAlign: TextAlign.center,
|
2018-09-28 13:33:15 +03:00
|
|
|
style: TextStyle(fontSize: 12.0),
|
2018-09-23 00:35:16 +03:00
|
|
|
softWrap: true,
|
2018-09-28 13:33:15 +03:00
|
|
|
maxLines: 3,
|
2018-09-23 00:35:16 +03:00
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-28 10:15:25 +03:00
|
|
|
Card _buildCard(HACard card) {
|
2018-09-15 12:56:42 +03:00
|
|
|
List<Widget> body = [];
|
2018-09-28 10:15:25 +03:00
|
|
|
body.add(_buildCardHeader(card.friendlyName));
|
|
|
|
body.addAll(_buildCardBody(card.entities));
|
|
|
|
Card result = Card(
|
|
|
|
child: new Column(mainAxisSize: MainAxisSize.min, children: body)
|
|
|
|
);
|
2018-09-15 12:56:42 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:58:21 +03:00
|
|
|
Widget _buildCardHeader(String name) {
|
2018-09-15 12:56:42 +03:00
|
|
|
var result;
|
2018-09-29 23:13:02 +03:00
|
|
|
if (name.trim().length > 0) {
|
2018-09-15 12:56:42 +03:00
|
|
|
result = new ListTile(
|
|
|
|
//leading: const Icon(Icons.device_hub),
|
|
|
|
//subtitle: Text(".."),
|
|
|
|
//trailing: Text("${data["state"]}"),
|
2018-09-16 00:06:07 +03:00
|
|
|
title: Text("$name",
|
2018-09-15 12:56:42 +03:00
|
|
|
textAlign: TextAlign.left,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
2018-09-16 00:06:07 +03:00
|
|
|
style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
|
2018-09-15 12:56:42 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
result = new Container(width: 0.0, height: 0.0);
|
|
|
|
}
|
|
|
|
return result;
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:05:12 +03:00
|
|
|
List<Widget> _buildCardBody(List ids) {
|
2018-09-15 12:56:42 +03:00
|
|
|
List<Widget> entities = [];
|
2018-09-16 00:06:07 +03:00
|
|
|
ids.forEach((id) {
|
2018-09-29 13:49:25 +03:00
|
|
|
var entity = _entities.get(id);
|
|
|
|
if (entity != null) {
|
2018-09-29 11:52:17 +03:00
|
|
|
entities.add(
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
|
2018-10-02 00:41:40 +03:00
|
|
|
child: entity.buildWidget(context, true),
|
2018-09-29 13:49:25 +03:00
|
|
|
));
|
2018-09-17 22:20:36 +03:00
|
|
|
}
|
2018-09-15 12:56:42 +03:00
|
|
|
});
|
|
|
|
return entities;
|
2018-09-12 22:31:58 +03:00
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
|
2018-09-16 14:58:21 +03:00
|
|
|
List<Tab> buildUIViewTabs() {
|
|
|
|
List<Tab> result = [];
|
2018-09-28 10:15:25 +03:00
|
|
|
if ((_entities != null) && (!_homeAssistant.uiBuilder.isEmpty)) {
|
|
|
|
_homeAssistant.uiBuilder.views.forEach((viewId, view) {
|
2018-09-16 14:58:21 +03:00
|
|
|
result.add(
|
2018-09-17 22:20:36 +03:00
|
|
|
Tab(
|
2018-09-28 13:33:15 +03:00
|
|
|
icon: MaterialDesignIcons.createIconWidgetFromEntityData(_entities.get(viewId), 24.0, null) ??
|
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"),
|
|
|
|
size: 24.0,
|
|
|
|
)
|
2018-09-17 22:20:36 +03:00
|
|
|
)
|
2018-09-16 14:58:21 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:12:11 +03:00
|
|
|
Widget _buildAppTitle() {
|
2018-09-15 01:46:15 +03:00
|
|
|
Row titleRow = Row(
|
2018-09-27 14:51:57 +03:00
|
|
|
children: [Text(_homeAssistant != null ? _homeAssistant.locationName : "")],
|
2018-09-15 01:46:15 +03:00
|
|
|
);
|
2018-09-20 23:21:03 +03:00
|
|
|
if (_isLoading) {
|
2018-09-15 01:46:15 +03:00
|
|
|
titleRow.children.add(Padding(
|
|
|
|
child: JumpingDotsProgressIndicator(
|
2018-09-17 00:28:19 +03:00
|
|
|
fontSize: 26.0,
|
2018-09-15 01:46:15 +03:00
|
|
|
color: Colors.white,
|
|
|
|
),
|
2018-09-17 00:28:19 +03:00
|
|
|
padding: const EdgeInsets.fromLTRB(5.0, 0.0, 0.0, 30.0),
|
2018-09-16 00:06:07 +03:00
|
|
|
));
|
2018-09-15 01:46:15 +03:00
|
|
|
}
|
|
|
|
return titleRow;
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
|
|
|
|
2018-09-16 14:58:21 +03:00
|
|
|
Drawer _buildAppDrawer() {
|
|
|
|
return new Drawer(
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
new UserAccountsDrawerHeader(
|
2018-09-27 14:51:57 +03:00
|
|
|
accountName: Text(_homeAssistant != null ? _homeAssistant.locationName : "Unknown"),
|
2018-09-17 00:28:19 +03:00
|
|
|
accountEmail: Text(_instanceHost ?? "Not configured"),
|
|
|
|
currentAccountPicture: new Image.asset('images/hassio-192x192.png'),
|
2018-09-16 14:58:21 +03:00
|
|
|
),
|
|
|
|
new ListTile(
|
|
|
|
leading: Icon(Icons.settings),
|
2018-09-17 00:28:19 +03:00
|
|
|
title: Text("Connection settings"),
|
2018-09-16 14:58:21 +03:00
|
|
|
onTap: () {
|
2018-09-24 22:23:01 +03:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
Navigator.of(context).pushNamed('/connection-settings');
|
2018-09-16 14:58:21 +03:00
|
|
|
},
|
|
|
|
),
|
2018-09-24 00:05:57 +03:00
|
|
|
new ListTile(
|
|
|
|
leading: Icon(Icons.insert_drive_file),
|
|
|
|
title: Text("Log"),
|
|
|
|
onTap: () {
|
2018-09-24 22:23:01 +03:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
Navigator.of(context).pushNamed('/log-view');
|
2018-09-24 00:05:57 +03:00
|
|
|
},
|
|
|
|
),
|
2018-09-24 22:12:56 +03:00
|
|
|
new ListTile(
|
|
|
|
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:github-circle")),
|
2018-09-29 12:09:01 +03:00
|
|
|
title: Text("Report an issue"),
|
2018-09-24 22:12:56 +03:00
|
|
|
onTap: () {
|
2018-09-24 22:23:01 +03:00
|
|
|
Navigator.of(context).pop();
|
2018-10-02 00:41:40 +03:00
|
|
|
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
|
2018-09-24 22:12:56 +03:00
|
|
|
},
|
|
|
|
),
|
2018-10-03 14:36:23 +03:00
|
|
|
Container(
|
|
|
|
height: 30.0,
|
|
|
|
decoration: new BoxDecoration(
|
|
|
|
border: new Border(
|
|
|
|
top: BorderSide(
|
|
|
|
width: 2.0,
|
|
|
|
color: Colors.black26,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
new ListTile(
|
|
|
|
leading: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:coffee")),
|
|
|
|
title: Text("By me a coffee"),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
HAUtils.launchURL("https://www.buymeacoffee.com/estevez");
|
|
|
|
},
|
|
|
|
),
|
2018-09-16 14:58:21 +03:00
|
|
|
new AboutListTile(
|
2018-09-17 01:03:34 +03:00
|
|
|
applicationName: appName,
|
|
|
|
applicationVersion: appVersion,
|
2018-09-21 00:57:30 +03:00
|
|
|
applicationLegalese: "Keyboard Crumbs | www.keyboardcrumbs.io",
|
2018-09-16 14:58:21 +03:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:12:11 +03:00
|
|
|
_checkShowInfo(BuildContext context) {
|
2018-09-20 23:21:03 +03:00
|
|
|
if (_errorCodeToBeShown > 0) {
|
|
|
|
String message = _lastErrorMessage;
|
2018-09-18 00:12:11 +03:00
|
|
|
SnackBarAction action;
|
2018-09-20 23:21:03 +03:00
|
|
|
switch (_errorCodeToBeShown) {
|
2018-10-02 16:00:55 +03:00
|
|
|
case 9:
|
2018-10-03 14:36:23 +03:00
|
|
|
case 11:
|
|
|
|
case 7:
|
2018-09-18 00:12:11 +03:00
|
|
|
case 1: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Retry",
|
2018-09-20 23:21:03 +03:00
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
_refreshData();
|
|
|
|
},
|
2018-09-18 00:12:11 +03:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-20 23:21:03 +03:00
|
|
|
|
|
|
|
case 5: {
|
|
|
|
message = "Check connection settings";
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Open",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
Navigator.pushNamed(context, '/connection-settings');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 6: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Settings",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
Navigator.pushNamed(context, '/connection-settings');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:36:23 +03:00
|
|
|
case 10: {
|
2018-09-20 23:21:03 +03:00
|
|
|
action = SnackBarAction(
|
2018-10-03 14:36:23 +03:00
|
|
|
label: "Refresh",
|
2018-09-20 23:21:03 +03:00
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
_refreshData();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-21 00:01:53 +03:00
|
|
|
|
|
|
|
case 8: {
|
|
|
|
action = SnackBarAction(
|
|
|
|
label: "Reconnect",
|
|
|
|
onPressed: () {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
|
|
|
_refreshData();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-18 00:12:11 +03:00
|
|
|
}
|
|
|
|
Timer(Duration(seconds: 1), () {
|
|
|
|
_scaffoldKey.currentState.hideCurrentSnackBar();
|
|
|
|
_scaffoldKey.currentState.showSnackBar(
|
|
|
|
SnackBar(
|
2018-09-20 23:21:03 +03:00
|
|
|
content: Text("$message (code: $_errorCodeToBeShown)"),
|
2018-09-18 00:12:11 +03:00
|
|
|
action: action,
|
|
|
|
duration: Duration(hours: 1),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2018-09-21 00:39:49 +03:00
|
|
|
} else {
|
|
|
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
2018-09-18 00:12:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
|
|
|
|
|
2018-09-24 22:54:51 +03:00
|
|
|
Scaffold _buildScaffold(bool empty) {
|
|
|
|
return Scaffold(
|
|
|
|
key: _scaffoldKey,
|
|
|
|
appBar: AppBar(
|
|
|
|
title: _buildAppTitle(),
|
|
|
|
bottom: empty ? null : TabBar(tabs: buildUIViewTabs()),
|
|
|
|
),
|
|
|
|
drawer: _buildAppDrawer(),
|
|
|
|
body: empty ?
|
|
|
|
Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Icon(
|
|
|
|
MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"),
|
|
|
|
size: 100.0,
|
|
|
|
color: _errorCodeToBeShown == 0 ? Colors.blue : Colors.redAccent,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
:
|
|
|
|
TabBarView(
|
|
|
|
children: _buildViews()
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-10 00:34:52 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2018-09-18 00:12:11 +03:00
|
|
|
_checkShowInfo(context);
|
2018-09-16 14:58:21 +03:00
|
|
|
// This method is rerun every time setState is called.
|
2018-09-27 14:51:57 +03:00
|
|
|
if (_entities == null) {
|
2018-09-24 22:54:51 +03:00
|
|
|
return _buildScaffold(true);
|
2018-09-16 14:58:21 +03:00
|
|
|
} else {
|
|
|
|
return DefaultTabController(
|
|
|
|
length: _uiViewsCount,
|
2018-09-24 22:54:51 +03:00
|
|
|
child: _buildScaffold(false)
|
2018-09-16 14:58:21 +03:00
|
|
|
);
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|
2018-09-12 00:32:04 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2018-09-16 19:24:26 +03:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2018-09-21 00:39:49 +03:00
|
|
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
|
|
|
if (_settingsSubscription != null) _settingsSubscription.cancel();
|
2018-09-29 13:49:25 +03:00
|
|
|
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel();
|
2018-09-29 16:19:01 +03:00
|
|
|
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
|
2018-09-27 14:51:57 +03:00
|
|
|
_homeAssistant.closeConnection();
|
2018-09-12 00:32:04 +03:00
|
|
|
super.dispose();
|
|
|
|
}
|
2018-09-10 00:34:52 +03:00
|
|
|
}
|