Resolves #571, Resolves #490, Resolves #517

This commit is contained in:
estevez-dev
2020-07-07 22:49:51 +03:00
parent 3a7f3db6cd
commit dce93966e3
11 changed files with 235 additions and 418 deletions

View File

@ -56,7 +56,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
SharedPreferences.getInstance().then((prefs) {
HomeAssistant().currentDashboardPath = prefs.getString('lovelace_dashboard_url') ?? HomeAssistant.DEFAULT_DASHBOARD;
_fetchData(useCache: true);
LocationManager();
StartupUserMessagesManager().checkMessagesToShow();
MobileAppIntegrationManager.checkAppRegistration();
});

View File

@ -19,12 +19,10 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
105: "Passive (last known location)",
};
int _locationInterval = AppSettings().defaultLocationUpdateIntervalMinutes;
int _activeLocationInterval = AppSettings().defaultActiveLocationUpdateIntervalSeconds;
Duration _locationInterval;
bool _locationTrackingEnabled = false;
bool _foregroundLocationTrackingEnabled = false;
bool _wait = false;
bool _changedHere = false;
bool _showNotification = true;
int _accuracy = 102;
@override
@ -39,75 +37,72 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
await prefs.reload();
SharedPreferences.getInstance().then((prefs) {
setState(() {
_locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
_accuracy = prefs.getInt("location-updates-priority") ?? 102;
_foregroundLocationTrackingEnabled = (prefs.getInt("location-updates-state") ?? 0) > 0;
_locationInterval = prefs.getInt("location-interval") ??
AppSettings().defaultLocationUpdateIntervalMinutes;
_activeLocationInterval = prefs.getInt("location-updates-interval") ??
AppSettings().defaultActiveLocationUpdateIntervalSeconds;
if (_locationInterval < 15) {
_locationInterval = 15;
} else if (_locationInterval % 5 != 0) {
_locationInterval = 5 * (_locationInterval ~/ 5);
}
_locationTrackingEnabled = (prefs.getInt("location-updates-state") ?? 0) > 0;
_showNotification = prefs.getBool("location-updates-show-notification") ?? true;
_locationInterval = Duration(seconds: prefs.getInt("location-updates-interval") ??
AppSettings().defaultLocationUpdateIntervalSeconds);
});
});
}
void _incLocationInterval() {
if (_locationInterval < 720) {
if (_locationInterval.inSeconds < 60) {
setState(() {
_locationInterval = _locationInterval + 5;
_changedHere = true;
_locationInterval = _locationInterval + Duration(seconds: 5);
});
} else if (_locationInterval.inMinutes < 15) {
setState(() {
_locationInterval = _locationInterval + Duration(minutes: 1);
});
} else if (_locationInterval.inMinutes < 60) {
setState(() {
_locationInterval = _locationInterval + Duration(minutes: 5);
});
} else if (_locationInterval.inHours < 4) {
setState(() {
_locationInterval = _locationInterval + Duration(minutes: 10);
});
} else if (_locationInterval.inHours < 48) {
setState(() {
_locationInterval = _locationInterval + Duration(hours: 1);
});
}
}
void _decLocationInterval() {
if (_locationInterval > 15) {
setState(() {
_locationInterval = _locationInterval - 5;
_changedHere = true;
});
}
}
void _incActiveLocationInterval() {
if (_activeLocationInterval < 7200) {
setState(() {
_activeLocationInterval = _activeLocationInterval + 5;
_changedHere = true;
});
}
}
void _decActiveLocationInterval() {
if (_activeLocationInterval > 5) {
setState(() {
_activeLocationInterval = _activeLocationInterval - 5;
_changedHere = true;
});
if (_locationInterval.inSeconds > 5) {
if (_locationInterval.inSeconds <= 60) {
setState(() {
_locationInterval = _locationInterval - Duration(seconds: 5);
});
} else if (_locationInterval.inMinutes <= 15) {
setState(() {
_locationInterval = _locationInterval - Duration(minutes: 1);
});
} else if (_locationInterval.inMinutes <= 60) {
setState(() {
_locationInterval = _locationInterval - Duration(minutes: 5);
});
} else if (_locationInterval.inHours <= 4) {
setState(() {
_locationInterval = _locationInterval - Duration(minutes: 10);
});
} else if (_locationInterval.inHours > 4) {
setState(() {
_locationInterval = _locationInterval - Duration(hours: 1);
});
}
}
}
_switchLocationTrackingState(bool state) async {
if (state) {
await LocationManager().updateDeviceLocation();
}
await LocationManager().setSettings(_locationTrackingEnabled, _locationInterval);
setState(() {
_wait = false;
});
}
_switchForegroundLocationTrackingState(bool state) async {
await AppSettings().save({'location-updates-interval': _activeLocationInterval, 'location-updates-priority': _accuracy});
await AppSettings().save({'location-updates-interval': _locationInterval.inSeconds, 'location-updates-priority': _accuracy, 'location-updates-show-notification': _showNotification});
if (state) {
try {
await platform.invokeMethod('startLocationService');
} catch (e) {
_foregroundLocationTrackingEnabled = false;
_locationTrackingEnabled = false;
}
} else {
await platform.invokeMethod('stopLocationService');
@ -117,18 +112,61 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
});
}
String _formatInterval() {
String result = "";
Duration leftToShow = Duration(seconds: _locationInterval?.inSeconds ?? 0);
if (leftToShow.inHours > 0) {
result += "${leftToShow.inHours} h ";
leftToShow -= Duration(hours: leftToShow.inHours);
}
if (leftToShow.inMinutes > 0) {
result += "${leftToShow.inMinutes} m";
leftToShow -= Duration(hours: leftToShow.inMinutes);
}
if (leftToShow.inSeconds > 0) {
result += "${leftToShow.inSeconds} s";
leftToShow -= Duration(hours: leftToShow.inSeconds);
}
return result;
}
Widget _getNoteWidget(String text, bool important) {
return Text(
text,
style: important ? Theme.of(context).textTheme.caption.copyWith(color: Theme.of(context).errorColor) : Theme.of(context).textTheme.caption,
softWrap: true,
);
}
Widget _getNotes() {
List<Widget> notes = [];
if (_locationTrackingEnabled) {
notes.add(_getNoteWidget('* Stop location tracking to change settings', false));
}
if ((_locationInterval?.inMinutes ?? 15) < 15) {
notes.add(_getNoteWidget('* Notification is mandatory for location updates with interval less than every 15 minutes', false));
if (_accuracy < 102) {
notes.add(_getNoteWidget('* Battery consumption will be noticeable', true));
}
}
if (notes.isEmpty) {
return Container(width: 0, height: 0);
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: notes,
);
}
@override
Widget build(BuildContext context) {
return ListView(
scrollDirection: Axis.vertical,
padding: const EdgeInsets.all(20.0),
children: <Widget>[
Text("Passive location tracking", style: Theme.of(context).textTheme.title),
Text("Works in background not affecting phone battery. Usually sends last known device location. Can't be more frequent than once in 15 minutes.",
style: Theme.of(context).textTheme.caption,
softWrap: true,
),
Container(height: Sizes.rowPadding,),
Text("Location tracking", style: Theme.of(context).textTheme.title),
Container(height: Sizes.rowPadding),
Row(
children: <Widget>[
Text("Enable"),
@ -145,64 +183,27 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
],
),
Container(height: Sizes.rowPadding),
Text("Send device location every"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
//Expanded(child: Container(),),
FlatButton(
padding: EdgeInsets.all(0.0),
child: Text("-", style: Theme.of(context).textTheme.title),
onPressed: () => _decLocationInterval(),
),
Text("$_locationInterval minutes", style: Theme.of(context).textTheme.title),
FlatButton(
padding: EdgeInsets.all(0.0),
child: Text("+", style: Theme.of(context).textTheme.title),
onPressed: () => _incLocationInterval(),
),
],
),
Container(height: Sizes.rowPadding),
Text("Active location tracking", style: Theme.of(context).textTheme.title),
Container(height: Sizes.rowPadding),
Row(
children: <Widget>[
Text("Enable"),
Switch(
value: _foregroundLocationTrackingEnabled,
onChanged: _wait ? null : (value) {
setState(() {
_foregroundLocationTrackingEnabled = value;
_wait = true;
});
_switchForegroundLocationTrackingState(value);
},
),
],
),
Container(height: Sizes.rowPadding),
Text("Accuracy:", style: Theme.of(context).textTheme.body2),
Container(height: Sizes.rowPadding),
DropdownButton<int>(
value: _accuracy,
iconSize: 30.0,
isExpanded: true,
disabledHint: Text(locationAccuracy[_accuracy]),
items: locationAccuracy.keys.map((value) {
return new DropdownMenuItem<int>(
value: value,
child: Text('${locationAccuracy[value]}'),
);
}).toList(),
onChanged: _foregroundLocationTrackingEnabled ? null : (val) {
onChanged: _locationTrackingEnabled ? null : (val) {
setState(() {
_accuracy = val;
});
},
),
Container(height: Sizes.rowPadding),
Text("Update intervals"),
Text("Update interval"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
@ -210,25 +211,43 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
//Expanded(child: Container(),),
FlatButton(
padding: EdgeInsets.all(0.0),
child: Text("-", style: Theme.of(context).textTheme.title),
onPressed: _foregroundLocationTrackingEnabled ? null : () => _decActiveLocationInterval(),
child: Text("-", style: Theme.of(context).textTheme.headline4),
onPressed: _locationTrackingEnabled ? null : () => _decLocationInterval(),
),
Expanded(
child: Text(_formatInterval(),
textAlign: TextAlign.center,
style: _locationTrackingEnabled ? Theme.of(context).textTheme.title.copyWith(color: HAClientTheme().getDisabledStateColor(context)) : Theme.of(context).textTheme.title),
),
Text("$_activeLocationInterval seconds",
style: _foregroundLocationTrackingEnabled ? Theme.of(context).textTheme.title.copyWith(color: HAClientTheme().getDisabledStateColor(context)) : Theme.of(context).textTheme.title),
FlatButton(
padding: EdgeInsets.all(0.0),
child: Text("+", style: Theme.of(context).textTheme.title),
onPressed: _foregroundLocationTrackingEnabled ? null : () => _incActiveLocationInterval(),
child: Text("+", style: Theme.of(context).textTheme.headline4),
onPressed: _locationTrackingEnabled ? null : () => _incLocationInterval(),
),
],
),
Container(height: Sizes.rowPadding),
Row(
children: <Widget>[
Text("Show notification"),
Switch(
value: _showNotification,
onChanged: (_locationTrackingEnabled || (_locationInterval?.inMinutes ?? 0) < 15) ? null : (value) {
setState(() {
_showNotification = value;
});
},
),
],
),
Container(height: Sizes.rowPadding),
_getNotes()
]
);
}
@override
void dispose() {
LocationManager().setSettings(_locationTrackingEnabled, _locationInterval);
super.dispose();
}
}