Hook up synchronizing the state to the drawing perspective

This commit is contained in:
Peter Hutterer 2019-07-15 15:39:19 +10:00
parent 517a7e6882
commit 5f5c49b6d0
3 changed files with 89 additions and 3 deletions

View File

@ -15,6 +15,46 @@
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkSpinner" id="spinner_sync">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">5</property>
<property name="margin_right">5</property>
<property name="active">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="label" translatable="yes">last synchronized:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label_last_sync">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">2 seconds ago</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
@ -23,7 +63,7 @@
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">1</property>
<property name="position">3</property>
</packing>
</child>
<child>
@ -32,12 +72,12 @@
<property name="can_focus">False</property>
<property name="margin_left">10</property>
<property name="margin_right">10</property>
<property name="icon_name">battery-000</property>
<property name="icon_name">battery-empty-symbolic</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">4</property>
</packing>
</child>
</object>

View File

@ -16,6 +16,7 @@ from .drawing import Drawing
from .svg import JsonSvg
import json
import time
import gi
gi.require_version("Gtk", "3.0")
@ -23,6 +24,24 @@ import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('drawingperspective')
def relative_time(seconds):
MIN = 60
H = 60 * MIN
DAY = 24 * H
WEEK = 7 * DAY
if seconds < 30:
return 'just now'
if seconds < 5 * MIN:
return 'a few minutes ago'
if seconds < H:
return f'{int(seconds/MIN/10) * 10} minutes ago'
if seconds < DAY:
return f'{int(seconds/H)} hours ago'
if seconds < 4 * WEEK:
return f'{int(seconds/D)} days ago'
return 'a long time ago'
@Gtk.Template(resource_path="/org/freedesktop/TuhiGui/ui/DrawingPerspective.ui")
class DrawingPerspective(Gtk.Stack):
@ -30,10 +49,15 @@ class DrawingPerspective(Gtk.Stack):
image_battery = Gtk.Template.Child()
flowbox_drawings = Gtk.Template.Child()
spinner_sync = Gtk.Template.Child()
label_last_sync = Gtk.Template.Child()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.known_drawings = []
self.last_sync_time = 0
self._sync_label_timer = GObject.timeout_add_seconds(60, self._update_sync_label)
self._update_sync_label()
def _update_drawings(self, device, pspec):
for ts in self.device.drawings_available:
@ -56,6 +80,7 @@ class DrawingPerspective(Gtk.Stack):
device.connect('notify::connected', self._on_connected)
device.connect('notify::listening', self._on_listening_stopped)
device.connect('notify::sync-state', self._on_sync_state)
self.device.connect('notify::drawings-available',
self._update_drawings)
@ -79,6 +104,19 @@ class DrawingPerspective(Gtk.Stack):
def name(self):
return "drawing_perspective"
def _on_sync_state(self, device, pspec):
if device.sync_state:
self.spinner_sync.start()
else:
self.spinner_sync.stop()
self.last_sync_time = time.time()
self._update_sync_label()
def _update_sync_label(self):
now = time.time()
self.label_last_sync.set_text(f'{relative_time(now - self.last_sync_time)}')
return True
def _on_connected(self, device, pspec):
# Turns out we don't really care about whether the device is
# connected or not, it has little effect on how we work here

View File

@ -169,6 +169,7 @@ class TuhiKeteDevice(_DBusObject):
self.is_registering = False
self._bluez_device = BlueZDevice(self.property('BlueZDevice'))
self._bluez_device.connect('notify::connected', self._on_connected)
self._sync_state = 0
@classmethod
def is_device_address(cls, string):
@ -208,6 +209,10 @@ class TuhiKeteDevice(_DBusObject):
def connected(self):
return self._bluez_device.connected
@GObject.Property
def sync_state(self):
return self._sync_state
def _on_connected(self, bluez_device, pspec):
self.notify('connected')
@ -246,6 +251,9 @@ class TuhiKeteDevice(_DBusObject):
elif err < 0:
logger.error(f'{self}: an error occured: {os.strerror(-err)}')
self.notify('listening')
elif signal == 'SyncState':
self._sync_state = parameters[0]
self.notify('sync-state')
def _on_properties_changed(self, proxy, changed_props, invalidated_props):
if changed_props is None: