Skip to content

Instantly share code, notes, and snippets.

@wray
Created August 26, 2015 20:56
Show Gist options
  • Save wray/1f699e6240fb9b0a94a0 to your computer and use it in GitHub Desktop.
Save wray/1f699e6240fb9b0a94a0 to your computer and use it in GitHub Desktop.
Item-App
# coding: utf-8
import ui
import shelve
# Setting up variables for the views (in lieu of using the NavigationView)
item_db = None
add_item = None
show_item = None
# Initializing the database and view
# shelve is essentially just like a Python dictionary, only the dictionary is saved
# to the filesystem so that the data persists. It will be interesting to see how or
# even if this translates when converting for a native iOS app in XCode.
_db = shelve.open('test1') # the string is the filename used for the database
names = _db.keys() # This simply gets the list of keys in the dictionary
db = ui.ListDataSource(names) # This data source is used by the main screen list
# Syncing the database and view -- to be called every time an item is saved
#
def sync_db():
_db.sync()
names = _db.keys()
db.items = names # resetting the items in the data source should cause a refresh of the list
print('resetting: ' + str(names))
############################## Models #####################################
# Item class -- specific to database structure
# This will help persist and retrieve items from an ordered tuple in a dictionary keyed by name.
class Item (object):
# Attributes are listed here
# Simply add more if you want more
# Please note that the "name" attributes is used as the key in the
# shelf dictionary (primary key)
# Also, note the dictionary works like this:
# d = { name: (name, type, rating, ...)}
# It is keyed by name with a tuple containing all attributes (repeating name)
# So, to add attributes, just add them to the model and the tuple used to save.
name = ''
type = ''
rating = 0
def _load_from_tup(self,name):
self.name = name
tup = _db[self.name]
self.type = tup[1]
self.rating = tup[2]
# Coupled to dictionary structure
# type is required for new item
#
def __init__(self, name=None, type=None, rating=None):
self.name = name
if self.name is None:
pass
elif type is None:
self._load_from_tup(name)
else:
self.type = type
self.rating = rating
# Coupled to dictionary structure
#
def save(self):
# Making sure name is not an empty string
if len(self.name) != 0:
_db[self.name] = (self.name,self.type,self.rating)
sync_db()
# Coupled to dictionary structure
#
def find(self,key):
print("looking up " + key)
self._load_from_tup(key)
#################################################################
######### View Callbacks, Delegates, and helpers #################
# Callback used when adding a new item
#
def add_item(sender):
print("Clicked add button: " + add_item['item_name'].text)
name = str(add_item['item_name'].text)
type = add_item['item_type'].data_source.items[add_item['item_type'].selected_row[1]]['title']
print(type)
rating = int(add_item['item_rating'].value * 10)
item = Item(name,type,rating)
item.save()
add_item.close()
add_item.send_to_back()
item_db.bring_to_front()
# Callback used to present the add new item sheet
def add(sender):
print("Clicked on plus")
item_db.send_to_back()
add_item.present('sheet')
# Main Table View Delegate
class MainItemListDelegate (object):
def _populate_show(self, item):
show_item['item_name'].enabled = False
show_item['item_name'].text = item.name
show_item['item_type'].enabled = False
show_item['item_type'].text = str(item.type)
show_item['item_rating'].enabled = False
show_item['item_rating'].text = str(item.rating)
def tableview_did_select(self, tableview, section, row):
print("Selected: " + str(row))
item = Item()
item.find(db.items[row])
self._populate_show(item)
item_db.send_to_back()
show_item.present('sheet')
###################################################################
# Main View
item_db = ui.load_view('ItemDB')
# Add Item View
add_item = ui.load_view('NewItem')
# Show Item View
show_item = ui.load_view('ShowItem')
# Setup the db and item lists initially
main_item_list = item_db['item_list']
main_item_list.data_source = db
main_item_list.delegate = MainItemListDelegate()
main_item_list.allows_selection = True
# Present the main screen (item list)
item_db.present('sheet')
[{"class":"View","attributes":{"name":"Items","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{0, 0}, {540, 575}}","nodes":[{"class":"TableView","attributes":{"background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","data_source_number_of_lines":1,"enabled":true,"flex":"WH","row_height":44,"data_source_items":"","data_source_delete_enabled":true,"editing":false,"name":"item_list","uuid":"36A4F633-3048-4A48-B8A7-B5DF62AC17D2","data_source_font_size":18},"frame":"{{110, 127}, {320, 320}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"add_item","uuid":"A4947688-3C94-437F-AAB1-9B33B0D1597E","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"add","image_name":"ionicons-plus-round-32","title":""},"frame":"{{350, 96}, {80, 32}}","nodes":[]}]}]
[{"class":"View","attributes":{"name":"New Item","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{0, 0}, {540, 575}}","nodes":[{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"Name","flex":"","name":"label1","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","uuid":"B60AAEFD-2A38-4DF7-9FE9-F31BC7E482D6"},"frame":"{{88.5, 161.5}, {150, 32}}","nodes":[]},{"class":"TextField","attributes":{"font_size":17,"enabled":true,"flex":"","name":"item_name","border_style":3,"text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","uuid":"77E2A719-6192-4B1D-A017-BEF1FA4D9440"},"frame":"{{246.5, 161.5}, {200, 32}}","nodes":[]},{"class":"Button","attributes":{"font_size":15,"enabled":true,"flex":"","font_bold":false,"name":"button1","corner_radius":2,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"add_item","border_width":2,"uuid":"788C467B-FF5E-44FC-8B90-8084FAE682AB","title":"Add"},"frame":"{{230, 403.5}, {80, 32}}","nodes":[]},{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"Type","flex":"","name":"label2","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","uuid":"480E3137-527E-4907-BCB2-84703842108E"},"frame":"{{88.5, 233.5}, {150, 32}}","nodes":[]},{"class":"TableView","attributes":{"enabled":true,"data_source_font_size":18,"data_source_number_of_lines":1,"flex":"WH","name":"item_type","data_source_delete_enabled":true,"row_height":44,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","data_source_items":"Intern\nInstructor\nAdmin","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","uuid":"BE2B2DE2-4605-41CA-A26B-9B5F5D86BE31"},"frame":"{{246.5, 228}, {253.5, 41.5}}","nodes":[]},{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"Rating","flex":"","name":"label3","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","uuid":"E0F735C6-6E44-442B-84FD-A9C644D84181"},"frame":"{{88.5, 305}, {150, 32}}","nodes":[]},{"class":"Slider","attributes":{"name":"item_rating","value":0.5,"uuid":"B1E49430-0675-4016-98D0-A7705B4DCEDE","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":"W"},"frame":"{{246.5, 305}, {200, 34}}","nodes":[]}]}]
[{"class":"View","attributes":{"name":"Item Details","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{0, 0}, {540, 575}}","nodes":[{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"Name","flex":"","name":"label1","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","uuid":"5FBE4B78-953E-4860-82EC-186D3D789D06"},"frame":"{{102, 78.5}, {150, 32}}","nodes":[]},{"class":"TextField","attributes":{"font_size":17,"enabled":true,"flex":"","name":"item_name","border_style":3,"text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","uuid":"F82DC20E-1FAA-4211-AD91-E2DD156C9099"},"frame":"{{249, 78}, {200, 32}}","nodes":[]},{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"Type","flex":"","name":"label2","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","uuid":"692FD7E3-F1BC-4CB6-A3ED-C07E851107DE"},"frame":"{{102, 170}, {150, 32}}","nodes":[]},{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"Rating","flex":"","name":"label3","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","uuid":"E12BC0E5-E469-4B28-BBC2-E9CFB1D31F18"},"frame":"{{102, 258.5}, {150, 32}}","nodes":[]},{"class":"TextField","attributes":{"font_size":17,"enabled":true,"flex":"","name":"item_type","border_style":3,"text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","uuid":"0A4D509E-026B-428E-89E4-5797B7A29CD1"},"frame":"{{249, 170}, {200, 32}}","nodes":[]},{"class":"TextField","attributes":{"font_size":17,"enabled":true,"flex":"","name":"item_rating","border_style":3,"text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"left","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","uuid":"F63AD028-77E5-4BC4-863F-334235E1299C"},"frame":"{{249, 258.5}, {200, 32}}","nodes":[]}]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment