flip_store_callback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. return FlipStoreViewAppList;
  187. }
  188. void settings_item_selected(void *context, uint32_t index)
  189. {
  190. FlipStoreApp *app = (FlipStoreApp *)context;
  191. if (!app)
  192. {
  193. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  194. return;
  195. }
  196. switch (index)
  197. {
  198. case 0: // Input SSID
  199. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputSSID);
  200. break;
  201. case 1: // Input Password
  202. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputPass);
  203. break;
  204. default:
  205. FURI_LOG_E(TAG, "Unknown configuration item index");
  206. break;
  207. }
  208. }
  209. void dialog_callback(DialogExResult result, void *context)
  210. {
  211. furi_assert(context);
  212. FlipStoreApp *app = (FlipStoreApp *)context;
  213. if (result == DialogExResultLeft) // No
  214. {
  215. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  216. }
  217. else if (result == DialogExResultRight)
  218. {
  219. // delete the app then return to the app list
  220. // pop up a message
  221. popup_set_header(app->popup, "Success", 0, 0, AlignLeft, AlignTop);
  222. popup_set_text(app->popup, "App deleted successfully.", 0, 60, AlignLeft, AlignTop);
  223. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  224. furi_delay_ms(2000); // delay for 2 seconds
  225. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  226. }
  227. }
  228. void popup_callback(void *context)
  229. {
  230. FlipStoreApp *app = (FlipStoreApp *)context;
  231. if (!app)
  232. {
  233. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  234. return;
  235. }
  236. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenu);
  237. }
  238. /**
  239. * @brief Navigation callback for exiting the application
  240. * @param context The context - unused
  241. * @return next view id (VIEW_NONE to exit the app)
  242. */
  243. uint32_t callback_exit_app(void *context)
  244. {
  245. // Exit the application
  246. if (!context)
  247. {
  248. FURI_LOG_E(TAG, "Context is NULL");
  249. return VIEW_NONE;
  250. }
  251. UNUSED(context);
  252. return VIEW_NONE; // Return VIEW_NONE to exit the app
  253. }
  254. void callback_submenu_choices(void *context, uint32_t index)
  255. {
  256. FlipStoreApp *app = (FlipStoreApp *)context;
  257. if (!app)
  258. {
  259. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  260. return;
  261. }
  262. switch (index)
  263. {
  264. case FlipStoreSubmenuIndexMain:
  265. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewMain);
  266. break;
  267. case FlipStoreSubmenuIndexAbout:
  268. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  269. break;
  270. case FlipStoreSubmenuIndexSettings:
  271. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  272. break;
  273. case FlipStoreSubmenuIndexAppList:
  274. flip_store_category_index = 0;
  275. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  276. break;
  277. case FlipStoreSubmenuIndexAppListBluetooth:
  278. flip_store_category_index = 0;
  279. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListBluetooth, "Bluetooth", &app->submenu_app_list_bluetooth));
  280. break;
  281. case FlipStoreSubmenuIndexAppListGames:
  282. flip_store_category_index = 1;
  283. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGames, "Games", &app->submenu_app_list_games));
  284. break;
  285. case FlipStoreSubmenuIndexAppListGPIO:
  286. flip_store_category_index = 2;
  287. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGPIO, "GPIO", &app->submenu_app_list_gpio));
  288. break;
  289. case FlipStoreSubmenuIndexAppListInfrared:
  290. flip_store_category_index = 3;
  291. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListInfrared, "Infrared", &app->submenu_app_list_infrared));
  292. break;
  293. case FlipStoreSubmenuIndexAppListiButton:
  294. flip_store_category_index = 4;
  295. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListiButton, "iButton", &app->submenu_app_list_ibutton));
  296. break;
  297. case FlipStoreSubmenuIndexAppListMedia:
  298. flip_store_category_index = 5;
  299. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListMedia, "Media", &app->submenu_app_list_media));
  300. break;
  301. case FlipStoreSubmenuIndexAppListNFC:
  302. flip_store_category_index = 6;
  303. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListNFC, "NFC", &app->submenu_app_list_nfc));
  304. break;
  305. case FlipStoreSubmenuIndexAppListRFID:
  306. flip_store_category_index = 7;
  307. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListRFID, "RFID", &app->submenu_app_list_rfid));
  308. break;
  309. case FlipStoreSubmenuIndexAppListSubGHz:
  310. flip_store_category_index = 8;
  311. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListSubGHz, "Sub-GHz", &app->submenu_app_list_subghz));
  312. break;
  313. case FlipStoreSubmenuIndexAppListTools:
  314. flip_store_category_index = 9;
  315. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListTools, "Tools", &app->submenu_app_list_tools));
  316. break;
  317. case FlipStoreSubmenuIndexAppListUSB:
  318. flip_store_category_index = 10;
  319. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListUSB, "USB", &app->submenu_app_list_usb));
  320. break;
  321. default:
  322. // Check if the index is within the app list range
  323. if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  324. {
  325. // Get the app index
  326. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  327. // Check if the app index is valid
  328. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  329. {
  330. // Get the app name
  331. char *app_name = flip_catalog[app_index].app_name;
  332. // Check if the app name is valid
  333. if (app_name != NULL && strlen(app_name) > 0)
  334. {
  335. app_selected_index = app_index;
  336. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  337. }
  338. else
  339. {
  340. FURI_LOG_E(TAG, "Invalid app name");
  341. }
  342. }
  343. else
  344. {
  345. FURI_LOG_E(TAG, "Invalid app index");
  346. }
  347. }
  348. else
  349. {
  350. FURI_LOG_E(TAG, "Unknown submenu index");
  351. }
  352. break;
  353. }
  354. }