2018-07-29 12:33:46 -04:00
""" Official TiLDA MK4 Badge Store App
switches between app libraries , updates and installs apps .
2018-07-15 06:53:48 -04:00
To publish apps use https : / / badge . emfcamp . org """
___license___ = " MIT "
2018-07-29 12:33:46 -04:00
___title___ = " Badge Store "
2018-08-31 10:55:28 -04:00
___dependencies___ = [ " badge_store " , " dialogs " , " ugfx_helper " , " app " , " database " , " ospath " ]
2018-07-22 06:34:07 -04:00
___categories___ = [ " System " ]
2018-07-15 06:53:48 -04:00
___bootstrapped___ = True
2018-08-31 10:55:28 -04:00
import ugfx_helper , os , database , wifi , app , ospath
2018-08-02 17:42:45 -04:00
from dialogs import *
from lib . badge_store import BadgeStore
2018-09-04 13:28:12 -04:00
from app import *
2018-07-15 06:53:48 -04:00
### VIEWS ###
2018-08-21 15:47:39 -04:00
ugfx_helper . init ( )
2018-08-27 17:20:51 -04:00
url = database . get ( " badge_store.url " , " http://badgeserver.emfcamp.org/2018 " )
repo = database . get ( " badge_store.repo " , " emfcamp/Mk4-Apps " )
ref = database . get ( " badge_store.ref " , " master " )
store = BadgeStore ( url = url , repo = repo , ref = ref )
2018-08-02 17:42:45 -04:00
title = " TiLDA Badge Store "
2018-07-15 06:53:48 -04:00
def clear ( ) :
2018-08-31 10:55:28 -04:00
ugfx . clear ( )
2018-07-15 06:53:48 -04:00
2018-08-02 17:42:45 -04:00
def show_categories ( ) :
2018-08-31 10:55:28 -04:00
clear ( )
2018-09-04 14:25:50 -04:00
with WaitingMessage ( title = title , text = " Loading categories... " ) :
2018-08-02 17:42:45 -04:00
menu_items = [ { " title " : c , " category " : c } for c in store . get_categories ( ) ]
2018-07-15 06:53:48 -04:00
2018-09-04 14:25:50 -04:00
option = prompt_option ( menu_items , none_text = " Back " , title = " Install: Categories " )
2018-08-02 17:42:45 -04:00
if option :
show_apps ( option [ " category " ] )
else :
return
def show_apps ( c ) :
2018-08-31 10:55:28 -04:00
clear ( )
2018-08-02 17:42:45 -04:00
menu_items = [ { " title " : a , " app " : a } for a in store . get_apps ( c ) ]
2018-09-04 14:25:50 -04:00
option = prompt_option ( menu_items , none_text = " Back " , title = " Install: " + c )
2018-07-15 06:53:48 -04:00
2018-08-02 17:42:45 -04:00
if option :
2018-09-04 13:28:12 -04:00
show_app ( option [ " app " ] , c )
2018-08-02 17:42:45 -04:00
else :
2018-09-04 14:30:47 -04:00
show_categories ( )
2018-08-02 17:42:45 -04:00
2018-09-04 13:28:12 -04:00
def show_app ( a , c ) :
2018-08-31 10:55:28 -04:00
clear ( )
2018-09-04 14:25:50 -04:00
with WaitingMessage ( title = title , text = " Loading app description... " ) :
2018-08-02 17:42:45 -04:00
app_info = store . get_app ( a )
2018-09-14 18:26:48 -04:00
# Try to get the 'title' key from app_info, falling back to the value of a if not present
name = app_info . get ( " title " , a )
desc = app_info [ " description " ] . strip ( )
app_text = """ App: \n {} \n \n Description: \n {} """ . format ( name , desc )
2018-09-04 14:25:50 -04:00
install = prompt_boolean ( app_text , title = " Install App " , true_text = " Install " , false_text = " Back " )
2018-08-02 17:42:45 -04:00
if install :
2018-09-14 18:26:48 -04:00
app_text = " App: \n {} \n \n " . format ( name )
2018-09-04 12:08:01 -04:00
with WaitingMessage ( title = " Installing App... " , text = " %s Getting ready... " % app_text ) as message :
2018-09-10 08:36:12 -04:00
installers = store . install ( [ a ] )
2018-08-02 17:42:45 -04:00
n = len ( installers )
for i , installer in enumerate ( installers ) :
2018-09-04 12:08:01 -04:00
message . text = " %s %s ( %s / %s ) " % ( app_text + " Downloading files... \n \n " , installer . path , i + 1 , n )
2018-08-02 17:42:45 -04:00
installer . download ( )
2018-08-31 10:55:28 -04:00
app . uncache_apps ( )
2018-08-02 17:42:45 -04:00
2018-09-04 13:28:12 -04:00
launch = prompt_boolean (
" %s Successfully installed. \n \n Press A to launch the app. \n \n Press B to list more \" %s \" apps. " % ( app_text , c ) , title = " Install Success! " , true_text = " Launch " , false_text = " Back " )
if ( launch ) :
for app_obj in get_apps ( ) :
if app_obj . name == a :
app_obj . boot ( )
else :
show_apps ( c )
2018-09-15 10:59:37 -04:00
else :
show_apps ( c )
2018-09-04 13:28:12 -04:00
2018-08-02 17:42:45 -04:00
def show_update ( ) :
2018-08-31 10:55:28 -04:00
clear ( )
2018-09-04 15:10:02 -04:00
update = prompt_boolean ( " Do you want to update all apps on this badge? " , title = " Update all Apps " , true_text = " OK " , false_text = " Back " )
2018-08-31 10:55:28 -04:00
if update :
clear ( )
2018-09-16 11:29:10 -04:00
with WaitingMessage ( title = title , text = " Getting updates... " ) as message :
2018-09-04 15:10:02 -04:00
update_text = " Downloading files: "
2018-08-31 10:55:28 -04:00
installers = store . install ( _get_current_apps ( ) )
n = len ( installers )
for i , installer in enumerate ( installers ) :
2018-09-04 15:10:02 -04:00
message . text = " %s \n \n %s ( %s / %s ) " % ( update_text , installer . path , i + 1 , n )
2018-08-31 10:55:28 -04:00
installer . download ( )
2018-09-04 15:10:02 -04:00
notice ( " Your badge has been successfully updated. " , title = " Update Success! " , close_text = " Back " )
2018-07-15 06:53:48 -04:00
2018-08-02 17:42:45 -04:00
def show_remove ( ) :
2018-08-31 10:55:28 -04:00
clear ( )
2018-09-04 16:27:30 -04:00
app_to_remove = prompt_option ( _get_current_apps ( ) , title = " Remove App... " , none_text = " Back " , text = " Select an App to remove. " )
2018-08-31 10:55:28 -04:00
if app_to_remove :
ospath . recursive_rmdir ( app_to_remove )
app . uncache_apps ( )
2018-09-14 18:26:48 -04:00
app_text = """ App: \n {} """ . format ( app_to_remove )
notice ( " \" %s \" \n \n The app has now been removed. " % app_text , title = " Remove Success! " , close_text = " Back " )
2018-07-15 06:53:48 -04:00
def main_menu ( ) :
while True :
clear ( )
menu_items = [
2018-08-02 17:42:45 -04:00
{ " title " : " Install Apps " , " function " : show_categories } ,
2018-08-31 10:55:28 -04:00
{ " title " : " Update all Apps " , " function " : show_update } ,
{ " title " : " Remove App " , " function " : show_remove }
2018-07-15 06:53:48 -04:00
]
2018-08-02 17:42:45 -04:00
option = prompt_option ( menu_items , none_text = " Exit " , text = " What do you want to do? " , title = title )
2018-07-15 06:53:48 -04:00
if option :
option [ " function " ] ( )
2018-08-31 13:04:20 -04:00
else :
break
2018-07-15 06:53:48 -04:00
2018-08-23 18:07:25 -04:00
def _get_current_apps ( ) :
return [ a . name for a in app . get_apps ( ) ]
2018-08-21 15:47:39 -04:00
wifi . connect ( show_wait_message = True )
2018-07-15 06:53:48 -04:00
main_menu ( )
2018-08-31 10:55:28 -04:00
app . restart_to_default ( )