Skip to content

Commit

Permalink
use proper plugin configuration api
Browse files Browse the repository at this point in the history
  • Loading branch information
oakkitten committed Aug 27, 2020
1 parent f2d9b88 commit 26613f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
54 changes: 35 additions & 19 deletions delay_siblings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@
from aqt import mw
from aqt.utils import tooltip

ENABLED = 'sibling_delaying_enabled'
QUIET = 'sibling_delaying_quiet'
ENABLED = 'enabled'
QUIET = 'quiet'

# see the sources of both anki/sched.py and anki/schedv2.py:
# card types: 0=new, 1=lrn, 2=rev, 3=relrn
# queue types: 0=new/cram, 1=lrn, 2=rev, 3=day lrn, -1=suspended, -2=buried
CARD_TYPE_REVIEWING = 2
QUEUE_TYPE_SUSPENDED = -1

# current deck, only used for storing settings
deck: Dict
global_config: Dict
current_deck_config: Dict

# noinspection PyPep8Naming
def showAnswer():
if not deck[ENABLED]:
if not current_deck_config[ENABLED]:
return

siblings = get_siblings(mw.reviewer.card)
Expand Down Expand Up @@ -83,7 +83,7 @@ def showAnswer():
new_when = random.randint(min_new_when, max_new_when)
reschedule(sibling, new_when + mw.col.sched.today, filtered)

if (new_when - when) >= 14 or not deck[QUIET]:
if (new_when - when) >= 14 or not current_deck_config[QUIET]:
question = stripHTML(sibling.q())
out.append(f"Sibling: {question} (interval: <b>{interval}</b> days)<br>"
f"Rescheduling: <b>{due}</b> → <span style='color: crimson'><b>{new_when}</b></span> "
Expand Down Expand Up @@ -148,14 +148,15 @@ def reschedule(sibling: Card, due: int, filtered: bool):

########################################################################################################################


def flip_enabled(_key):
enabled = deck[ENABLED] = not deck[ENABLED]
enabled = current_deck_config[ENABLED] = not current_deck_config[ENABLED]
menu_quiet.setEnabled(enabled)
save_global_config()


def flip_quiet(_key):
deck[QUIET] = not deck[QUIET]
current_deck_config[QUIET] = not current_deck_config[QUIET]
save_global_config()


mw.form.menuTools.addSeparator()
Expand All @@ -168,24 +169,39 @@ def flip_quiet(_key):
menu_quiet.triggered.connect(flip_quiet)
mw.form.menuTools.addAction(menu_quiet)

########################################################################################################################

def load_global_config():
global global_config
global_config = mw.addonManager.getConfig(__name__)


def save_global_config():
mw.addonManager.writeConfig(__name__, global_config)


def load_current_deck_config(deck_id: int):
global current_deck_config
current_deck_config = global_config.setdefault(str(deck_id), {}) # str() as json keys can't be numbers
current_deck_config.setdefault(ENABLED, False)
current_deck_config.setdefault(QUIET, False)


# noinspection PyPep8Naming
def afterStateChange(next_state: str, _prev, *_args):
if next_state in ["overview", "review"]:
global deck
deck = mw.col.decks.current()
enabled = deck.setdefault(ENABLED, False)
quiet = deck.setdefault(QUIET, False)
load_current_deck_config(mw.col.decks.current()["id"])
menu_enabled.setEnabled(True)
menu_enabled.setChecked(enabled)
menu_quiet.setChecked(quiet)
menu_quiet.setEnabled(enabled)
else:
load_current_deck_config(0)
menu_enabled.setEnabled(False)
menu_enabled.setChecked(False)
menu_quiet.setEnabled(False)
menu_quiet.setChecked(False)
menu_enabled.setChecked(current_deck_config[ENABLED])
menu_quiet.setChecked(current_deck_config[QUIET])
menu_quiet.setEnabled(current_deck_config[ENABLED])


load_global_config()
load_current_deck_config(0)

addHook('showAnswer', showAnswer)
addHook('afterStateChange', afterStateChange)
3 changes: 3 additions & 0 deletions delay_siblings/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": 0
}

2 comments on commit 26613f4

@Poker-Kid
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, do I have to copy now this configuration to my text "Configure 'Delay siblings' "?
Thank you so much in advance. I'm aboslutely not sure what I am doing here ^^

@oakkitten
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @Poker-Kid, are you the one asking about the tools menu?

if so, you don't have to copy this anywhere. just select a deck and tick here:

Untitled

(the option will be greyed out if no deck is selected)

Please sign in to comment.