Created
August 6, 2017 07:52
-
-
Save Phuket2/6f99eb7ac64aa3bab77329df14c129ad to your computer and use it in GitHub Desktop.
form_dialog_wapper.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ui | |
import dialogs | |
import datetime as dt | |
class FormDialogWrapper(object): | |
''' | |
A Wrapper/Helper class around the dialogs.form_dialog | |
Does it need one?? Maybe not. But by attempting to do one, I learn more | |
about the form_dialog. | |
To me these wrappers/helps also seem such a good idea in the begining. Then I get | |
less convienced about the need for the wrapper as I get more into it. | |
Its possibly because you start to feel more comportable with the data etc... | |
Anyway, this is not finished. I feel this will be defunct before long anyway. | |
I still wanted to share it. Even if to bring attention to how nice the dialogs | |
module is. Can save you a lot of work. | |
I know there is some questionable coding and data contstructs in here. | |
But learn by doing, right? | |
''' | |
_default_section = {'title':'General', 'footer':None, 'fields':[]} | |
def __init__(self, title='Form Dialog', default_section_name=None, | |
default_footer=None, *args, **kwargs): | |
if default_section_name: | |
self._default_section['title']=default_section_name | |
if default_footer: | |
self._default_section['footer']=default_footer | |
self.title = title | |
self.sections = [self._default_section] | |
def get_section(self, section): | |
result= [s for s in self.sections if s['title'] == section] | |
return result[0] if result != [] else None | |
def add_section(self, section, footer=None): | |
''' | |
If the section does not exist, adds a new section | |
if the section exists just updates the footer, a little silly | |
''' | |
sec = self.get_section(section) | |
if not sec: | |
self.sections.append({'title':section, 'footer':footer, 'fields':[]}) | |
else: | |
sec['footer']=footer | |
def add_field(self, section=None, fldtype='text', title='Field', value='', | |
key=None, tint_color=None, icon=None, placeholder='', | |
autocorrection=False, autocapitalization=False): | |
''' | |
adds a fld to a section. If a section is not specified, the default section is | |
used. | |
You can call this method without any params, although its not smart as its hard to | |
define the key for the return dict. I could do this with a counter appending | |
it to the default field and setting it to the key I guess. I think omz does | |
something like this. | |
This needs more work on type checking. A lot of the params dont play nice | |
together, just due to the varying meanings. Having the defaults make it a little | |
harder because trying to do everything in one method call | |
''' | |
section = section if section else self.sections[0]['title'] | |
sec = self.get_section(section) | |
# this is bad...I need to find the right logic pattern to | |
# deal with this case | |
if not sec: | |
sec = self.get_section(self.sections[0]['title']) | |
key = key if key else title | |
# I did think about using a class for a fld representation. | |
# I was torn, about if I am just over complicating something simple. | |
fld = dict(section=section, | |
type=fldtype, | |
title= title, | |
value=value, | |
key=key, | |
tint_color= tint_color, | |
icon=icon, | |
placeholder=placeholder, | |
autocorrection=autocorrection, | |
autocapitalization=autocapitalization, | |
) | |
# maybe should raise an error here... trying to be to nice? | |
if fldtype == 'datetime' or fldtype == 'date' or fldtype == 'time': | |
if not isinstance(value, dt.datetime): | |
fld['value'] = dt.datetime.now() | |
if fldtype == 'switch' or fldtype == 'check': | |
if not isinstance(value, bool): | |
fld.value = False | |
sec['fields'].append(fld) | |
def show(self): | |
''' | |
Call the dialogs.form_dialog. Refactor the internal format a little | |
to fit the sections param. | |
Only sections that has one or more flds are used. | |
''' | |
# reformat self.sections a little [(title, fields[{}], footer)] | |
# I thought it was too difficult to keep it in this exact format. | |
secs = [(s['title'], s['fields'], s['footer']) for s in self.sections if s['fields']] | |
x = dialogs.form_dialog(title = self.title, sections = secs) | |
# do some saving here! | |
# I think I will do a save and load later, just for the practice. Try to use | |
# the json hooks for serialising the datetime objects etc... | |
return x | |
if __name__ == '__main__': | |
fd = FormDialogWrapper('My Form Dialog','Person', 'End of Section') | |
fd.add_section('Preferences') | |
fd.add_section('xfactor', 'Cool Footer') | |
fd.add_field(title='First', placeholder='Enter First Name', autocapitalization=True ) | |
fd.add_field(title='Last', placeholder= 'Enter Last Name', autocapitalization = True) | |
fd.add_field(title='Date of Birth', fldtype='date') | |
fd.add_field(section='xfactor', fldtype='check', title='You Have it?', | |
value=True,tint_color = 'deeppink') | |
fd.add_field(section='Preferences', title='Set Alarm', fldtype='switch', | |
value=True, icon='emj:Alarm_Clock') | |
fd.add_field(section='Preferences',title='Mailing List', fldtype='switch', | |
value=True, tint_color='orange') | |
fd.add_field(section='Preferences', title='Creation Date', fldtype='datetime', | |
tint_color='purple', icon='iob:document_text_32') | |
result = fd.show() | |
print(result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment