flip_store_callback.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. #include <callback/flip_store_callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. bool flip_store_app_does_exist = false;
  12. uint32_t selected_firmware_index = 0;
  13. static uint32_t callback_to_app_category_list(void *context);
  14. static bool flip_store_dl_app_fetch(DataLoaderModel *model)
  15. {
  16. UNUSED(model);
  17. return flip_store_install_app(categories[flip_store_category_index]);
  18. }
  19. static char *flip_store_dl_app_parse(DataLoaderModel *model)
  20. {
  21. UNUSED(model);
  22. if (fhttp.state != IDLE)
  23. {
  24. return NULL;
  25. }
  26. // free the resources
  27. flipper_http_deinit();
  28. return "App installed successfully.";
  29. }
  30. static void flip_store_dl_app_switch_to_view(FlipStoreApp *app)
  31. {
  32. flip_store_generic_switch_to_view(app, flip_catalog[app_selected_index].app_name, flip_store_dl_app_fetch, flip_store_dl_app_parse, 1, callback_to_app_category_list, FlipStoreViewLoader);
  33. }
  34. //
  35. static bool flip_store_fetch_app_list(DataLoaderModel *model)
  36. {
  37. UNUSED(model);
  38. if (!app_instance)
  39. {
  40. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  41. return false;
  42. }
  43. // initialize the http
  44. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  45. {
  46. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP.");
  47. return false;
  48. }
  49. fhttp.state = IDLE;
  50. flip_catalog_free();
  51. snprintf(
  52. fhttp.file_path,
  53. sizeof(fhttp.file_path),
  54. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  55. fhttp.save_received_data = true;
  56. fhttp.is_bytes_request = false;
  57. char url[128];
  58. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/flipper/apps/%s/max/", categories[flip_store_category_index]);
  59. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  60. }
  61. static bool set_appropriate_list(Submenu **submenu)
  62. {
  63. if (!submenu)
  64. {
  65. FURI_LOG_E(TAG, "Submenu is NULL");
  66. return false;
  67. }
  68. if (!easy_flipper_set_submenu(submenu, FlipStoreViewAppListCategory, categories[flip_store_category_index], callback_to_app_list, &app_instance->view_dispatcher))
  69. {
  70. return false;
  71. }
  72. if (flip_store_process_app_list() && submenu && flip_catalog)
  73. {
  74. submenu_reset(*submenu);
  75. // add each app name to submenu
  76. for (int i = 0; i < MAX_APP_COUNT; i++)
  77. {
  78. if (strlen(flip_catalog[i].app_name) > 0)
  79. {
  80. submenu_add_item(*submenu, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app_instance);
  81. }
  82. else
  83. {
  84. break;
  85. }
  86. }
  87. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipStoreViewAppListCategory);
  88. return true;
  89. }
  90. else
  91. {
  92. FURI_LOG_E(TAG, "Failed to process the app list");
  93. return false;
  94. }
  95. return false;
  96. }
  97. static char *flip_store_parse_app_list(DataLoaderModel *model)
  98. {
  99. UNUSED(model);
  100. if (!app_instance)
  101. {
  102. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  103. return "Failed to fetch app list.";
  104. }
  105. return set_appropriate_list(&app_instance->submenu_app_list_category) ? "App list fetched successfully." : "Failed to fetch app list.";
  106. }
  107. static void flip_store_switch_to_app_list(FlipStoreApp *app)
  108. {
  109. flip_store_generic_switch_to_view(app, categories[flip_store_category_index], flip_store_fetch_app_list, flip_store_parse_app_list, 1, callback_to_submenu, FlipStoreViewLoader);
  110. }
  111. //
  112. static bool flip_store_fetch_firmware(DataLoaderModel *model)
  113. {
  114. if (!app_instance)
  115. {
  116. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  117. return false;
  118. }
  119. // initialize the http
  120. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  121. {
  122. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP.");
  123. return false;
  124. }
  125. fhttp.state = IDLE;
  126. if (model->request_index == 0)
  127. {
  128. firmware_free();
  129. firmwares = firmware_alloc();
  130. if (!firmwares)
  131. {
  132. return false;
  133. }
  134. firmware_request_success = flip_store_get_firmware_file(
  135. firmwares[selected_firmware_index].links[0],
  136. firmwares[selected_firmware_index].name,
  137. strrchr(firmwares[selected_firmware_index].links[0], '/') + 1);
  138. return firmware_request_success;
  139. }
  140. else if (model->request_index == 1)
  141. {
  142. firmware_request_success_2 = flip_store_get_firmware_file(
  143. firmwares[selected_firmware_index].links[1],
  144. firmwares[selected_firmware_index].name,
  145. strrchr(firmwares[selected_firmware_index].links[1], '/') + 1);
  146. return firmware_request_success_2;
  147. }
  148. else if (model->request_index == 2)
  149. {
  150. firmware_request_success_3 = flip_store_get_firmware_file(
  151. firmwares[selected_firmware_index].links[2],
  152. firmwares[selected_firmware_index].name,
  153. strrchr(firmwares[selected_firmware_index].links[2], '/') + 1);
  154. return firmware_request_success_3;
  155. }
  156. return false;
  157. }
  158. static char *flip_store_parse_firmware(DataLoaderModel *model)
  159. {
  160. // free the resources
  161. flipper_http_deinit();
  162. if (model->request_index == 0)
  163. {
  164. if (firmware_request_success)
  165. {
  166. return "File 1 installed.";
  167. }
  168. }
  169. else if (model->request_index == 1)
  170. {
  171. if (firmware_request_success_2)
  172. {
  173. return "File 2 installed.";
  174. }
  175. }
  176. else if (model->request_index == 2)
  177. {
  178. if (firmware_request_success_3)
  179. {
  180. return "Firmware downloaded successfully";
  181. }
  182. }
  183. return NULL;
  184. }
  185. static void flip_store_switch_to_firmware_list(FlipStoreApp *app)
  186. {
  187. flip_store_generic_switch_to_view(app, firmwares[selected_firmware_index].name, flip_store_fetch_firmware, flip_store_parse_firmware, FIRMWARE_LINKS, callback_to_firmware_list, FlipStoreViewLoader);
  188. }
  189. // Function to draw the message on the canvas with word wrapping
  190. void draw_description(Canvas *canvas, const char *description, int x, int y)
  191. {
  192. if (description == NULL || strlen(description) == 0)
  193. {
  194. FURI_LOG_E(TAG, "User message is NULL.");
  195. return;
  196. }
  197. if (!canvas)
  198. {
  199. FURI_LOG_E(TAG, "Canvas is NULL.");
  200. return;
  201. }
  202. size_t msg_length = strlen(description);
  203. size_t start = 0;
  204. int line_num = 0;
  205. char line[MAX_LINE_LENGTH + 1]; // Buffer for the current line (+1 for null terminator)
  206. while (start < msg_length && line_num < 4)
  207. {
  208. size_t remaining = msg_length - start;
  209. size_t len = (remaining > MAX_LINE_LENGTH) ? MAX_LINE_LENGTH : remaining;
  210. if (remaining > MAX_LINE_LENGTH)
  211. {
  212. // Find the last space within the first 'len' characters
  213. size_t last_space = len;
  214. while (last_space > 0 && description[start + last_space - 1] != ' ')
  215. {
  216. last_space--;
  217. }
  218. if (last_space > 0)
  219. {
  220. len = last_space; // Adjust len to the position of the last space
  221. }
  222. }
  223. // Copy the substring to 'line' and null-terminate it
  224. memcpy(line, description + start, len);
  225. line[len] = '\0'; // Ensure the string is null-terminated
  226. // Draw the string on the canvas
  227. // Adjust the y-coordinate based on the line number
  228. canvas_draw_str_aligned(canvas, x, y + line_num * 10, AlignLeft, AlignTop, line);
  229. // Update the start position for the next line
  230. start += len;
  231. // Skip any spaces to avoid leading spaces on the next line
  232. while (start < msg_length && description[start] == ' ')
  233. {
  234. start++;
  235. }
  236. // Increment the line number
  237. line_num++;
  238. }
  239. }
  240. void flip_store_view_draw_callback_app_list(Canvas *canvas, void *model)
  241. {
  242. UNUSED(model);
  243. canvas_clear(canvas);
  244. canvas_set_font(canvas, FontPrimary);
  245. char title[64];
  246. snprintf(title, 64, "%s (v.%s)", flip_catalog[app_selected_index].app_name, flip_catalog[app_selected_index].app_version);
  247. canvas_draw_str(canvas, 0, 10, title);
  248. canvas_set_font(canvas, FontSecondary);
  249. draw_description(canvas, flip_catalog[app_selected_index].app_description, 0, 13);
  250. if (flip_store_app_does_exist)
  251. {
  252. canvas_draw_icon(canvas, 0, 53, &I_ButtonLeft_4x7);
  253. canvas_draw_str_aligned(canvas, 7, 54, AlignLeft, AlignTop, "Delete");
  254. canvas_draw_icon(canvas, 45, 53, &I_ButtonBACK_10x8);
  255. canvas_draw_str_aligned(canvas, 57, 54, AlignLeft, AlignTop, "Back");
  256. }
  257. else
  258. {
  259. canvas_draw_icon(canvas, 0, 53, &I_ButtonBACK_10x8);
  260. canvas_draw_str_aligned(canvas, 12, 54, AlignLeft, AlignTop, "Back");
  261. }
  262. canvas_draw_icon(canvas, 90, 53, &I_ButtonRight_4x7);
  263. canvas_draw_str_aligned(canvas, 97, 54, AlignLeft, AlignTop, "Install");
  264. }
  265. bool flip_store_input_callback(InputEvent *event, void *context)
  266. {
  267. FlipStoreApp *app = (FlipStoreApp *)context;
  268. if (!app)
  269. {
  270. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  271. return false;
  272. }
  273. if (event->type == InputTypeShort)
  274. {
  275. if (event->key == InputKeyLeft && flip_store_app_does_exist)
  276. {
  277. // Left button clicked, delete the app
  278. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppDelete);
  279. return true;
  280. }
  281. if (event->key == InputKeyRight)
  282. {
  283. // Right button clicked, download the app
  284. flip_store_dl_app_switch_to_view(app);
  285. return true;
  286. }
  287. }
  288. else if (event->type == InputTypePress)
  289. {
  290. if (event->key == InputKeyBack)
  291. {
  292. // Back button clicked, switch to the previous view.
  293. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppListCategory);
  294. return true;
  295. }
  296. }
  297. return false;
  298. }
  299. void flip_store_text_updated_ssid(void *context)
  300. {
  301. FlipStoreApp *app = (FlipStoreApp *)context;
  302. if (!app)
  303. {
  304. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  305. return;
  306. }
  307. // store the entered text
  308. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  309. // Ensure null-termination
  310. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  311. // save the setting
  312. save_char("WiFi-SSID", app->uart_text_input_buffer);
  313. // update the variable item text
  314. if (app->variable_item_ssid)
  315. {
  316. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer);
  317. // get value of password
  318. char pass[64];
  319. if (load_char("WiFi-Password", pass, sizeof(pass)))
  320. {
  321. if (strlen(pass) > 0 && strlen(app->uart_text_input_buffer) > 0)
  322. {
  323. // save the settings
  324. save_settings(app->uart_text_input_buffer, pass);
  325. // initialize the http
  326. if (flipper_http_init(flipper_http_rx_callback, app))
  327. {
  328. // save the wifi if the device is connected
  329. if (!flipper_http_save_wifi(app->uart_text_input_buffer, pass))
  330. {
  331. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  332. }
  333. // free the resources
  334. flipper_http_deinit();
  335. }
  336. else
  337. {
  338. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  339. }
  340. }
  341. }
  342. }
  343. // switch to the settings view
  344. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  345. }
  346. void flip_store_text_updated_pass(void *context)
  347. {
  348. FlipStoreApp *app = (FlipStoreApp *)context;
  349. if (!app)
  350. {
  351. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  352. return;
  353. }
  354. // store the entered text
  355. strncpy(app->uart_text_input_buffer, app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size);
  356. // Ensure null-termination
  357. app->uart_text_input_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  358. // save the setting
  359. save_char("WiFi-Password", app->uart_text_input_buffer);
  360. // update the variable item text
  361. if (app->variable_item_pass)
  362. {
  363. // variable_item_set_current_value_text(app->variable_item_pass, app->uart_text_input_buffer);
  364. }
  365. // get value of ssid
  366. char ssid[64];
  367. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  368. {
  369. if (strlen(ssid) > 0 && strlen(app->uart_text_input_buffer) > 0)
  370. {
  371. // save the settings
  372. save_settings(ssid, app->uart_text_input_buffer);
  373. // initialize the http
  374. if (flipper_http_init(flipper_http_rx_callback, app))
  375. {
  376. // save the wifi if the device is connected
  377. if (!flipper_http_save_wifi(ssid, app->uart_text_input_buffer))
  378. {
  379. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  380. }
  381. // free the resources
  382. flipper_http_deinit();
  383. }
  384. else
  385. {
  386. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  387. }
  388. }
  389. }
  390. // switch to the settings view
  391. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  392. }
  393. uint32_t callback_to_submenu(void *context)
  394. {
  395. UNUSED(context);
  396. firmware_free();
  397. return FlipStoreViewSubmenu;
  398. }
  399. uint32_t callback_to_submenu_options(void *context)
  400. {
  401. UNUSED(context);
  402. firmware_free();
  403. return FlipStoreViewSubmenuOptions;
  404. }
  405. uint32_t callback_to_firmware_list(void *context)
  406. {
  407. UNUSED(context);
  408. sent_firmware_request = false;
  409. sent_firmware_request_2 = false;
  410. sent_firmware_request_3 = false;
  411. //
  412. firmware_request_success = false;
  413. firmware_request_success_2 = false;
  414. firmware_request_success_3 = false;
  415. //
  416. firmware_download_success = false;
  417. firmware_download_success_2 = false;
  418. firmware_download_success_3 = false;
  419. return FlipStoreViewFirmwares;
  420. }
  421. static uint32_t callback_to_app_category_list(void *context)
  422. {
  423. UNUSED(context);
  424. return FlipStoreViewAppListCategory;
  425. }
  426. uint32_t callback_to_app_list(void *context)
  427. {
  428. UNUSED(context);
  429. flip_store_sent_request = false;
  430. flip_store_success = false;
  431. flip_store_saved_data = false;
  432. flip_store_saved_success = false;
  433. flip_store_app_does_exist = false;
  434. sent_firmware_request = false;
  435. return FlipStoreViewAppList;
  436. }
  437. static uint32_t callback_to_wifi_settings(void *context)
  438. {
  439. UNUSED(context);
  440. return FlipStoreViewSettings;
  441. }
  442. void dialog_delete_callback(DialogExResult result, void *context)
  443. {
  444. FlipStoreApp *app = (FlipStoreApp *)context;
  445. if (!app)
  446. {
  447. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  448. return;
  449. }
  450. if (result == DialogExResultLeft) // No
  451. {
  452. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  453. }
  454. else if (result == DialogExResultRight)
  455. {
  456. // delete the app then return to the app list
  457. if (!delete_app(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]))
  458. {
  459. // pop up a message
  460. easy_flipper_dialog("Error", "Issue deleting app.");
  461. furi_delay_ms(2000); // delay for 2 seconds
  462. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  463. }
  464. else
  465. {
  466. // pop up a message
  467. easy_flipper_dialog("Success", "App deleted successfully.");
  468. furi_delay_ms(2000); // delay for 2 seconds
  469. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  470. }
  471. }
  472. }
  473. void dialog_firmware_callback(DialogExResult result, void *context)
  474. {
  475. FlipStoreApp *app = (FlipStoreApp *)context;
  476. if (!app)
  477. {
  478. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  479. return;
  480. }
  481. if (result == DialogExResultLeft) // No
  482. {
  483. // switch to the firmware list
  484. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  485. }
  486. else if (result == DialogExResultRight)
  487. {
  488. // download the firmware then return to the firmware list
  489. flip_store_switch_to_firmware_list(app);
  490. }
  491. }
  492. static bool alloc_about_view(FlipStoreApp *app)
  493. {
  494. if (!app)
  495. {
  496. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  497. return false;
  498. }
  499. if (!app->widget_about)
  500. {
  501. if (!easy_flipper_set_widget(
  502. &app->widget_about,
  503. FlipStoreViewAbout,
  504. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  505. callback_to_submenu,
  506. &app->view_dispatcher))
  507. {
  508. return false;
  509. }
  510. if (!app->widget_about)
  511. {
  512. return false;
  513. }
  514. }
  515. return true;
  516. }
  517. static void free_about_view()
  518. {
  519. if (!app_instance)
  520. {
  521. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  522. return;
  523. }
  524. if (app_instance->widget_about != NULL)
  525. {
  526. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipStoreViewAbout);
  527. widget_free(app_instance->widget_about);
  528. app_instance->widget_about = NULL;
  529. }
  530. }
  531. static bool alloc_text_input_view(void *context, char *title)
  532. {
  533. FlipStoreApp *app = (FlipStoreApp *)context;
  534. if (!app)
  535. {
  536. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  537. return false;
  538. }
  539. if (!title)
  540. {
  541. FURI_LOG_E(TAG, "Title is NULL");
  542. return false;
  543. }
  544. app->uart_text_input_buffer_size = 64;
  545. if (!app->uart_text_input_buffer)
  546. {
  547. if (!easy_flipper_set_buffer(&app->uart_text_input_buffer, app->uart_text_input_buffer_size))
  548. {
  549. return false;
  550. }
  551. }
  552. if (!app->uart_text_input_temp_buffer)
  553. {
  554. if (!easy_flipper_set_buffer(&app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size))
  555. {
  556. return false;
  557. }
  558. }
  559. if (!app->uart_text_input)
  560. {
  561. if (!easy_flipper_set_uart_text_input(
  562. &app->uart_text_input,
  563. FlipStoreViewTextInput,
  564. title,
  565. app->uart_text_input_temp_buffer,
  566. app->uart_text_input_buffer_size,
  567. strcmp(title, "SSID") == 0 ? flip_store_text_updated_ssid : flip_store_text_updated_pass,
  568. callback_to_wifi_settings,
  569. &app->view_dispatcher,
  570. app))
  571. {
  572. return false;
  573. }
  574. if (!app->uart_text_input)
  575. {
  576. return false;
  577. }
  578. char ssid[64];
  579. char pass[64];
  580. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  581. {
  582. if (strcmp(title, "SSID") == 0)
  583. {
  584. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size);
  585. }
  586. else
  587. {
  588. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size);
  589. }
  590. }
  591. }
  592. return true;
  593. }
  594. static void free_text_input_view(void *context)
  595. {
  596. FlipStoreApp *app = (FlipStoreApp *)context;
  597. if (!app)
  598. {
  599. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  600. return;
  601. }
  602. if (app->uart_text_input)
  603. {
  604. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewTextInput);
  605. uart_text_input_free(app->uart_text_input);
  606. app->uart_text_input = NULL;
  607. }
  608. if (app->uart_text_input_buffer)
  609. {
  610. free(app->uart_text_input_buffer);
  611. app->uart_text_input_buffer = NULL;
  612. }
  613. if (app->uart_text_input_temp_buffer)
  614. {
  615. free(app->uart_text_input_temp_buffer);
  616. app->uart_text_input_temp_buffer = NULL;
  617. }
  618. }
  619. static bool alloc_variable_item_list(void *context)
  620. {
  621. FlipStoreApp *app = (FlipStoreApp *)context;
  622. if (!app)
  623. {
  624. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  625. return false;
  626. }
  627. if (!app->variable_item_list)
  628. {
  629. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipStoreViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  630. return false;
  631. if (!app->variable_item_list)
  632. return false;
  633. if (!app->variable_item_ssid)
  634. {
  635. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  636. variable_item_set_current_value_text(app->variable_item_ssid, "");
  637. }
  638. if (!app->variable_item_pass)
  639. {
  640. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  641. variable_item_set_current_value_text(app->variable_item_pass, "");
  642. }
  643. char ssid[64];
  644. char pass[64];
  645. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  646. {
  647. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  648. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  649. save_char("WiFi-SSID", ssid);
  650. save_char("WiFi-Password", pass);
  651. }
  652. }
  653. return true;
  654. }
  655. static void free_variable_item_list()
  656. {
  657. if (!app_instance)
  658. {
  659. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  660. return;
  661. }
  662. if (app_instance->variable_item_list)
  663. {
  664. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipStoreViewSettings);
  665. variable_item_list_free(app_instance->variable_item_list);
  666. app_instance->variable_item_list = NULL;
  667. }
  668. if (app_instance->variable_item_ssid)
  669. {
  670. free(app_instance->variable_item_ssid);
  671. app_instance->variable_item_ssid = NULL;
  672. }
  673. if (app_instance->variable_item_pass)
  674. {
  675. free(app_instance->variable_item_pass);
  676. app_instance->variable_item_pass = NULL;
  677. }
  678. }
  679. static bool alloc_dialog_firmware(void *context)
  680. {
  681. FlipStoreApp *app = (FlipStoreApp *)context;
  682. if (!app->dialog_firmware)
  683. {
  684. if (!easy_flipper_set_dialog_ex(
  685. &app->dialog_firmware,
  686. FlipStoreViewFirmwareDialog,
  687. "Download Firmware",
  688. 0,
  689. 0,
  690. "Are you sure you want to\ndownload this firmware?",
  691. 0,
  692. 10,
  693. "No",
  694. "Yes",
  695. NULL,
  696. dialog_firmware_callback,
  697. callback_to_firmware_list,
  698. &app->view_dispatcher,
  699. app))
  700. {
  701. return false;
  702. }
  703. if (!app->dialog_firmware)
  704. {
  705. return false;
  706. }
  707. dialog_ex_set_header(app->dialog_firmware, firmwares[selected_firmware_index].name, 0, 0, AlignLeft, AlignTop);
  708. }
  709. return true;
  710. }
  711. static void free_dialog_firmware(void *context)
  712. {
  713. FlipStoreApp *app = (FlipStoreApp *)context;
  714. if (!app)
  715. {
  716. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  717. return;
  718. }
  719. if (app->dialog_firmware)
  720. {
  721. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  722. dialog_ex_free(app->dialog_firmware);
  723. app->dialog_firmware = NULL;
  724. }
  725. }
  726. static bool alloc_app_info_view(void *context)
  727. {
  728. FlipStoreApp *app = (FlipStoreApp *)context;
  729. if (!app)
  730. {
  731. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  732. return false;
  733. }
  734. if (!app->view_app_info)
  735. {
  736. if (!easy_flipper_set_view(
  737. &app->view_app_info,
  738. FlipStoreViewAppInfo,
  739. flip_store_view_draw_callback_app_list,
  740. flip_store_input_callback,
  741. callback_to_app_category_list,
  742. &app->view_dispatcher,
  743. app))
  744. {
  745. return false;
  746. }
  747. if (!app->view_app_info)
  748. {
  749. return false;
  750. }
  751. }
  752. return true;
  753. }
  754. static void free_app_info_view(void *context)
  755. {
  756. FlipStoreApp *app = (FlipStoreApp *)context;
  757. if (!app)
  758. {
  759. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  760. return;
  761. }
  762. if (app->view_app_info)
  763. {
  764. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppInfo);
  765. view_free(app->view_app_info);
  766. app->view_app_info = NULL;
  767. }
  768. }
  769. static void free_all_views(bool should_free_variable_item_list)
  770. {
  771. free_about_view();
  772. flip_catalog_free();
  773. if (should_free_variable_item_list)
  774. {
  775. free_variable_item_list();
  776. }
  777. if (app_instance->submenu_app_list_category)
  778. {
  779. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipStoreViewAppListCategory);
  780. submenu_free(app_instance->submenu_app_list_category);
  781. app_instance->submenu_app_list_category = NULL;
  782. }
  783. free_text_input_view(app_instance);
  784. free_dialog_firmware(app_instance);
  785. free_app_info_view(app_instance);
  786. }
  787. uint32_t callback_exit_app(void *context)
  788. {
  789. UNUSED(context);
  790. free_all_views(true);
  791. return VIEW_NONE; // Return VIEW_NONE to exit the app
  792. }
  793. void callback_submenu_choices(void *context, uint32_t index)
  794. {
  795. FlipStoreApp *app = (FlipStoreApp *)context;
  796. if (!app)
  797. {
  798. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  799. return;
  800. }
  801. switch (index)
  802. {
  803. case FlipStoreSubmenuIndexAbout:
  804. free_all_views(true);
  805. if (!alloc_about_view(app))
  806. {
  807. FURI_LOG_E(TAG, "Failed to set about view");
  808. return;
  809. }
  810. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  811. break;
  812. case FlipStoreSubmenuIndexSettings:
  813. free_all_views(true);
  814. if (!alloc_variable_item_list(app))
  815. {
  816. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  817. return;
  818. }
  819. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  820. break;
  821. case FlipStoreSubmenuIndexOptions:
  822. free_all_views(true);
  823. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  824. break;
  825. case FlipStoreSubmenuIndexAppList:
  826. flip_store_category_index = 0;
  827. flip_store_app_does_exist = false;
  828. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  829. break;
  830. case FlipStoreSubmenuIndexFirmwares:
  831. if (!app->submenu_firmwares)
  832. {
  833. FURI_LOG_E(TAG, "Submenu firmwares is NULL");
  834. return;
  835. }
  836. firmwares = firmware_alloc();
  837. if (firmwares == NULL)
  838. {
  839. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  840. return;
  841. }
  842. submenu_reset(app->submenu_firmwares);
  843. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  844. for (int i = 0; i < FIRMWARE_COUNT; i++)
  845. {
  846. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  847. }
  848. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  849. break;
  850. case FlipStoreSubmenuIndexAppListBluetooth:
  851. free_all_views(true);
  852. flip_store_category_index = 0;
  853. flip_store_app_does_exist = false;
  854. flip_store_switch_to_app_list(app);
  855. break;
  856. case FlipStoreSubmenuIndexAppListGames:
  857. free_all_views(true);
  858. flip_store_category_index = 1;
  859. flip_store_app_does_exist = false;
  860. flip_store_switch_to_app_list(app);
  861. break;
  862. case FlipStoreSubmenuIndexAppListGPIO:
  863. free_all_views(true);
  864. flip_store_category_index = 2;
  865. flip_store_app_does_exist = false;
  866. flip_store_switch_to_app_list(app);
  867. break;
  868. case FlipStoreSubmenuIndexAppListInfrared:
  869. free_all_views(true);
  870. flip_store_category_index = 3;
  871. flip_store_app_does_exist = false;
  872. flip_store_switch_to_app_list(app);
  873. break;
  874. case FlipStoreSubmenuIndexAppListiButton:
  875. free_all_views(true);
  876. flip_store_category_index = 4;
  877. flip_store_app_does_exist = false;
  878. flip_store_switch_to_app_list(app);
  879. break;
  880. case FlipStoreSubmenuIndexAppListMedia:
  881. free_all_views(true);
  882. flip_store_category_index = 5;
  883. flip_store_app_does_exist = false;
  884. flip_store_switch_to_app_list(app);
  885. break;
  886. case FlipStoreSubmenuIndexAppListNFC:
  887. free_all_views(true);
  888. flip_store_category_index = 6;
  889. flip_store_app_does_exist = false;
  890. flip_store_switch_to_app_list(app);
  891. break;
  892. case FlipStoreSubmenuIndexAppListRFID:
  893. free_all_views(true);
  894. flip_store_category_index = 7;
  895. flip_store_app_does_exist = false;
  896. flip_store_switch_to_app_list(app);
  897. break;
  898. case FlipStoreSubmenuIndexAppListSubGHz:
  899. free_all_views(true);
  900. flip_store_category_index = 8;
  901. flip_store_app_does_exist = false;
  902. flip_store_switch_to_app_list(app);
  903. break;
  904. case FlipStoreSubmenuIndexAppListTools:
  905. free_all_views(true);
  906. flip_store_category_index = 9;
  907. flip_store_app_does_exist = false;
  908. flip_store_switch_to_app_list(app);
  909. break;
  910. case FlipStoreSubmenuIndexAppListUSB:
  911. free_all_views(true);
  912. flip_store_category_index = 10;
  913. flip_store_app_does_exist = false;
  914. flip_store_switch_to_app_list(app);
  915. break;
  916. default:
  917. // Check if the index is within the firmwares list range
  918. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  919. {
  920. // Get the firmware index
  921. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  922. // Check if the firmware index is valid
  923. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  924. {
  925. // Get the firmware name
  926. selected_firmware_index = firmware_index;
  927. // Switch to the firmware download view
  928. free_all_views(true);
  929. if (!alloc_dialog_firmware(app))
  930. {
  931. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  932. return;
  933. }
  934. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  935. }
  936. else
  937. {
  938. FURI_LOG_E(TAG, "Invalid firmware index");
  939. easy_flipper_dialog("Error", "Issue parsing firmware.");
  940. }
  941. }
  942. // Check if the index is within the app list range
  943. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  944. {
  945. // Get the app index
  946. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  947. // Check if the app index is valid
  948. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  949. {
  950. // Get the app name
  951. char *app_name = flip_catalog[app_index].app_name;
  952. // Check if the app name is valid
  953. if (app_name != NULL && strlen(app_name) > 0)
  954. {
  955. app_selected_index = app_index;
  956. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  957. free_app_info_view(app);
  958. if (!alloc_app_info_view(app))
  959. {
  960. FURI_LOG_E(TAG, "Failed to allocate app info view");
  961. return;
  962. }
  963. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  964. }
  965. else
  966. {
  967. FURI_LOG_E(TAG, "Invalid app name");
  968. }
  969. }
  970. else
  971. {
  972. FURI_LOG_E(TAG, "Invalid app index");
  973. }
  974. }
  975. else
  976. {
  977. FURI_LOG_E(TAG, "Unknown submenu index");
  978. }
  979. break;
  980. }
  981. }
  982. void settings_item_selected(void *context, uint32_t index)
  983. {
  984. FlipStoreApp *app = (FlipStoreApp *)context;
  985. if (!app)
  986. {
  987. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  988. return;
  989. }
  990. char ssid[64];
  991. char pass[64];
  992. switch (index)
  993. {
  994. case 0: // Input SSID
  995. // Text Input
  996. free_all_views(false);
  997. if (!alloc_text_input_view(app, "SSID"))
  998. {
  999. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1000. return;
  1001. }
  1002. // load SSID
  1003. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  1004. {
  1005. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  1006. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  1007. }
  1008. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1009. break;
  1010. case 1: // Input Password
  1011. free_all_views(false);
  1012. if (!alloc_text_input_view(app, "Password"))
  1013. {
  1014. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1015. return;
  1016. }
  1017. // load password
  1018. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  1019. {
  1020. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  1021. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  1022. }
  1023. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  1024. break;
  1025. default:
  1026. FURI_LOG_E(TAG, "Unknown configuration item index");
  1027. break;
  1028. }
  1029. }
  1030. static void flip_store_widget_set_text(char *message, Widget **widget)
  1031. {
  1032. if (widget == NULL)
  1033. {
  1034. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  1035. DEV_CRASH();
  1036. return;
  1037. }
  1038. if (message == NULL)
  1039. {
  1040. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  1041. DEV_CRASH();
  1042. return;
  1043. }
  1044. widget_reset(*widget);
  1045. uint32_t message_length = strlen(message); // Length of the message
  1046. uint32_t i = 0; // Index tracker
  1047. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1048. char *formatted_message; // Buffer to hold the final formatted message
  1049. // Allocate buffer with double the message length plus one for safety
  1050. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1051. {
  1052. return;
  1053. }
  1054. while (i < message_length)
  1055. {
  1056. uint32_t max_line_length = 31; // Maximum characters per line
  1057. uint32_t remaining_length = message_length - i; // Remaining characters
  1058. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1059. // Check for newline character within the current segment
  1060. uint32_t newline_pos = i;
  1061. bool found_newline = false;
  1062. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1063. {
  1064. if (message[newline_pos] == '\n')
  1065. {
  1066. found_newline = true;
  1067. break;
  1068. }
  1069. }
  1070. if (found_newline)
  1071. {
  1072. // If newline found, set line_length up to the newline
  1073. line_length = newline_pos - i;
  1074. }
  1075. // Temporary buffer to hold the current line
  1076. char line[32];
  1077. strncpy(line, message + i, line_length);
  1078. line[line_length] = '\0';
  1079. // If newline was found, skip it for the next iteration
  1080. if (found_newline)
  1081. {
  1082. i += line_length + 1; // +1 to skip the '\n' character
  1083. }
  1084. else
  1085. {
  1086. // Check if the line ends in the middle of a word and adjust accordingly
  1087. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1088. {
  1089. // Find the last space within the current line to avoid breaking a word
  1090. char *last_space = strrchr(line, ' ');
  1091. if (last_space != NULL)
  1092. {
  1093. // Adjust the line_length to avoid cutting the word
  1094. line_length = last_space - line;
  1095. line[line_length] = '\0'; // Null-terminate at the space
  1096. }
  1097. }
  1098. // Move the index forward by the determined line_length
  1099. i += line_length;
  1100. // Skip any spaces at the beginning of the next line
  1101. while (i < message_length && message[i] == ' ')
  1102. {
  1103. i++;
  1104. }
  1105. }
  1106. // Manually copy the fixed line into the formatted_message buffer
  1107. for (uint32_t j = 0; j < line_length; j++)
  1108. {
  1109. formatted_message[formatted_index++] = line[j];
  1110. }
  1111. // Add a newline character for line spacing
  1112. formatted_message[formatted_index++] = '\n';
  1113. }
  1114. // Null-terminate the formatted_message
  1115. formatted_message[formatted_index] = '\0';
  1116. // Add the formatted message to the widget
  1117. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1118. }
  1119. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1120. {
  1121. if (!canvas || !model)
  1122. {
  1123. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1124. return;
  1125. }
  1126. SerialState http_state = fhttp.state;
  1127. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1128. DataState data_state = data_loader_model->data_state;
  1129. char *title = data_loader_model->title;
  1130. canvas_set_font(canvas, FontSecondary);
  1131. if (http_state == INACTIVE)
  1132. {
  1133. canvas_draw_str(canvas, 0, 7, "WiFi Dev Board disconnected.");
  1134. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1135. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1136. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1137. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1138. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1139. return;
  1140. }
  1141. if (data_state == DataStateError || data_state == DataStateParseError)
  1142. {
  1143. flip_store_request_error(canvas);
  1144. return;
  1145. }
  1146. canvas_draw_str(canvas, 0, 7, title);
  1147. canvas_draw_str(canvas, 0, 17, "Loading...");
  1148. if (data_state == DataStateInitial)
  1149. {
  1150. return;
  1151. }
  1152. if (http_state == SENDING)
  1153. {
  1154. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1155. return;
  1156. }
  1157. if (http_state == RECEIVING || data_state == DataStateRequested)
  1158. {
  1159. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1160. return;
  1161. }
  1162. if (http_state == IDLE && data_state == DataStateReceived)
  1163. {
  1164. canvas_draw_str(canvas, 0, 27, "Processing...");
  1165. return;
  1166. }
  1167. if (http_state == IDLE && data_state == DataStateParsed)
  1168. {
  1169. canvas_draw_str(canvas, 0, 27, "Processed...");
  1170. return;
  1171. }
  1172. }
  1173. static void flip_store_loader_process_callback(void *context)
  1174. {
  1175. if (context == NULL)
  1176. {
  1177. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1178. DEV_CRASH();
  1179. return;
  1180. }
  1181. FlipStoreApp *app = (FlipStoreApp *)context;
  1182. View *view = app->view_loader;
  1183. DataState current_data_state;
  1184. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1185. if (current_data_state == DataStateInitial)
  1186. {
  1187. with_view_model(
  1188. view,
  1189. DataLoaderModel * model,
  1190. {
  1191. model->data_state = DataStateRequested;
  1192. DataLoaderFetch fetch = model->fetcher;
  1193. if (fetch == NULL)
  1194. {
  1195. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1196. model->data_state = DataStateError;
  1197. return;
  1198. }
  1199. // Clear any previous responses
  1200. if (fhttp.last_response != NULL)
  1201. {
  1202. free(fhttp.last_response);
  1203. fhttp.last_response = NULL;
  1204. }
  1205. // strncpy(fhttp.last_response, "", 1);
  1206. bool request_status = fetch(model);
  1207. if (!request_status)
  1208. {
  1209. model->data_state = DataStateError;
  1210. }
  1211. },
  1212. true);
  1213. }
  1214. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1215. {
  1216. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1217. {
  1218. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1219. {
  1220. FURI_LOG_DEV(TAG, "PONG received.");
  1221. }
  1222. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1223. {
  1224. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1225. }
  1226. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1227. {
  1228. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1229. }
  1230. else if (strlen(fhttp.last_response) == 0)
  1231. {
  1232. // Still waiting on response
  1233. }
  1234. else
  1235. {
  1236. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1237. }
  1238. }
  1239. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1240. {
  1241. // continue waiting
  1242. }
  1243. else if (fhttp.state == INACTIVE)
  1244. {
  1245. // inactive. try again
  1246. }
  1247. else if (fhttp.state == ISSUE)
  1248. {
  1249. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1250. }
  1251. else
  1252. {
  1253. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1254. DEV_CRASH();
  1255. }
  1256. }
  1257. else if (current_data_state == DataStateReceived)
  1258. {
  1259. with_view_model(
  1260. view,
  1261. DataLoaderModel * model,
  1262. {
  1263. char *data_text;
  1264. if (model->parser == NULL)
  1265. {
  1266. data_text = NULL;
  1267. FURI_LOG_DEV(TAG, "Parser is NULL");
  1268. DEV_CRASH();
  1269. }
  1270. else
  1271. {
  1272. data_text = model->parser(model);
  1273. }
  1274. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1275. model->data_text = data_text;
  1276. if (data_text == NULL)
  1277. {
  1278. model->data_state = DataStateParseError;
  1279. }
  1280. else
  1281. {
  1282. model->data_state = DataStateParsed;
  1283. }
  1284. },
  1285. true);
  1286. }
  1287. else if (current_data_state == DataStateParsed)
  1288. {
  1289. with_view_model(
  1290. view,
  1291. DataLoaderModel * model,
  1292. {
  1293. if (++model->request_index < model->request_count)
  1294. {
  1295. model->data_state = DataStateInitial;
  1296. }
  1297. else
  1298. {
  1299. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "Empty result", &app->widget_result);
  1300. if (model->data_text != NULL)
  1301. {
  1302. free(model->data_text);
  1303. model->data_text = NULL;
  1304. }
  1305. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1306. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1307. }
  1308. },
  1309. true);
  1310. }
  1311. }
  1312. static void flip_store_loader_timer_callback(void *context)
  1313. {
  1314. if (context == NULL)
  1315. {
  1316. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1317. DEV_CRASH();
  1318. return;
  1319. }
  1320. FlipStoreApp *app = (FlipStoreApp *)context;
  1321. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1322. }
  1323. static void flip_store_loader_on_enter(void *context)
  1324. {
  1325. if (context == NULL)
  1326. {
  1327. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1328. DEV_CRASH();
  1329. return;
  1330. }
  1331. FlipStoreApp *app = (FlipStoreApp *)context;
  1332. View *view = app->view_loader;
  1333. with_view_model(
  1334. view,
  1335. DataLoaderModel * model,
  1336. {
  1337. view_set_previous_callback(view, model->back_callback);
  1338. if (model->timer == NULL)
  1339. {
  1340. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1341. }
  1342. furi_timer_start(model->timer, 250);
  1343. },
  1344. true);
  1345. }
  1346. static void flip_store_loader_on_exit(void *context)
  1347. {
  1348. if (context == NULL)
  1349. {
  1350. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1351. DEV_CRASH();
  1352. return;
  1353. }
  1354. FlipStoreApp *app = (FlipStoreApp *)context;
  1355. View *view = app->view_loader;
  1356. with_view_model(
  1357. view,
  1358. DataLoaderModel * model,
  1359. {
  1360. if (model->timer)
  1361. {
  1362. furi_timer_stop(model->timer);
  1363. }
  1364. },
  1365. false);
  1366. }
  1367. void flip_store_loader_init(View *view)
  1368. {
  1369. if (view == NULL)
  1370. {
  1371. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1372. DEV_CRASH();
  1373. return;
  1374. }
  1375. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1376. view_set_enter_callback(view, flip_store_loader_on_enter);
  1377. view_set_exit_callback(view, flip_store_loader_on_exit);
  1378. }
  1379. void flip_store_loader_free_model(View *view)
  1380. {
  1381. if (view == NULL)
  1382. {
  1383. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1384. DEV_CRASH();
  1385. return;
  1386. }
  1387. with_view_model(
  1388. view,
  1389. DataLoaderModel * model,
  1390. {
  1391. if (model->timer)
  1392. {
  1393. furi_timer_free(model->timer);
  1394. model->timer = NULL;
  1395. }
  1396. if (model->parser_context)
  1397. {
  1398. free(model->parser_context);
  1399. model->parser_context = NULL;
  1400. }
  1401. },
  1402. false);
  1403. }
  1404. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1405. {
  1406. if (context == NULL)
  1407. {
  1408. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1409. DEV_CRASH();
  1410. return false;
  1411. }
  1412. switch (index)
  1413. {
  1414. case FlipStoreCustomEventProcess:
  1415. flip_store_loader_process_callback(context);
  1416. return true;
  1417. default:
  1418. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1419. return false;
  1420. }
  1421. }
  1422. void flip_store_generic_switch_to_view(FlipStoreApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1423. {
  1424. if (app == NULL)
  1425. {
  1426. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1427. DEV_CRASH();
  1428. return;
  1429. }
  1430. View *view = app->view_loader;
  1431. if (view == NULL)
  1432. {
  1433. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1434. DEV_CRASH();
  1435. return;
  1436. }
  1437. with_view_model(
  1438. view,
  1439. DataLoaderModel * model,
  1440. {
  1441. model->title = title;
  1442. model->fetcher = fetcher;
  1443. model->parser = parser;
  1444. model->request_index = 0;
  1445. model->request_count = request_count;
  1446. model->back_callback = back;
  1447. model->data_state = DataStateInitial;
  1448. model->data_text = NULL;
  1449. },
  1450. true);
  1451. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1452. }