flip_store_callback.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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. // we'll have to loop 8 at a time due to memory constraints
  736. static void fetch_appropiate_app_list(FlipStoreApp *app, int iteration)
  737. {
  738. FlipperHTTP *fhttp = flipper_http_alloc();
  739. if (!fhttp)
  740. {
  741. FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
  742. return;
  743. }
  744. bool fetch_app_list()
  745. {
  746. fhttp->state = IDLE;
  747. flip_catalog_free();
  748. snprintf(
  749. fhttp->file_path,
  750. sizeof(fhttp->file_path),
  751. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_store/%s.json", categories[flip_store_category_index]);
  752. fhttp->save_received_data = true;
  753. fhttp->is_bytes_request = false;
  754. char url[256];
  755. // load 8 at a time
  756. 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]);
  757. return flipper_http_get_request_with_headers(fhttp, url, "{\"Content-Type\":\"application/json\"}");
  758. }
  759. bool parse_app_list()
  760. {
  761. return set_appropriate_list(fhttp, app);
  762. }
  763. flipper_http_loading_task(fhttp, fetch_app_list, parse_app_list, FlipStoreViewAppListCategory, FlipStoreViewSubmenuOptions, &app->view_dispatcher);
  764. flipper_http_free(fhttp);
  765. }
  766. void callback_submenu_choices(void *context, uint32_t index)
  767. {
  768. FlipStoreApp *app = (FlipStoreApp *)context;
  769. if (!app)
  770. {
  771. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  772. return;
  773. }
  774. switch (index)
  775. {
  776. case FlipStoreSubmenuIndexAbout:
  777. free_all_views(app, true);
  778. if (!alloc_about_view(app))
  779. {
  780. FURI_LOG_E(TAG, "Failed to set about view");
  781. return;
  782. }
  783. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAbout);
  784. break;
  785. case FlipStoreSubmenuIndexSettings:
  786. free_all_views(app, true);
  787. if (!alloc_variable_item_list(app))
  788. {
  789. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  790. return;
  791. }
  792. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSettings);
  793. break;
  794. case FlipStoreSubmenuIndexOptions:
  795. free_all_views(app, true);
  796. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewSubmenuOptions);
  797. break;
  798. case FlipStoreSubmenuIndexAppList:
  799. flip_store_category_index = 0;
  800. flip_store_app_does_exist = false;
  801. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppList);
  802. break;
  803. case FlipStoreSubmenuIndexFirmwares:
  804. firmwares = firmware_alloc();
  805. if (firmwares == NULL)
  806. {
  807. FURI_LOG_E(TAG, "Failed to allocate memory for firmwares");
  808. return;
  809. }
  810. submenu_reset(app->submenu_firmwares);
  811. submenu_set_header(app->submenu_firmwares, "ESP32 Firmwares");
  812. for (int i = 0; i < FIRMWARE_COUNT; i++)
  813. {
  814. submenu_add_item(app->submenu_firmwares, firmwares[i].name, FlipStoreSubmenuIndexStartFirmwares + i, callback_submenu_choices, app);
  815. }
  816. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwares);
  817. break;
  818. case FlipStoreSubmenuIndexAppListBluetooth:
  819. free_all_views(app, true);
  820. flip_store_category_index = 0;
  821. flip_store_app_does_exist = false;
  822. fetch_appropiate_app_list(app, 0);
  823. break;
  824. case FlipStoreSubmenuIndexAppListGames:
  825. free_all_views(app, true);
  826. flip_store_category_index = 1;
  827. flip_store_app_does_exist = false;
  828. fetch_appropiate_app_list(app, 0);
  829. break;
  830. case FlipStoreSubmenuIndexAppListGPIO:
  831. free_all_views(app, true);
  832. flip_store_category_index = 2;
  833. flip_store_app_does_exist = false;
  834. fetch_appropiate_app_list(app, 0);
  835. break;
  836. case FlipStoreSubmenuIndexAppListInfrared:
  837. free_all_views(app, true);
  838. flip_store_category_index = 3;
  839. flip_store_app_does_exist = false;
  840. fetch_appropiate_app_list(app, 0);
  841. break;
  842. case FlipStoreSubmenuIndexAppListiButton:
  843. free_all_views(app, true);
  844. flip_store_category_index = 4;
  845. flip_store_app_does_exist = false;
  846. fetch_appropiate_app_list(app, 0);
  847. break;
  848. case FlipStoreSubmenuIndexAppListMedia:
  849. free_all_views(app, true);
  850. flip_store_category_index = 5;
  851. flip_store_app_does_exist = false;
  852. fetch_appropiate_app_list(app, 0);
  853. break;
  854. case FlipStoreSubmenuIndexAppListNFC:
  855. free_all_views(app, true);
  856. flip_store_category_index = 6;
  857. flip_store_app_does_exist = false;
  858. fetch_appropiate_app_list(app, 0);
  859. break;
  860. case FlipStoreSubmenuIndexAppListRFID:
  861. free_all_views(app, true);
  862. flip_store_category_index = 7;
  863. flip_store_app_does_exist = false;
  864. fetch_appropiate_app_list(app, 0);
  865. break;
  866. case FlipStoreSubmenuIndexAppListSubGHz:
  867. free_all_views(app, true);
  868. flip_store_category_index = 8;
  869. flip_store_app_does_exist = false;
  870. fetch_appropiate_app_list(app, 0);
  871. break;
  872. case FlipStoreSubmenuIndexAppListTools:
  873. free_all_views(app, true);
  874. flip_store_category_index = 9;
  875. flip_store_app_does_exist = false;
  876. fetch_appropiate_app_list(app, 0);
  877. break;
  878. case FlipStoreSubmenuIndexAppListUSB:
  879. free_all_views(app, true);
  880. flip_store_category_index = 10;
  881. flip_store_app_does_exist = false;
  882. fetch_appropiate_app_list(app, 0);
  883. break;
  884. default:
  885. // Check if the index is within the firmwares list range
  886. if (index >= FlipStoreSubmenuIndexStartFirmwares && index < FlipStoreSubmenuIndexStartFirmwares + 3)
  887. {
  888. // Get the firmware index
  889. uint32_t firmware_index = index - FlipStoreSubmenuIndexStartFirmwares;
  890. // Check if the firmware index is valid
  891. if ((int)firmware_index >= 0 && firmware_index < FIRMWARE_COUNT)
  892. {
  893. // Get the firmware name
  894. selected_firmware_index = firmware_index;
  895. // Switch to the firmware download view
  896. free_dialog_firmware(app);
  897. if (!alloc_dialog_firmware(app))
  898. {
  899. FURI_LOG_E(TAG, "Failed to allocate dialog firmware");
  900. return;
  901. }
  902. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewFirmwareDialog);
  903. }
  904. else
  905. {
  906. FURI_LOG_E(TAG, "Invalid firmware index");
  907. easy_flipper_dialog("Error", "Issue parsing firmware.");
  908. }
  909. }
  910. // Check if the index is within the app list range
  911. else if (index >= FlipStoreSubmenuIndexStartAppList && index < FlipStoreSubmenuIndexStartAppList + MAX_APP_COUNT)
  912. {
  913. // Get the app index
  914. uint32_t app_index = index - FlipStoreSubmenuIndexStartAppList;
  915. // Check if the app index is valid
  916. if ((int)app_index >= 0 && app_index < MAX_APP_COUNT)
  917. {
  918. // Get the app name
  919. char *app_name = flip_catalog[app_index].app_name;
  920. // Check if the app name is valid
  921. if (app_name != NULL && strlen(app_name) > 0)
  922. {
  923. app_selected_index = app_index;
  924. flip_store_app_does_exist = app_exists(flip_catalog[app_selected_index].app_id, categories[flip_store_category_index]);
  925. free_app_info_view(app);
  926. if (!alloc_app_info_view(app))
  927. {
  928. FURI_LOG_E(TAG, "Failed to allocate app info view");
  929. return;
  930. }
  931. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewAppInfo);
  932. }
  933. else
  934. {
  935. FURI_LOG_E(TAG, "Invalid app name");
  936. }
  937. }
  938. else
  939. {
  940. FURI_LOG_E(TAG, "Invalid app index");
  941. }
  942. }
  943. else
  944. {
  945. FURI_LOG_E(TAG, "Unknown submenu index");
  946. }
  947. break;
  948. }
  949. }
  950. static void settings_item_selected(void *context, uint32_t index)
  951. {
  952. FlipStoreApp *app = (FlipStoreApp *)context;
  953. if (!app)
  954. {
  955. FURI_LOG_E(TAG, "FlipStoreApp is NULL");
  956. return;
  957. }
  958. char ssid[64];
  959. char pass[64];
  960. switch (index)
  961. {
  962. case 0: // Input SSID
  963. // Text Input
  964. free_all_views(app, false);
  965. if (!alloc_text_input_view(app, "SSID"))
  966. {
  967. FURI_LOG_E(TAG, "Failed to allocate text input view");
  968. return;
  969. }
  970. // load SSID
  971. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  972. {
  973. strncpy(app->uart_text_input_temp_buffer, ssid, app->uart_text_input_buffer_size - 1);
  974. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  975. }
  976. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  977. break;
  978. case 1: // Input Password
  979. free_all_views(app, false);
  980. if (!alloc_text_input_view(app, "Password"))
  981. {
  982. FURI_LOG_E(TAG, "Failed to allocate text input view");
  983. return;
  984. }
  985. // load password
  986. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  987. {
  988. strncpy(app->uart_text_input_temp_buffer, pass, app->uart_text_input_buffer_size - 1);
  989. app->uart_text_input_temp_buffer[app->uart_text_input_buffer_size - 1] = '\0';
  990. }
  991. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewTextInput);
  992. break;
  993. default:
  994. FURI_LOG_E(TAG, "Unknown configuration item index");
  995. break;
  996. }
  997. }
  998. static void flip_store_widget_set_text(char *message, Widget **widget)
  999. {
  1000. if (widget == NULL)
  1001. {
  1002. FURI_LOG_E(TAG, "flip_store_set_widget_text - widget is NULL");
  1003. DEV_CRASH();
  1004. return;
  1005. }
  1006. if (message == NULL)
  1007. {
  1008. FURI_LOG_E(TAG, "flip_store_set_widget_text - message is NULL");
  1009. DEV_CRASH();
  1010. return;
  1011. }
  1012. widget_reset(*widget);
  1013. uint32_t message_length = strlen(message); // Length of the message
  1014. uint32_t i = 0; // Index tracker
  1015. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1016. char *formatted_message; // Buffer to hold the final formatted message
  1017. // Allocate buffer with double the message length plus one for safety
  1018. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1019. {
  1020. return;
  1021. }
  1022. while (i < message_length)
  1023. {
  1024. uint32_t max_line_length = 31; // Maximum characters per line
  1025. uint32_t remaining_length = message_length - i; // Remaining characters
  1026. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1027. // Check for newline character within the current segment
  1028. uint32_t newline_pos = i;
  1029. bool found_newline = false;
  1030. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1031. {
  1032. if (message[newline_pos] == '\n')
  1033. {
  1034. found_newline = true;
  1035. break;
  1036. }
  1037. }
  1038. if (found_newline)
  1039. {
  1040. // If newline found, set line_length up to the newline
  1041. line_length = newline_pos - i;
  1042. }
  1043. // Temporary buffer to hold the current line
  1044. char line[32];
  1045. strncpy(line, message + i, line_length);
  1046. line[line_length] = '\0';
  1047. // If newline was found, skip it for the next iteration
  1048. if (found_newline)
  1049. {
  1050. i += line_length + 1; // +1 to skip the '\n' character
  1051. }
  1052. else
  1053. {
  1054. // Check if the line ends in the middle of a word and adjust accordingly
  1055. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1056. {
  1057. // Find the last space within the current line to avoid breaking a word
  1058. char *last_space = strrchr(line, ' ');
  1059. if (last_space != NULL)
  1060. {
  1061. // Adjust the line_length to avoid cutting the word
  1062. line_length = last_space - line;
  1063. line[line_length] = '\0'; // Null-terminate at the space
  1064. }
  1065. }
  1066. // Move the index forward by the determined line_length
  1067. i += line_length;
  1068. // Skip any spaces at the beginning of the next line
  1069. while (i < message_length && message[i] == ' ')
  1070. {
  1071. i++;
  1072. }
  1073. }
  1074. // Manually copy the fixed line into the formatted_message buffer
  1075. for (uint32_t j = 0; j < line_length; j++)
  1076. {
  1077. formatted_message[formatted_index++] = line[j];
  1078. }
  1079. // Add a newline character for line spacing
  1080. formatted_message[formatted_index++] = '\n';
  1081. }
  1082. // Null-terminate the formatted_message
  1083. formatted_message[formatted_index] = '\0';
  1084. // Add the formatted message to the widget
  1085. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1086. }
  1087. static void flip_store_request_error(Canvas *canvas, FlipperHTTP *fhttp)
  1088. {
  1089. if (!canvas)
  1090. {
  1091. FURI_LOG_E(TAG, "Canvas is NULL");
  1092. return;
  1093. }
  1094. if (!fhttp)
  1095. {
  1096. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  1097. return;
  1098. }
  1099. if (fhttp->last_response != NULL)
  1100. {
  1101. if (strstr(fhttp->last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  1102. {
  1103. canvas_clear(canvas);
  1104. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1105. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1106. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1107. }
  1108. else if (strstr(fhttp->last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  1109. {
  1110. canvas_clear(canvas);
  1111. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  1112. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1113. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1114. }
  1115. else
  1116. {
  1117. FURI_LOG_E(TAG, "Received an error: %s", fhttp->last_response);
  1118. canvas_draw_str(canvas, 0, 42, "Unusual error...");
  1119. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1120. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1121. }
  1122. }
  1123. else
  1124. {
  1125. canvas_clear(canvas);
  1126. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  1127. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  1128. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  1129. }
  1130. }
  1131. void flip_store_loader_draw_callback(Canvas *canvas, void *model)
  1132. {
  1133. if (!canvas || !model)
  1134. {
  1135. FURI_LOG_E(TAG, "flip_store_loader_draw_callback - canvas or model is NULL");
  1136. return;
  1137. }
  1138. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1139. SerialState http_state = data_loader_model->fhttp->state;
  1140. DataState data_state = data_loader_model->data_state;
  1141. char *title = data_loader_model->title;
  1142. canvas_set_font(canvas, FontSecondary);
  1143. if (http_state == INACTIVE)
  1144. {
  1145. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1146. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1147. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1148. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1149. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1150. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1151. return;
  1152. }
  1153. if (data_state == DataStateError || data_state == DataStateParseError)
  1154. {
  1155. flip_store_request_error(canvas, data_loader_model->fhttp);
  1156. return;
  1157. }
  1158. canvas_draw_str(canvas, 0, 7, title);
  1159. canvas_draw_str(canvas, 0, 17, "Loading...");
  1160. if (data_state == DataStateInitial)
  1161. {
  1162. return;
  1163. }
  1164. if (http_state == SENDING)
  1165. {
  1166. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1167. return;
  1168. }
  1169. if (http_state == RECEIVING || data_state == DataStateRequested)
  1170. {
  1171. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1172. return;
  1173. }
  1174. if (http_state == IDLE && data_state == DataStateReceived)
  1175. {
  1176. canvas_draw_str(canvas, 0, 27, "Processing...");
  1177. return;
  1178. }
  1179. if (http_state == IDLE && data_state == DataStateParsed)
  1180. {
  1181. canvas_draw_str(canvas, 0, 27, "Processed...");
  1182. return;
  1183. }
  1184. }
  1185. static void flip_store_loader_process_callback(void *context)
  1186. {
  1187. if (context == NULL)
  1188. {
  1189. FURI_LOG_E(TAG, "flip_store_loader_process_callback - context is NULL");
  1190. DEV_CRASH();
  1191. return;
  1192. }
  1193. FlipStoreApp *app = (FlipStoreApp *)context;
  1194. View *view = app->view_loader;
  1195. DataState current_data_state;
  1196. DataLoaderModel *loader_model = NULL;
  1197. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; loader_model = model; }, false);
  1198. if (!loader_model || !loader_model->fhttp)
  1199. {
  1200. FURI_LOG_E(TAG, "Model or fhttp is NULL");
  1201. DEV_CRASH();
  1202. return;
  1203. }
  1204. if (current_data_state == DataStateInitial)
  1205. {
  1206. with_view_model(
  1207. view,
  1208. DataLoaderModel * model,
  1209. {
  1210. model->data_state = DataStateRequested;
  1211. DataLoaderFetch fetch = model->fetcher;
  1212. if (fetch == NULL)
  1213. {
  1214. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1215. model->data_state = DataStateError;
  1216. return;
  1217. }
  1218. // Clear any previous responses
  1219. strncpy(model->fhttp->last_response, "", 1);
  1220. bool request_status = fetch(model);
  1221. if (!request_status)
  1222. {
  1223. model->data_state = DataStateError;
  1224. }
  1225. },
  1226. true);
  1227. }
  1228. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1229. {
  1230. if (loader_model->fhttp->state == IDLE && loader_model->fhttp->last_response != NULL)
  1231. {
  1232. if (strstr(loader_model->fhttp->last_response, "[PONG]") != NULL)
  1233. {
  1234. FURI_LOG_DEV(TAG, "PONG received.");
  1235. }
  1236. else if (strncmp(loader_model->fhttp->last_response, "[SUCCESS]", 9) == 0)
  1237. {
  1238. FURI_LOG_DEV(TAG, "SUCCESS received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1239. }
  1240. else if (strncmp(loader_model->fhttp->last_response, "[ERROR]", 9) == 0)
  1241. {
  1242. FURI_LOG_DEV(TAG, "ERROR received. %s", loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1243. }
  1244. else if (strlen(loader_model->fhttp->last_response) == 0)
  1245. {
  1246. // Still waiting on response
  1247. }
  1248. else
  1249. {
  1250. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1251. }
  1252. }
  1253. else if (loader_model->fhttp->state == SENDING || loader_model->fhttp->state == RECEIVING)
  1254. {
  1255. // continue waiting
  1256. }
  1257. else if (loader_model->fhttp->state == INACTIVE)
  1258. {
  1259. // inactive. try again
  1260. }
  1261. else if (loader_model->fhttp->state == ISSUE)
  1262. {
  1263. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1264. }
  1265. else
  1266. {
  1267. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", loader_model->fhttp->state, loader_model->fhttp->last_response ? loader_model->fhttp->last_response : "NULL");
  1268. DEV_CRASH();
  1269. }
  1270. }
  1271. else if (current_data_state == DataStateReceived)
  1272. {
  1273. with_view_model(
  1274. view,
  1275. DataLoaderModel * model,
  1276. {
  1277. char *data_text;
  1278. if (model->parser == NULL)
  1279. {
  1280. data_text = NULL;
  1281. FURI_LOG_DEV(TAG, "Parser is NULL");
  1282. DEV_CRASH();
  1283. }
  1284. else
  1285. {
  1286. data_text = model->parser(model);
  1287. }
  1288. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", model->fhttp->last_response ? model->fhttp->last_response : "NULL", data_text ? data_text : "NULL");
  1289. model->data_text = data_text;
  1290. if (data_text == NULL)
  1291. {
  1292. model->data_state = DataStateParseError;
  1293. }
  1294. else
  1295. {
  1296. model->data_state = DataStateParsed;
  1297. }
  1298. },
  1299. true);
  1300. }
  1301. else if (current_data_state == DataStateParsed)
  1302. {
  1303. with_view_model(
  1304. view,
  1305. DataLoaderModel * model,
  1306. {
  1307. if (++model->request_index < model->request_count)
  1308. {
  1309. model->data_state = DataStateInitial;
  1310. }
  1311. else
  1312. {
  1313. flip_store_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1314. if (model->data_text != NULL)
  1315. {
  1316. free(model->data_text);
  1317. model->data_text = NULL;
  1318. }
  1319. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1320. view_dispatcher_switch_to_view(app->view_dispatcher, FlipStoreViewWidgetResult);
  1321. }
  1322. },
  1323. true);
  1324. }
  1325. }
  1326. static void flip_store_loader_timer_callback(void *context)
  1327. {
  1328. if (context == NULL)
  1329. {
  1330. FURI_LOG_E(TAG, "flip_store_loader_timer_callback - context is NULL");
  1331. DEV_CRASH();
  1332. return;
  1333. }
  1334. FlipStoreApp *app = (FlipStoreApp *)context;
  1335. view_dispatcher_send_custom_event(app->view_dispatcher, FlipStoreCustomEventProcess);
  1336. }
  1337. static void flip_store_loader_on_enter(void *context)
  1338. {
  1339. if (context == NULL)
  1340. {
  1341. FURI_LOG_E(TAG, "flip_store_loader_on_enter - context is NULL");
  1342. DEV_CRASH();
  1343. return;
  1344. }
  1345. FlipStoreApp *app = (FlipStoreApp *)context;
  1346. View *view = app->view_loader;
  1347. with_view_model(
  1348. view,
  1349. DataLoaderModel * model,
  1350. {
  1351. view_set_previous_callback(view, model->back_callback);
  1352. if (model->timer == NULL)
  1353. {
  1354. model->timer = furi_timer_alloc(flip_store_loader_timer_callback, FuriTimerTypePeriodic, app);
  1355. }
  1356. furi_timer_start(model->timer, 250);
  1357. },
  1358. true);
  1359. }
  1360. static void flip_store_loader_on_exit(void *context)
  1361. {
  1362. if (context == NULL)
  1363. {
  1364. FURI_LOG_E(TAG, "flip_store_loader_on_exit - context is NULL");
  1365. DEV_CRASH();
  1366. return;
  1367. }
  1368. FlipStoreApp *app = (FlipStoreApp *)context;
  1369. View *view = app->view_loader;
  1370. with_view_model(
  1371. view,
  1372. DataLoaderModel * model,
  1373. {
  1374. if (model->timer)
  1375. {
  1376. furi_timer_stop(model->timer);
  1377. }
  1378. },
  1379. false);
  1380. }
  1381. void flip_store_loader_init(View *view)
  1382. {
  1383. if (view == NULL)
  1384. {
  1385. FURI_LOG_E(TAG, "flip_store_loader_init - view is NULL");
  1386. DEV_CRASH();
  1387. return;
  1388. }
  1389. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1390. view_set_enter_callback(view, flip_store_loader_on_enter);
  1391. view_set_exit_callback(view, flip_store_loader_on_exit);
  1392. }
  1393. void flip_store_loader_free_model(View *view)
  1394. {
  1395. if (view == NULL)
  1396. {
  1397. FURI_LOG_E(TAG, "flip_store_loader_free_model - view is NULL");
  1398. DEV_CRASH();
  1399. return;
  1400. }
  1401. with_view_model(
  1402. view,
  1403. DataLoaderModel * model,
  1404. {
  1405. if (model->timer)
  1406. {
  1407. furi_timer_free(model->timer);
  1408. model->timer = NULL;
  1409. }
  1410. if (model->parser_context)
  1411. {
  1412. // do not free the context here, it is the app context
  1413. // free(model->parser_context);
  1414. // model->parser_context = NULL;
  1415. }
  1416. if (model->fhttp)
  1417. {
  1418. flipper_http_free(model->fhttp);
  1419. model->fhttp = NULL;
  1420. }
  1421. },
  1422. false);
  1423. }
  1424. bool flip_store_custom_event_callback(void *context, uint32_t index)
  1425. {
  1426. if (context == NULL)
  1427. {
  1428. FURI_LOG_E(TAG, "flip_store_custom_event_callback - context is NULL");
  1429. DEV_CRASH();
  1430. return false;
  1431. }
  1432. switch (index)
  1433. {
  1434. case FlipStoreCustomEventProcess:
  1435. flip_store_loader_process_callback(context);
  1436. return true;
  1437. default:
  1438. FURI_LOG_DEV(TAG, "flip_store_custom_event_callback. Unknown index: %ld", index);
  1439. return false;
  1440. }
  1441. }
  1442. 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)
  1443. {
  1444. if (app == NULL)
  1445. {
  1446. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - app is NULL");
  1447. DEV_CRASH();
  1448. return;
  1449. }
  1450. View *view = app->view_loader;
  1451. if (view == NULL)
  1452. {
  1453. FURI_LOG_E(TAG, "flip_store_generic_switch_to_view - view is NULL");
  1454. DEV_CRASH();
  1455. return;
  1456. }
  1457. with_view_model(
  1458. view,
  1459. DataLoaderModel * model,
  1460. {
  1461. model->title = title;
  1462. model->fetcher = fetcher;
  1463. model->parser = parser;
  1464. model->request_index = 0;
  1465. model->request_count = request_count;
  1466. model->back_callback = back;
  1467. model->data_state = DataStateInitial;
  1468. model->data_text = NULL;
  1469. //
  1470. model->parser_context = app;
  1471. if (!model->fhttp)
  1472. {
  1473. model->fhttp = flipper_http_alloc();
  1474. }
  1475. },
  1476. true);
  1477. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1478. }