flip_store_callback.c 13 KB

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