Skip to content

Commit

Permalink
Fix STadas#20 and dont connect to rpc in main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
STadas committed May 3, 2022
1 parent 7bedd19 commit 50bf327
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import time
import os
import asyncio
from typing import Union
from threading import Thread

Expand Down Expand Up @@ -30,7 +31,6 @@ def __init__(self):
self.start_time = round(time.time())
self.cfg_disc_id = self.__cfg_val(self.main_cfg, 'discord_client', str)
self.default_disc_id = "745326655395856514"
self.connect_rpc()

if self.__cfg_val(self.main_cfg, 'activity', bool):
self.rpc_next_details = self.__cfg_val(self.status_cfg,
Expand Down Expand Up @@ -65,7 +65,7 @@ def __get_resolved_cfg(self,
def connect_rpc(self) -> None:
"""Connect to the Discord Rich Presence"""
try:
pp_utils.get_event_loop(True)
asyncio.set_event_loop(pp.get_event_loop(True))
self.rpc = pp.Presence(self.cfg_disc_id if self.cfg_disc_id else self.default_disc_id)
self.rpc.connect()
self.connected = True
Expand Down Expand Up @@ -167,11 +167,18 @@ def __update_rpc_next_state(self) -> None:
print("Couldn't find deck:", ex)

try:
counts = {
"new": node.new_count,
"learn": node.learn_count,
"review": node.review_count
}
if count_deck and self.last_deck is not None:
counts = {
"new": node.new_count,
"learn": node.learn_count,
"review": node.review_count,
}
else:
counts = {
"new": sum(ch.new_count for ch in node.children),
"learn": sum(ch.learn_count for ch in node.children),
"review": sum(ch.review_count for ch in node.children),
}
counts_keys = self.__cfg_val(self.main_cfg, 'counts', list)
if isinstance(counts_keys, list):
for key in counts_keys:
Expand All @@ -180,13 +187,13 @@ def __update_rpc_next_state(self) -> None:
print("Deck doesn't have the expected attributes:", ex)

card_count_parens = self.__cfg_val(self.main_cfg, 'card_count_parens', bool)

paren_left = ""
paren_right = ""
if card_count_parens:
paren_left = "("
paren_right = ")"

if due_count == 0:
self.rpc_next_state = self.__cfg_val(self.status_cfg,
'no_cards_left_txt',
Expand All @@ -199,14 +206,13 @@ def __update_rpc_next_state(self) -> None:
def on_state(self, state, _old_state):
"""Take current state and old_state from hook; If browsing, skip
'edit' hook; Call update"""
if self.__cfg_val(self.main_cfg, 'card_count', bool):
self.__update_rpc_next_state()

if not self.__cfg_val(self.main_cfg, 'activity', bool):
self.rpc_next_details = " "
return

if state == "deckBrowser":
self.last_deck = None
if self.rpc_next_state != self.__cfg_val(self.status_cfg,
'no_cards_left_txt',
str):
Expand Down Expand Up @@ -234,16 +240,21 @@ def on_state(self, state, _old_state):
self.rpc_next_details = reviews_msg

elif state == "browse":
self.last_deck = None
self.skip_edit = True
self.rpc_next_details = self.__cfg_val(self.status_cfg,
'browsing_status',
str)

elif state == "edit":
self.last_deck = None
self.rpc_next_details = self.__cfg_val(self.status_cfg,
'editing_status',
str)

if self.__cfg_val(self.main_cfg, 'card_count', bool):
self.__update_rpc_next_state()

def on_browse(self, _x):
"""Handle browse state"""
self.on_state("browse", "dummy")
Expand All @@ -267,7 +278,6 @@ def job(self):


ac = Ankicord()
ac.connect_rpc()
t = Thread(target=ac.job, daemon=True)
t.start()

Expand Down

0 comments on commit 50bf327

Please sign in to comment.