Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Update stream-setting checkboxes from subscription events. #981

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions zulipterminal/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __init__(self, controller: Any) -> None:

self._notified_user_of_notification_failure = False

self.callbacks: Dict[str, Any] = {}

# Events fetched once at startup
self.initial_data_to_fetch: List[str] = [
"realm",
Expand Down Expand Up @@ -178,6 +180,28 @@ def __init__(self, controller: Any) -> None:
self.new_user_input = True
self._start_presence_updates()

def register_callback(
self, obj: Any, event: str, callback: Callable[[Any], None]
) -> None:
"""
Registers a callback from UI elements that needs
to be triggered on events.
"""
if event not in self.callbacks:
self.callbacks[event] = {}
self.callbacks[event] = (obj, callback)

def process_callback(self, event: str, stream_id: Optional[int]) -> None:
"""
This function processes and calls the callback function that
was registered in self.callbacks and is recently triggered.
"""
if event in self.callbacks and event == "stream_info_open":
caller = self.callbacks[event]
if caller[0].stream_id == stream_id:
callback = caller[1]
callback()

def get_focus_in_current_narrow(self) -> Union[int, Set[None]]:
"""
Returns the focus in the current narrow.
Expand Down Expand Up @@ -954,7 +978,6 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:
self.muted_streams.add(stream_id)
self.unread_counts["all_msg"] -= unread_count
stream_button.mark_muted()
self.controller.update_screen()
elif event.get("property", None) == "pin_to_top":
stream_id = event["stream_id"]

Expand All @@ -974,7 +997,8 @@ def get_stream_by_id(streams: List[StreamData], stream_id: int) -> StreamData:
sort_streams(self.unpinned_streams)
sort_streams(self.pinned_streams)
self.controller.view.left_panel.update_stream_view()
self.controller.update_screen()
self.process_callback("stream_info_open", event.get("stream_id", None))
self.controller.update_screen()
elif event["op"] in ("peer_add", "peer_remove"):
# NOTE: ZFL 35 commit was not atomic with API change
# (ZFL >=35 can use new plural style)
Expand Down
14 changes: 14 additions & 0 deletions zulipterminal/ui_tools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,10 @@ def __init__(self, controller: Any, stream_id: int) -> None:

self.widgets.append(muted_setting)
self.widgets.append(pinned_setting)

self.controller.model.register_callback(
self, "stream_info_open", self.update_checkboxes_from_events
)
super().__init__(controller, self.widgets, "STREAM_DESC", popup_width, title)

def toggle_mute_status(self, button: Any, new_state: bool) -> None:
Expand All @@ -1250,6 +1254,16 @@ def toggle_mute_status(self, button: Any, new_state: bool) -> None:
def toggle_pinned_status(self, button: Any, new_state: bool) -> None:
self.controller.model.toggle_stream_pinned_status(self.stream_id)

@asynch
def update_checkboxes_from_events(self) -> None:
self.widgets[-2].set_state(
self.controller.model.is_muted_stream(self.stream_id), False
)

self.widgets[-1].set_state(
self.controller.model.is_pinned_stream(self.stream_id), False
)

def keypress(self, size: urwid_Size, key: str) -> str:
if is_command_key("STREAM_MEMBERS", key):
self.controller.show_stream_members(stream_id=self.stream_id)
Expand Down