This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/main.dart

318 lines
12 KiB
Dart
Raw Normal View History

2018-09-10 00:34:52 +03:00
import 'dart:convert';
import 'dart:async';
2020-04-25 17:15:19 +03:00
import 'dart:math' as math;
2020-02-12 23:13:49 +02:00
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
2018-09-10 00:34:52 +03:00
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:web_socket_channel/io.dart';
import 'package:event_bus/event_bus.dart';
2018-09-16 19:24:26 +03:00
import 'package:flutter/widgets.dart';
import 'package:cached_network_image/cached_network_image.dart';
2019-03-13 17:05:15 +02:00
import 'package:url_launcher/url_launcher.dart' as urlLauncher;
import 'package:flutter/services.dart';
2018-09-29 16:19:01 +03:00
import 'package:date_format/date_format.dart';
2018-10-07 23:06:06 +03:00
import 'package:http/http.dart' as http;
import 'package:charts_flutter/flutter.dart' as charts;
2019-01-23 23:34:45 +02:00
import 'package:flutter_markdown/flutter_markdown.dart';
2019-03-13 17:05:15 +02:00
import 'package:flutter_custom_tabs/flutter_custom_tabs.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2019-03-30 00:29:52 +02:00
import 'package:device_info/device_info.dart';
2019-08-21 21:30:11 +03:00
import 'package:in_app_purchase/in_app_purchase.dart';
import 'plugins/dynamic_multi_column_layout.dart';
import 'plugins/spoiler_card.dart';
2019-10-20 20:54:29 +03:00
import 'package:workmanager/workmanager.dart' as workManager;
import 'package:geolocator/geolocator.dart';
2019-10-24 21:34:38 +03:00
import 'package:battery/battery.dart';
2020-02-11 14:06:19 +02:00
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
2020-02-13 00:42:43 +02:00
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart' as standaloneWebview;
import 'package:webview_flutter/webview_flutter.dart';
2020-04-07 23:20:57 +03:00
import 'package:syncfusion_flutter_core/core.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';
2019-09-08 19:04:12 +03:00
import 'utils/logger.dart';
2020-04-09 20:10:21 +03:00
import '.secrets.dart';
2019-08-24 21:22:32 +03:00
part 'const.dart';
part 'utils/launcher.dart';
2019-08-24 21:22:32 +03:00
part 'entities/entity.class.dart';
part 'entities/entity_wrapper.class.dart';
part 'entities/timer/timer_entity.class.dart';
part 'entities/switch/switch_entity.class.dart';
part 'entities/button/button_entity.class.dart';
part 'entities/text/text_entity.class.dart';
part 'entities/climate/climate_entity.class.dart';
part 'entities/cover/cover_entity.class.dart';
part 'entities/date_time/date_time_entity.class.dart';
part 'entities/light/light_entity.class.dart';
part 'entities/select/select_entity.class.dart';
part 'entities/sun/sun_entity.class.dart';
part 'entities/sensor/sensor_entity.class.dart';
part 'entities/slider/slider_entity.dart';
part 'entities/media_player/media_player_entity.class.dart';
part 'entities/lock/lock_entity.class.dart';
part 'entities/group/group_entity.class.dart';
part 'entities/fan/fan_entity.class.dart';
part 'entities/automation/automation_entity.class.dart';
part 'entities/camera/camera_entity.class.dart';
part 'entities/alarm_control_panel/alarm_control_panel_entity.class.dart';
2019-09-09 18:50:35 +03:00
part 'entities/entity_model.widget.dart';
part 'entities/default_entity_container.widget.dart';
part 'entities/missed_entity.widget.dart';
2020-04-25 18:59:07 +03:00
part 'cards/entity_button_card.dart';
2019-09-09 18:50:35 +03:00
part 'pages/widgets/entity_attributes_list.dart';
part 'entities/entity_icon.widget.dart';
part 'entities/entity_name.widget.dart';
part 'pages/widgets/last_updated.dart';
part 'entities/climate/widgets/mode_swicth.dart';
part 'entities/climate/widgets/mode_selector.dart';
part 'entities/universal_slider.widget.dart';
part 'entities/flat_service_button.widget.dart';
part 'entities/light/widgets/light_color_picker.dart';
part 'entities/camera/widgets/camera_stream_view.dart';
part 'plugins/history_chart/entity_history.dart';
part 'plugins/history_chart/simple_state_history_chart.dart';
part 'plugins/history_chart/numeric_state_history_chart.dart';
part 'plugins/history_chart/combined_history_chart.dart';
part 'plugins/history_chart/history_control_widget.dart';
part 'plugins/history_chart/entity_history_moment.dart';
2019-08-24 21:22:32 +03:00
part 'entities/switch/widget/switch_state.dart';
part 'entities/slider/widgets/slider_controls.dart';
part 'entities/text/widgets/text_input_state.dart';
part 'entities/select/widgets/select_state.dart';
2019-09-09 18:50:35 +03:00
part 'entities/simple_state.widget.dart';
part 'entities/entity_picture.widget.dart';
2019-08-24 21:22:32 +03:00
part 'entities/timer/widgets/timer_state.dart';
part 'entities/climate/widgets/climate_state.widget.dart';
part 'entities/cover/widgets/cover_state.dart';
part 'entities/date_time/widgets/date_time_state.dart';
part 'entities/lock/widgets/lock_state.dart';
part 'entities/climate/widgets/climate_controls.dart';
part 'entities/climate/widgets/temperature_control_widget.dart';
2019-08-24 21:22:32 +03:00
part 'entities/cover/widgets/cover_controls.widget.dart';
part 'entities/light/widgets/light_controls.dart';
part 'entities/media_player/widgets/media_player_widgets.dart';
part 'entities/fan/widgets/fan_controls.dart';
part 'entities/alarm_control_panel/widgets/alarm_control_panel_controls.widget.dart';
2019-10-14 15:02:49 +03:00
part 'entities/vacuum/vacuum_entity.class.dart';
part 'entities/vacuum/widgets/vacuum_controls.dart';
2019-10-16 19:34:29 +03:00
part 'entities/vacuum/widgets/vacuum_state_button.dart';
part 'entities/error_entity_widget.dart';
2020-04-11 19:09:35 +03:00
part 'pages/settings/connection_settings.part.dart';
part 'pages/purchase.page.dart';
part 'pages/widgets/product_purchase.widget.dart';
part 'pages/widgets/page_loading_indicator.dart';
part 'pages/widgets/bottom_info_bar.dart';
part 'pages/widgets/page_loading_error.dart';
2019-08-24 21:22:32 +03:00
part 'pages/panel.page.dart';
2020-02-11 22:53:29 +02:00
part 'pages/main/main.page.dart';
2020-04-11 19:09:35 +03:00
part 'pages/settings/integration_settings.part.dart';
part 'pages/settings/app_settings.page.dart';
part 'pages/settings/lookandfeel_settings.part.dart';
2020-04-01 20:04:32 +03:00
part 'pages/zha_page.dart';
part 'pages/quick_start.page.dart';
part 'home_assistant.class.dart';
2019-08-24 21:22:32 +03:00
part 'pages/entity.page.dart';
2019-09-09 18:50:35 +03:00
part 'utils/mdi.class.dart';
2018-09-26 22:16:50 +03:00
part 'entity_collection.class.dart';
part 'managers/auth_manager.class.dart';
part 'managers/location_manager.class.dart';
2019-08-31 22:06:52 +03:00
part 'managers/mobile_app_integration_manager.class.dart';
part 'managers/connection_manager.class.dart';
part 'managers/device_info_manager.class.dart';
2019-08-31 23:55:32 +03:00
part 'managers/startup_user_messages_manager.class.dart';
2020-04-04 23:54:32 +03:00
part 'managers/theme_manager.dart';
2019-09-07 18:23:04 +03:00
part 'ui.dart';
part 'view.class.dart';
2019-09-07 15:47:09 +03:00
part 'cards/card.class.dart';
2019-09-07 18:23:04 +03:00
part 'panels/panel_class.dart';
2019-09-14 12:31:50 +03:00
part 'viewWidget.widget.dart';
2019-09-07 18:23:04 +03:00
part 'cards/widgets/card_header.widget.dart';
part 'panels/config_panel_widget.dart';
part 'panels/widgets/link_to_web_config.dart';
2019-09-05 00:17:08 +03:00
part 'types/ha_error.dart';
part 'types/event_bus_events.dart';
2020-04-25 18:59:07 +03:00
part 'cards/gauge_card.dart';
part 'cards/widgets/card_wrapper.widget.dart';
2020-04-25 20:38:21 +03:00
part 'cards/entities_card.dart';
part 'cards/alarm_panel_card.dart';
part 'cards/horizontal_srack_card.dart';
part 'cards/markdown_card.dart';
part 'cards/media_control_card.dart';
part 'cards/unsupported_card.dart';
2020-05-14 15:13:23 +03:00
part 'cards/light_card.dart';
part 'cards/error_card.dart';
2020-04-25 20:38:21 +03:00
part 'cards/vertical_stack_card.dart';
2020-04-25 18:59:07 +03:00
part 'cards/glance_card.dart';
part 'pages/play_media.page.dart';
2019-09-14 19:07:21 +03:00
part 'entities/entity_page_layout.widget.dart';
2019-09-15 10:39:08 +03:00
part 'entities/media_player/widgets/media_player_seek_bar.widget.dart';
part 'entities/media_player/widgets/media_player_progress_bar.widget.dart';
2019-09-15 20:23:03 +03:00
part 'pages/whats_new.page.dart';
2020-03-12 14:22:38 +02:00
part 'pages/fullscreen.page.dart';
2020-05-03 13:30:51 +03:00
part 'popups.dart';
2020-05-09 16:38:05 +03:00
part 'cards/badges.dart';
2020-05-13 13:57:26 +03:00
part 'managers/app_settings.dart';
2018-09-10 00:34:52 +03:00
EventBus eventBus = new EventBus();
2020-05-24 15:42:31 +03:00
//final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
//FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
2020-05-06 21:19:41 +03:00
const String appName = 'HA Client';
2020-05-23 18:52:43 +03:00
const String appVersion = String.fromEnvironment('versionName', defaultValue: '0.0.0');
2020-05-25 15:34:53 +03:00
const whatsNewUrl = 'http://ha-client.app/service/whats_new_1.1.0-b2.md';
Future<void> _reportError(dynamic error, dynamic stackTrace) async {
// Print the exception to the console.
if (Logger.isInDebugMode) {
2020-05-01 19:24:13 +03:00
Logger.e('Caught error: $error', skipCrashlytics: true);
Logger.p(stackTrace);
}
2020-02-11 14:06:19 +02:00
Crashlytics.instance.recordError(error, stackTrace);
}
2019-08-28 19:23:04 +03:00
void main() async {
2020-04-30 01:37:12 +03:00
Crashlytics.instance.enableInDevMode = false;
2020-04-09 20:10:21 +03:00
SyncfusionLicense.registerLicense(secrets['syncfusion_license_key']);
2020-02-11 14:06:19 +02:00
FlutterError.onError = (FlutterErrorDetails details) {
2020-05-01 19:24:13 +03:00
Logger.e("Caut Flutter runtime error: ${details.exception}", skipCrashlytics: true);
if (Logger.isInDebugMode) {
FlutterError.dumpErrorToConsole(details);
}
2020-02-11 14:06:19 +02:00
Crashlytics.instance.recordFlutterError(details);
};
2020-04-11 19:09:35 +03:00
WidgetsFlutterBinding.ensureInitialized();
2020-05-13 15:46:25 +03:00
AppSettings().loadAppTheme();
2020-04-11 19:09:35 +03:00
runZoned(() {
2020-04-11 19:09:35 +03:00
runApp(new HAClientApp(
2020-05-13 15:46:25 +03:00
theme: AppSettings().appTheme,
2020-04-11 19:09:35 +03:00
));
}, onError: (error, stack) {
_reportError(error, stack);
});
}
2018-09-10 00:34:52 +03:00
class HAClientApp extends StatefulWidget {
2020-04-11 19:09:35 +03:00
final AppTheme theme;
const HAClientApp({Key key, this.theme: AppTheme.defaultTheme}) : super(key: key);
@override
_HAClientAppState createState() => new _HAClientAppState();
}
class _HAClientAppState extends State<HAClientApp> {
2020-04-11 19:09:35 +03:00
StreamSubscription<List<PurchaseDetails>> _purchaseUpdateSubscription;
StreamSubscription _themeChangeSubscription;
AppTheme _currentTheme = AppTheme.defaultTheme;
@override
void initState() {
InAppPurchaseConnection.enablePendingPurchases();
final Stream purchaseUpdates =
InAppPurchaseConnection.instance.purchaseUpdatedStream;
2020-04-11 19:09:35 +03:00
_purchaseUpdateSubscription = purchaseUpdates.listen((purchases) {
_handlePurchaseUpdates(purchases);
});
2020-04-11 19:09:35 +03:00
_currentTheme = widget.theme;
_themeChangeSubscription = eventBus.on<ChangeThemeEvent>().listen((event){
setState(() {
_currentTheme = event.theme;
});
});
2020-02-17 21:22:38 +02:00
workManager.Workmanager.initialize(
updateDeviceLocationIsolate,
isInDebugMode: false
);
super.initState();
}
void _handlePurchaseUpdates(purchase) {
if (purchase is List<PurchaseDetails>) {
if (purchase[0].status == PurchaseStatus.purchased) {
2020-05-03 13:30:51 +03:00
eventBus.fire(ShowPopupEvent(
2020-05-03 18:26:44 +03:00
popup: Popup(
title: "Thanks a lot!",
body: "Thank you for supporting HA Client development!",
2020-05-03 13:30:51 +03:00
positiveText: "Ok"
)
));
} else {
Logger.d("Purchase change handler: ${purchase[0].status}");
}
} else {
Logger.e("Something wrong with purchase handling. Got: $purchase");
}
}
2018-09-10 00:34:52 +03:00
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: appName,
2020-04-11 19:09:35 +03:00
theme: HAClientTheme().getThemeData(_currentTheme),
darkTheme: HAClientTheme().darkTheme,
2020-04-07 00:43:38 +03:00
debugShowCheckedModeBanner: false,
initialRoute: "/",
routes: {
2019-09-01 00:12:16 +03:00
"/": (context) => MainPage(title: 'HA Client'),
2020-04-11 19:09:35 +03:00
"/app-settings": (context) => AppSettingsPage(),
2020-05-03 17:26:46 +03:00
"/connection-settings": (context) => AppSettingsPage(showSection: AppSettingsSection.connectionSettings),
2020-04-11 19:09:35 +03:00
"/integration-settings": (context) => AppSettingsPage(showSection: AppSettingsSection.integrationSettings),
2019-08-23 14:13:58 +03:00
"/putchase": (context) => PurchasePage(title: "Support app development"),
2019-09-15 17:29:49 +03:00
"/play-media": (context) => PlayMediaPage(
mediaUrl: "${ModalRoute.of(context).settings.arguments != null ? (ModalRoute.of(context).settings.arguments as Map)['url'] : ''}",
mediaType: "${ModalRoute.of(context).settings.arguments != null ? (ModalRoute.of(context).settings.arguments as Map)['type'] ?? '' : ''}",
),
2020-02-13 00:42:43 +02:00
"/webview": (context) => standaloneWebview.WebviewScaffold(
url: "${(ModalRoute.of(context).settings.arguments as Map)['url']}",
appBar: new AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop()
),
title: new Text("${(ModalRoute.of(context).settings.arguments as Map)['title']}"),
),
),
2020-01-27 22:06:02 +02:00
"/whats-new": (context) => WhatsNewPage(),
"/quick-start": (context) => QuickStartPage(),
2020-04-01 20:04:32 +03:00
"/haclient_zha": (context) => ZhaPage(),
2020-02-13 00:42:43 +02:00
"/auth": (context) => new standaloneWebview.WebviewScaffold(
2020-05-13 15:46:25 +03:00
url: "${AppSettings().oauthUrl}",
2020-01-27 22:06:02 +02:00
appBar: new AppBar(
2020-01-27 23:12:05 +02:00
leading: IconButton(
icon: Icon(Icons.help),
onPressed: () => Launcher.launchURLInCustomTab(context: context, url: "https://ha-client.app/help/connection")
2020-01-27 23:12:05 +02:00
),
title: new Text("Login"),
2020-01-27 23:12:05 +02:00
actions: <Widget>[
FlatButton(
child: Text("Long-lived token", style: Theme.of(context).textTheme.button.copyWith(
decoration: TextDecoration.underline
)),
2020-01-27 23:12:05 +02:00
onPressed: () {
2020-05-03 18:26:44 +03:00
eventBus.fire(ShowPopupEvent(
goBackFirst: true,
popup: TokenLoginPopup()
));
2020-01-27 23:12:05 +02:00
},
)
],
2020-01-27 22:06:02 +02:00
),
)
},
2018-09-10 00:34:52 +03:00
);
}
@override
void dispose() {
2020-04-11 19:09:35 +03:00
_purchaseUpdateSubscription.cancel();
_themeChangeSubscription.cancel();
super.dispose();
}
2019-11-14 12:58:56 +02:00
}