flip_store_callback.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef FLIP_STORE_CALLBACK_H
  2. #define FLIP_STORE_CALLBACK_H
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <stdbool.h>
  8. #include <flip_store_apps.h>
  9. // Callback for drawing the main screen
  10. static void flip_store_view_draw_callback_main(Canvas *canvas, void *model)
  11. {
  12. UNUSED(model);
  13. canvas_set_font(canvas, FontSecondary);
  14. if (fhttp.state == INACTIVE)
  15. {
  16. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  17. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  18. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  19. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  20. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  21. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  22. return;
  23. }
  24. if (!flip_store_sent_request)
  25. {
  26. flip_store_sent_request = true;
  27. if (!flip_store_install_app(canvas, categories[flip_store_category_index]))
  28. {
  29. canvas_clear(canvas);
  30. canvas_draw_str(canvas, 0, 10, "Failed to install app.");
  31. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  32. }
  33. }
  34. else
  35. {
  36. if (flip_store_success)
  37. {
  38. if (fhttp.state == RECEIVING)
  39. {
  40. canvas_clear(canvas);
  41. canvas_draw_str(canvas, 0, 10, "Downloading app...");
  42. canvas_draw_str(canvas, 0, 60, "Please wait...");
  43. return;
  44. }
  45. else if (fhttp.state == IDLE)
  46. {
  47. canvas_clear(canvas);
  48. canvas_draw_str(canvas, 0, 10, "App installed successfully.");
  49. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  50. }
  51. }
  52. else
  53. {
  54. canvas_clear(canvas);
  55. canvas_draw_str(canvas, 0, 10, "Failed to install app.");
  56. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  57. }
  58. }
  59. }
  60. static void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  61. {
  62. UNUSED(model);
  63. canvas_clear(canvas);
  64. canvas_set_font(canvas, FontPrimary);
  65. // Adjusted to access flip_catalog as an array of structures
  66. canvas_draw_str(canvas, 0, 10, flip_catalog[app_selected_index].app_name);
  67. // canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7); (future implementation)
  68. // canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete"); (future implementation)
  69. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  70. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  71. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  72. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  73. }
  74. static bool flip_store_input_callback(InputEvent *event, void *context)
  75. {
  76. FlipStoreApp *app = (FlipStoreApp *)context;
  77. if (!app)
  78. {
  79. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  80. return false;
  81. }
  82. if (event->type == InputTypeShort)
  83. {
  84. // Future implementation
  85. // if (event->key == InputKeyLeft)
  86. //{
  87. // Left button clicked, delete the app with DialogEx confirmation
  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. static 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. static 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. static 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. static 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. return FlipStoreViewAppList;
  194. }
  195. static void settings_item_selected(void *context, uint32_t index)
  196. {
  197. FlipStoreApp *app = (FlipStoreApp *)context;
  198. if (!app)
  199. {
  200. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  201. return;
  202. }
  203. switch (index)
  204. {
  205. case 0: // Input SSID
  206. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputSSID);
  207. break;
  208. case 1: // Input Password
  209. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInputPass);
  210. break;
  211. default:
  212. FURI_LOG_E(TAG, "Unknown configuration item index");
  213. break;
  214. }
  215. }
  216. void dialog_callback(DialogExResult result, void *context)
  217. {
  218. furi_assert(context);
  219. FlipStoreApp *app = (FlipStoreApp *)context;
  220. if (result == DialogExResultLeft) // No
  221. {
  222. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  223. }
  224. else if (result == DialogExResultRight)
  225. {
  226. // delete the app then return to the app list
  227. // pop up a message
  228. popup_set_header(app->popup, "Success", 0, 0, AlignLeft, AlignTop);
  229. popup_set_text(app->popup, "App deleted successfully.", 0, 60, AlignLeft, AlignTop);
  230. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewPopup);
  231. furi_delay_ms(2000); // delay for 2 seconds
  232. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  233. }
  234. }
  235. void popup_callback(void *context)
  236. {
  237. FlipStoreApp *app = (FlipStoreApp *)context;
  238. if (!app)
  239. {
  240. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  241. return;
  242. }
  243. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenu);
  244. }
  245. /**
  246. * @brief Navigation callback for exiting the application
  247. * @param context The context - unused
  248. * @return next view id (VIEW_NONE to exit the app)
  249. */
  250. static uint32_t callback_exit_app(void *context)
  251. {
  252. // Exit the application
  253. if (!context)
  254. {
  255. FURI_LOG_E(TAG, "Context is NULL");
  256. return VIEW_NONE;
  257. }
  258. UNUSED(context);
  259. return VIEW_NONE; // Return VIEW_NONE to exit the app
  260. }
  261. static void callback_submenu_choices(void *context, uint32_t index)
  262. {
  263. FlipStoreApp *app = (FlipStoreApp *)context;
  264. if (!app)
  265. {
  266. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  267. return;
  268. }
  269. switch (index)
  270. {
  271. case FlipStoreSubmenuIndexMain:
  272. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewMain);
  273. break;
  274. case FlipStoreSubmenuIndexAbout:
  275. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  276. break;
  277. case FlipStoreSubmenuIndexSettings:
  278. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  279. break;
  280. case FlipStoreSubmenuIndexAppList:
  281. flip_store_category_index = 0;
  282. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  283. break;
  284. case FlipStoreSubmenuIndexAppListBluetooth:
  285. flip_store_category_index = 0;
  286. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListBluetooth, "Bluetooth", &app->submenu_app_list_bluetooth));
  287. break;
  288. case FlipStoreSubmenuIndexAppListGames:
  289. flip_store_category_index = 1;
  290. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGames, "Games", &app->submenu_app_list_games));
  291. break;
  292. case FlipStoreSubmenuIndexAppListGPIO:
  293. flip_store_category_index = 2;
  294. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListGPIO, "GPIO", &app->submenu_app_list_gpio));
  295. break;
  296. case FlipStoreSubmenuIndexAppListInfrared:
  297. flip_store_category_index = 3;
  298. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListInfrared, "Infrared", &app->submenu_app_list_infrared));
  299. break;
  300. case FlipStoreSubmenuIndexAppListiButton:
  301. flip_store_category_index = 4;
  302. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListiButton, "iButton", &app->submenu_app_list_ibutton));
  303. break;
  304. case FlipStoreSubmenuIndexAppListMedia:
  305. flip_store_category_index = 5;
  306. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListMedia, "Media", &app->submenu_app_list_media));
  307. break;
  308. case FlipStoreSubmenuIndexAppListNFC:
  309. flip_store_category_index = 6;
  310. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListNFC, "NFC", &app->submenu_app_list_nfc));
  311. break;
  312. case FlipStoreSubmenuIndexAppListRFID:
  313. flip_store_category_index = 7;
  314. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListRFID, "RFID", &app->submenu_app_list_rfid));
  315. break;
  316. case FlipStoreSubmenuIndexAppListSubGHz:
  317. flip_store_category_index = 8;
  318. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListSubGHz, "Sub-GHz", &app->submenu_app_list_subghz));
  319. break;
  320. case FlipStoreSubmenuIndexAppListTools:
  321. flip_store_category_index = 9;
  322. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListTools, "Tools", &app->submenu_app_list_tools));
  323. break;
  324. case FlipStoreSubmenuIndexAppListUSB:
  325. flip_store_category_index = 10;
  326. view_dispatcher_switch_to_view(app->view_dispatcher, flip_store_handle_app_list(app, FlipStoreViewAppListUSB, "USB", &app->submenu_app_list_usb));
  327. break;
  328. default:
  329. // Check if the index is within the app list range
  330. if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  331. {
  332. // Get the app index
  333. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  334. // Check if the app index is valid
  335. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  336. {
  337. // Get the app name
  338. char *app_name = flip_catalog[app_index].app_name;
  339. // Check if the app name is valid
  340. if (app_name != NULL && strlen(app_name) > 0)
  341. {
  342. app_selected_index = app_index;
  343. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  344. }
  345. else
  346. {
  347. FURI_LOG_E(TAG, "Invalid app name");
  348. }
  349. }
  350. else
  351. {
  352. FURI_LOG_E(TAG, "Invalid app index");
  353. }
  354. }
  355. else
  356. {
  357. FURI_LOG_E(TAG, "Unknown submenu index");
  358. }
  359. break;
  360. }
  361. }
  362. #endif // FLIP_STORE_CALLBACK_H