2019-10-23 21:22:52 +03:00
part of ' ../main.dart ' ;
class IntegrationSettingsPage extends StatefulWidget {
IntegrationSettingsPage ( { Key key , this . title } ) : super ( key: key ) ;
final String title ;
@ override
_IntegrationSettingsPageState createState ( ) = > new _IntegrationSettingsPageState ( ) ;
}
class _IntegrationSettingsPageState extends State < IntegrationSettingsPage > {
int _locationInterval = LocationManager ( ) . defaultUpdateIntervalMinutes ;
bool _locationTrackingEnabled = false ;
2019-10-24 21:58:48 +03:00
bool _wait = false ;
2019-10-23 21:22:52 +03:00
@ override
void initState ( ) {
super . initState ( ) ;
_loadSettings ( ) ;
}
_loadSettings ( ) async {
SharedPreferences prefs = await SharedPreferences . getInstance ( ) ;
await prefs . reload ( ) ;
SharedPreferences . getInstance ( ) . then ( ( prefs ) {
setState ( ( ) {
_locationTrackingEnabled = prefs . getBool ( " location-enabled " ) ? ? false ;
_locationInterval = prefs . getInt ( " location-interval " ) ? ? LocationManager ( ) . defaultUpdateIntervalMinutes ;
} ) ;
} ) ;
}
void incLocationInterval ( ) {
if ( _locationInterval < 720 ) {
setState ( ( ) {
_locationInterval = _locationInterval + 1 ;
} ) ;
}
}
void decLocationInterval ( ) {
2019-10-28 18:28:01 +02:00
if ( _locationInterval > 15 ) {
2019-10-23 21:22:52 +03:00
setState ( ( ) {
_locationInterval = _locationInterval - 1 ;
} ) ;
}
}
restart ( ) {
eventBus . fire ( ShowPopupDialogEvent (
title: " Are you sure you want to restart Home Assistant? " ,
body: " This will restart your Home Assistant server. " ,
positiveText: " Sure. Make it so " ,
negativeText: " What?? No! " ,
onPositive: ( ) {
ConnectionManager ( ) . callService ( domain: " homeassistant " , service: " restart " , entityId: null ) ;
} ,
) ) ;
}
stop ( ) {
eventBus . fire ( ShowPopupDialogEvent (
title: " Are you sure you want to STOP Home Assistant? " ,
body: " This will STOP your Home Assistant server. It means that your web interface as well as HA Client will not work untill you'll find a way to start your server using ssh or something. " ,
positiveText: " Sure. Make it so " ,
negativeText: " What?? No! " ,
onPositive: ( ) {
ConnectionManager ( ) . callService ( domain: " homeassistant " , service: " stop " , entityId: null ) ;
} ,
) ) ;
}
updateRegistration ( ) {
MobileAppIntegrationManager . checkAppRegistration ( showOkDialog: true ) ;
}
resetRegistration ( ) {
eventBus . fire ( ShowPopupDialogEvent (
title: " Waaaait " ,
body: " If you don't whant to have duplicate integrations and entities in your HA for your current device, first you need to remove MobileApp integration from Integration settings in HA and restart server. " ,
positiveText: " Done it already " ,
negativeText: " Ok, I will " ,
onPositive: ( ) {
MobileAppIntegrationManager . checkAppRegistration ( showOkDialog: true , forceRegister: true ) ;
} ,
) ) ;
}
2019-10-24 22:18:27 +03:00
_switchLocationTrackingState ( bool state ) async {
if ( state ) {
await LocationManager ( ) . updateDeviceLocation ( ) ;
}
await LocationManager ( ) . setSettings ( _locationTrackingEnabled , _locationInterval ) ;
setState ( ( ) {
_wait = false ;
} ) ;
}
2019-10-23 21:22:52 +03:00
@ override
Widget build ( BuildContext context ) {
return new Scaffold (
appBar: new AppBar (
leading: IconButton ( icon: Icon ( Icons . arrow_back ) , onPressed: ( ) {
Navigator . pop ( context ) ;
} ) ,
title: new Text ( widget . title ) ,
) ,
body: ListView (
scrollDirection: Axis . vertical ,
padding: const EdgeInsets . all ( 20.0 ) ,
children: < Widget > [
Text ( " Location tracking " , style: TextStyle ( fontSize: Sizes . largeFontSize - 2 ) ) ,
Container ( height: Sizes . rowPadding , ) ,
2019-10-24 22:41:58 +03:00
InkWell (
onTap: ( ) = > Launcher . launchURLInCustomTab ( context: context , url: " http://ha-client.homemade.systems/docs#location-tracking " ) ,
child: Text (
" Please read documentation! " ,
style: TextStyle (
color: Colors . blue ,
fontSize: 16 ,
decoration: TextDecoration . underline
)
) ,
) ,
Container ( height: Sizes . rowPadding , ) ,
2019-10-23 21:22:52 +03:00
Row (
children: < Widget > [
Text ( " Enable device location tracking " ) ,
Switch (
value: _locationTrackingEnabled ,
2019-10-24 21:58:48 +03:00
onChanged: _wait ? null : ( value ) {
2019-10-23 21:22:52 +03:00
setState ( ( ) {
_locationTrackingEnabled = value ;
2019-10-24 21:58:48 +03:00
_wait = true ;
} ) ;
2019-10-24 22:18:27 +03:00
_switchLocationTrackingState ( value ) ;
2019-10-23 21:22:52 +03:00
} ,
) ,
] ,
) ,
Container ( height: Sizes . rowPadding , ) ,
Text ( " Location update interval in minutes: " ) ,
Row (
mainAxisAlignment: MainAxisAlignment . center ,
mainAxisSize: MainAxisSize . max ,
children: < Widget > [
//Expanded(child: Container(),),
FlatButton (
padding: EdgeInsets . all ( 0.0 ) ,
2019-10-24 22:18:27 +03:00
child: Text ( " - " , style: TextStyle ( fontSize: Sizes . largeFontSize ) ) ,
onPressed: ( ) = > decLocationInterval ( ) ,
2019-10-23 21:22:52 +03:00
) ,
Text ( " $ _locationInterval " , style: TextStyle ( fontSize: Sizes . largeFontSize ) ) ,
FlatButton (
padding: EdgeInsets . all ( 0.0 ) ,
2019-10-24 22:18:27 +03:00
child: Text ( " + " , style: TextStyle ( fontSize: Sizes . largeFontSize ) ) ,
onPressed: ( ) = > incLocationInterval ( ) ,
2019-10-23 21:22:52 +03:00
) ,
] ,
) ,
Divider ( ) ,
2019-10-24 22:18:27 +03:00
Text ( " Integration status " , style: TextStyle ( fontSize: Sizes . largeFontSize - 2 ) ) ,
2019-10-23 21:22:52 +03:00
Container ( height: Sizes . rowPadding , ) ,
Text ( " ${ HomeAssistant ( ) . userName } 's ${ DeviceInfoManager ( ) . model } , ${ DeviceInfoManager ( ) . osName } ${ DeviceInfoManager ( ) . osVersion } " ) ,
Container ( height: 6.0 , ) ,
Text ( " Here you can manually check if HA Client integration with your Home Assistant works fine. As mobileApp integration in Home Assistant is still in development, this is not 100% correct check. " ) ,
//Divider(),
Row (
mainAxisSize: MainAxisSize . max ,
children: < Widget > [
RaisedButton (
color: Colors . blue ,
onPressed: ( ) = > updateRegistration ( ) ,
2019-10-24 22:18:27 +03:00
child: Text ( " Check integration " , style: TextStyle ( color: Colors . white ) )
2019-10-23 21:22:52 +03:00
) ,
Container ( width: 10.0 , ) ,
RaisedButton (
color: Colors . redAccent ,
onPressed: ( ) = > resetRegistration ( ) ,
2019-10-24 22:18:27 +03:00
child: Text ( " Reset integration " , style: TextStyle ( color: Colors . white ) )
2019-10-23 21:22:52 +03:00
)
] ,
) ,
]
) ,
) ;
}
@ override
void dispose ( ) {
LocationManager ( ) . setSettings ( _locationTrackingEnabled , _locationInterval ) ;
super . dispose ( ) ;
}
}