diff --git a/lib/main.dart b/lib/main.dart index cd16751c..d24cb0df 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,11 +1,11 @@ import 'package:bluez/bluez.dart'; import 'package:flutter/material.dart'; +import 'package:linux_datetime_service/linux_datetime.dart'; import 'package:nm/nm.dart'; import 'package:provider/provider.dart'; import 'package:settings/app.dart'; import 'package:settings/schemas/schemas.dart'; import 'package:settings/services/bluetooth_service.dart'; -import 'package:settings/services/date_time_service.dart'; import 'package:settings/services/display/display_service.dart'; import 'package:settings/services/hostname_service.dart'; import 'package:settings/services/house_keeping_service.dart'; diff --git a/lib/services/date_time_service.dart b/lib/services/date_time_service.dart deleted file mode 100644 index a8c2bf9c..00000000 --- a/lib/services/date_time_service.dart +++ /dev/null @@ -1,175 +0,0 @@ -import 'dart:async'; - -import 'package:dbus/dbus.dart'; - -const _kDateTimeInterface = 'org.freedesktop.timedate1'; -const _kDateTimePath = '/org/freedesktop/timedate1'; -// const _kListTimezoneMethodName = 'ListTimezones'; -// const _kSetLocalRtcMethodName = 'SetLocalRTC'; -const _kSetNtpMethodName = 'SetNTP'; -const _kSetTimeMethodName = 'SetTime'; -const _kSetTimezoneMethodName = 'SetTimezone'; -// const _kCanNTPPropertyName = 'CanNTP'; -// const _kLocalRTCPropertyName = 'LocalRTC'; -const _kNTPPropertyName = 'NTP'; -// const _kNTPSynchronizedPropertyName = 'NTPSynchronized'; -// const _kRTCTimeUSecPropertyName = 'RTCTimeUSec'; -const _kTimeUSecPropertyName = 'TimeUSec'; -const _kTimezonePropertyName = 'Timezone'; - -class DateTimeService { - DateTimeService() : _object = _createObject(); - - final DBusRemoteObject _object; - StreamSubscription? _propertyListener; - - static DBusRemoteObject _createObject() => DBusRemoteObject( - DBusClient.system(), - name: _kDateTimeInterface, - path: DBusObjectPath(_kDateTimePath), - ); - - Future init() async { - await _initTimezone(); - await _initNtp(); - _dateTime = await getDateTime(); - _propertyListener ??= _object.propertiesChanged.listen(_updateProperties); - } - - void _updateProperties(DBusPropertiesChangedSignal signal) { - if (signal.hasChangedTimeZone()) { - _object.getTimezone().then(_updateTimezone); - _object.getDateTime().then(_updateDateTime); - } - if (signal.hasChangedDateTime()) { - _object.getDateTime().then(_updateDateTime); - } - if (signal.hasChangedNtp()) { - _object.getNtp().then(_updateNtp); - } - } - - Future dispose() async { - await _propertyListener?.cancel(); - await _object.client.close(); - _propertyListener = null; - } - - // Timezone - String? _timezone; - String? get timezone => _timezone; - set timezone(String? value) { - if (value == null) return; - _object.setTimeZone(value); - } - - Stream get timezoneChanged => _timezoneController.stream; - final _timezoneController = StreamController.broadcast(); - - void _updateTimezone(String? value) { - if (_timezone == value) return; - _timezone = value; - if (!_timezoneController.isClosed) { - _timezoneController.add(_timezone); - } - } - - Future _initTimezone() async { - _updateTimezone(await _object.getTimezone()); - } - - // Date and time - DateTime? _dateTime; - DateTime? get dateTime => _dateTime; - set dateTime(DateTime? dateTime) { - if (dateTime == null) return; - _object.setTime(dateTime.microsecondsSinceEpoch); - } - - void _updateDateTime(DateTime? value) { - if (_dateTime?.second == value?.second) return; - getDateTime().then((value) => _dateTime = value); - } - - Future getDateTime() async { - return await _object.getDateTime(); - } - - // NTP - bool? _ntp; - bool? get ntp => _ntp; - set ntp(bool? value) { - if (value == null) return; - _object.setNtp(value); - } - - Stream get ntpChanged => _ntpController.stream; - final _ntpController = StreamController.broadcast(); - - void _updateNtp(bool? value) { - if (_ntp == value) return; - _ntp = value; - if (!_ntpController.isClosed) { - _ntpController.add(_ntp); - } - } - - Future _initNtp() async { - _updateNtp(await _object.getNtp()); - } -} - -extension _DateTimeRemoteObject on DBusRemoteObject { - Future getTimezone() async { - final timeZone = - await getProperty(_kDateTimeInterface, _kTimezonePropertyName); - return (timeZone as DBusString).value; - } - - Future setTimeZone(String timezone) async { - final args = [DBusString(timezone), const DBusBoolean(false)]; - await callMethod(_kDateTimeInterface, _kSetTimezoneMethodName, args); - } - - Future setTime(int time) async { - final ntp = await getNtp(); - if (ntp == null || ntp == true) return; - final args = [ - DBusInt64(time), - const DBusBoolean(false), - const DBusBoolean(false) - ]; - await callMethod(_kDateTimeInterface, _kSetTimeMethodName, args); - } - - Future getDateTime() async { - final timeUSec = - await getProperty(_kDateTimeInterface, _kTimeUSecPropertyName); - return DateTime.fromMicrosecondsSinceEpoch((timeUSec as DBusUint64).value); - } - - Future getNtp() async { - final ntpSync = await getProperty(_kDateTimeInterface, _kNTPPropertyName); - return (ntpSync as DBusBoolean).value; - } - - Future setNtp(bool? value) async { - if (value == null) return; - final args = [DBusBoolean(value), const DBusBoolean(false)]; - await callMethod(_kDateTimeInterface, _kSetNtpMethodName, args); - } -} - -extension _ChangedDateTime on DBusPropertiesChangedSignal { - bool hasChangedTimeZone() { - return changedProperties.containsKey(_kTimezonePropertyName); - } - - bool hasChangedDateTime() { - return changedProperties.containsKey(_kTimeUSecPropertyName); - } - - bool hasChangedNtp() { - return changedProperties.containsKey(_kNTPPropertyName); - } -} diff --git a/lib/view/pages/date_and_time/date_time_model.dart b/lib/view/pages/date_and_time/date_time_model.dart index 55865587..115564a8 100644 --- a/lib/view/pages/date_and_time/date_time_model.dart +++ b/lib/view/pages/date_and_time/date_time_model.dart @@ -2,10 +2,10 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; +import 'package:linux_datetime_service/linux_datetime.dart'; import 'package:safe_change_notifier/safe_change_notifier.dart'; import 'package:settings/l10n/l10n.dart'; import 'package:settings/schemas/schemas.dart'; -import 'package:settings/services/date_time_service.dart'; import 'package:settings/services/settings_service.dart'; const _kAutomaticTimezone = 'automatic-timezone'; diff --git a/lib/view/pages/date_and_time/date_time_page.dart b/lib/view/pages/date_and_time/date_time_page.dart index 89439cf9..d3f255af 100644 --- a/lib/view/pages/date_and_time/date_time_page.dart +++ b/lib/view/pages/date_and_time/date_time_page.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; +import 'package:linux_datetime_service/linux_datetime.dart'; import 'package:provider/provider.dart'; import 'package:settings/constants.dart'; import 'package:settings/l10n/l10n.dart'; -import 'package:settings/services/date_time_service.dart'; import 'package:settings/services/settings_service.dart'; import 'package:settings/view/common/yaru_switch_row.dart'; import 'package:settings/view/pages/date_and_time/date_time_model.dart'; diff --git a/pubspec.lock b/pubspec.lock index c87a52b3..f0a0eb67 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -541,6 +541,15 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.1" + linux_datetime_service: + dependency: "direct main" + description: + path: "." + ref: HEAD + resolved-ref: a78261de84040901fae06f31cf94217460030765 + url: "https://github.com/ubuntu-flutter-community/linux_datetime" + source: git + version: "1.0.0" linux_system_info: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index cfd51d9e..adef6e3b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,6 +29,8 @@ dependencies: handy_window: ^0.3.1 http: ^1.1.0 intl: ^0.18.0 + linux_datetime_service: + git: https://github.com/ubuntu-flutter-community/linux_datetime linux_system_info: git: url: https://github.com/Feichtmeier/linux_system_info.git