Massive refactor: seperate files, screen state, dialog back, edit profile, local images

sammachin-gprs
Daniel Saul 2018-09-02 13:28:54 +01:00
parent d717f61037
commit 116613f424
9 changed files with 420 additions and 156 deletions

BIN
tildr/biglogo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

@ -6,9 +6,13 @@ ___dependencies___ = ["wifi", "http", "ugfx_helper", "sleep", "dialogs", "sim800
___categories___ = ["Other"]
___bootstrapped___ = True
import app, buttons, ugfx, ugfx_helper, http, dialogs, sim800, database, ujson
import app, buttons, ugfx, ugfx_helper, sleep, http, dialogs, sim800, database, ujson
from tilda import Buttons
from tildr.profile import get_profile
from tildr.shared import clear
from tildr import splash, profile, person, nomore
api_url = "http://emf2018.us-east-2.elasticbeanstalk.com"
ugfx_helper.init()
@ -19,173 +23,63 @@ style.set_enabled([ugfx.WHITE, ugfx.WHITE, ugfx.html_color(0x888888), ugfx.html_
style.set_background(ugfx.html_color(0x000000))
ugfx.set_default_style(style)
def render_splash_screen():
ugfx.clear(ugfx.html_color(0x000000))
try:
logo = http.get("https://i.imgur.com/0TjxEPs.png").raise_for_status().content
ugfx.display_image(
int((ugfx.width() - 164)/2),
20,
bytearray(logo))
except:
pass
def error_screen(state):
ugfx.text(5, 100, "Error: try again later :(", ugfx.WHITE)
ugfx.text(160, 100, "TILDR", ugfx.WHITE)
ugfx.text(0, 270, "Find your match @emfcamp ;)", ugfx.WHITE)
ugfx.text(45, 300, "Press A to begin", ugfx.WHITE)
def error_actions(state):
if buttons.is_triggered(Buttons.BTN_A):
state['next'] = "SPLASH"
screens = {
'SPLASH': {'render': splash.screen, 'actions': splash.actions},
'PROFILE': {'render': profile.screen, 'actions': profile.actions},
'ERROR': {'render': error_screen, 'actions': error_actions},
'NEXT_PERSON': {'render': person.screen, 'actions': person.actions},
'NO_MORE': {'render': nomore.screen, 'actions': nomore.actions}
}
state = {
'api': api_url,
'running': True,
'screen': None,
'next': "SPLASH",
'ui': [],
'profile': None
}
def run_splash_screen():
while True:
if buttons.is_triggered(Buttons.BTN_Menu):
return False
if buttons.is_triggered(Buttons.BTN_A):
return True
def destroy(state):
for item in state['ui']:
try:
item.hide()
except:
pass
item.destroy()
def create_profile(my_profile):
ugfx.clear(ugfx.html_color(0x000000))
name, age = "", ""
while name == "":
name = dialogs.prompt_text("What's your name?")
while age == "":
age = dialogs.prompt_text("What's your age?")
tag_line = dialogs.prompt_text("Tell us your tagline:")
looking_for = dialogs.prompt_text("And what you're looking for:")
contact = dialogs.prompt_text("And your twitter username?")
imei = sim800.imei()
top_left_logo()
ugfx.text(5, 100, "Working...", ugfx.BLACK)
profile = {
'unique_identifier': imei,
'username': name,
'age': age,
'tag_line': tag_line,
'looking_for': looking_for,
'contact': contact
}
profile_json = ujson.dumps(profile)
try:
http.post(api_url+'/create_user', json=profile).raise_for_status().close()
except:
ugfx.clear()
ugfx.text(5, 100, "Error. Try again later. :(", ugfx.BLACK)
return False
database.set("tildr_profile", profile_json)
return True
state['ui'] = []
def main_screen(my_profile):
while True:
next_person(my_profile)
while True:
if buttons.is_triggered(Buttons.BTN_Menu):
return False
if buttons.is_triggered(Buttons.BTN_B) or buttons.is_triggered(Buttons.JOY_Right):
break
# if buttons.is_triggered(Buttons.BTN_A) or buttons.is_triggered(Buttons.JOY_Left):
# create_profile(my_profile)
# break
state['profile'] = get_profile()
while state['running']:
def next_person(my_profile):
ugfx.clear(ugfx.html_color(0x000000))
ugfx.text(5, 100, "Loading...", ugfx.WHITE)
try:
resp = http.get(api_url+'/get_user/'+my_profile['unique_identifier']).json()
except:
ugfx.clear()
ugfx.text(5, 100, "Error. Try again later. :(", ugfx.BLACK)
return
# Move to next screen
if state['next']:
destroy(state)
nxt = state['next']
state['screen'] = nxt
state['next'] = None
if resp['success']:
display_person(resp['value'])
else:
no_more(my_profile)
clear()
screens[nxt]['render'](state)
sleep.wfi()
def display_person(person):
top_left_logo()
s = state['screen']
screens[s]['actions'](state)
ugfx.set_default_font(ugfx.FONT_TITLE)
ugfx.Label(5, 90, 230, 40, person["username"], justification=ugfx.Label.LEFTTOP)
ugfx.set_default_font(ugfx.FONT_SMALL)
ugfx.text(200, 92, person["age"], ugfx.WHITE)
if buttons.is_triggered(Buttons.BTN_Menu):
state['running'] = False
ugfx.Label(5, 120, 230, 60, person["tag_line"])
ugfx.Label(5, 200, 230, 40, person["looking_for"])
ugfx.text(5, 190, "Looking for...", ugfx.RED)
ugfx.text(5, 245, person["contact"], ugfx.BLUE)
# ugfx.Button(0, 280, 100, 40, "< Edit profile", parent=None, shape=ugfx.Button.RECT, style=None)
ugfx.Button(160, 280, 100, 40, "Swipe >", parent=None, shape=ugfx.Button.RECT, style=None)
def no_more(my_profile):
top_left_logo()
ugfx.set_default_font(ugfx.FONT_TITLE)
ugfx.Label(5, 90, 230, 50, "You've swiped everybody!", justification=ugfx.Label.CENTERTOP)
ugfx.set_default_font(ugfx.FONT_SMALL)
ugfx.Label(5, 160, 230, 20, "Soz "+my_profile["username"], justification=ugfx.Label.CENTERTOP)
ugfx.Label(5, 180, 230, 20, "Come back later ;)", justification=ugfx.Label.CENTERTOP)
# ugfx.Button(0, 280, 100, 40, "< Edit profile", parent=None, shape=ugfx.Button.RECT, style=None)
ugfx.Button(160, 280, 100, 40, "Try again >", parent=None, shape=ugfx.Button.RECT, style=None)
def get_profile():
profile_json = database.get("tildr_profile")
if profile_json is None:
return None
profile = ujson.loads(profile_json)
return profile
def top_left_logo():
ugfx.clear(ugfx.html_color(0x000000))
try:
logo = http.get("https://i.imgur.com/5HXmXBU.png").raise_for_status().content
ugfx.display_image(0, 5, bytearray(logo))
except:
pass
def quit_loop():
while True:
if buttons.is_triggered(Buttons.BTN_Menu):
return False
while True:
profile = get_profile()
if profile is None:
render_splash_screen()
if not run_splash_screen():
break
profile = {
'username': "",
'age': "",
'tag_line': "",
'looking_for': "",
'contact': ""
}
if not create_profile(profile):
continue
main_screen(profile)
if not quit_loop():
break
app.restart_to_default()

32
tildr/nomore.py Normal file
View File

@ -0,0 +1,32 @@
import ugfx, buttons
from tilda import Buttons
from tildr.shared import top_left_logo
def screen(state):
window = ugfx.Container(0, 0, 240, 320)
window.show()
top_left_logo()
ugfx.set_default_font(ugfx.FONT_TITLE)
l1 = ugfx.Label(5, 90, 230, 50, "You've swiped everybody!", parent=window, justification=ugfx.Label.CENTERTOP)
ugfx.set_default_font(ugfx.FONT_SMALL)
l2 = ugfx.Label(5, 160, 230, 20, "Soz "+state["profile"]["username"], parent=window, justification=ugfx.Label.CENTERTOP)
l3 = ugfx.Label(5, 180, 230, 20, "Come back later ;)", parent=window, justification=ugfx.Label.CENTERTOP)
b2 = ugfx.Button(0, 280, 120, 40, "< Edit profile", parent=window, shape=ugfx.Button.RECT, style=None)
b1 = ugfx.Button(120, 280, 120, 40, "Try again >", parent=window, shape=ugfx.Button.RECT, style=None)
state['ui'].append(window)
state['ui'].append(l1)
state['ui'].append(l2)
state['ui'].append(l3)
state['ui'].append(b1)
state['ui'].append(b2)
def actions(state):
if buttons.is_triggered(Buttons.BTN_B) or buttons.is_triggered(Buttons.JOY_Right):
state['next'] = "NEXT_PERSON"
if buttons.is_triggered(Buttons.BTN_A) or buttons.is_triggered(Buttons.JOY_Left):
state['next'] = "PROFILE"

163
tildr/old.py Normal file
View File

@ -0,0 +1,163 @@
def render_splash_screen():
ugfx.clear(ugfx.html_color(0x000000))
try:
logo = http.get("https://i.imgur.com/0TjxEPs.png").raise_for_status().content
ugfx.display_image(
int((ugfx.width() - 164)/2),
20,
bytearray(logo))
except:
pass
ugfx.text(160, 100, "TILDR", ugfx.WHITE)
ugfx.text(0, 270, "Find your match @emfcamp ;)", ugfx.WHITE)
ugfx.text(45, 300, "Press A to begin", ugfx.WHITE)
def run_splash_screen():
while True:
if buttons.is_triggered(Buttons.BTN_Menu):
return False
if buttons.is_triggered(Buttons.BTN_A):
return True
def create_profile(my_profile):
ugfx.clear(ugfx.html_color(0x000000))
name, age = "", ""
while name == "":
name = dialogs.prompt_text("What's your name?")
while age == "":
age = dialogs.prompt_text("What's your age?")
tag_line = dialogs.prompt_text("Tell us your tagline:")
looking_for = dialogs.prompt_text("And what you're looking for:")
contact = dialogs.prompt_text("And your twitter username?")
imei = sim800.imei()
top_left_logo()
ugfx.text(5, 100, "Working...", ugfx.BLACK)
profile = {
'unique_identifier': imei,
'username': name,
'age': age,
'tag_line': tag_line,
'looking_for': looking_for,
'contact': contact
}
profile_json = ujson.dumps(profile)
try:
http.post(api_url+'/create_user', json=profile).raise_for_status().close()
except:
ugfx.clear()
ugfx.text(5, 100, "Error. Try again later. :(", ugfx.BLACK)
return False
database.set("tildr_profile", profile_json)
return True
def main_screen(my_profile):
while True:
next_person(my_profile)
while True:
if buttons.is_triggered(Buttons.BTN_Menu):
return False
if buttons.is_triggered(Buttons.BTN_B) or buttons.is_triggered(Buttons.JOY_Right):
break
# if buttons.is_triggered(Buttons.BTN_A) or buttons.is_triggered(Buttons.JOY_Left):
# create_profile(my_profile)
# break
def next_person(my_profile):
ugfx.clear(ugfx.html_color(0x000000))
ugfx.text(5, 100, "Loading...", ugfx.WHITE)
try:
resp = http.get(api_url+'/get_user/'+my_profile['unique_identifier']).json()
except:
ugfx.clear()
ugfx.text(5, 100, "Error. Try again later. :(", ugfx.BLACK)
return
if resp['success']:
display_person(resp['value'])
else:
no_more(my_profile)
def display_person(person):
top_left_logo()
ugfx.set_default_font(ugfx.FONT_TITLE)
ugfx.Label(5, 90, 230, 40, person["username"], justification=ugfx.Label.LEFTTOP)
ugfx.set_default_font(ugfx.FONT_SMALL)
ugfx.text(200, 92, person["age"], ugfx.WHITE)
ugfx.Label(5, 120, 230, 60, person["tag_line"])
ugfx.Label(5, 200, 230, 40, person["looking_for"])
ugfx.text(5, 190, "Looking for...", ugfx.RED)
ugfx.text(5, 245, person["contact"], ugfx.BLUE)
# ugfx.Button(0, 280, 100, 40, "< Edit profile", parent=None, shape=ugfx.Button.RECT, style=None)
ugfx.Button(160, 280, 100, 40, "Swipe >", parent=None, shape=ugfx.Button.RECT, style=None)
def no_more(my_profile):
top_left_logo()
ugfx.set_default_font(ugfx.FONT_TITLE)
ugfx.Label(5, 90, 230, 50, "You've swiped everybody!", justification=ugfx.Label.CENTERTOP)
ugfx.set_default_font(ugfx.FONT_SMALL)
ugfx.Label(5, 160, 230, 20, "Soz "+my_profile["username"], justification=ugfx.Label.CENTERTOP)
ugfx.Label(5, 180, 230, 20, "Come back later ;)", justification=ugfx.Label.CENTERTOP)
# ugfx.Button(0, 280, 100, 40, "< Edit profile", parent=None, shape=ugfx.Button.RECT, style=None)
ugfx.Button(160, 280, 100, 40, "Try again >", parent=None, shape=ugfx.Button.RECT, style=None)
def top_left_logo():
ugfx.clear(ugfx.html_color(0x000000))
try:
logo = http.get("https://i.imgur.com/5HXmXBU.png").raise_for_status().content
ugfx.display_image(0, 5, bytearray(logo))
except:
pass
def quit_loop():
while True:
if buttons.is_triggered(Buttons.BTN_Menu):
return False
while True:
profile = get_profile()
if profile is None:
render_splash_screen()
if not run_splash_screen():
break
profile = {
'username': "",
'age': "",
'tag_line': "",
'looking_for': "",
'contact': ""
}
if not create_profile(profile):
continue
main_screen(profile)
if not quit_loop():
break

66
tildr/person.py Normal file
View File

@ -0,0 +1,66 @@
import http, ugfx, buttons
from tilda import Buttons
from tildr.shared import top_left_logo
def get_next_person(state):
try:
resp = http.get(state['api']+'/get_user/'+state['profile']['unique_identifier']).json()
except:
return None
if not resp['success']:
return None
return resp['value']
def screen(state):
loading = ugfx.Container(0, 0, 240, 320)
loading.text(5, 100, "Loading...", ugfx.WHITE)
state['ui'].append(loading)
loading.show()
person = get_next_person(state)
loading.hide()
if person is None:
state['next'] = "NO_MORE"
return
window = ugfx.Container(0, 0, 240, 320)
window.show()
top_left_logo()
ugfx.set_default_font(ugfx.FONT_TITLE)
l1 = ugfx.Label(5, 90, 230, 40, person["username"], parent=window, justification=ugfx.Label.LEFTTOP)
ugfx.set_default_font(ugfx.FONT_SMALL)
window.text(200, 92, person["age"], ugfx.WHITE)
l2 = ugfx.Label(5, 120, 230, 60, person["tag_line"], parent=window)
l3 = ugfx.Label(5, 200, 230, 40, person["looking_for"], parent=window)
window.text(5, 180, "Looking for...", ugfx.RED)
if not person["contact"].startswith("@"):
person["contact"] = "@" + person["contact"]
window.text(5, 245, person["contact"], ugfx.BLUE)
b2 = ugfx.Button(0, 280, 120, 40, "< Edit profile", parent=window, shape=ugfx.Button.RECT, style=None)
b1 = ugfx.Button(120, 280, 120, 40, "Swipe >", parent=window, shape=ugfx.Button.RECT, style=None)
state['ui'].append(window)
state['ui'].append(l1)
state['ui'].append(l2)
state['ui'].append(l3)
state['ui'].append(b1)
state['ui'].append(b2)
def actions(state):
if buttons.is_triggered(Buttons.BTN_B) or buttons.is_triggered(Buttons.JOY_Right):
state['next'] = "NEXT_PERSON"
if buttons.is_triggered(Buttons.BTN_A) or buttons.is_triggered(Buttons.JOY_Left):
state['next'] = "PROFILE"

66
tildr/profile.py Normal file
View File

@ -0,0 +1,66 @@
import database, ujson, sim800, dialogs, http
def get_profile():
profile_json = database.get("tildr_profile")
if profile_json is None:
return {}
profile = ujson.loads(profile_json)
return profile
def create_profile(state):
try:
http.post(state['api']+'/create_user', json=state['profile']).raise_for_status().close()
except Exception as ex:
print(ex)
return False
profile_json = ujson.dumps(state['profile'])
database.set("tildr_profile", profile_json)
return True
def screen(state):
if state['profile'] is None:
state['profile'] = {
'unique_identifier': "",
'username': "",
'age': "",
'tag_line': "",
'looking_for': "",
'contact': ""
}
ds = [
["username", "What's your name?"],
["age", "What's your age?"],
["tag_line", "Tell us your tagline"],
["looking_for", "And what you're looking for"],
["contact", "And your twitter username"],
]
i = 0
while i < len(ds):
res = dialogs.prompt_text(ds[i][1], init_text=state['profile'][ds[i][0]])
if res is None:
i -= 1
if i < 0:
state['next'] = "SPLASH"
return
elif res != "":
state['profile'][ds[i][0]] = res
i += 1
state['profile']['unique_identifier'] = sim800.imei()
if not create_profile(state):
state['next'] = "ERROR"
return
state['next'] = "NEXT_PERSON"
def actions(state):
return

12
tildr/shared.py Normal file
View File

@ -0,0 +1,12 @@
import ugfx, http
def clear():
ugfx.clear(ugfx.html_color(0x000000))
def top_left_logo():
try:
# logo = http.get("https://i.imgur.com/5HXmXBU.png").raise_for_status().content
ugfx.display_image(1, 5, "tildr/smalllogo.png")
except:
pass

BIN
tildr/smalllogo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 910 B

31
tildr/splash.py Normal file
View File

@ -0,0 +1,31 @@
import http, ugfx, buttons
from tilda import Buttons
from tildr.shared import clear
def screen(state):
window = ugfx.Container(0, 0, 240, 320)
window.show()
try:
# logo = http.get("https://i.imgur.com/0TjxEPs.png").raise_for_status().content
ugfx.display_image(
int((ugfx.width() - 164)/2),
20,
"tildr/biglogo.png")
except:
pass
window.text(160, 100, "TILDR", ugfx.WHITE)
window.text(0, 270, "Find your match @emfcamp ;)", ugfx.WHITE)
window.text(45, 300, "Press A to begin", ugfx.WHITE)
state['ui'].append(window)
def actions(state):
if buttons.is_triggered(Buttons.BTN_A):
if state['profile'] is None:
state['next'] = "PROFILE"
else:
state['next'] = "NEXT_PERSON"