Implement refresh

This commit is contained in:
Sumner Evans
2020-06-28 13:18:13 -06:00
parent 3efffe8b4f
commit 17aa93f951
5 changed files with 31 additions and 14 deletions

View File

@@ -100,6 +100,7 @@ class SublimeMusicApp(Gtk.Application):
add_action("go-to-playlist", self.on_go_to_playlist, parameter_type="s") add_action("go-to-playlist", self.on_go_to_playlist, parameter_type="s")
add_action("go-online", self.on_go_online) add_action("go-online", self.on_go_online)
add_action("refresh-devices", self.on_refresh_devices)
add_action( add_action(
"refresh-window", lambda *a: self.on_refresh_window(None, {}, True), "refresh-window", lambda *a: self.on_refresh_window(None, {}, True),
) )
@@ -294,6 +295,8 @@ class SublimeMusicApp(Gtk.Application):
) )
elif event.delta == PlayerDeviceEvent.Delta.REMOVE: elif event.delta == PlayerDeviceEvent.Delta.REMOVE:
if state_device == event.id:
self.player_manager.pause()
self.app_config.state.available_players[event.player_type].remove( self.app_config.state.available_players[event.player_type].remove(
(event.id, event.name) (event.id, event.name)
) )
@@ -574,6 +577,7 @@ class SublimeMusicApp(Gtk.Application):
player_name, option_name, value = player_setting player_name, option_name, value = player_setting
self.app_config.player_config[player_name][option_name] = value self.app_config.player_config[player_name][option_name] = value
del state_updates["__player_setting__"] del state_updates["__player_setting__"]
# TODO update the actual player settings
for k, v in state_updates.items(): for k, v in state_updates.items():
setattr(self.app_config.state, k, v) setattr(self.app_config.state, k, v)
@@ -771,6 +775,9 @@ class SublimeMusicApp(Gtk.Application):
def on_go_online(self, *args): def on_go_online(self, *args):
self.on_refresh_window(None, {"__settings__": {"offline_mode": False}}) self.on_refresh_window(None, {"__settings__": {"offline_mode": False}})
def on_refresh_devices(self, *args):
self.player_manager.refresh_players()
def reset_state(self): def reset_state(self):
if self.app_config.state.playing: if self.app_config.state.playing:
self.on_play_pause() self.on_play_pause()

View File

@@ -47,10 +47,10 @@ class PlayerEvent:
DISCONNECT = 5 DISCONNECT = 5
type: EventType type: EventType
device_id: str
playing: Optional[bool] = None playing: Optional[bool] = None
volume: Optional[float] = None volume: Optional[float] = None
stream_cache_duration: Optional[float] = None stream_cache_duration: Optional[float] = None
device_id: Optional[str] = None
@dataclass @dataclass

View File

@@ -77,7 +77,7 @@ class ChromecastPlayer(Player):
self._chromecasts: Dict[UUID, pychromecast.Chromecast] = {} self._chromecasts: Dict[UUID, pychromecast.Chromecast] = {}
self._current_chromecast: Optional[pychromecast.Chromecast] = None self._current_chromecast: Optional[pychromecast.Chromecast] = None
self.get_chromecasts_job = None self.stop_get_chromecasts = None
self.refresh_players() self.refresh_players()
def chromecast_discovered_callback(self, chromecast: Any): def chromecast_discovered_callback(self, chromecast: Any):
@@ -115,22 +115,22 @@ class ChromecastPlayer(Player):
if not chromecast_imported: if not chromecast_imported:
return return
if self.get_chromecasts_job is not None: if self.stop_get_chromecasts is not None:
pychromecast.discovery.stop_discovery(self.get_chromecasts_job) self.stop_get_chromecasts()
for id_, chromecast in self._chromecasts.items(): for id_, chromecast in self._chromecasts.items():
self.player_device_change_callback( self.player_device_change_callback(
PlayerDeviceEvent( PlayerDeviceEvent(
PlayerDeviceEvent.Delta.REMOVE, PlayerDeviceEvent.Delta.REMOVE,
type(self), type(self),
id_, str(id_),
chromecast.device.friendly_name, chromecast.device.friendly_name,
) )
) )
self._chromecasts = {} self._chromecasts = {}
self.get_chromecasts_job = pychromecast.get_chromecasts( self.stop_get_chromecasts = pychromecast.get_chromecasts(
blocking=False, callback=self.chromecast_discovered_callback blocking=False, callback=self.chromecast_discovered_callback
) )
@@ -141,9 +141,11 @@ class ChromecastPlayer(Player):
self._current_chromecast.wait() self._current_chromecast.wait()
def new_cast_status(self, status: Any): def new_cast_status(self, status: Any):
assert self._current_chromecast
self.on_player_event( self.on_player_event(
PlayerEvent( PlayerEvent(
PlayerEvent.EventType.VOLUME_CHANGE, PlayerEvent.EventType.VOLUME_CHANGE,
str(self._current_chromecast.device.uuid),
volume=(status.volume_level * 100 if not status.volume_muted else 0), volume=(status.volume_level * 100 if not status.volume_muted else 0),
) )
) )
@@ -152,9 +154,18 @@ class ChromecastPlayer(Player):
# Home app. # Home app.
if status.session_id is None: if status.session_id is None:
self.on_player_event( self.on_player_event(
PlayerEvent(PlayerEvent.EventType.PLAY_STATE_CHANGE, playing=False) PlayerEvent(
PlayerEvent.EventType.PLAY_STATE_CHANGE,
str(self._current_chromecast.device.uuid),
playing=False,
)
)
self.on_player_event(
PlayerEvent(
PlayerEvent.EventType.DISCONNECT,
str(self._current_chromecast.device.uuid),
)
) )
self.on_player_event(PlayerEvent(PlayerEvent.EventType.DISCONNECT))
self.song_loaded = False self.song_loaded = False
time_increment_order_token = 0 time_increment_order_token = 0
@@ -173,9 +184,11 @@ class ChromecastPlayer(Player):
self._timepos = status.current_time self._timepos = status.current_time
assert self._current_chromecast
self.on_player_event( self.on_player_event(
PlayerEvent( PlayerEvent(
PlayerEvent.EventType.PLAY_STATE_CHANGE, PlayerEvent.EventType.PLAY_STATE_CHANGE,
str(self._current_chromecast.device.uuid),
playing=(status.player_state in ("PLAYING", "BUFFERING")), playing=(status.player_state in ("PLAYING", "BUFFERING")),
) )
) )
@@ -307,6 +320,7 @@ class ChromecastPlayer(Player):
self.on_player_event( self.on_player_event(
PlayerEvent( PlayerEvent(
PlayerEvent.EventType.STREAM_CACHE_PROGRESS_CHANGE, PlayerEvent.EventType.STREAM_CACHE_PROGRESS_CHANGE,
str(self._current_chromecast.device.uuid),
stream_cache_duration=0, stream_cache_duration=0,
) )
) )

View File

@@ -57,6 +57,7 @@ class MPVPlayer(Player):
on_player_event( on_player_event(
PlayerEvent( PlayerEvent(
PlayerEvent.EventType.STREAM_CACHE_PROGRESS_CHANGE, PlayerEvent.EventType.STREAM_CACHE_PROGRESS_CHANGE,
"this device",
stream_cache_duration=value, stream_cache_duration=value,
) )
) )

View File

@@ -457,11 +457,6 @@ class PlayerControls(Gtk.ActionBar):
self.device_popover.popup() self.device_popover.popup()
self.device_popover.show_all() self.device_popover.show_all()
def on_device_refresh_click(self, _: Any):
# TODO: make this an action that does stuff with the player manager to delete
# all of the things and then restart retrieving the players.
pass
def on_play_queue_button_press(self, tree: Any, event: Gdk.EventButton) -> bool: def on_play_queue_button_press(self, tree: Any, event: Gdk.EventButton) -> bool:
if event.button == 3: # Right click if event.button == 3: # Right click
clicked_path = tree.get_path_at_pos(event.x, event.y) clicked_path = tree.get_path_at_pos(event.x, event.y)
@@ -677,7 +672,7 @@ class PlayerControls(Gtk.ActionBar):
device_popover_header.add(self.popover_label) device_popover_header.add(self.popover_label)
refresh_devices = IconButton("view-refresh-symbolic", "Refresh device list") refresh_devices = IconButton("view-refresh-symbolic", "Refresh device list")
refresh_devices.connect("clicked", self.on_device_refresh_click) refresh_devices.set_action_name("app.refresh-devices")
device_popover_header.pack_end(refresh_devices, False, False, 0) device_popover_header.pack_end(refresh_devices, False, False, 0)
device_popover_box.add(device_popover_header) device_popover_box.add(device_popover_header)