flip_library_callback.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. #include <callback/flip_library_callback.h>
  2. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  3. #ifdef DEVELOPMENT
  4. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  5. #define DEV_CRASH() furi_crash()
  6. #else
  7. #define FURI_LOG_DEV(tag, format, ...)
  8. #define DEV_CRASH()
  9. #endif
  10. static bool flip_library_random_fact_fetch(FactLoaderModel *model)
  11. {
  12. UNUSED(model);
  13. return flipper_http_get_request("https://uselessfacts.jsph.pl/api/v2/facts/random");
  14. }
  15. static char *flip_library_random_fact_parse(FactLoaderModel *model)
  16. {
  17. UNUSED(model);
  18. return get_json_value("text", fhttp.last_response, 128);
  19. }
  20. static void flip_library_random_fact_switch_to_view(FlipLibraryApp *app)
  21. {
  22. flip_library_generic_switch_to_view(app, "Random Fact", flip_library_random_fact_fetch, flip_library_random_fact_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  23. }
  24. static bool flip_library_cat_fact_fetch(FactLoaderModel *model)
  25. {
  26. UNUSED(model);
  27. return flipper_http_get_request_with_headers("https://catfact.ninja/fact", "{\"Content-Type\":\"application/json\"}");
  28. }
  29. static char *flip_library_cat_fact_parse(FactLoaderModel *model)
  30. {
  31. UNUSED(model);
  32. return get_json_value("fact", fhttp.last_response, 128);
  33. }
  34. static void flip_library_cat_fact_switch_to_view(FlipLibraryApp *app)
  35. {
  36. flip_library_generic_switch_to_view(app, "Random Cat Fact", flip_library_cat_fact_fetch, flip_library_cat_fact_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  37. }
  38. static bool flip_library_dog_fact_fetch(FactLoaderModel *model)
  39. {
  40. UNUSED(model);
  41. return flipper_http_get_request_with_headers("https://dog-api.kinduff.com/api/facts", "{\"Content-Type\":\"application/json\"}");
  42. }
  43. static char *flip_library_dog_fact_parse(FactLoaderModel *model)
  44. {
  45. UNUSED(model);
  46. return get_json_array_value("facts", 0, fhttp.last_response, 256);
  47. }
  48. static void flip_library_dog_fact_switch_to_view(FlipLibraryApp *app)
  49. {
  50. flip_library_generic_switch_to_view(app, "Random Dog Fact", flip_library_dog_fact_fetch, flip_library_dog_fact_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  51. }
  52. static bool flip_library_quote_fetch(FactLoaderModel *model)
  53. {
  54. UNUSED(model);
  55. return flipper_http_get_request("https://zenquotes.io/api/random");
  56. }
  57. static char *flip_library_quote_parse(FactLoaderModel *model)
  58. {
  59. UNUSED(model);
  60. // remove [ and ] from the start and end of the string
  61. char *response = fhttp.last_response;
  62. if (response[0] == '[')
  63. {
  64. response++;
  65. }
  66. if (response[strlen(response) - 1] == ']')
  67. {
  68. response[strlen(response) - 1] = '\0';
  69. }
  70. // remove white space from both sides
  71. while (response[0] == ' ')
  72. {
  73. response++;
  74. }
  75. while (response[strlen(response) - 1] == ' ')
  76. {
  77. response[strlen(response) - 1] = '\0';
  78. }
  79. return get_json_value("q", response, 128);
  80. }
  81. static void flip_library_quote_switch_to_view(FlipLibraryApp *app)
  82. {
  83. flip_library_generic_switch_to_view(app, "Random Quote", flip_library_quote_fetch, flip_library_quote_parse, 1, callback_to_random_facts, FlipLibraryViewLoader);
  84. }
  85. static bool flip_library_dictionary_fetch(FactLoaderModel *model)
  86. {
  87. UNUSED(model);
  88. char payload[128];
  89. snprintf(payload, sizeof(payload), "{\"word\":\"%s\"}", app_instance->uart_text_input_buffer_query);
  90. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/define/", "{\"Content-Type\":\"application/json\"}", payload);
  91. }
  92. static char *flip_library_dictionary_parse(FactLoaderModel *model)
  93. {
  94. UNUSED(model);
  95. char *defn = get_json_value("definition", fhttp.last_response, 16);
  96. if (defn == NULL)
  97. {
  98. defn = get_json_value("[ERROR]", fhttp.last_response, 16);
  99. }
  100. return defn;
  101. }
  102. static void flip_library_dictionary_switch_to_view(FlipLibraryApp *app)
  103. {
  104. uart_text_input_set_header_text(app->uart_text_input_query, "Enter a word");
  105. flip_library_generic_switch_to_view(app, "Defining", flip_library_dictionary_fetch, flip_library_dictionary_parse, 1, callback_to_submenu, FlipLibraryViewTextInputQuery);
  106. }
  107. static void flip_library_request_error_draw(Canvas *canvas)
  108. {
  109. if (canvas == NULL)
  110. {
  111. FURI_LOG_E(TAG, "flip_library_request_error_draw - canvas is NULL");
  112. DEV_CRASH();
  113. return;
  114. }
  115. if (fhttp.last_response != NULL)
  116. {
  117. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  118. {
  119. canvas_clear(canvas);
  120. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  121. canvas_draw_str(canvas, 0, 22, "Failed to reconnect.");
  122. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  123. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  124. }
  125. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  126. {
  127. canvas_clear(canvas);
  128. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  129. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  130. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  131. }
  132. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  133. {
  134. canvas_clear(canvas);
  135. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  136. }
  137. else
  138. {
  139. canvas_clear(canvas);
  140. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  141. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  142. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  143. }
  144. }
  145. else
  146. {
  147. canvas_clear(canvas);
  148. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  149. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  150. }
  151. }
  152. static void flip_library_widget_set_text(char *message, Widget **widget)
  153. {
  154. if (widget == NULL)
  155. {
  156. FURI_LOG_E(TAG, "flip_library_set_widget_text - widget is NULL");
  157. DEV_CRASH();
  158. return;
  159. }
  160. if (message == NULL)
  161. {
  162. FURI_LOG_E(TAG, "flip_library_set_widget_text - message is NULL");
  163. DEV_CRASH();
  164. return;
  165. }
  166. widget_reset(*widget);
  167. uint32_t message_length = strlen(message); // Length of the message
  168. uint32_t i = 0; // Index tracker
  169. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  170. char *formatted_message; // Buffer to hold the final formatted message
  171. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  172. {
  173. return;
  174. }
  175. while (i < message_length)
  176. {
  177. // TODO: Use canvas_glyph_width to calculate the maximum characters for the line
  178. uint32_t max_line_length = 29; // Maximum characters per line
  179. uint32_t remaining_length = message_length - i; // Remaining characters
  180. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  181. // Temporary buffer to hold the current line
  182. char line[30];
  183. strncpy(line, message + i, line_length);
  184. line[line_length] = '\0';
  185. // Check if the line ends in the middle of a word and adjust accordingly
  186. if (line_length == 29 && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  187. {
  188. // Find the last space within the 30-character segment
  189. char *last_space = strrchr(line, ' ');
  190. if (last_space != NULL)
  191. {
  192. // Adjust the line length to avoid cutting the word
  193. line_length = last_space - line;
  194. line[line_length] = '\0'; // Null-terminate at the space
  195. }
  196. }
  197. // Manually copy the fixed line into the formatted_message buffer
  198. for (uint32_t j = 0; j < line_length; j++)
  199. {
  200. formatted_message[formatted_index++] = line[j];
  201. }
  202. // Add a newline character for line spacing
  203. formatted_message[formatted_index++] = '\n';
  204. // Move i forward to the start of the next word
  205. i += line_length;
  206. // Skip spaces at the beginning of the next line
  207. while (message[i] == ' ')
  208. {
  209. i++;
  210. }
  211. }
  212. // Add the formatted message to the widget
  213. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  214. }
  215. void flip_library_loader_draw_callback(Canvas *canvas, void *model)
  216. {
  217. if (!canvas || !model)
  218. {
  219. FURI_LOG_E(TAG, "flip_library_loader_draw_callback - canvas or model is NULL");
  220. return;
  221. }
  222. SerialState http_state = fhttp.state;
  223. FactLoaderModel *fact_loader_model = (FactLoaderModel*)model;
  224. FactState fact_state = fact_loader_model->fact_state;
  225. char *title = fact_loader_model->title;
  226. canvas_set_font(canvas, FontSecondary);
  227. if (http_state == INACTIVE)
  228. {
  229. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  230. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  231. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  232. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  233. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  234. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  235. return;
  236. }
  237. if (fact_state == FactStateError || fact_state == FactStateParseError)
  238. {
  239. flip_library_request_error_draw(canvas);
  240. return;
  241. }
  242. canvas_draw_str(canvas, 0, 7, title);
  243. canvas_draw_str(canvas, 0, 15, "Loading...");
  244. if (fact_state == FactStateInitial)
  245. {
  246. return;
  247. }
  248. if (http_state == SENDING)
  249. {
  250. canvas_draw_str(canvas, 0, 22, "Sending...");
  251. return;
  252. }
  253. if (http_state == RECEIVING || fact_state == FactStateRequested)
  254. {
  255. canvas_draw_str(canvas, 0, 22, "Receiving...");
  256. return;
  257. }
  258. if (http_state == IDLE && fact_state == FactStateReceived)
  259. {
  260. canvas_draw_str(canvas, 0, 22, "Processing...");
  261. return;
  262. }
  263. if (http_state == IDLE && fact_state == FactStateParsed)
  264. {
  265. canvas_draw_str(canvas, 0, 22, "Processed...");
  266. return;
  267. }
  268. }
  269. static void flip_library_loader_process_callback(void *context)
  270. {
  271. if (context == NULL)
  272. {
  273. FURI_LOG_E(TAG, "flip_library_loader_process_callback - context is NULL");
  274. DEV_CRASH();
  275. return;
  276. }
  277. FlipLibraryApp *app = (FlipLibraryApp*)context;
  278. View *view = app->view_loader;
  279. FactState current_fact_state;
  280. with_view_model(view, FactLoaderModel *model, { current_fact_state = model->fact_state; }, false);
  281. if (current_fact_state == FactStateInitial)
  282. {
  283. with_view_model(
  284. view,
  285. FactLoaderModel *model,
  286. {
  287. model->fact_state = FactStateRequested;
  288. FactLoaderFetch fetch = model->fetcher;
  289. if (fetch == NULL)
  290. {
  291. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  292. model->fact_state = FactStateError;
  293. return;
  294. }
  295. // Clear any previous responses
  296. strncpy(fhttp.last_response, "", 1);
  297. bool request_status = fetch(model);
  298. if (!request_status)
  299. {
  300. model->fact_state = FactStateError;
  301. }
  302. },
  303. true);
  304. }
  305. else if (current_fact_state == FactStateRequested || current_fact_state == FactStateError)
  306. {
  307. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  308. {
  309. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  310. {
  311. FURI_LOG_DEV(TAG, "PONG received.");
  312. }
  313. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  314. {
  315. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  316. }
  317. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  318. {
  319. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  320. }
  321. else if (strlen(fhttp.last_response) == 0)
  322. {
  323. // Still waiting on response
  324. }
  325. else
  326. {
  327. with_view_model(view, FactLoaderModel *model, { model->fact_state = FactStateReceived; }, true);
  328. }
  329. }
  330. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  331. {
  332. // continue waiting
  333. }
  334. else if (fhttp.state == INACTIVE)
  335. {
  336. // inactive. try again
  337. }
  338. else if (fhttp.state == ISSUE)
  339. {
  340. with_view_model(view, FactLoaderModel *model, { model->fact_state = FactStateError; }, true);
  341. }
  342. else
  343. {
  344. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  345. DEV_CRASH();
  346. }
  347. }
  348. else if (current_fact_state == FactStateReceived)
  349. {
  350. with_view_model(
  351. view,
  352. FactLoaderModel *model,
  353. {
  354. char *fact_text;
  355. if (model->parser == NULL)
  356. {
  357. fact_text = NULL;
  358. FURI_LOG_DEV(TAG, "Parser is NULL");
  359. DEV_CRASH();
  360. }
  361. else
  362. {
  363. fact_text = model->parser(model);
  364. }
  365. FURI_LOG_DEV(TAG, "Parsed fact: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", fact_text ? fact_text : "NULL");
  366. model->fact_text = fact_text;
  367. if (fact_text == NULL)
  368. {
  369. model->fact_state = FactStateParseError;
  370. }
  371. else
  372. {
  373. model->fact_state = FactStateParsed;
  374. }
  375. },
  376. true);
  377. }
  378. else if (current_fact_state == FactStateParsed)
  379. {
  380. with_view_model(
  381. view,
  382. FactLoaderModel *model,
  383. {
  384. if (++model->request_index < model->request_count)
  385. {
  386. model->fact_state = FactStateInitial;
  387. }
  388. else
  389. {
  390. flip_library_widget_set_text(model->fact_text != NULL ? model->fact_text : "Empty result", &app_instance->widget_result);
  391. if (model->fact_text != NULL)
  392. {
  393. free(model->fact_text);
  394. model->fact_text = NULL;
  395. }
  396. view_set_previous_callback(widget_get_view(app_instance->widget_result), model->back_callback);
  397. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewWidgetResult);
  398. }
  399. },
  400. true);
  401. }
  402. }
  403. static void flip_library_loader_timer_callback(void *context)
  404. {
  405. if (context == NULL)
  406. {
  407. FURI_LOG_E(TAG, "flip_library_loader_timer_callback - context is NULL");
  408. DEV_CRASH();
  409. return;
  410. }
  411. FlipLibraryApp *app = (FlipLibraryApp*)context;
  412. view_dispatcher_send_custom_event(app->view_dispatcher, FlipLibraryCustomEventProcess);
  413. }
  414. static void flip_library_loader_on_enter(void *context)
  415. {
  416. if (context == NULL)
  417. {
  418. FURI_LOG_E(TAG, "flip_library_loader_on_enter - context is NULL");
  419. DEV_CRASH();
  420. return;
  421. }
  422. FlipLibraryApp *app = (FlipLibraryApp*)context;
  423. View *view = app->view_loader;
  424. with_view_model(
  425. view,
  426. FactLoaderModel *model,
  427. {
  428. view_set_previous_callback(view, model->back_callback);
  429. if (model->timer == NULL)
  430. {
  431. model->timer = furi_timer_alloc(flip_library_loader_timer_callback, FuriTimerTypePeriodic, app);
  432. }
  433. furi_timer_start(model->timer, 250);
  434. },
  435. true);
  436. }
  437. static void flip_library_loader_on_exit(void *context)
  438. {
  439. if (context == NULL)
  440. {
  441. FURI_LOG_E(TAG, "flip_library_loader_on_exit - context is NULL");
  442. DEV_CRASH();
  443. return;
  444. }
  445. FlipLibraryApp *app = (FlipLibraryApp*)context;
  446. View *view = app->view_loader;
  447. with_view_model(
  448. view,
  449. FactLoaderModel *model,
  450. {
  451. if (model->timer)
  452. {
  453. furi_timer_stop(model->timer);
  454. }
  455. },
  456. false);
  457. }
  458. void flip_library_loader_init(View *view)
  459. {
  460. if (view == NULL)
  461. {
  462. FURI_LOG_E(TAG, "flip_library_loader_init - view is NULL");
  463. DEV_CRASH();
  464. return;
  465. }
  466. view_allocate_model(view, ViewModelTypeLocking, sizeof(FactLoaderModel));
  467. view_set_enter_callback(view, flip_library_loader_on_enter);
  468. view_set_exit_callback(view, flip_library_loader_on_exit);
  469. }
  470. void flip_library_loader_free_model(View *view)
  471. {
  472. if (view == NULL)
  473. {
  474. FURI_LOG_E(TAG, "flip_library_loader_free_model - view is NULL");
  475. DEV_CRASH();
  476. return;
  477. }
  478. with_view_model(
  479. view,
  480. FactLoaderModel *model,
  481. {
  482. if (model->timer)
  483. {
  484. furi_timer_free(model->timer);
  485. model->timer = NULL;
  486. }
  487. if (model->parser_context)
  488. {
  489. free(model->parser_context);
  490. model->parser_context = NULL;
  491. }
  492. },
  493. false);
  494. }
  495. bool flip_library_custom_event_callback(void *context, uint32_t index)
  496. {
  497. if (context == NULL)
  498. {
  499. FURI_LOG_E(TAG, "flip_library_custom_event_callback - context is NULL");
  500. DEV_CRASH();
  501. return false;
  502. }
  503. switch (index)
  504. {
  505. case FlipLibraryCustomEventProcess:
  506. flip_library_loader_process_callback(context);
  507. return true;
  508. default:
  509. FURI_LOG_DEV(TAG, "flip_library_custom_event_callback. Unknown index: %ld", index);
  510. return false;
  511. }
  512. }
  513. void callback_submenu_choices(void *context, uint32_t index)
  514. {
  515. if (context == NULL)
  516. {
  517. FURI_LOG_E(TAG, "callback_submenu_choices - context is NULL");
  518. DEV_CRASH();
  519. return;
  520. }
  521. FlipLibraryApp *app = (FlipLibraryApp*)context;
  522. switch (index)
  523. {
  524. case FlipLibrarySubmenuIndexRandomFacts:
  525. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFacts);
  526. break;
  527. case FlipLibrarySubmenuIndexAbout:
  528. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewAbout);
  529. break;
  530. case FlipLibrarySubmenuIndexSettings:
  531. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  532. break;
  533. case FlipLibrarySubmenuIndexDictionary:
  534. flip_library_dictionary_switch_to_view(app);
  535. break;
  536. case FlipLibrarySubmenuIndexRandomFactsCats:
  537. flip_library_cat_fact_switch_to_view(app);
  538. break;
  539. case FlipLibrarySubmenuIndexRandomFactsDogs:
  540. flip_library_dog_fact_switch_to_view(app);
  541. break;
  542. case FlipLibrarySubmenuIndexRandomFactsQuotes:
  543. flip_library_quote_switch_to_view(app);
  544. break;
  545. case FlipLibrarySubmenuIndexRandomFactsAll:
  546. flip_library_random_fact_switch_to_view(app);
  547. break;
  548. default:
  549. break;
  550. }
  551. }
  552. void text_updated_ssid(void *context)
  553. {
  554. FlipLibraryApp *app = (FlipLibraryApp*)context;
  555. if (!app)
  556. {
  557. FURI_LOG_E(TAG, "text_updated_ssid - FlipLibraryApp is NULL");
  558. DEV_CRASH();
  559. return;
  560. }
  561. // store the entered text
  562. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  563. // Ensure null-termination
  564. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  565. // update the variable item text
  566. if (app->variable_item_ssid)
  567. {
  568. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  569. }
  570. // save settings
  571. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  572. // save wifi settings to devboard
  573. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  574. {
  575. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  576. {
  577. FURI_LOG_E(TAG, "Failed to save wifi settings.");
  578. }
  579. }
  580. // switch to the settings view
  581. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  582. }
  583. void text_updated_password(void *context)
  584. {
  585. FlipLibraryApp *app = (FlipLibraryApp*)context;
  586. if (!app)
  587. {
  588. FURI_LOG_E(TAG, "text_updated_password - FlipLibraryApp is NULL");
  589. DEV_CRASH();
  590. return;
  591. }
  592. // store the entered text
  593. strncpy(app->uart_text_input_buffer_password, app->uart_text_input_temp_buffer_password, app->uart_text_input_buffer_size_password);
  594. // Ensure null-termination
  595. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  596. // update the variable item text
  597. if (app->variable_item_password)
  598. {
  599. variable_item_set_current_value_text(app->variable_item_password, app->uart_text_input_buffer_password);
  600. }
  601. // save settings
  602. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  603. // save wifi settings to devboard
  604. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  605. {
  606. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  607. {
  608. FURI_LOG_E(TAG, "Failed to save wifi settings");
  609. }
  610. }
  611. // switch to the settings view
  612. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  613. }
  614. void text_updated_query(void *context)
  615. {
  616. FlipLibraryApp *app = (FlipLibraryApp*)context;
  617. if (!app)
  618. {
  619. FURI_LOG_E(TAG, "text_updated_query - FlipLibraryApp is NULL");
  620. DEV_CRASH();
  621. return;
  622. }
  623. // store the entered text
  624. strncpy(app->uart_text_input_buffer_query, app->uart_text_input_temp_buffer_query, app->uart_text_input_buffer_size_query);
  625. // Ensure null-termination
  626. app->uart_text_input_buffer_query[app->uart_text_input_buffer_size_query - 1] = '\0';
  627. // switch to the loader view
  628. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewLoader);
  629. }
  630. uint32_t callback_to_submenu(void *context)
  631. {
  632. UNUSED(context);
  633. return FlipLibraryViewSubmenuMain;
  634. }
  635. uint32_t callback_to_wifi_settings(void *context)
  636. {
  637. UNUSED(context);
  638. return FlipLibraryViewSettings;
  639. }
  640. uint32_t callback_to_random_facts(void *context)
  641. {
  642. UNUSED(context);
  643. return FlipLibraryViewRandomFacts;
  644. }
  645. void settings_item_selected(void *context, uint32_t index)
  646. {
  647. FlipLibraryApp *app = (FlipLibraryApp*)context;
  648. if (!app)
  649. {
  650. FURI_LOG_E(TAG, "settings_item_selected - FlipLibraryApp is NULL");
  651. DEV_CRASH();
  652. return;
  653. }
  654. switch (index)
  655. {
  656. case 0: // Input SSID
  657. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputSSID);
  658. break;
  659. case 1: // Input Password
  660. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputPassword);
  661. break;
  662. default:
  663. FURI_LOG_E(TAG, "Unknown configuration item index");
  664. break;
  665. }
  666. }
  667. /**
  668. * @brief Navigation callback for exiting the application
  669. * @param context The context - unused
  670. * @return next view id (VIEW_NONE to exit the app)
  671. */
  672. uint32_t callback_exit_app(void *context)
  673. {
  674. // Exit the application
  675. if (!context)
  676. {
  677. FURI_LOG_E(TAG, "callback_exit_app - Context is NULL");
  678. return VIEW_NONE;
  679. }
  680. UNUSED(context);
  681. return VIEW_NONE; // Return VIEW_NONE to exit the app
  682. }
  683. void flip_library_generic_switch_to_view(FlipLibraryApp *app, char *title, FactLoaderFetch fetcher, FactLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  684. {
  685. if (app == NULL)
  686. {
  687. FURI_LOG_E(TAG, "flip_library_generic_switch_to_view - app is NULL");
  688. DEV_CRASH();
  689. return;
  690. }
  691. View *view = app->view_loader;
  692. if (view == NULL)
  693. {
  694. FURI_LOG_E(TAG, "flip_library_generic_switch_to_view - view is NULL");
  695. DEV_CRASH();
  696. return;
  697. }
  698. with_view_model(
  699. view,
  700. FactLoaderModel *model,
  701. {
  702. model->title = title;
  703. model->fetcher = fetcher;
  704. model->parser = parser;
  705. model->request_index = 0;
  706. model->request_count = request_count;
  707. model->back_callback = back;
  708. model->fact_state = FactStateInitial;
  709. model->fact_text = NULL;
  710. },
  711. true);
  712. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  713. }