Skip to content

Commit

Permalink
DateTime: add datetime page/model/service (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier committed Feb 28, 2022
1 parent 36a1167 commit 11bdb15
Show file tree
Hide file tree
Showing 11 changed files with 1,148 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ sudo apt install libhandy-1-dev
- [X] Accessability page
- [ ] Users page
- [ ] Preferred apps page
- [ ] Date and time page
- [X] Date and time page
- [ ] Wacom page
- [X] Info page
6 changes: 6 additions & 0 deletions lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,11 @@
"usersPageTitle": "Benutzer",
"defaultAppsPageTitle": "Vorgabeanwendungen",
"dateAndTimePageTitle": "Datum und Uhrzeit",
"dateAndTimePageAutoDateTime": "Datum und Zeit automatisch setzen",
"dateAndTimePageAutoTimezone": "Zeitzone automatisch setzen ",
"dateAndTimePageTwentyFour": "24-Stunden Format",
"dateAndTimePageSecondsInPanel": "Zeige Sekunden in Leiste",
"dateAndTimePageWeekdayInPanel": "Zeige Wochentag in Leiste",
"dateAndTimePageWeekNumberInCalendar": "Zeige Kalenderwoche in Leiste",
"infoPageTitle": "Info"
}
6 changes: 6 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,11 @@
"usersPageTitle": "Users",
"defaultAppsPageTitle": "Default Applications",
"dateAndTimePageTitle": "Date and time",
"dateAndTimePageAutoDateTime": "Automatic date and time",
"dateAndTimePageAutoTimezone": "Automatic timezone",
"dateAndTimePageTwentyFour": "24h format",
"dateAndTimePageSecondsInPanel": "Show seconds in panel",
"dateAndTimePageWeekdayInPanel": "Show weekday in panel",
"dateAndTimePageWeekNumberInCalendar": "Show week number in calendar",
"infoPageTitle": "Info"
}
8 changes: 7 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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/hostname_service.dart';
import 'package:settings/services/house_keeping_service.dart';
import 'package:settings/services/input_source_service.dart';
Expand Down Expand Up @@ -68,7 +69,12 @@ void main() async {
),
Provider<HouseKeepingService>(
create: (_) => HouseKeepingService(),
)
dispose: (_, service) => service.dispose(),
),
Provider<DateTimeService>(
create: (_) => DateTimeService(),
dispose: (_, service) => service.dispose(),
),
],
child: const UbuntuSettingsApp(),
),
Expand Down
2 changes: 2 additions & 0 deletions lib/schemas/schemas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ const schemaPrivacy = 'org.gnome.desktop.privacy';
const schemaLocation = 'org.gnome.system.location';
const schemaScreenSaver = 'org.gnome.desktop.screensaver';
const schemaSession = 'org.gnome.desktop.session';
const schemaDateTime = 'org.gnome.desktop.datetime';
const schemaCalendar = 'org.gnome.desktop.calendar';
173 changes: 173 additions & 0 deletions lib/services/date_time_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
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<DBusPropertiesChangedSignal>? _propertyListener;

static DBusRemoteObject _createObject() =>
DBusRemoteObject(DBusClient.system(),
name: _kDateTimeInterface, path: DBusObjectPath(_kDateTimePath));

Future<void> 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<void> 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<String?> get timezoneChanged => _timezoneController.stream;
final _timezoneController = StreamController<String?>.broadcast();

void _updateTimezone(String? value) {
if (_timezone == value) return;
_timezone = value;
if (!_timezoneController.isClosed) {
_timezoneController.add(_timezone);
}
}

Future<void> _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<DateTime?> 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<bool?> get ntpChanged => _ntpController.stream;
final _ntpController = StreamController<bool?>.broadcast();

void _updateNtp(bool? value) {
if (_ntp == value) return;
_ntp = value;
if (!_ntpController.isClosed) {
_ntpController.add(_ntp);
}
}

Future<void> _initNtp() async {
_updateNtp(await _object.getNtp());
}
}

extension _DateTimeRemoteObject on DBusRemoteObject {
Future<String?> getTimezone() async {
final timeZone =
await getProperty(_kDateTimeInterface, _kTimezonePropertyName);
return (timeZone as DBusString).value;
}

Future<void> setTimeZone(String timezone) async {
final args = [DBusString(timezone), const DBusBoolean(false)];
callMethod(_kDateTimeInterface, _kSetTimezoneMethodName, args);
}

Future<void> setTime(int time) async {
bool? ntp = await getNtp();
if (ntp == null || ntp == true) return;
final args = [
DBusInt64(time),
const DBusBoolean(false),
const DBusBoolean(false)
];
callMethod(_kDateTimeInterface, _kSetTimeMethodName, args);
}

Future<DateTime?> getDateTime() async {
final timeUSec =
await getProperty(_kDateTimeInterface, _kTimeUSecPropertyName);
return DateTime.fromMicrosecondsSinceEpoch((timeUSec as DBusUint64).value);
}

Future<bool?> getNtp() async {
final ntpSync = await getProperty(_kDateTimeInterface, _kNTPPropertyName);
return (ntpSync as DBusBoolean).value;
}

Future<void> setNtp(bool? value) async {
if (value == null) return;
final args = [DBusBoolean(value), const DBusBoolean(false)];
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);
}
}
28 changes: 0 additions & 28 deletions lib/view/pages/date_and_time/date_and_time_page.dart

This file was deleted.

Loading

0 comments on commit 11bdb15

Please sign in to comment.