flip_store_callback.c 49 KB

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