From ef15026203080a29cbc30764a8e48718eabbc101 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Sun, 16 Jun 2019 16:32:55 +0300 Subject: [PATCH] Fix authentication process. App register in background --- lib/connection.class.dart | 82 ++++++++++++++++++++--------------- lib/home_assistant.class.dart | 2 +- lib/main.dart | 1 + pubspec.lock | 23 ++++++---- pubspec.yaml | 1 + 5 files changed, 64 insertions(+), 45 deletions(-) diff --git a/lib/connection.class.dart b/lib/connection.class.dart index 3deadb1..3bdb352 100644 --- a/lib/connection.class.dart +++ b/lib/connection.class.dart @@ -105,56 +105,65 @@ class Connection { _disconnect().then((_) { completer?.completeError(HAError("Connection timeout")); }); - }).then((_) => completer?.complete()).catchError((e) { + }).then((_) { + Logger.d("doConnect is finished 1"); + completer?.complete(); + }).catchError((e) { completer?.completeError(e); }); } else { + Logger.d("doConnect is finished 2"); completer?.complete(); } } Completer connecting; - Future _connect() async { + Future _connect() { if (connecting != null && !connecting.isCompleted) { Logger.w("Previous connection attempt pending..."); return connecting.future; } else { connecting = Completer(); - await _disconnect(); - Logger.d("Socket connecting: $_webSocketAPIEndpoint..."); - _socket = IOWebSocketChannel.connect( - _webSocketAPIEndpoint, pingInterval: Duration(seconds: 15)); - _socketSubscription = _socket.stream.listen( - (message) { - isConnected = true; - var data = json.decode(message); - if (data["type"] == "auth_required") { - Logger.d("[Received] <== ${data.toString()}"); - _authenticate().then((_) => connecting.complete()).catchError(( - e) { - if (!connecting.isCompleted) connecting.completeError(e); - }); - } else if (data["type"] == "auth_ok") { - Logger.d("[Received] <== ${data.toString()}"); - _messageResolver["auth"]?.complete(); - _messageResolver.remove("auth"); - if (!connecting.isCompleted) connecting.complete(); - } else if (data["type"] == "auth_invalid") { - Logger.d("[Received] <== ${data.toString()}"); - _messageResolver["auth"]?.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()])); - _messageResolver.remove("auth"); - logout().then((_) { - if (!connecting.isCompleted) connecting.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()])); - }); - } else { - _handleMessage(data); - } - }, - cancelOnError: true, - onDone: () => _handleSocketClose(connecting), - onError: (e) => _handleSocketError(e, connecting) - ); + _disconnect().then((_) { + Logger.d("Socket connecting: $_webSocketAPIEndpoint..."); + _socket = IOWebSocketChannel.connect( + _webSocketAPIEndpoint, pingInterval: Duration(seconds: 15)); + _socketSubscription = _socket.stream.listen( + (message) { + isConnected = true; + var data = json.decode(message); + if (data["type"] == "auth_required") { + Logger.d("[Received] <== ${data.toString()}"); + _authenticate().then((_) { + Logger.d('Authentication complete'); + connecting.complete(); + }).catchError((e) { + if (!connecting.isCompleted) connecting.completeError(e); + }); + } else if (data["type"] == "auth_ok") { + Logger.d("[Received] <== ${data.toString()}"); + _messageResolver["auth"]?.complete(); + _messageResolver.remove("auth"); + if (_token != null) { + if (!connecting.isCompleted) connecting.complete(); + } + } else if (data["type"] == "auth_invalid") { + Logger.d("[Received] <== ${data.toString()}"); + _messageResolver["auth"]?.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()])); + _messageResolver.remove("auth"); + logout().then((_) { + if (!connecting.isCompleted) connecting.completeError(HAError("${data["message"]}", actions: [HAErrorAction.loginAgain()])); + }); + } else { + _handleMessage(data); + } + }, + cancelOnError: true, + onDone: () => _handleSocketClose(connecting), + onError: (e) => _handleSocketError(e, connecting) + ); + }); return connecting.future; } } @@ -262,6 +271,7 @@ class Connection { ).then((_) { Logger.d("Requesting long-lived token..."); _getLongLivedToken().then((_) { + Logger.d("getLongLivedToken finished"); completer.complete(); }).catchError((e) { Logger.e("Can't get long-lived token: $e"); diff --git a/lib/home_assistant.class.dart b/lib/home_assistant.class.dart index 88a1699..2888111 100644 --- a/lib/home_assistant.class.dart +++ b/lib/home_assistant.class.dart @@ -58,7 +58,6 @@ class HomeAssistant { futures.add(_getServices()); futures.add(_getUserInfo()); futures.add(_getPanels()); - futures.add(checkAppRegistration()); futures.add(Connection().sendSocketMessage( type: "subscribe_events", additionalData: {"event_type": "state_changed"}, @@ -67,6 +66,7 @@ class HomeAssistant { if (isMobileAppEnabled) { _createUI(); _fetchCompleter.complete(); + checkAppRegistration(); } else { _fetchCompleter.completeError(HAError("Mobile app component not found", actions: [HAErrorAction.tryAgain(), HAErrorAction(type: HAErrorActionType.URL ,title: "Help",url: "http://ha-client.homemade.systems/docs#mobile-app")])); } diff --git a/lib/main.dart b/lib/main.dart index 1dd468a..37e8cb8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -347,6 +347,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker void _showDialog({String title, String body, var onPositive, var onNegative, String positiveText, String negativeText}) { // flutter defined function showDialog( + barrierDismissible: false, context: context, builder: (BuildContext context) { // return object of type Dialog diff --git a/pubspec.lock b/pubspec.lock index cd760de..079e23f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -21,7 +21,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" + version: "2.1.0" boolean_selector: dependency: transitive description: @@ -141,6 +141,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.7.0" + flutter_local_notifications: + dependency: "direct main" + description: + name: flutter_local_notifications + url: "https://pub.dartlang.org" + source: hosted + version: "0.7.1+3" flutter_markdown: dependency: "direct main" description: @@ -222,7 +229,7 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.3+1" + version: "0.12.5" meta: dependency: transitive description: @@ -264,7 +271,7 @@ packages: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.4.0" + version: "1.5.0" petitparser: dependency: transitive description: @@ -292,7 +299,7 @@ packages: name: quiver url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.2" shared_preferences: dependency: "direct main" description: @@ -311,7 +318,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.4" + version: "1.5.5" sqflite: dependency: transitive description: @@ -332,7 +339,7 @@ packages: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "1.6.8" + version: "2.0.0" string_scanner: dependency: transitive description: @@ -360,7 +367,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.2" + version: "0.2.4" typed_data: dependency: transitive description: @@ -411,5 +418,5 @@ packages: source: hosted version: "2.1.15" sdks: - dart: ">=2.1.0 <3.0.0" + dart: ">=2.2.0 <3.0.0" flutter: ">=1.2.1 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 50a59b5..a32868c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -24,6 +24,7 @@ dependencies: flutter_webview_plugin: ^0.3.1 flutter_secure_storage: ^3.2.0 device_info: ^0.4.0+1 + flutter_local_notifications: ^0.7.1+3 dev_dependencies: flutter_test: