Skip to content

Instantly share code, notes, and snippets.

@cvpe
Created December 24, 2019 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvpe/a9b92152cde155a0d00be87fb03521c2 to your computer and use it in GitHub Desktop.
Save cvpe/a9b92152cde155a0d00be87fb03521c2 to your computer and use it in GitHub Desktop.
dialog new field types.py
import dialogs
import datetime
import ui
from objc_util import *
#================ copy dialogs.form_dialog: begin
import collections
import sys
PY3 = sys.version_info[0] >= 3
if PY3:
basestring = str
def my_form_dialog(title='', fields=None, sections=None, done_button_title='Done'):
if not sections and not fields:
raise ValueError('sections or fields are required')
if not sections:
sections = [('', fields)]
if not isinstance(title, basestring):
raise TypeError('title must be a string')
for section in sections:
if not isinstance(section, collections.Sequence):
raise TypeError('Sections must be sequences (title, fields)')
if len(section) < 2:
raise TypeError('Sections must have 2 or 3 items (title, fields[, footer]')
if not isinstance(section[0], basestring):
raise TypeError('Section titles must be strings')
if not isinstance(section[1], collections.Sequence):
raise TypeError('Expected a sequence of field dicts')
for field in section[1]:
if not isinstance(field, dict):
raise TypeError('fields must be dicts')
#========== modify 1: begin
c = dialogs._FormDialogController(title, sections, done_button_title=done_button_title)
for s in range(0,len(c.sections)):
for i in range(0,len(c.cells[s])): # loop on rows of section s
cell = c.cells[s][i]
if len(cell.content_view.subviews) > 0:
tf = cell.content_view.subviews[0] # ui.TextField of value in row
item = c.sections[s][1][i] # section s, 1=items, row i
if 'segments' in item:
segmented = ui.SegmentedControl()
segmented.name = cell.text_label.text
segmented.frame = tf.frame
segmented.x = c.view.width - segmented.width - 8
segmented.segments = item['segments']
cell.content_view.remove_subview(tf)
del c.values[tf.name]
del tf
cell.content_view.add_subview(segmented)
#========== modify 1: end
c.container_view.present('sheet')
c.container_view.wait_modal()
# Get rid of the view to avoid a retain cycle:
c.container_view = None
if c.was_canceled:
return None
#========== modify 2: begin
for s in range(0,len(c.sections)):
for i in range(0,len(c.cells[s])): # loop on rows of section s
cell = c.cells[s][i]
if len(cell.content_view.subviews) > 0:
tf = cell.content_view.subviews[0] # ui.TextField of value in row
if isinstance(tf, ui.SegmentedControl):
if tf.selected_index >= 0:
item = c.sections[s][1][i] # section s, 1=items, row i
c.values[tf.name] = item['segments'][tf.selected_index]
#========== modify 2: end
return c.values
#================ copy dialogs.form_dialog: end
form_list_of_sections = []
sectionA_dicts = []
sectionA_dicts.append(dict(type = 'text', title = 'First Name',
key = 'first', placeholder = 'John'))
sectionA_dicts.append(dict(type = 'text', title = 'Last Name',
key = 'last', placeholder = 'Doe'))
sectionA_dicts.append(dict(type = 'number', title = 'age',
key = 'age', placeholder='30'))
segments = [ui.Image.named('test:Lenna').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL), ui.Image.named('test:Mandrill').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)]
sectionA_dicts.append(dict(type = 'text', title = 'My segmented images',
key = 'segm_img', segments = segments))
segments = ['yes','no']
sectionA_dicts.append(dict(type = 'text', title = 'My segmented control',
key = 'segm', segments = segments))
sectionA_dicts.append(dict(type = 'text', title = 'My button',
key = 'button', button = ['iob:ios7_checkmark_32','iob:ios7_checkmark_outline_32']))
sectionA_dicts.append(dict(type = 'text', title = 'My face',
key = 'face', button = ['emj:Smiling_1','emj:Disappointed']))
form_list_of_sections.append(('Section A', sectionA_dicts, 'Section A ends'))
sectionB_dicts = []
sectionB_dicts.append(dict(type = 'date', title = 'Date Of Birth',
key = 'DOB', value = datetime.date.today()))
sectionB_dicts.append(dict(type = 'url', title = 'Home Page',
key = 'homepage', placeholder = 'http://example.com'))
form_list_of_sections.append(('Section B', sectionB_dicts, 'Section B ends'))
sectionC_dicts = []
sectionC_dicts.append(dict(type = 'email', title = 'email',
key = 'email', placeholder = 'name@mailserver.com'))
sectionC_dicts.append(dict(type = 'switch', title = 'is_married',
key = 'is_married', value = True))
sectionC_dicts.append(dict(type = 'check', title = 'is_registered',
key = 'is_registered', value = False))
form_list_of_sections.append(('Section C', sectionC_dicts, 'Section C ends'))
#diag = my_form_dialog(title = 'Form Dialog', sections=form_list_of_sections)
def segmented_action(sender):
global c
idx = sender.selected_index
if idx >= 0:
c.values[sender.name] = sender.segments[idx]
def button_action(sender):
global c
sender.idx = 1 - sender.idx
sender.image = ui.Image.named(sender.images[sender.idx]).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
c.values[sender.name] = sender.idx
def objcsegmentselection_(_self, _cmd, _sender):
idx = ObjCInstance(_sender).selectedSegmentIndex()
if idx >= 0:
c.values[ObjCInstance(_sender).name] = idx
def my_tableview_cell_for_row(self,tv, section, row):
global c
#print('section=',section,'row=',row)
c = self
cell = self.cells[section][row]
if len(cell.content_view.subviews) > 0:
tf = cell.content_view.subviews[0] # ui.TextField of value in row
item = self.sections[section][1][row]
new_type = False
if 'segments' in item:
if isinstance(item['segments'][0],str):
# segments are strings
# check if ui.SegmentedControl already added
for sv in cell.content_view.subviews:
if isinstance(tf, ui.SegmentedControl):
return cell
segmented = ui.SegmentedControl()
segmented.name = cell.text_label.text
segmented.action = segmented_action
segmented.frame = tf.frame
segmented.x = c.view.width - segmented.width - 8
segmented.segments = item['segments']
cell.content_view.add_subview(segmented)
elif isinstance(item['segments'][0],ui.Image):
# segments are ui.Image
# we will create an UISegmentedControl instead of tf
# thus we will never more see it as subview of cell.content_view
items = []
for ui_image in item['segments']:
items.append(ObjCInstance(ui_image))
segmented = ObjCClass('UISegmentedControl').alloc().initWithItems_(items)
x = c.view.width - tf.width - 8
frame = CGRect(CGPoint(x,tf.y), CGSize(tf.width,tf.height))
segmented.name = tf.name # used in action
segmented.setFrame_(frame)
for sv in segmented.subviews():
# UIImageView of each UISegment
sv.subviews()[0].setContentMode_(1) # scaleAspectFit
ActionTarget = create_objc_class('ActionTarget', methods=[objcsegmentselection_])
target = ActionTarget.new().autorelease()
segmented.addTarget_action_forControlEvents_(target, 'objcsegmentselection:',1 << 12) # UIControlEventValueChanged
ObjCInstance(cell.content_view).addSubview_(segmented)
new_type = True
elif 'button' in item:
# check if Button already added
for sv in cell.content_view.subviews:
if isinstance(tf, ui.Button):
return cell
button = ui.Button()
button.name = cell.text_label.text
button.action = button_action
button.frame = tf.frame
button.width = button.height
button.x = c.view.width - button.width - 8
button.images = item['button']
button.title = ''
button.idx = 0
button.image = ui.Image.named(button.images[button.idx]).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
cell.content_view.add_subview(button)
new_type = True
elif type(tf) is ui.TextField:
tf.alignment=ui.ALIGN_RIGHT
if new_type:
cell.content_view.remove_subview(tf)
del self.values[tf.name]
del tf
return cell
dialogs._FormDialogController.tableview_cell_for_row = my_tableview_cell_for_row
diag = dialogs.form_dialog(title = 'Form Dialog', sections=form_list_of_sections)
print(diag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment