flip_store_callback.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include <callback/flip_store_callback.h>
  2. bool flip_store_app_does_exist = false;
  3. // Callback for drawing the main screen
  4. void flip_store_view_draw_callback_main(Canvas *canvas, void *model)
  5. {
  6. UNUSED(model);
  7. canvas_set_font(canvas, FontSecondary);
  8. if (fhttp.state == INACTIVE)
  9. {
  10. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  11. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  12. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  13. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  14. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  15. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  16. return;
  17. }
  18. if (!flip_store_sent_request)
  19. {
  20. flip_store_sent_request = true;
  21. if (!flip_store_install_app(canvas, categories[flip_store_category_index]))
  22. {
  23. canvas_clear(canvas);
  24. canvas_draw_str(canvas, 0, 10, "Failed to install app.");
  25. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  26. }
  27. }
  28. else
  29. {
  30. if (flip_store_success)
  31. {
  32. if (fhttp.state == RECEIVING)
  33. {
  34. canvas_clear(canvas);
  35. canvas_draw_str(canvas, 0, 10, "Downloading app...");
  36. canvas_draw_str(canvas, 0, 60, "Please wait...");
  37. return;
  38. }
  39. else if (fhttp.state == IDLE)
  40. {
  41. canvas_clear(canvas);
  42. canvas_draw_str(canvas, 0, 10, "App installed successfully.");
  43. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  44. }
  45. }
  46. else
  47. {
  48. canvas_clear(canvas);
  49. canvas_draw_str(canvas, 0, 10, "Failed to install app.");
  50. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  51. }
  52. }
  53. }
  54. void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  55. {
  56. UNUSED(model);
  57. canvas_clear(canvas);
  58. canvas_set_font(canvas, FontPrimary);
  59. canvas_draw_str(canvas, 0, 10, flip_catalog[app_selected_index].app_name);
  60. if (flip_store_app_does_exist)
  61. {
  62. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  63. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  64. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  65. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  66. }
  67. else
  68. {
  69. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  70. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  71. }
  72. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  73. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  74. }
  75. bool flip_store_input_callback(InputEvent *event, void *context)
  76. {
  77. FlipStoreApp *app = (FlipStoreApp *)context;
  78. if (!app)
  79. {
  80. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  81. return false;
  82. }
  83. if (event->type == InputTypeShort)
  84. {
  85. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  86. {
  87. // Left button clicked, delete the app
  88. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  89. return true;
  90. }
  91. if (event->key == InputKeyRight)
  92. {
  93. // Right button clicked, download the app
  94. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewMain);
  95. return true;
  96. }
  97. }
  98. else if (event->type == InputTypePress)
  99. {
  100. if (event->key == InputKeyBack)
  101. {
  102. // Back button clicked, switch to the previous view.
  103. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. void flip_store_text_updated_ssid(void *context)
  110. {
  111. FlipStoreApp *app = (FlipStoreApp *)context;
  112. if (!app)
  113. {
  114. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  115. return;
  116. }
  117. // store the entered text
  118. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  119. // Ensure null-termination
  120. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  121. // update the variable item text
  122. if (app->variable_item_ssid)
  123. {
  124. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  125. }
  126. // save the settings
  127. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  128. // if SSID and PASS are not empty, connect to the WiFi
  129. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  130. {
  131. // save wifi settings
  132. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  133. {
  134. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  135. }
  136. }
  137. // switch to the settings view
  138. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  139. }
  140. void flip_store_text_updated_pass(void *context)
  141. {
  142. FlipStoreApp *app = (FlipStoreApp *)context;
  143. if (!app)
  144. {
  145. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  146. return;
  147. }
  148. // store the entered text
  149. strncpy(app->uart_text_input_buffer_pass, app->uart_text_input_temp_buffer_pass, app->uart_text_input_buffer_size_pass);
  150. // Ensure null-termination
  151. app->uart_text_input_buffer_pass[app->uart_text_input_buffer_size_pass - 1] = '\0';
  152. // update the variable item text
  153. if (app->variable_item_pass)
  154. {
  155. variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer_pass);
  156. }
  157. // save the settings
  158. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass);
  159. // if SSID and PASS are not empty, connect to the WiFi
  160. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_pass) > 0)
  161. {
  162. // save wifi settings
  163. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_pass))
  164. {
  165. FURI_LOG_E(TAG, "Failed to save WiFi settings");
  166. }
  167. }
  168. // switch to the settings view
  169. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  170. }
  171. uint32_t callback_to_submenu(void *context)
  172. {
  173. if (!context)
  174. {
  175. FURI_LOG_E(TAG, "Context is NULL");
  176. return VIEW_NONE;
  177. }
  178. UNUSED(context);
  179. return FlipStoreViewSubmenu;
  180. }
  181. uint32_t callback_to_app_list(void *context)
  182. {
  183. if (!context)
  184. {
  185. FURI_LOG_E(TAG, "Context is NULL");
  186. return VIEW_NONE;
  187. }
  188. UNUSED(context);
  189. flip_store_sent_request = false;
  190. flip_store_success = false;
  191. flip_store_saved_data = false;
  192. flip_store_saved_success = false;
  193. flip_store_app_does_exist = false;
  194. return FlipStoreViewAppList;
  195. }
  196. void settings_item_selected(void *context, uint32_t index)
  197. {
  198. FlipStoreApp *app = (FlipStoreApp *)context;
  199. if (!app)
  200. {
  201. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  202. return;
  203. }
  204. switch (index)
  205. {
  206. case 0: // Input SSID
  207. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputSSID);
  208. break;
  209. case 1: // Input Password
  210. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputPass);
  211. break;
  212. default:
  213. FURI_LOG_E(TAG, "Unknown configuration item index");
  214. break;
  215. }
  216. }
  217. void dialog_callback(DialogExResult result, void *context)
  218. {
  219. furi_assert(context);
  220. FlipStoreApp *app = (FlipStoreApp *)context;
  221. if (result == DialogExResultLeft) // No
  222. {
  223. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  224. }
  225. else if (result == DialogExResultRight)
  226. {
  227. // delete the app then return to the app list
  228. if (!delete_app(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]))
  229. {
  230. // pop up a message
  231. popup_set_header(app->popup, "[ERROR]", 0, 0, AlignLeft, AlignTop);
  232. popup_set_text(app->popup, "Issue deleting app.", 0, 50, AlignLeft, AlignTop);
  233. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  234. furi_delay_ms(2000); // delay for 2 seconds
  235. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  236. }
  237. else
  238. {
  239. // pop up a message
  240. popup_set_header(app->popup, "[SUCCESS]", 0, 0, AlignLeft, AlignTop);
  241. popup_set_text(app->popup, "App deleted successfully.", 0, 50, AlignLeft, AlignTop);
  242. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  243. furi_delay_ms(2000); // delay for 2 seconds
  244. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  245. }
  246. }
  247. }
  248. void popup_callback(void *context)
  249. {
  250. FlipStoreApp *app = (FlipStoreApp *)context;
  251. if (!app)
  252. {
  253. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  254. return;
  255. }
  256. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenu);
  257. }
  258. uint32_t callback_exit_app(void *context)
  259. {
  260. // Exit the application
  261. if (!context)
  262. {
  263. FURI_LOG_E(TAG, "Context is NULL");
  264. return VIEW_NONE;
  265. }
  266. UNUSED(context);
  267. return VIEW_NONE; // Return VIEW_NONE to exit the app
  268. }
  269. void callback_submenu_choices(void *context, uint32_t index)
  270. {
  271. FlipStoreApp *app = (FlipStoreApp *)context;
  272. if (!app)
  273. {
  274. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  275. return;
  276. }
  277. switch (index)
  278. {
  279. case FlipStoreSubmenuIndexMain:
  280. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewMain);
  281. break;
  282. case FlipStoreSubmenuIndexAbout:
  283. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  284. break;
  285. case FlipStoreSubmenuIndexSettings:
  286. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  287. break;
  288. case FlipStoreSubmenuIndexAppList:
  289. flip_store_category_index = 0;
  290. flip_store_app_does_exist = false;
  291. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  292. break;
  293. case FlipStoreSubmenuIndexAppListBluetooth:
  294. flip_store_category_index = 0;
  295. flip_store_app_does_exist = false;
  296. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListBluetooth, "Bluetooth", &app->submenu_app_list_bluetooth));
  297. break;
  298. case FlipStoreSubmenuIndexAppListGames:
  299. flip_store_category_index = 1;
  300. flip_store_app_does_exist = false;
  301. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGames, "Games", &app->submenu_app_list_games));
  302. break;
  303. case FlipStoreSubmenuIndexAppListGPIO:
  304. flip_store_category_index = 2;
  305. flip_store_app_does_exist = false;
  306. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGPIO, "GPIO", &app->submenu_app_list_gpio));
  307. break;
  308. case FlipStoreSubmenuIndexAppListInfrared:
  309. flip_store_category_index = 3;
  310. flip_store_app_does_exist = false;
  311. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListInfrared, "Infrared", &app->submenu_app_list_infrared));
  312. break;
  313. case FlipStoreSubmenuIndexAppListiButton:
  314. flip_store_category_index = 4;
  315. flip_store_app_does_exist = false;
  316. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListiButton, "iButton", &app->submenu_app_list_ibutton));
  317. break;
  318. case FlipStoreSubmenuIndexAppListMedia:
  319. flip_store_category_index = 5;
  320. flip_store_app_does_exist = false;
  321. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListMedia, "Media", &app->submenu_app_list_media));
  322. break;
  323. case FlipStoreSubmenuIndexAppListNFC:
  324. flip_store_category_index = 6;
  325. flip_store_app_does_exist = false;
  326. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListNFC, "NFC", &app->submenu_app_list_nfc));
  327. break;
  328. case FlipStoreSubmenuIndexAppListRFID:
  329. flip_store_category_index = 7;
  330. flip_store_app_does_exist = false;
  331. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListRFID, "RFID", &app->submenu_app_list_rfid));
  332. break;
  333. case FlipStoreSubmenuIndexAppListSubGHz:
  334. flip_store_category_index = 8;
  335. flip_store_app_does_exist = false;
  336. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListSubGHz, "Sub-GHz", &app->submenu_app_list_subghz));
  337. break;
  338. case FlipStoreSubmenuIndexAppListTools:
  339. flip_store_category_index = 9;
  340. flip_store_app_does_exist = false;
  341. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListTools, "Tools", &app->submenu_app_list_tools));
  342. break;
  343. case FlipStoreSubmenuIndexAppListUSB:
  344. flip_store_category_index = 10;
  345. flip_store_app_does_exist = false;
  346. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListUSB, "USB", &app->submenu_app_list_usb));
  347. break;
  348. default:
  349. // Check if the index is within the app list range
  350. if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  351. {
  352. // Get the app index
  353. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  354. // Check if the app index is valid
  355. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  356. {
  357. // Get the app name
  358. char *app_name = flip_catalog[app_index].app_name;
  359. // Check if the app name is valid
  360. if (app_name != NULL && strlen(app_name) > 0)
  361. {
  362. app_selected_index = app_index;
  363. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  364. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  365. }
  366. else
  367. {
  368. FURI_LOG_E(TAG, "Invalid app name");
  369. }
  370. }
  371. else
  372. {
  373. FURI_LOG_E(TAG, "Invalid app index");
  374. }
  375. }
  376. else
  377. {
  378. FURI_LOG_E(TAG, "Unknown submenu index");
  379. }
  380. break;
  381. }
  382. }