flip_store_callback.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  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. return FlipStoreViewAppList;
  361. }
  362. static uint32_t callback_to_wifi_settings(void *context)
  363. {
  364. UNUSED(context);
  365. return FlipStoreViewSettings;
  366. }
  367. static void dialog_firmware_callback(DialogExResult result, void *context)
  368. {
  369. FlipStoreApp *app = (FlipStoreApp *)context;
  370. if (!app)
  371. {
  372. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  373. return;
  374. }
  375. if (result == DialogExResultLeft) // No
  376. {
  377. // switch to the firmware list
  378. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  379. }
  380. else if (result == DialogExResultRight)
  381. {
  382. // download the firmware then return to the firmware list
  383. flip_store_switch_to_firmware_list(app);
  384. }
  385. }
  386. static bool alloc_about_view(FlipStoreApp *app)
  387. {
  388. if (!app)
  389. {
  390. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  391. return false;
  392. }
  393. if (!app->widget_about)
  394. {
  395. if (!easy_flipper_set_widget(
  396. &app->widget_about,
  397. FlipStoreViewAbout,
  398. "Welcome to the FlipStore!\n------\nDownload apps via WiFi and\nrun them on your Flipper!\n------\nwww.github.com/jblanked",
  399. callback_to_submenu,
  400. &app->view_dispatcher))
  401. {
  402. return false;
  403. }
  404. if (!app->widget_about)
  405. {
  406. return false;
  407. }
  408. }
  409. return true;
  410. }
  411. static void free_about_view(FlipStoreApp *app)
  412. {
  413. if (app && app->widget_about != NULL)
  414. {
  415. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAbout);
  416. widget_free(app->widget_about);
  417. app->widget_about = NULL;
  418. }
  419. }
  420. static bool alloc_text_input_view(void *context, char *title)
  421. {
  422. FlipStoreApp *app = (FlipStoreApp *)context;
  423. if (!app)
  424. {
  425. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  426. return false;
  427. }
  428. if (!title)
  429. {
  430. FURI_LOG_E(TAG, "Title is NULL");
  431. return false;
  432. }
  433. app->uart_text_input_buffer_size = 64;
  434. if (!app->uart_text_input_buffer)
  435. {
  436. if (!easy_flipper_set_buffer(&app->uart_text_input_buffer, app->uart_text_input_buffer_size))
  437. {
  438. return false;
  439. }
  440. }
  441. if (!app->uart_text_input_temp_buffer)
  442. {
  443. if (!easy_flipper_set_buffer(&app->uart_text_input_temp_buffer, app->uart_text_input_buffer_size))
  444. {
  445. return false;
  446. }
  447. }
  448. if (!app->uart_text_input)
  449. {
  450. if (!easy_flipper_set_uart_text_input(
  451. &app->uart_text_input,
  452. FlipStoreViewTextInput,
  453. title,
  454. app->uart_text_input_temp_buffer,
  455. app->uart_text_input_buffer_size,
  456. strcmp(title, "SSID") == 0 ? flip_store_text_updated_ssid : flip_store_text_updated_pass,
  457. callback_to_wifi_settings,
  458. &app->view_dispatcher,
  459. app))
  460. {
  461. return false;
  462. }
  463. if (!app->uart_text_input)
  464. {
  465. return false;
  466. }
  467. char ssid[64];
  468. char pass[64];
  469. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  470. {
  471. if (strcmp(title, "SSID") == 0)
  472. {
  473. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size);
  474. }
  475. else
  476. {
  477. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size);
  478. }
  479. }
  480. }
  481. return true;
  482. }
  483. static void free_text_input_view(FlipStoreApp *app)
  484. {
  485. if (!app)
  486. {
  487. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  488. return;
  489. }
  490. if (app->uart_text_input)
  491. {
  492. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewTextInput);
  493. uart_text_input_free(app->uart_text_input);
  494. app->uart_text_input = NULL;
  495. }
  496. if (app->uart_text_input_buffer)
  497. {
  498. free(app->uart_text_input_buffer);
  499. app->uart_text_input_buffer = NULL;
  500. }
  501. if (app->uart_text_input_temp_buffer)
  502. {
  503. free(app->uart_text_input_temp_buffer);
  504. app->uart_text_input_temp_buffer = NULL;
  505. }
  506. }
  507. static void settings_item_selected(void *context, uint32_t index);
  508. static bool alloc_variable_item_list(void *context)
  509. {
  510. FlipStoreApp *app = (FlipStoreApp *)context;
  511. if (!app)
  512. {
  513. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  514. return false;
  515. }
  516. if (!app->variable_item_list)
  517. {
  518. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipStoreViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  519. return false;
  520. if (!app->variable_item_list)
  521. return false;
  522. if (!app->variable_item_ssid)
  523. {
  524. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  525. variable_item_set_current_value_text(app->variable_item_ssid, "");
  526. }
  527. if (!app->variable_item_pass)
  528. {
  529. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  530. variable_item_set_current_value_text(app->variable_item_pass, "");
  531. }
  532. char ssid[64];
  533. char pass[64];
  534. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  535. {
  536. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  537. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  538. save_char("WiFi-SSID", ssid);
  539. save_char("WiFi-Password", pass);
  540. }
  541. }
  542. return true;
  543. }
  544. static void free_variable_item_list(FlipStoreApp *app)
  545. {
  546. if (!app)
  547. {
  548. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  549. return;
  550. }
  551. if (app->variable_item_list)
  552. {
  553. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewSettings);
  554. variable_item_list_free(app->variable_item_list);
  555. app->variable_item_list = NULL;
  556. }
  557. if (app->variable_item_ssid)
  558. {
  559. free(app->variable_item_ssid);
  560. app->variable_item_ssid = NULL;
  561. }
  562. if (app->variable_item_pass)
  563. {
  564. free(app->variable_item_pass);
  565. app->variable_item_pass = NULL;
  566. }
  567. }
  568. static bool alloc_dialog_firmware(void *context)
  569. {
  570. FlipStoreApp *app = (FlipStoreApp *)context;
  571. if (!app)
  572. {
  573. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  574. return false;
  575. }
  576. if (!app->dialog_firmware)
  577. {
  578. if (!easy_flipper_set_dialog_ex(
  579. &app->dialog_firmware,
  580. FlipStoreViewFirmwareDialog,
  581. "Download Firmware",
  582. 0,
  583. 0,
  584. "Are you sure you want to\ndownload this firmware?",
  585. 0,
  586. 10,
  587. "No",
  588. "Yes",
  589. NULL,
  590. dialog_firmware_callback,
  591. callback_to_firmware_list,
  592. &app->view_dispatcher,
  593. app))
  594. {
  595. return false;
  596. }
  597. if (!app->dialog_firmware)
  598. {
  599. return false;
  600. }
  601. dialog_ex_set_header(app->dialog_firmware, firmwares[selected_firmware_index].name, 0, 0, AlignLeft, AlignTop);
  602. }
  603. return true;
  604. }
  605. static void free_dialog_firmware(FlipStoreApp *app)
  606. {
  607. if (app && app->dialog_firmware)
  608. {
  609. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  610. dialog_ex_free(app->dialog_firmware);
  611. app->dialog_firmware = NULL;
  612. }
  613. }
  614. static bool alloc_app_info_view(FlipStoreApp *app)
  615. {
  616. if (!app)
  617. {
  618. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  619. return false;
  620. }
  621. if (!app->view_app_info)
  622. {
  623. if (!easy_flipper_set_view(
  624. &app->view_app_info,
  625. FlipStoreViewAppInfo,
  626. flip_store_view_draw_callback_app_list,
  627. flip_store_input_callback,
  628. callback_to_app_category_list,
  629. &app->view_dispatcher,
  630. app))
  631. {
  632. return false;
  633. }
  634. if (!app->view_app_info)
  635. {
  636. return false;
  637. }
  638. }
  639. return true;
  640. }
  641. static void free_app_info_view(FlipStoreApp *app)
  642. {
  643. if (!app)
  644. {
  645. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  646. return;
  647. }
  648. if (app->view_app_info)
  649. {
  650. view_dispatcher_remove_view(app->view_dispatcher, FlipStoreViewAppInfo);
  651. view_free(app->view_app_info);
  652. app->view_app_info = NULL;
  653. }
  654. }
  655. uint32_t callback_to_submenu_options(void *context)
  656. {
  657. FlipStoreApp *app = (FlipStoreApp *)context;
  658. if (!app)
  659. {
  660. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  661. return FlipStoreViewSubmenuOptions;
  662. }
  663. firmware_free();
  664. flip_catalog_free();
  665. free_category_submenu(app);
  666. return FlipStoreViewSubmenuOptions;
  667. }
  668. void free_all_views(FlipStoreApp *app, bool should_free_variable_item_list)
  669. {
  670. if (!app)
  671. {
  672. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  673. return;
  674. }
  675. free_about_view(app);
  676. flip_catalog_free();
  677. if (should_free_variable_item_list)
  678. {
  679. free_variable_item_list(app);
  680. }
  681. free_category_submenu(app);
  682. free_text_input_view(app);
  683. free_dialog_firmware(app);
  684. free_app_info_view(app);
  685. firmware_free();
  686. }
  687. uint32_t callback_exit_app(void *context)
  688. {
  689. UNUSED(context);
  690. return VIEW_NONE; // Return VIEW_NONE to exit the app
  691. }
  692. static bool set_appropriate_list(FlipperHTTP *fhttp, FlipStoreApp *app)
  693. {
  694. if (!fhttp || !app)
  695. {
  696. FURI_LOG_E(TAG, "FlipperHTTP oor app is NULL");
  697. return false;
  698. }
  699. if (!easy_flipper_set_submenu(&app->submenu_app_list_category, FlipStoreViewAppListCategory, categories[flip_store_category_index], callback_to_app_list, &app->view_dispatcher))
  700. {
  701. FURI_LOG_E(TAG, "Failed to set submenu");
  702. return false;
  703. }
  704. if (flip_store_process_app_list(fhttp) && app->submenu_app_list_category && flip_catalog)
  705. {
  706. submenu_reset(app->submenu_app_list_category);
  707. submenu_set_header(app->submenu_app_list_category, categories[flip_store_category_index]);
  708. // add each app name to submenu
  709. for (int i = 0; i < MAX_APP_COUNT; i++)
  710. {
  711. if (strlen(flip_catalog[i].app_name) > 0)
  712. {
  713. submenu_add_item(app->submenu_app_list_category, flip_catalog[i].app_name, FlipStoreSubmenuIndexStartAppList + i, callback_submenu_choices, app);
  714. }
  715. else
  716. {
  717. break;
  718. }
  719. }
  720. return true;
  721. }
  722. FURI_LOG_E(TAG, "Failed to process the app list");
  723. return false;
  724. }
  725. // we'll have to loop 8 at a time due to memory constraints
  726. static void fetch_appropiate_app_list(FlipStoreApp *app, int iteration)
  727. {
  728. FlipperHTTP *fhttp = flipper_http_alloc();
  729. if (!fhttp)
  730. {
  731. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  732. return;
  733. }
  734. bool fetch_app_list()
  735. {
  736. fhttp->state = IDLE;
  737. flip_catalog_free();
  738. snprintf(
  739. fhttp->file_path,
  740. sizeof(fhttp->file_path),
  741. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  742. fhttp->save_received_data = true;
  743. fhttp->is_bytes_request = false;
  744. char url[256];
  745. // load 8 at a time
  746. snprintf(url, sizeof(url), "https://catalog.flipperzero.one/api/v0/0/application?limit=8&is_latest_release_version=true&offset=%d&sort_by=updated_at&sort_order=-1&category_id=%s", iteration, category_ids[flip_store_category_index]);
  747. return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
  748. }
  749. bool parse_app_list()
  750. {
  751. return set_appropriate_list(fhttp, app);
  752. }
  753. flipper_http_loading_task(fhttp, fetch_app_list, parse_app_list, FlipStoreViewAppListCategory, FlipStoreViewSubmenuOptions, &app->view_dispatcher);
  754. flipper_http_free(fhttp);
  755. }
  756. void callback_submenu_choices(void *context, uint32_t index)
  757. {
  758. FlipStoreApp *app = (FlipStoreApp *)context;
  759. if (!app)
  760. {
  761. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  762. return;
  763. }
  764. switch (index)
  765. {
  766. case FlipStoreSubmenuIndexAbout:
  767. free_all_views(app, true);
  768. if (!alloc_about_view(app))
  769. {
  770. FURI_LOG_E(TAG, "Failed to set about view");
  771. return;
  772. }
  773. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  774. break;
  775. case FlipStoreSubmenuIndexSettings:
  776. free_all_views(app, true);
  777. if (!alloc_variable_item_list(app))
  778. {
  779. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  780. return;
  781. }
  782. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  783. break;
  784. case FlipStoreSubmenuIndexOptions:
  785. free_all_views(app, true);
  786. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  787. break;
  788. case FlipStoreSubmenuIndexAppList:
  789. flip_store_category_index = 0;
  790. flip_store_app_does_exist = false;
  791. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  792. break;
  793. case FlipStoreSubmenuIndexFirmwares:
  794. firmwares = firmware_alloc();
  795. if (firmwares == NULL)
  796. {
  797. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  798. return;
  799. }
  800. submenu_reset(app->submenu_firmwares);
  801. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  802. for (int i = 0; i < FIRMWARE_COUNT; i++)
  803. {
  804. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  805. }
  806. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  807. break;
  808. case FlipStoreSubmenuIndexAppListBluetooth:
  809. free_all_views(app, true);
  810. flip_store_category_index = 0;
  811. flip_store_app_does_exist = false;
  812. fetch_appropiate_app_list(app, 0);
  813. break;
  814. case FlipStoreSubmenuIndexAppListGames:
  815. free_all_views(app, true);
  816. flip_store_category_index = 1;
  817. flip_store_app_does_exist = false;
  818. fetch_appropiate_app_list(app, 0);
  819. break;
  820. case FlipStoreSubmenuIndexAppListGPIO:
  821. free_all_views(app, true);
  822. flip_store_category_index = 2;
  823. flip_store_app_does_exist = false;
  824. fetch_appropiate_app_list(app, 0);
  825. break;
  826. case FlipStoreSubmenuIndexAppListInfrared:
  827. free_all_views(app, true);
  828. flip_store_category_index = 3;
  829. flip_store_app_does_exist = false;
  830. fetch_appropiate_app_list(app, 0);
  831. break;
  832. case FlipStoreSubmenuIndexAppListiButton:
  833. free_all_views(app, true);
  834. flip_store_category_index = 4;
  835. flip_store_app_does_exist = false;
  836. fetch_appropiate_app_list(app, 0);
  837. break;
  838. case FlipStoreSubmenuIndexAppListMedia:
  839. free_all_views(app, true);
  840. flip_store_category_index = 5;
  841. flip_store_app_does_exist = false;
  842. fetch_appropiate_app_list(app, 0);
  843. break;
  844. case FlipStoreSubmenuIndexAppListNFC:
  845. free_all_views(app, true);
  846. flip_store_category_index = 6;
  847. flip_store_app_does_exist = false;
  848. fetch_appropiate_app_list(app, 0);
  849. break;
  850. case FlipStoreSubmenuIndexAppListRFID:
  851. free_all_views(app, true);
  852. flip_store_category_index = 7;
  853. flip_store_app_does_exist = false;
  854. fetch_appropiate_app_list(app, 0);
  855. break;
  856. case FlipStoreSubmenuIndexAppListSubGHz:
  857. free_all_views(app, true);
  858. flip_store_category_index = 8;
  859. flip_store_app_does_exist = false;
  860. fetch_appropiate_app_list(app, 0);
  861. break;
  862. case FlipStoreSubmenuIndexAppListTools:
  863. free_all_views(app, true);
  864. flip_store_category_index = 9;
  865. flip_store_app_does_exist = false;
  866. fetch_appropiate_app_list(app, 0);
  867. break;
  868. case FlipStoreSubmenuIndexAppListUSB:
  869. free_all_views(app, true);
  870. flip_store_category_index = 10;
  871. flip_store_app_does_exist = false;
  872. fetch_appropiate_app_list(app, 0);
  873. break;
  874. default:
  875. // Check if the index is within the firmwares list range
  876. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  877. {
  878. // Get the firmware index
  879. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  880. // Check if the firmware index is valid
  881. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  882. {
  883. // Get the firmware name
  884. selected_firmware_index = firmware_index;
  885. // Switch to the firmware download view
  886. free_dialog_firmware(app);
  887. if (!alloc_dialog_firmware(app))
  888. {
  889. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  890. return;
  891. }
  892. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  893. }
  894. else
  895. {
  896. FURI_LOG_E(TAG, "Invalid firmware index");
  897. easy_flipper_dialog("Error", "Issue parsing firmware.");
  898. }
  899. }
  900. // Check if the index is within the app list range
  901. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  902. {
  903. // Get the app index
  904. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  905. // Check if the app index is valid
  906. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  907. {
  908. // Get the app name
  909. char *app_name = flip_catalog[app_index].app_name;
  910. // Check if the app name is valid
  911. if (app_name != NULL && strlen(app_name) > 0)
  912. {
  913. app_selected_index = app_index;
  914. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  915. free_app_info_view(app);
  916. if (!alloc_app_info_view(app))
  917. {
  918. FURI_LOG_E(TAG, "Failed to allocate app info view");
  919. return;
  920. }
  921. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  922. }
  923. else
  924. {
  925. FURI_LOG_E(TAG, "Invalid app name");
  926. }
  927. }
  928. else
  929. {
  930. FURI_LOG_E(TAG, "Invalid app index");
  931. }
  932. }
  933. else
  934. {
  935. FURI_LOG_E(TAG, "Unknown submenu index");
  936. }
  937. break;
  938. }
  939. }
  940. static void settings_item_selected(void *context, uint32_t index)
  941. {
  942. FlipStoreApp *app = (FlipStoreApp *)context;
  943. if (!app)
  944. {
  945. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  946. return;
  947. }
  948. char ssid[64];
  949. char pass[64];
  950. switch (index)
  951. {
  952. case 0: // Input SSID
  953. // Text Input
  954. free_all_views(app, false);
  955. if (!alloc_text_input_view(app, "SSID"))
  956. {
  957. FURI_LOG_E(TAG, "Failed to allocate text input view");
  958. return;
  959. }
  960. // load SSID
  961. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  962. {
  963. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  964. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  965. }
  966. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  967. break;
  968. case 1: // Input Password
  969. free_all_views(app, false);
  970. if (!alloc_text_input_view(app, "Password"))
  971. {
  972. FURI_LOG_E(TAG, "Failed to allocate text input view");
  973. return;
  974. }
  975. // load password
  976. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  977. {
  978. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  979. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  980. }
  981. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  982. break;
  983. default:
  984. FURI_LOG_E(TAG, "Unknown configuration item index");
  985. break;
  986. }
  987. }
  988. static void flip_store_widget_set_text(char *message, Widget **widget)
  989. {
  990. if (widget == NULL)
  991. {
  992. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  993. DEV_CRASH();
  994. return;
  995. }
  996. if (message == NULL)
  997. {
  998. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  999. DEV_CRASH();
  1000. return;
  1001. }
  1002. widget_reset(*widget);
  1003. uint32_t message_length = strlen(message); // Length of the message
  1004. uint32_t i = 0; // Index tracker
  1005. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1006. char *formatted_message; // Buffer to hold the final formatted message
  1007. // Allocate buffer with double the message length plus one for safety
  1008. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1009. {
  1010. return;
  1011. }
  1012. while (i < message_length)
  1013. {
  1014. uint32_t max_line_length = 31; // Maximum characters per line
  1015. uint32_t remaining_length = message_length - i; // Remaining characters
  1016. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1017. // Check for newline character within the current segment
  1018. uint32_t newline_pos = i;
  1019. bool found_newline = false;
  1020. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1021. {
  1022. if (message[newline_pos] == '\n')
  1023. {
  1024. found_newline = true;
  1025. break;
  1026. }
  1027. }
  1028. if (found_newline)
  1029. {
  1030. // If newline found, set line_length up to the newline
  1031. line_length = newline_pos - i;
  1032. }
  1033. // Temporary buffer to hold the current line
  1034. char line[32];
  1035. strncpy(line, message + i, line_length);
  1036. line[line_length] = '\0';
  1037. // If newline was found, skip it for the next iteration
  1038. if (found_newline)
  1039. {
  1040. i += line_length + 1; // +1 to skip the '\n' character
  1041. }
  1042. else
  1043. {
  1044. // Check if the line ends in the middle of a word and adjust accordingly
  1045. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1046. {
  1047. // Find the last space within the current line to avoid breaking a word
  1048. char *last_space = strrchr(line, ' ');
  1049. if (last_space != NULL)
  1050. {
  1051. // Adjust the line_length to avoid cutting the word
  1052. line_length = last_space - line;
  1053. line[line_length] = '\0'; // Null-terminate at the space
  1054. }
  1055. }
  1056. // Move the index forward by the determined line_length
  1057. i += line_length;
  1058. // Skip any spaces at the beginning of the next line
  1059. while (i < message_length && message[i] == ' ')
  1060. {
  1061. i++;
  1062. }
  1063. }
  1064. // Manually copy the fixed line into the formatted_message buffer
  1065. for (uint32_t j = 0; j < line_length; j++)
  1066. {
  1067. formatted_message[formatted_index++] = line[j];
  1068. }
  1069. // Add a newline character for line spacing
  1070. formatted_message[formatted_index++] = '\n';
  1071. }
  1072. // Null-terminate the formatted_message
  1073. formatted_message[formatted_index] = '\0';
  1074. // Add the formatted message to the widget
  1075. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1076. }
  1077. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1078. {
  1079. if (!canvas)
  1080. {
  1081. FURI_LOG_E(TAG, "Canvas is NULL");
  1082. return;
  1083. }
  1084. if (!fhttp)
  1085. {
  1086. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1087. return;
  1088. }
  1089. if (fhttp->last_response != NULL)
  1090. {
  1091. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1092. {
  1093. canvas_clear(canvas);
  1094. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1095. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1096. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1097. }
  1098. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1099. {
  1100. canvas_clear(canvas);
  1101. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1102. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1103. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1104. }
  1105. else
  1106. {
  1107. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1108. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1109. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1110. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1111. }
  1112. }
  1113. else
  1114. {
  1115. canvas_clear(canvas);
  1116. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown 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. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1122. {
  1123. if (!canvas || !model)
  1124. {
  1125. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1126. return;
  1127. }
  1128. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1129. SerialState http_state = data_loader_model->fhttp->state;
  1130. DataState data_state = data_loader_model->data_state;
  1131. char *title = data_loader_model->title;
  1132. canvas_set_font(canvas, FontSecondary);
  1133. if (http_state == INACTIVE)
  1134. {
  1135. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1136. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1137. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1138. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1139. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1140. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1141. return;
  1142. }
  1143. if (data_state == DataStateError || data_state == DataStateParseError)
  1144. {
  1145. flip_store_request_error(canvas, data_loader_model->fhttp);
  1146. return;
  1147. }
  1148. canvas_draw_str(canvas, 0, 7, title);
  1149. canvas_draw_str(canvas, 0, 17, "Loading...");
  1150. if (data_state == DataStateInitial)
  1151. {
  1152. return;
  1153. }
  1154. if (http_state == SENDING)
  1155. {
  1156. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1157. return;
  1158. }
  1159. if (http_state == RECEIVING || data_state == DataStateRequested)
  1160. {
  1161. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1162. return;
  1163. }
  1164. if (http_state == IDLE && data_state == DataStateReceived)
  1165. {
  1166. canvas_draw_str(canvas, 0, 27, "Processing...");
  1167. return;
  1168. }
  1169. if (http_state == IDLE && data_state == DataStateParsed)
  1170. {
  1171. canvas_draw_str(canvas, 0, 27, "Processed...");
  1172. return;
  1173. }
  1174. }
  1175. static void flip_store_loader_process_callback(void *context)
  1176. {
  1177. if (context == NULL)
  1178. {
  1179. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1180. DEV_CRASH();
  1181. return;
  1182. }
  1183. FlipStoreApp *app = (FlipStoreApp *)context;
  1184. View *view = app->view_loader;
  1185. DataState current_data_state;
  1186. DataLoaderModel *loader_model = NULL;
  1187. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1188. if (!loader_model || !loader_model->fhttp)
  1189. {
  1190. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1191. DEV_CRASH();
  1192. return;
  1193. }
  1194. if (current_data_state == DataStateInitial)
  1195. {
  1196. with_view_model(
  1197. view,
  1198. DataLoaderModel * model,
  1199. {
  1200. model->data_state = DataStateRequested;
  1201. DataLoaderFetch fetch = model->fetcher;
  1202. if (fetch == NULL)
  1203. {
  1204. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1205. model->data_state = DataStateError;
  1206. return;
  1207. }
  1208. // Clear any previous responses
  1209. strncpy(model->fhttp->last_response, "", 1);
  1210. bool request_status = fetch(model);
  1211. if (!request_status)
  1212. {
  1213. model->data_state = DataStateError;
  1214. }
  1215. },
  1216. true);
  1217. }
  1218. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1219. {
  1220. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1221. {
  1222. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1223. {
  1224. FURI_LOG_DEV(TAG, "PONG received.");
  1225. }
  1226. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1227. {
  1228. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1229. }
  1230. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1231. {
  1232. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1233. }
  1234. else if (strlen(loader_model->fhttp->last_response) == 0)
  1235. {
  1236. // Still waiting on response
  1237. }
  1238. else
  1239. {
  1240. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1241. }
  1242. }
  1243. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1244. {
  1245. // continue waiting
  1246. }
  1247. else if (loader_model->fhttp->state == INACTIVE)
  1248. {
  1249. // inactive. try again
  1250. }
  1251. else if (loader_model->fhttp->state == ISSUE)
  1252. {
  1253. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1254. }
  1255. else
  1256. {
  1257. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1258. DEV_CRASH();
  1259. }
  1260. }
  1261. else if (current_data_state == DataStateReceived)
  1262. {
  1263. with_view_model(
  1264. view,
  1265. DataLoaderModel * model,
  1266. {
  1267. char *data_text;
  1268. if (model->parser == NULL)
  1269. {
  1270. data_text = NULL;
  1271. FURI_LOG_DEV(TAG, "Parser is NULL");
  1272. DEV_CRASH();
  1273. }
  1274. else
  1275. {
  1276. data_text = model->parser(model);
  1277. }
  1278. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1279. model->data_text = data_text;
  1280. if (data_text == NULL)
  1281. {
  1282. model->data_state = DataStateParseError;
  1283. }
  1284. else
  1285. {
  1286. model->data_state = DataStateParsed;
  1287. }
  1288. },
  1289. true);
  1290. }
  1291. else if (current_data_state == DataStateParsed)
  1292. {
  1293. with_view_model(
  1294. view,
  1295. DataLoaderModel * model,
  1296. {
  1297. if (++model->request_index < model->request_count)
  1298. {
  1299. model->data_state = DataStateInitial;
  1300. }
  1301. else
  1302. {
  1303. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1304. if (model->data_text != NULL)
  1305. {
  1306. free(model->data_text);
  1307. model->data_text = NULL;
  1308. }
  1309. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1310. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1311. }
  1312. },
  1313. true);
  1314. }
  1315. }
  1316. static void flip_store_loader_timer_callback(void *context)
  1317. {
  1318. if (context == NULL)
  1319. {
  1320. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1321. DEV_CRASH();
  1322. return;
  1323. }
  1324. FlipStoreApp *app = (FlipStoreApp *)context;
  1325. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1326. }
  1327. static void flip_store_loader_on_enter(void *context)
  1328. {
  1329. if (context == NULL)
  1330. {
  1331. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1332. DEV_CRASH();
  1333. return;
  1334. }
  1335. FlipStoreApp *app = (FlipStoreApp *)context;
  1336. View *view = app->view_loader;
  1337. with_view_model(
  1338. view,
  1339. DataLoaderModel * model,
  1340. {
  1341. view_set_previous_callback(view, model->back_callback);
  1342. if (model->timer == NULL)
  1343. {
  1344. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1345. }
  1346. furi_timer_start(model->timer, 250);
  1347. },
  1348. true);
  1349. }
  1350. static void flip_store_loader_on_exit(void *context)
  1351. {
  1352. if (context == NULL)
  1353. {
  1354. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1355. DEV_CRASH();
  1356. return;
  1357. }
  1358. FlipStoreApp *app = (FlipStoreApp *)context;
  1359. View *view = app->view_loader;
  1360. with_view_model(
  1361. view,
  1362. DataLoaderModel * model,
  1363. {
  1364. if (model->timer)
  1365. {
  1366. furi_timer_stop(model->timer);
  1367. }
  1368. },
  1369. false);
  1370. }
  1371. void flip_store_loader_init(View *view)
  1372. {
  1373. if (view == NULL)
  1374. {
  1375. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1376. DEV_CRASH();
  1377. return;
  1378. }
  1379. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1380. view_set_enter_callback(view, flip_store_loader_on_enter);
  1381. view_set_exit_callback(view, flip_store_loader_on_exit);
  1382. }
  1383. void flip_store_loader_free_model(View *view)
  1384. {
  1385. if (view == NULL)
  1386. {
  1387. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1388. DEV_CRASH();
  1389. return;
  1390. }
  1391. with_view_model(
  1392. view,
  1393. DataLoaderModel * model,
  1394. {
  1395. if (model->timer)
  1396. {
  1397. furi_timer_free(model->timer);
  1398. model->timer = NULL;
  1399. }
  1400. if (model->parser_context)
  1401. {
  1402. // do not free the context here, it is the app context
  1403. // free(model->parser_context);
  1404. // model->parser_context = NULL;
  1405. }
  1406. if (model->fhttp)
  1407. {
  1408. flipper_http_free(model->fhttp);
  1409. model->fhttp = NULL;
  1410. }
  1411. },
  1412. false);
  1413. }
  1414. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1415. {
  1416. if (context == NULL)
  1417. {
  1418. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1419. DEV_CRASH();
  1420. return false;
  1421. }
  1422. switch (index)
  1423. {
  1424. case FlipStoreCustomEventProcess:
  1425. flip_store_loader_process_callback(context);
  1426. return true;
  1427. default:
  1428. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1429. return false;
  1430. }
  1431. }
  1432. 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)
  1433. {
  1434. if (app == NULL)
  1435. {
  1436. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1437. DEV_CRASH();
  1438. return;
  1439. }
  1440. View *view = app->view_loader;
  1441. if (view == NULL)
  1442. {
  1443. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1444. DEV_CRASH();
  1445. return;
  1446. }
  1447. with_view_model(
  1448. view,
  1449. DataLoaderModel * model,
  1450. {
  1451. model->title = title;
  1452. model->fetcher = fetcher;
  1453. model->parser = parser;
  1454. model->request_index = 0;
  1455. model->request_count = request_count;
  1456. model->back_callback = back;
  1457. model->data_state = DataStateInitial;
  1458. model->data_text = NULL;
  1459. //
  1460. model->parser_context = app;
  1461. if (!model->fhttp)
  1462. {
  1463. model->fhttp = flipper_http_alloc();
  1464. }
  1465. },
  1466. true);
  1467. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1468. }