2019-08-24 21:22:32 +03:00
part of ' ../main.dart ' ;
2018-09-10 03:06:35 +03:00
2018-09-17 00:28:19 +03:00
class ConnectionSettingsPage extends StatefulWidget {
2019-03-20 19:01:30 +02:00
ConnectionSettingsPage ( { Key key , this . title } ) : super ( key: key ) ;
2018-09-10 03:06:35 +03:00
final String title ;
@ override
2018-09-17 00:28:19 +03:00
_ConnectionSettingsPageState createState ( ) = > new _ConnectionSettingsPageState ( ) ;
2018-09-10 03:06:35 +03:00
}
2018-09-17 00:28:19 +03:00
class _ConnectionSettingsPageState extends State < ConnectionSettingsPage > {
2018-09-10 03:06:35 +03:00
String _hassioDomain = " " ;
2018-10-06 20:03:20 +03:00
String _newHassioDomain = " " ;
2018-10-06 16:01:38 +03:00
String _hassioPort = " " ;
2018-10-06 20:03:20 +03:00
String _newHassioPort = " " ;
2018-09-16 21:27:16 +03:00
String _socketProtocol = " wss " ;
2018-10-06 20:03:20 +03:00
String _newSocketProtocol = " wss " ;
2019-08-26 18:04:40 +03:00
String _longLivedToken = " " ;
String _newLongLivedToken = " " ;
2019-01-25 22:29:16 +02:00
bool _useLovelace = true ;
bool _newUseLovelace = true ;
2018-09-10 03:06:35 +03:00
2019-03-19 23:07:40 +02:00
String oauthUrl ;
2019-08-26 18:04:40 +03:00
bool useOAuth = false ;
2019-03-19 23:07:40 +02:00
2018-09-10 03:06:35 +03:00
@ override
void initState ( ) {
super . initState ( ) ;
_loadSettings ( ) ;
2019-03-20 19:01:30 +02:00
2018-09-10 03:06:35 +03:00
}
_loadSettings ( ) async {
SharedPreferences prefs = await SharedPreferences . getInstance ( ) ;
2019-08-26 18:04:40 +03:00
final storage = new FlutterSecureStorage ( ) ;
try {
useOAuth = prefs . getBool ( " oauth-used " ) ? ? true ;
} catch ( e ) {
useOAuth = true ;
}
if ( ! useOAuth ) {
try {
_longLivedToken = _newLongLivedToken =
await storage . read ( key: " hacl_llt " ) ;
} catch ( e ) {
_longLivedToken = _newLongLivedToken = " " ;
await storage . delete ( key: " hacl_llt " ) ;
}
}
2018-09-10 03:06:35 +03:00
setState ( ( ) {
2018-10-06 20:03:20 +03:00
_hassioDomain = _newHassioDomain = prefs . getString ( " hassio-domain " ) ? ? " " ;
_hassioPort = _newHassioPort = prefs . getString ( " hassio-port " ) ? ? " " ;
_socketProtocol = _newSocketProtocol = prefs . getString ( " hassio-protocol " ) ? ? ' wss ' ;
2018-10-27 00:54:05 +03:00
try {
2019-01-25 22:29:16 +02:00
_useLovelace = _newUseLovelace = prefs . getBool ( " use-lovelace " ) ? ? true ;
2018-10-27 00:54:05 +03:00
} catch ( e ) {
2019-01-25 22:29:16 +02:00
_useLovelace = _newUseLovelace = true ;
2018-10-27 00:54:05 +03:00
}
2018-10-06 20:03:20 +03:00
} ) ;
}
2018-11-06 14:01:00 +02:00
bool _checkConfigChanged ( ) {
2019-03-20 23:05:25 +02:00
return (
2018-11-05 20:34:56 +02:00
( _newHassioPort ! = _hassioPort ) | |
( _newHassioDomain ! = _hassioDomain ) | |
( _newSocketProtocol ! = _socketProtocol ) | |
2019-08-26 18:04:40 +03:00
( _newUseLovelace ! = _useLovelace ) | |
( _newLongLivedToken ! = _longLivedToken ) ) ;
2018-11-05 20:34:56 +02:00
2018-09-10 03:06:35 +03:00
}
_saveSettings ( ) async {
2019-09-05 00:51:29 +03:00
_newHassioDomain = _newHassioDomain . trim ( ) ;
2018-10-07 09:55:37 +03:00
if ( _newHassioDomain . indexOf ( " http " ) = = 0 & & _newHassioDomain . indexOf ( " // " ) > 0 ) {
_newHassioDomain = _newHassioDomain . split ( " // " ) [ 1 ] ;
2018-09-23 22:24:48 +03:00
}
2019-08-21 18:48:53 +03:00
_newHassioDomain = _newHassioDomain . split ( " / " ) [ 0 ] ;
2018-09-10 03:06:35 +03:00
SharedPreferences prefs = await SharedPreferences . getInstance ( ) ;
2019-08-26 18:04:40 +03:00
final storage = new FlutterSecureStorage ( ) ;
if ( _newLongLivedToken . isNotEmpty ) {
2019-09-05 00:51:29 +03:00
_newLongLivedToken = _newLongLivedToken . trim ( ) ;
2019-08-26 18:04:40 +03:00
prefs . setBool ( " oauth-used " , false ) ;
await storage . write ( key: " hacl_llt " , value: _newLongLivedToken ) ;
} else if ( ! useOAuth ) {
await storage . delete ( key: " hacl_llt " ) ;
}
2018-10-07 09:55:37 +03:00
prefs . setString ( " hassio-domain " , _newHassioDomain ) ;
2019-09-05 00:48:20 +03:00
if ( _newHassioPort = = null | | _newHassioPort . isEmpty ) {
_newHassioPort = _newSocketProtocol = = " wss " ? " 443 " : " 80 " ;
2019-09-05 00:51:29 +03:00
} else {
_newHassioPort = _newHassioPort . trim ( ) ;
2019-09-05 00:48:20 +03:00
}
2018-10-07 09:55:37 +03:00
prefs . setString ( " hassio-port " , _newHassioPort ) ;
prefs . setString ( " hassio-protocol " , _newSocketProtocol ) ;
prefs . setString ( " hassio-res-protocol " , _newSocketProtocol = = " wss " ? " https " : " http " ) ;
2018-10-27 00:54:05 +03:00
prefs . setBool ( " use-lovelace " , _newUseLovelace ) ;
2018-09-10 03:06:35 +03:00
}
@ override
Widget build ( BuildContext context ) {
return new Scaffold (
appBar: new AppBar (
leading: IconButton ( icon: Icon ( Icons . arrow_back ) , onPressed: ( ) {
2018-10-03 14:36:23 +03:00
Navigator . pop ( context ) ;
2018-09-10 03:06:35 +03:00
} ) ,
title: new Text ( widget . title ) ,
2018-10-03 14:36:23 +03:00
actions: < Widget > [
IconButton (
icon: Icon ( Icons . check ) ,
2018-11-05 20:34:56 +02:00
onPressed: ( ) {
2018-11-06 14:01:00 +02:00
if ( _checkConfigChanged ( ) ) {
2018-12-15 14:09:37 +02:00
Logger . d ( " Settings changed. Saving... " ) ;
2018-11-05 20:34:56 +02:00
_saveSettings ( ) . then ( ( r ) {
2018-10-03 14:36:23 +03:00
Navigator . pop ( context ) ;
2018-10-06 16:01:38 +03:00
eventBus . fire ( SettingsChangedEvent ( true ) ) ;
2018-10-03 14:36:23 +03:00
} ) ;
2018-11-05 20:34:56 +02:00
} else {
2018-12-15 14:09:37 +02:00
Logger . d ( " Settings was not changed " ) ;
2018-11-05 20:34:56 +02:00
Navigator . pop ( context ) ;
}
}
2018-10-03 14:36:23 +03:00
)
] ,
2018-09-10 03:06:35 +03:00
) ,
body: ListView (
2018-11-23 18:16:38 +02:00
scrollDirection: Axis . vertical ,
2018-09-10 03:06:35 +03:00
padding: const EdgeInsets . all ( 20.0 ) ,
children: < Widget > [
2018-10-27 00:54:05 +03:00
Text (
" Connection settings " ,
style: TextStyle (
color: Colors . black45 ,
fontSize: 20.0
) ,
) ,
2018-09-16 21:27:16 +03:00
new Row (
children: [
2018-09-24 10:27:08 +03:00
Text ( " Use ssl (HTTPS) " ) ,
2018-09-16 21:27:16 +03:00
Switch (
2018-10-06 20:03:20 +03:00
value: ( _newSocketProtocol = = " wss " ) ,
2018-09-16 21:27:16 +03:00
onChanged: ( value ) {
2018-11-05 20:34:56 +02:00
setState ( ( ) {
_newSocketProtocol = value ? " wss " : " ws " ;
} ) ;
2018-09-16 21:27:16 +03:00
} ,
)
] ,
) ,
2018-09-10 03:06:35 +03:00
new TextField (
decoration: InputDecoration (
labelText: " Home Assistant domain or ip address "
) ,
2019-09-05 00:43:45 +03:00
controller: TextEditingController . fromValue ( TextEditingValue ( text: _newHassioDomain ) ) ,
2018-09-10 03:06:35 +03:00
onChanged: ( value ) {
2018-10-06 20:03:20 +03:00
_newHassioDomain = value ;
2018-11-06 14:01:00 +02:00
}
2018-09-10 03:06:35 +03:00
) ,
new TextField (
decoration: InputDecoration (
2018-10-06 16:01:38 +03:00
labelText: " Home Assistant port (default is 8123) "
2018-09-10 03:06:35 +03:00
) ,
2019-09-05 00:43:45 +03:00
controller: TextEditingController . fromValue ( TextEditingValue ( text: _newHassioPort ) ) ,
2018-09-10 03:06:35 +03:00
onChanged: ( value ) {
2018-10-06 20:03:20 +03:00
_newHassioPort = value ;
2018-11-06 14:01:00 +02:00
}
2018-09-10 03:06:35 +03:00
) ,
2018-12-14 17:03:18 +02:00
new Text (
" Try ports 80 and 443 if default is not working and you don't know why. " ,
style: TextStyle ( color: Colors . grey ) ,
) ,
2018-10-27 00:54:05 +03:00
Padding (
padding: EdgeInsets . only ( top: 20.0 ) ,
child: Text (
" UI " ,
style: TextStyle (
color: Colors . black45 ,
fontSize: 20.0
) ,
) ,
) ,
new Row (
children: [
Text ( " Use Lovelace UI " ) ,
Switch (
value: _newUseLovelace ,
onChanged: ( value ) {
2018-11-05 20:34:56 +02:00
setState ( ( ) {
_newUseLovelace = value ;
} ) ;
2018-10-27 00:54:05 +03:00
} ,
)
] ,
) ,
2019-08-26 18:04:40 +03:00
Text (
" Authentication settings " ,
style: TextStyle (
color: Colors . black45 ,
fontSize: 20.0
) ,
) ,
Container ( height: 10.0 , ) ,
Text (
" You can leave this field blank to make app generate new long-lived token automatically by asking you to login to your Home Assistant. Use this field only if you still want to use manually generated long-lived token. Leave it blank if you don't understand what we are talking about. " ,
style: TextStyle ( color: Colors . redAccent ) ,
) ,
new TextField (
decoration: InputDecoration (
labelText: " Long-lived token "
) ,
2019-09-05 00:43:45 +03:00
controller: TextEditingController . fromValue ( TextEditingValue ( text: _newLongLivedToken ) ) ,
2019-08-26 18:04:40 +03:00
onChanged: ( value ) {
_newLongLivedToken = value ;
}
) ,
2018-09-10 03:06:35 +03:00
] ,
) ,
) ;
}
2018-10-06 20:03:20 +03:00
@ override
void dispose ( ) {
super . dispose ( ) ;
}
2018-09-24 10:27:08 +03:00
}