TLDR: I’ve been working on an add-on that inserts one or more progress bars right above the bottom button box of the Add Cards window. It should work like a stopwatch (example here), meaning that it should starting counting when the user opens the Add Card window for the first time, it should stop when the counting hits the time limit (defined by the user) and resume from the time it stopped.
The problem is that if I manage to make it stop when the counting hits the time limit it sums the break time to the elapsed time after I add a card. However, if I solve this problem, it doesn’t stop counting during the break time anymore.
How to solve it?
You can read the commented source code here. You can download the add-on zip file here.
- How the add-on works
-
When the user opens the Add Card window the source code stores the corresponding timestamp using the
time.monotonic()
function. Then it gets the difference between the current time and that timestamp and insert it to a database file whenever the user adds a card. -
If the counting hits the time limit it also store the break time into the database. If it’s not break time, the code inserts a value of 0 (zero) to the database.
-
When the user opens the Add Cards window the
self.inject_pBar()
function retrieves the most recent values stored in the database and attributes it to the progress bars and the label. -
When the user adds a card the
self.update_pBar()
updates the mentioned values.
-
For better understanding the problem let me give you an illustrative example:
Consider that I set the time limit to 20 seconds and I added a card at 21 seconds. In this case, a message box opens and starts counting the break time (let’s say it was 30 seconds). Then, if I hit the continue button it resumes counting from 21 seconds, however, let’s say I add the next card at 30 seconds, it jumps to 60 seconds instead of continue counting from 30 seconds. -
But why?
The reason is that the add-on always get the last value stored in the database (in this case 30 seconds of break time), but I only want it to make this calculation when the break time hits. I’ve tried different approaches but none of them suited entirely (you can find them in the source code I linked above).