Keep invalid input separate from a successful UI result

·18m ago
Share:PostShare

A form can show a correct old result beside newly invalid input. That is a state bug even if the arithmetic is right. This AppUI example invalidates the previous result whenever the field changes and keeps error text separate from a successful conversion. Run the example I ran this form in PythonIDE on an English iPhone simulator. import re import appui state = appui. State(minutes='', result='Enter whole minutes.', error='') def parse_minutes(text): if not re.fullmatch(r'[0-9]+', text): raise ValueError('Use whole minutes, for example 95.') value = int(text) if value > 10080: raise ValueError('Use at most 10080 minutes (one week).') return divmod(value, 60) def set_minutes(value): state.minutes = value state.result = 'Input changed; convert again.' state.error = '' def convert(): try: hours, minutes = parse_minutes(state.minutes) except ValueError as error: state.error = str(error) state.result = '' return state.result = f'{hours} hours, {minutes} minutes' assert parse_minutes('95') == (1, 35) assert parse_minutes('0') == (0, 0) def body(): return appui. NavigationStack( ) Read the behavior and its boundaries The pure parse_minutes function owns the input contract: ASCII whole digits, non-negative, at most one week. It returns hours and remaining minutes using divmod. Keeping this rule outside body makes it testable without rendering a screen. Empty text and decimals are deliberately invalid, while zero is valid. The named on_change callback updates state and marks the previous answer stale. Convert either clears the error and writes a new result or clears the result and displays a specific error. There is no saving, timer or background task. Editing and conversion callbacks were tested in the actual app. The actual interaction checks included initial empty input, 95, 0, -1, 2.5, and 10081, plus editing after a success and recovering after a failure. A phone keyboard is a convenience, not validation: pasted text can bypass assumptions about available keys. Observed run: 95 produced 1 hour and 35 minutes; 0 produced zero hours/minutes. Empty input, -1 and 2.5 produced the whole-minutes error; 10081 produced the one-week limit error. Editing marked the previous answer stale, and a valid input recovered after an error. Try one change Allow surrounding spaces by normalizing in the pure function. Add tests for " 95 " and an all-space value before changing the UI, then confirm stale results still clear on every edit. I build PythonIDE, a Python coding app for iPhone and iPad. AI assisted with drafting and translation; the source and interaction results were checked in PythonIDE on an iPhone simulator. View on the App Store

D

Dev Community

Original source

Read full story

Related Stories

Technology18m ago

Disney patent reveals plans for holographic chocolate and sweets

Disney has filed a patent application for a technology that could create holographic effects on chocolate and other confectionery using microscopic surface textures. The patent application, titled 'Microscopic Physical Textures for Confections', describes a confectionery product with microscopic physical textures imprinted onto its surface. These microscopic textures, the patent says, would be configured to "interact with light to form an image based on the encoded hologram". According to Disney's application, the technology could produce different colours, optical illusions and movements, potentially allowing an animation or a character or object to appear to move across the surface of a sweet or chocolate. One example described in the patent is an image "appearing to float and move along a surface of the confection". "As light interacts with the microscopic physical textures on the confections, the light is diffracted in a number of directions and angles resulting in a number of different colors being viewed, which can be used to create images," the application explains. "As the view of the confection changes, the way in which the light is viewed to interact with the microscopic physical textures change, resulting in the produced image changing. "As a result, a holographic effect is produced on the surface of the confection." Chocolate that appears to move The patent also states that the technology "does not require a usage of chemicals or additives", instead creating the effect by altering the physical texture of the confectionery's surface. The application reflects a growing trend across the attractions industry to transform food and drink into an experience in its own right, with operators embracing more creative and immersive offerings.

BBlooloop

Headlines and briefs on this site are for information only. Always verify details on the original source or live status page.

Read Disclaimer