DEV Community

Wenlo
Wenlo

Posted on

Make a reading-progress widget without hiding state boundaries

If you read two pages against a one-page goal, the ring should be full, but the label should still say 2/1 pages. This example uses PythonIDE’s widget module to clamp only the drawing, not the recorded count.

I tested this in Widget Preview on an iPhone simulator, not on the iPhone Home Screen.

Code

import widget

# Candidate derived from the current documented quickstart.
goal = widget.param.number('Reading target', 20, min=1, max=200)
pages = widget.state.int('reading_pages_demo', 0)
accent = widget.param.color('Accent', '#6366F1')

# Clamp the drawing only; keep raw state visible for review.
count = int(pages)
target = int(goal)
visible = max(0, min(count, target))
label = f'{count}/{target} pages'
status = 'Goal reached' if count >= target else 'Keep reading'
if count < 0:
    status = 'Negative count: correct the entry'

w = widget.Widget(background=('#F5F3FF', '#1E1B4B'), padding=14)
w.text('Reading check-in', size=16, weight='semibold')\
    .line_limit(1).min_scale(0.72)
w.ring_chart(visible, total=target, label=label, color=accent)
w.text(status, size=11, color='secondary')\
    .line_limit(2).min_scale(0.72)
with w.row(spacing=10):
    w.button('-1', action=pages.decrement(), style='plain')\
        .line_limit(1).min_scale(0.72)
    w.button('+1', action=pages.increment(),
             background=accent, color='#FFFFFF')\
        .line_limit(1).min_scale(0.72)
w.render()
Enter fullscreen mode Exit fullscreen mode

The target is a parameter from 1 to 200; the page count is widget state. The buttons use widget actions from pages.increment() and pages.decrement(), not ordinary AppUI callbacks. The expression max(0, min(count, target)) changes what the ring draws. It neither overwrites the raw count nor prevents a negative entry.

What the preview showed

Test Observed result
Initial preview 0/20 pages
Increment, then change the target to redraw 3/20 pages
Decrement, then set the target to 30 2/30 pages
Keep the count at 2 and set the target to 1 2/1 pages, a full ring and Goal reached
Decrement three more times, then redraw with target 20 -1/20 pages, an empty ring and a warning

Initial Widget Preview: 0/20 pages

After a redraw: 3/20 pages

Above target: 2/1 pages and a full ring

Negative count: -1/20 pages and a warning

Tapping the buttons did not immediately redraw the preview; the new count became visible after a target change. The screenshots show the state after a redraw. Home Screen installation, state after an app restart, and system refresh timing were not tested. This is a manually updated counter, not an automatic daily reset.

Try lowering the target below the current count. The ring should stop at full while the label still tells you how much you read. If you want to stop the count at zero, handle that when updating state rather than hiding it in the drawing.

I build PythonIDE for iPhone and iPad. AI assisted with drafting and translation; the screenshots are actual simulator Widget Preview captures.

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

The distinction between clamping the drawing and clamping the state is the whole lesson here — the ring stops at full for 2/1 pages while the label still says 2/1, and -1/20 shows an empty ring with a warning instead of silently fixing the count. That is exactly the kind of bug that only shows up when a real user overshoots a goal or taps decrement one too many times. You noted the buttons did not redraw the preview until the target changed — did you find out whether that delay is a Widget Preview quirk, or does it behave the same way on the actual iPhone Home Screen?

Collapse
 
_6a1b3c45cc08bec8733 profile image
Wenlo •

Thanks for reading so closely, Abdullah! In the simulator’s Widget Preview, the count only updated on screen after I changed the target. I haven’t tested this example on a real iPhone’s Home Screen yet, so I can’t say whether the same thing happens there.