|
| 1 | +# InputActivity |
| 2 | + |
| 3 | +`InputActivity` is a generic, reusable single-value input screen. It handles the UI and user input for one setting, then returns the value to the caller via `startActivityForResult`. It does **not** persist anything itself. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`InputActivity` is useful any time you need to ask the user for a single value: a text string, a choice from radio buttons or a dropdown, or an integer from a slider. It supports the same input widgets as `SettingActivity`, but is independent of `SharedPreferences`. |
| 8 | + |
| 9 | +`SettingActivity` is a thin wrapper around `InputActivity` that adds `SharedPreferences` persistence, row-label updates, and `changed_callback` handling. Apps that only need the input UI can use `InputActivity` directly. |
| 10 | + |
| 11 | +## Basic Usage |
| 12 | + |
| 13 | +```python |
| 14 | +from mpos import Activity, Intent, InputActivity |
| 15 | + |
| 16 | +class MyApp(Activity): |
| 17 | + def ask_for_password(self): |
| 18 | + intent = Intent(activity_class=InputActivity) |
| 19 | + intent.putExtra("setting", { |
| 20 | + "title": "WiFi Password", |
| 21 | + "placeholder": "Enter password", |
| 22 | + "ui": "textarea" |
| 23 | + }) |
| 24 | + intent.putExtra("value", "") |
| 25 | + self.startActivityForResult(intent, self.on_password_result) |
| 26 | + |
| 27 | + def on_password_result(self, result): |
| 28 | + if result and result.get("result_code"): |
| 29 | + password = result["data"]["value"] |
| 30 | + print("Password entered:", password) |
| 31 | +``` |
| 32 | + |
| 33 | +## Intent Extras |
| 34 | + |
| 35 | +### Required extras |
| 36 | + |
| 37 | +- **`setting`** (dict): Metadata describing the input. |
| 38 | + |
| 39 | +### Optional extras |
| 40 | + |
| 41 | +- **`value`** (string): Initial value to display. If omitted, `setting["default_value"]` is used, falling back to `""`. |
| 42 | + |
| 43 | +## Setting Dictionary Structure |
| 44 | + |
| 45 | +The `setting` dict has the same shape as the one used by `SettingActivity` and `SettingsActivity`: |
| 46 | + |
| 47 | +### Required properties |
| 48 | + |
| 49 | +- **`title`** (string): Display name shown at the top of the input screen |
| 50 | + |
| 51 | +### Optional properties |
| 52 | + |
| 53 | +- **`key`** (string): Identifier returned in the result. Not used by `InputActivity` itself, but helpful for callers. |
| 54 | +- **`ui`** (string): UI type. Options: `"textarea"` (default), `"radiobuttons"`, `"dropdown"`, `"slider"`, `"activity"` |
| 55 | +- **`ui_options`** (list): List of `(label, value)` tuples for `radiobuttons` and `dropdown` |
| 56 | +- **`placeholder`** (string): Placeholder text for `textarea` |
| 57 | +- **`default_value`** (string): Default value if no `value` extra is provided |
| 58 | +- **`note`** (string): Short informational text shown below the input widget |
| 59 | +- **`min`** (int): Minimum value for `"slider"` (default: `0`) |
| 60 | +- **`max`** (int): Maximum value for `"slider"` (default: `100`) |
| 61 | +- **`allow_deselect`** (bool): For `radiobuttons`, allow the user to un-select the active option (default: `False`) |
| 62 | + |
| 63 | +## Result Contract |
| 64 | + |
| 65 | +`InputActivity` returns a result dict with the standard `Activity` result shape: |
| 66 | + |
| 67 | +```python |
| 68 | +{ |
| 69 | + "result_code": True, # True on Save, False on Cancel/back |
| 70 | + "data": { |
| 71 | + "value": "<new_value>", |
| 72 | + "key": "<setting key>", |
| 73 | + "ui": "<ui type>" |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +The caller is responsible for persisting or acting on the returned `value`. |
| 79 | + |
| 80 | +## UI Types |
| 81 | + |
| 82 | +### Textarea (default) |
| 83 | + |
| 84 | +Text input with an on-screen keyboard. If the device has a camera, a "Scan data from QR code" button is also shown. |
| 85 | + |
| 86 | +```python |
| 87 | +{ |
| 88 | + "title": "API Key", |
| 89 | + "key": "api_key", |
| 90 | + "placeholder": "Enter your API key" |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Radio Buttons |
| 95 | + |
| 96 | +Mutually exclusive selection from a small list of options. |
| 97 | + |
| 98 | +```python |
| 99 | +{ |
| 100 | + "title": "Theme", |
| 101 | + "key": "theme", |
| 102 | + "ui": "radiobuttons", |
| 103 | + "ui_options": [ |
| 104 | + ("Light", "light"), |
| 105 | + ("Dark", "dark"), |
| 106 | + ("Auto", "auto") |
| 107 | + ] |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### Dropdown |
| 112 | + |
| 113 | +Compact selection from a larger list of options. |
| 114 | + |
| 115 | +```python |
| 116 | +{ |
| 117 | + "title": "Language", |
| 118 | + "key": "language", |
| 119 | + "ui": "dropdown", |
| 120 | + "ui_options": [ |
| 121 | + ("English", "en"), |
| 122 | + ("German", "de"), |
| 123 | + ("French", "fr"), |
| 124 | + ("Spanish", "es") |
| 125 | + ] |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### Slider |
| 130 | + |
| 131 | +Integer selection within a range. |
| 132 | + |
| 133 | +```python |
| 134 | +{ |
| 135 | + "title": "Volume", |
| 136 | + "key": "volume", |
| 137 | + "ui": "slider", |
| 138 | + "default_value": "50", |
| 139 | + "min": 0, |
| 140 | + "max": 100 |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +## Cancel and Back Handling |
| 145 | + |
| 146 | +Pressing the **Cancel** button or swiping back returns: |
| 147 | + |
| 148 | +```python |
| 149 | +{"result_code": False, "data": {"value": "...", "key": "...", "ui": "..."}} |
| 150 | +``` |
| 151 | + |
| 152 | +Callers should check `result.get("result_code")` before using the value. |
| 153 | + |
| 154 | +## See Also |
| 155 | + |
| 156 | +- [`SettingActivity`](setting-activity.md) - Wrapper that persists the input value to `SharedPreferences` |
| 157 | +- [`SettingsActivity`](settings-activity.md) - List of multiple settings, each edited through `SettingActivity`/`InputActivity` |
| 158 | +- [`SharedPreferences`](preferences.md) - For persisting values yourself |
0 commit comments