66
77from mpos .apps import Activity , Intent
88from mpos .app import App
9- from mpos import TaskManager , DownloadManager
9+ from mpos import TaskManager , DownloadManager , SettingActivity
1010import mpos .ui
1111from mpos .content .package_manager import PackageManager
1212from mpos .config import SharedPreferences
@@ -28,7 +28,6 @@ class AppStore(Activity):
2828
2929 apps = []
3030
31- _DEFAULT_BACKEND = 0
3231 _ICON_SIZE = 64
3332
3433 # Hardcoded list for now:
@@ -38,33 +37,33 @@ class AppStore(Activity):
3837 ("BadgeHub Prod" , _BACKEND_API_BADGEHUB , _BADGEHUB_PROD_BASE_URL , _BADGEHUB_LIST , _BADGEHUB_DETAILS )
3938 ]
4039
40+ _DEFAULT_BACKEND = _BACKEND_API_GITHUB + "," + _GITHUB_PROD_BASE_URL + "/" + _GITHUB_LIST
41+
4142 @staticmethod
42- def get_backend_urls (index ):
43+ def get_backend_pref_string (index ):
4344 backend_info = AppStore .backends [index ]
4445 if backend_info :
4546 api = backend_info [1 ]
4647 base_url = backend_info [2 ]
4748 list_suffix = backend_info [3 ]
4849 details_suffix = backend_info [4 ]
49- if api == AppStore . _BACKEND_API_GITHUB :
50- return ( base_url + "/" + list_suffix , None )
51- else :
52- return ( base_url + "/" + list_suffix , base_url + "/" + details_suffix )
50+ toreturn = api + "," + base_url + "/" + list_suffix
51+ if api == AppStore . _BACKEND_API_BADGEHUB :
52+ toreturn += "," + base_url + "/" + details_suffix
53+ return toreturn
5354
5455 @staticmethod
55- def get_backend_type (index ):
56- backend_info = AppStore .backends [index ]
57- if backend_info :
58- return backend_info [1 ]
56+ def backend_pref_string_to_backend (string ):
57+ return string .split ("," )
5958
60- def get_backend_urls_from_settings (self ):
61- return AppStore .get_backend_urls (self .prefs .get_int ("backend" , self ._DEFAULT_BACKEND ))
59+ def get_backend_type_from_settings (self ):
60+ return AppStore .backend_pref_string_to_backend (self .prefs .get_string ("backend" , self ._DEFAULT_BACKEND ))[ 0 ]
6261
6362 def get_backend_list_url_from_settings (self ):
64- return self .get_backend_urls_from_settings ()[ 0 ]
63+ return AppStore . backend_pref_string_to_backend ( self .prefs . get_string ( "backend" , self . _DEFAULT_BACKEND ))[ 1 ]
6564
6665 def get_backend_details_url_from_settings ():
67- return self .get_backend_urls_from_settings ()[ 1 ]
66+ return AppStore . backend_pref_string_to_backend ( self .prefs . get_string ( "backend" , self . _DEFAULT_BACKEND ))[ 2 ]
6867
6968 can_check_network = True
7069 prefs = None
@@ -89,7 +88,6 @@ def onCreate(self):
8988 self .settings_button .set_size (settings_size , settings_size )
9089 self .settings_button .align (lv .ALIGN .TOP_RIGHT , - settings_margin , 10 )
9190 self .settings_button .add_event_cb (self .settings_button_tap ,lv .EVENT .CLICKED ,None )
92- self .settings_button .add_flag (lv .obj .FLAG .HIDDEN ) # hide because not functional for now
9391 settings_label = lv .label (self .settings_button )
9492 settings_label .set_text (lv .SYMBOL .SETTINGS )
9593 settings_label .set_style_text_font (lv .font_montserrat_24 , 0 )
@@ -98,14 +96,10 @@ def onCreate(self):
9896
9997 def onResume (self , screen ):
10098 super ().onResume (screen )
101- # This gets called at startup and also after closing AppStoreSettings
102- if len (self .apps ):
103- return # already have the list (if refresh after settings is needed, finished_settings_callback will do it)
104- if self .prefs : # prefs is abused to distinguish between a fresh start and a return after AppStoreSettings
105- return # prefs is set so it's not a fresh start - it's a return after after AppStoreSettings
106- print ("It's a fresh start; loading preferences and refreshing list..." )
107- self .prefs = SharedPreferences (self .PACKAGE )
108- self .refresh_list ()
99+ if not self .prefs :
100+ self .prefs = SharedPreferences (self .PACKAGE )
101+ if not len (self .apps ):
102+ self .refresh_list ()
109103
110104 def refresh_list (self ):
111105 try :
@@ -119,20 +113,18 @@ def refresh_list(self):
119113 TaskManager .create_task (self .download_app_index (self .get_backend_list_url_from_settings ()))
120114
121115 def settings_button_tap (self , event ):
122- print ("Settings button clicked" )
123- # Handle traditional settings (existing code)
124- intent = Intent (activity_class = AppStoreSettings )
116+ intent = Intent (activity_class = SettingActivity )
125117 intent .putExtra ("prefs" , self .prefs )
126- intent .putExtra ("setting" , {"title" : "Backend" , "key" : "backend" , "value_label" : None , "cont" : None , "ui" : "radiobuttons" , "ui_options" : [(backend [0 ], None ) for backend in AppStore .backends ]},)
127- self .startActivityForResult (intent , self .finished_settings_callback )
118+ intent .putExtra ("setting" , {"title" : "AppStore Backend" ,
119+ "key" : "backend" ,
120+ "ui" : "radiobuttons" ,
121+ "ui_options" : [(backend [0 ], AppStore .get_backend_pref_string (index )) for index , backend in enumerate (AppStore .backends )],
122+ "changed_callback" : self .backend_changed })
123+ self .startActivity (intent )
128124
129- def finished_settings_callback (self , result ):
130- print (f"finished_settings_callback result: { result } " )
131- if result .get ("result_code" ) is True :
132- print ("Settings updated, reloading app list..." )
133- self .refresh_list ()
134- else :
135- print ("Settings not updated, nothing to do." )
125+ def backend_changed (self , new_value ):
126+ print (f"backend changed to { new_value } , refreshing..." )
127+ self .refresh_list ()
136128
137129 async def download_app_index (self , json_url ):
138130 try :
@@ -147,10 +139,11 @@ async def download_app_index(self, json_url):
147139 print (f"Got response text: { response [0 :20 ]} " )
148140 try :
149141 parsed = json .loads (response )
150- print (f"parsed json: { parsed } " )
142+ #print(f"parsed json: {parsed}")
143+ self .apps .clear ()
151144 for app in parsed :
152145 try :
153- backend_type = AppStore . get_backend_type ( self .prefs . get_int ( "backend" , self . _DEFAULT_BACKEND ) )
146+ backend_type = self .get_backend_type_from_settings ( )
154147 if backend_type == self ._BACKEND_API_BADGEHUB :
155148 self .apps .append (AppStore .badgehub_app_to_mpos_app (app ))
156149 else :
@@ -290,7 +283,7 @@ async def fetch_badgehub_app_details(self, app_obj):
290283 print (f"Got response text: { response [0 :20 ]} " )
291284 try :
292285 parsed = json .loads (response )
293- print (f"parsed json: { parsed } " )
286+ # print(f"parsed json: {parsed}")
294287 print ("Using short_description as long_description because backend doesn't support it..." )
295288 app_obj .long_description = app_obj .short_description
296289 print ("Finding version number..." )
@@ -434,7 +427,7 @@ def onCreate(self):
434427 self .setContentView (app_detail_screen )
435428
436429 def onResume (self , screen ):
437- backend_type = AppStore . get_backend_type ( self .prefs . get_int ( "backend" , self . _DEFAULT_BACKEND ) )
430+ backend_type = self .get_backend_type_from_settings ( )
438431 if backend_type == self .appstore ._BACKEND_API_BADGEHUB :
439432 TaskManager .create_task (self .fetch_and_set_app_details ())
440433 else :
@@ -603,17 +596,3 @@ async def download_and_install(self, app_obj, dest_folder):
603596 self .progress_bar .set_value (0 , False )
604597 self .set_install_label (app_fullname )
605598 self .install_button .remove_state (lv .STATE .DISABLED )
606-
607-
608- class AppStoreSettings (Activity ):
609- prefs = None
610-
611- def onCreate (self ):
612- self .prefs = self .getIntent ().extras .get ("prefs" )
613- # Create main screen
614- screen = lv .obj ()
615- screen .set_size (lv .pct (100 ), lv .pct (100 ))
616- screen .set_style_pad_all (1 , 0 )
617- label = lv .label (screen )
618- label .set_text ("AppStoreSettings should go here." )
619- self .setContentView (screen )
0 commit comments