Skip to content

Settings

Johan Degraeve edited this page Mar 9, 2019 · 11 revisions

Summary

Settings are stored in UserDefaults, see xdrip/View Controllers/SettingsViewController/View Models/UserDefaults+Settings.swift

Add a setting

Update UserDefaults

Update file xdrip/View Controllers/SettingsViewController/View Models/UserDefaults+Settings.swift

add a value to the enum Key

add a computed property to get and set the value with the expected type

Add Settings, existing Section in Settings Page

To add a setting to an existing Section in the Settings Page.

In the folder xdrip/View Controllers/SettingsViewController/View Models there's a ViewModel per section that can appear in a settings page. That file needs to be updated (example for NightScout settings, the file is xdrip/View Controllers/SettingsViewController/View Models/SettingsViewNightScoutSettingsViewModel.swift

Extend the enum Setting

The class in the file needs to be updated to support the new case.

func onRowSelect(index: Int) -> SelectedRowAction

Defines what should happen if the user clicks a specific row.

This is defined by returning an instance of SelectedRowAction

There are different cases :

nothing

Do nothing when user clicks a row.

This is the case for example when the row has a UISwitch. Clicking the switch will trigger a change of the setting.
There's no need to do anything additional

askText

This is for when a text input is required. Example url for Nightscout.

The parameters are

title:String? can be used as title field when asking text
message:String? an additional message giving more info about what type of input is required
keyboardType:UIKeyboardType?, eg numeric, .. see https://developer.apple.com/documentation/uikit/uikeyboardtype
placeHolder:String?, default value that can be shown
actionTitle:String?, eg "Ok" or "Add". Default value will be read from Text files
cancelTitle:String?, eg "Cancel". Default value will be read from Text files
actionHandler: ((_ text: String) -> Void), what to do when user confirms the text, usually this will be to store the value in the setting. Also a value check can be done, eg check the length of a transmitter id
cancelHandler: (() -> Void)?, What to do when user cancels the change, usually it will be nil value

zz

zz

func text(index: Int) -> String

Defines the text to be shown to the user. The text will appear on the left of the row. The value should come from the a Texts class LINK TO LOCALIZATION

func accessoryType(index: Int) -> UITableViewCell.AccessoryType

Defines what kind of accessoryType to show when user clicks a row

Appears in the right side of the row, see https://developer.apple.com/documentation/uikit/uitableviewcell/accessorytype

The type disclosureIndicator is used whenever clicking will open a popup, example to enter the NightScout url

func detailedText(index: Int) -> String?

Defines text to be shown on right of the row. Example for row with NightScout url, this is the url that is currently stored in the settings.

func uiView(index: Int) -> (view: UIView?, reloadSection: Bool)

If a UIView is to be added in the row. It will appear on the right.

For the moment ony used for UISwitch, see https://developer.apple.com/documentation/uikit/views_and_controls

The returned view must include action to take when value of the UISwitch changes. This will usually be to change the value of the setting from on to off or vice versa.

Example look at SettingsViewNightScoutSettingsViewModel.swift