flip_library_callback.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. #ifndef FLIP_LIBRARY_CALLBACK_H
  2. #define FLIP_LIBRARY_CALLBACK_H
  3. static uint32_t random_facts_index = 0;
  4. static bool sent_random_fact_request = false;
  5. static bool random_fact_request_success = false;
  6. static bool random_fact_request_success_all = false;
  7. char *random_fact = NULL;
  8. static FlipLibraryApp *app_instance = NULL;
  9. #define MAX_TOKENS 512 // Adjust based on expected JSON size
  10. // Helper function to compare JSON keys
  11. int jsoneq(const char *json, jsmntok_t *tok, const char *s)
  12. {
  13. if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
  14. strncmp(json + tok->start, s, tok->end - tok->start) == 0)
  15. {
  16. return 0;
  17. }
  18. return -1;
  19. }
  20. // return the value of the key in the JSON data
  21. // works for the first level of the JSON data
  22. char *get_json_value(char *key, char *json_data, uint32_t max_tokens)
  23. {
  24. // Parse the JSON feed
  25. if (json_data != NULL)
  26. {
  27. jsmn_parser parser;
  28. jsmn_init(&parser);
  29. // Allocate tokens array on the heap
  30. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  31. if (tokens == NULL)
  32. {
  33. FURI_LOG_E(TAG, "Failed to allocate memory for JSON tokens.");
  34. return NULL;
  35. }
  36. int ret = jsmn_parse(&parser, json_data, strlen(json_data), tokens, max_tokens);
  37. if (ret < 0)
  38. {
  39. // Handle parsing errors
  40. FURI_LOG_E(TAG, "Failed to parse JSON: %d", ret);
  41. free(tokens);
  42. return NULL;
  43. }
  44. // Ensure that the root element is an object
  45. if (ret < 1 || tokens[0].type != JSMN_OBJECT)
  46. {
  47. FURI_LOG_E(TAG, "Root element is not an object.");
  48. free(tokens);
  49. return NULL;
  50. }
  51. // Loop through the tokens to find the key
  52. for (int i = 1; i < ret; i++)
  53. {
  54. if (jsoneq(json_data, &tokens[i], key) == 0)
  55. {
  56. // We found the key. Now, return the associated value.
  57. int length = tokens[i + 1].end - tokens[i + 1].start;
  58. char *value = malloc(length + 1);
  59. if (value == NULL)
  60. {
  61. FURI_LOG_E(TAG, "Failed to allocate memory for value.");
  62. free(tokens);
  63. return NULL;
  64. }
  65. strncpy(value, json_data + tokens[i + 1].start, length);
  66. value[length] = '\0'; // Null-terminate the string
  67. free(tokens); // Free the token array
  68. return value; // Return the extracted value
  69. }
  70. }
  71. // Free the token array if key was not found
  72. free(tokens);
  73. }
  74. else
  75. {
  76. FURI_LOG_E(TAG, "JSON data is NULL");
  77. }
  78. FURI_LOG_E(TAG, "Failed to find the key in the JSON.");
  79. return NULL; // Return NULL if something goes wrong
  80. }
  81. // Parse JSON to find the "text" key
  82. char *flip_library_parse_random_fact()
  83. {
  84. return get_json_value("text", fhttp.received_data, 128);
  85. }
  86. char *flip_library_parse_cat_fact()
  87. {
  88. return get_json_value("fact", fhttp.received_data, 128);
  89. }
  90. char *flip_library_parse_dictionary()
  91. {
  92. return get_json_value("definition", fhttp.received_data, 16);
  93. }
  94. static void flip_library_request_error(Canvas *canvas)
  95. {
  96. if (fhttp.received_data == NULL)
  97. {
  98. if (fhttp.last_response != NULL)
  99. {
  100. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  101. {
  102. canvas_clear(canvas);
  103. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  104. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  105. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  106. }
  107. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  108. {
  109. canvas_clear(canvas);
  110. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  111. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  112. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  113. }
  114. else
  115. {
  116. canvas_clear(canvas);
  117. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  118. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  119. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  120. }
  121. }
  122. else
  123. {
  124. canvas_clear(canvas);
  125. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  126. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  127. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  128. }
  129. }
  130. else
  131. {
  132. canvas_clear(canvas);
  133. canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
  134. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  135. }
  136. }
  137. static void flip_library_draw_fact(char *message, Widget **widget)
  138. {
  139. if (app_instance == NULL)
  140. {
  141. FURI_LOG_E(TAG, "App instance is NULL");
  142. return;
  143. }
  144. widget_reset(*widget);
  145. uint32_t fact_length = strlen(message); // Length of the message
  146. uint32_t i = 0; // Index tracker
  147. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  148. char *formatted_message; // Buffer to hold the final formatted message
  149. if (!easy_flipper_set_buffer(&formatted_message, fact_length * 2 + 1))
  150. {
  151. return;
  152. }
  153. while (i < fact_length)
  154. {
  155. uint32_t max_line_length = 29; // Maximum characters per line
  156. uint32_t remaining_length = fact_length - i; // Remaining characters
  157. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  158. // Temporary buffer to hold the current line
  159. char fact_line[30];
  160. strncpy(fact_line, message + i, line_length);
  161. fact_line[line_length] = '\0';
  162. // Check if the line ends in the middle of a word and adjust accordingly
  163. if (line_length == 29 && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  164. {
  165. // Find the last space within the 30-character segment
  166. char *last_space = strrchr(fact_line, ' ');
  167. if (last_space != NULL)
  168. {
  169. // Adjust the line length to avoid cutting the word
  170. line_length = last_space - fact_line;
  171. fact_line[line_length] = '\0'; // Null-terminate at the space
  172. }
  173. }
  174. // Manually copy the fixed line into the formatted_message buffer
  175. for (uint32_t j = 0; j < line_length; j++)
  176. {
  177. formatted_message[formatted_index++] = fact_line[j];
  178. }
  179. // Add a newline character for line spacing
  180. formatted_message[formatted_index++] = '\n';
  181. // Move i forward to the start of the next word
  182. i += line_length;
  183. // Skip spaces at the beginning of the next line
  184. while (message[i] == ' ')
  185. {
  186. i++;
  187. }
  188. }
  189. // Add the formatted message to the widget
  190. widget_add_text_scroll_element(
  191. *widget,
  192. 0,
  193. 0,
  194. 128,
  195. 64,
  196. formatted_message);
  197. }
  198. // Callback for drawing the main screen
  199. static void view_draw_callback_random_facts(Canvas *canvas, void *model)
  200. {
  201. if (!canvas || !app_instance)
  202. {
  203. return;
  204. }
  205. UNUSED(model);
  206. canvas_set_font(canvas, FontSecondary);
  207. if (fhttp.state == INACTIVE)
  208. {
  209. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  210. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  211. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  212. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  213. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  214. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  215. return;
  216. }
  217. if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsCats)
  218. {
  219. canvas_draw_str(canvas, 0, 7, "Random Cat Fact");
  220. canvas_draw_str(canvas, 0, 15, "Loading...");
  221. if (!sent_random_fact_request)
  222. {
  223. sent_random_fact_request = true;
  224. random_fact_request_success = flipper_http_get_request_with_headers("https://catfact.ninja/fact", "{\"Content-Type\":\"application/json\"}");
  225. if (!random_fact_request_success)
  226. {
  227. FURI_LOG_E(TAG, "Failed to send request");
  228. flip_library_request_error(canvas);
  229. return;
  230. }
  231. fhttp.state = RECEIVING;
  232. }
  233. else
  234. {
  235. if (fhttp.state == RECEIVING)
  236. {
  237. canvas_draw_str(canvas, 0, 22, "Receiving...");
  238. return;
  239. }
  240. // check status
  241. else if (fhttp.state == ISSUE || !random_fact_request_success)
  242. {
  243. flip_library_request_error(canvas);
  244. }
  245. else if (fhttp.state == IDLE && fhttp.received_data != NULL && !random_fact_request_success_all)
  246. {
  247. canvas_draw_str(canvas, 0, 22, "Processing...");
  248. // success
  249. // check status
  250. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  251. {
  252. flip_library_request_error(canvas);
  253. FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
  254. return;
  255. }
  256. else if (!random_fact_request_success_all)
  257. {
  258. random_fact = flip_library_parse_cat_fact();
  259. if (random_fact == NULL)
  260. {
  261. flip_library_request_error(canvas);
  262. fhttp.state = ISSUE;
  263. return;
  264. }
  265. // Mark success
  266. random_fact_request_success_all = true;
  267. // draw random facts
  268. flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
  269. // go to random facts widget
  270. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
  271. }
  272. }
  273. // likely redundant but just in case
  274. else if (fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL)
  275. {
  276. flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
  277. // go to random facts widget
  278. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
  279. }
  280. else // handle weird scenarios
  281. {
  282. // if received data isnt NULL
  283. if (fhttp.received_data != NULL)
  284. {
  285. // parse json to find the text key
  286. random_fact = flip_library_parse_cat_fact();
  287. if (random_fact == NULL)
  288. {
  289. flip_library_request_error(canvas);
  290. fhttp.state = ISSUE;
  291. return;
  292. }
  293. }
  294. }
  295. }
  296. }
  297. else if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsAll)
  298. {
  299. canvas_draw_str(canvas, 0, 10, "Random Fact");
  300. canvas_set_font(canvas, FontSecondary);
  301. canvas_draw_str(canvas, 0, 20, "Loading...");
  302. if (!sent_random_fact_request)
  303. {
  304. sent_random_fact_request = true;
  305. random_fact_request_success = flipper_http_get_request("https://uselessfacts.jsph.pl/api/v2/facts/random");
  306. if (!random_fact_request_success)
  307. {
  308. FURI_LOG_E(TAG, "Failed to send request");
  309. return;
  310. }
  311. fhttp.state = RECEIVING;
  312. }
  313. else
  314. {
  315. // check status
  316. if (fhttp.state == RECEIVING)
  317. {
  318. canvas_draw_str(canvas, 0, 30, "Receiving...");
  319. return;
  320. }
  321. // check status
  322. else if (fhttp.state == ISSUE || !random_fact_request_success)
  323. {
  324. flip_library_request_error(canvas);
  325. return;
  326. }
  327. else if (fhttp.state == IDLE && fhttp.received_data != NULL && !random_fact_request_success_all)
  328. {
  329. canvas_draw_str(canvas, 0, 30, "Processing...");
  330. // success
  331. // check status
  332. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  333. {
  334. flip_library_request_error(canvas);
  335. FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
  336. return;
  337. }
  338. // parse json to find the text key
  339. random_fact = flip_library_parse_random_fact();
  340. if (random_fact == NULL)
  341. {
  342. flip_library_request_error(canvas);
  343. fhttp.state = ISSUE;
  344. return;
  345. }
  346. // Mark success
  347. random_fact_request_success_all = true;
  348. // draw random facts
  349. flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
  350. // go to random facts widget
  351. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
  352. }
  353. // likely redundant but just in case
  354. else if (fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL)
  355. {
  356. // draw random facts
  357. flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
  358. // go to random facts widget
  359. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
  360. }
  361. else // handle weird scenarios
  362. {
  363. // if received data isnt NULL
  364. if (fhttp.received_data != NULL)
  365. {
  366. // parse json to find the text key
  367. random_fact = flip_library_parse_random_fact();
  368. if (random_fact == NULL)
  369. {
  370. flip_library_request_error(canvas);
  371. fhttp.state = ISSUE;
  372. return;
  373. }
  374. }
  375. }
  376. }
  377. }
  378. else
  379. {
  380. canvas_draw_str(canvas, 0, 7, "Random Fact");
  381. }
  382. }
  383. static void view_draw_callback_dictionary_run(Canvas *canvas, void *model)
  384. {
  385. if (!canvas || !app_instance || app_instance->uart_text_input_buffer_dictionary == NULL)
  386. {
  387. return;
  388. }
  389. UNUSED(model);
  390. canvas_set_font(canvas, FontSecondary);
  391. if (fhttp.state == INACTIVE)
  392. {
  393. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  394. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  395. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  396. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  397. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  398. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  399. return;
  400. }
  401. canvas_draw_str(canvas, 0, 10, "Defining, please wait...");
  402. if (!sent_random_fact_request)
  403. {
  404. sent_random_fact_request = true;
  405. char payload[128];
  406. snprintf(payload, sizeof(payload), "{\"word\":\"%s\"}", app_instance->uart_text_input_buffer_dictionary);
  407. random_fact_request_success = flipper_http_post_request_with_headers("https://www.flipsocial.net/api/define/", "{\"Content-Type\":\"application/json\"}", payload);
  408. if (!random_fact_request_success)
  409. {
  410. FURI_LOG_E(TAG, "Failed to send request");
  411. return;
  412. }
  413. fhttp.state = RECEIVING;
  414. }
  415. else
  416. {
  417. // check status
  418. if (fhttp.state == RECEIVING)
  419. {
  420. canvas_draw_str(canvas, 0, 20, "Receiving...");
  421. return;
  422. }
  423. // check status
  424. else if (fhttp.state == ISSUE || !random_fact_request_success)
  425. {
  426. flip_library_request_error(canvas);
  427. return;
  428. }
  429. else if (fhttp.state == IDLE && fhttp.received_data != NULL && !random_fact_request_success_all)
  430. {
  431. canvas_draw_str(canvas, 0, 20, "Processing...");
  432. // success
  433. // check status
  434. if (fhttp.state == ISSUE || fhttp.received_data == NULL)
  435. {
  436. flip_library_request_error(canvas);
  437. FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
  438. return;
  439. }
  440. // parse json to find the text key
  441. char *definition = flip_library_parse_dictionary();
  442. if (definition == NULL)
  443. {
  444. flip_library_request_error(canvas);
  445. fhttp.state = ISSUE;
  446. return;
  447. }
  448. // Mark success
  449. random_fact_request_success_all = true;
  450. // draw random facts
  451. flip_library_draw_fact(definition, &app_instance->widget_dictionary);
  452. // go to random facts widget
  453. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewDictionaryWidget);
  454. }
  455. // likely redundant but just in case
  456. else if (fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL)
  457. {
  458. // draw random facts
  459. flip_library_draw_fact(random_fact, &app_instance->widget_dictionary);
  460. // go to random facts widget
  461. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewDictionaryWidget);
  462. }
  463. else // handle weird scenarios
  464. {
  465. // if received data isnt NULL
  466. if (fhttp.received_data != NULL)
  467. {
  468. // parse json to find the text key
  469. char *definition = flip_library_parse_dictionary();
  470. if (definition == NULL)
  471. {
  472. flip_library_request_error(canvas);
  473. fhttp.state = ISSUE;
  474. return;
  475. }
  476. // draw random facts
  477. flip_library_draw_fact(definition, &app_instance->widget_dictionary);
  478. // go to random facts widget
  479. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewDictionaryWidget);
  480. free(definition);
  481. return;
  482. }
  483. }
  484. }
  485. }
  486. // Input callback for the view (async input handling)
  487. bool view_input_callback_random_facts(InputEvent *event, void *context)
  488. {
  489. if (!event || !context)
  490. {
  491. return false;
  492. }
  493. FlipLibraryApp *app = (FlipLibraryApp *)context;
  494. if (event->type == InputTypePress && event->key == InputKeyBack)
  495. {
  496. // Exit the app when the back button is pressed
  497. view_dispatcher_stop(app->view_dispatcher);
  498. return true;
  499. }
  500. return false;
  501. }
  502. static void callback_submenu_choices(void *context, uint32_t index)
  503. {
  504. FlipLibraryApp *app = (FlipLibraryApp *)context;
  505. if (!app)
  506. {
  507. FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
  508. return;
  509. }
  510. switch (index)
  511. {
  512. case FlipLibrarySubmenuIndexRandomFacts:
  513. random_facts_index = 0;
  514. sent_random_fact_request = false;
  515. random_fact = NULL;
  516. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFacts);
  517. break;
  518. case FlipLibrarySubmenuIndexAbout:
  519. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewAbout);
  520. break;
  521. case FlipLibrarySubmenuIndexSettings:
  522. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  523. break;
  524. case FlipLibrarySubmenuIndexDictionary:
  525. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewDictionaryTextInput);
  526. break;
  527. case FlipLibrarySubmenuIndexRandomFactsCats:
  528. random_facts_index = FlipLibrarySubmenuIndexRandomFactsCats;
  529. sent_random_fact_request = false;
  530. random_fact = NULL;
  531. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
  532. break;
  533. case FlipLibrarySubmenuIndexRandomFactsAll:
  534. random_facts_index = FlipLibrarySubmenuIndexRandomFactsAll;
  535. sent_random_fact_request = false;
  536. random_fact = NULL;
  537. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. static void text_updated_ssid(void *context)
  544. {
  545. FlipLibraryApp *app = (FlipLibraryApp *)context;
  546. if (!app)
  547. {
  548. FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
  549. return;
  550. }
  551. // store the entered text
  552. strncpy(app->uart_text_input_buffer_ssid, app->uart_text_input_temp_buffer_ssid, app->uart_text_input_buffer_size_ssid);
  553. // Ensure null-termination
  554. app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
  555. // update the variable item text
  556. if (app->variable_item_ssid)
  557. {
  558. variable_item_set_current_value_text(app->variable_item_ssid, app->uart_text_input_buffer_ssid);
  559. }
  560. // save settings
  561. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  562. // save wifi settings to devboard
  563. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  564. {
  565. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  566. {
  567. FURI_LOG_E(TAG, "Failed to save wifi settings");
  568. }
  569. }
  570. // switch to the settings view
  571. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  572. }
  573. static void text_updated_password(void *context)
  574. {
  575. FlipLibraryApp *app = (FlipLibraryApp *)context;
  576. if (!app)
  577. {
  578. FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
  579. return;
  580. }
  581. // store the entered text
  582. strncpy(app->uart_text_input_buffer_password, app->uart_text_input_temp_buffer_password, app->uart_text_input_buffer_size_password);
  583. // Ensure null-termination
  584. app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
  585. // update the variable item text
  586. if (app->variable_item_password)
  587. {
  588. variable_item_set_current_value_text(app->variable_item_password, app->uart_text_input_buffer_password);
  589. }
  590. // save settings
  591. save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
  592. // save wifi settings to devboard
  593. if (strlen(app->uart_text_input_buffer_ssid) > 0 && strlen(app->uart_text_input_buffer_password) > 0)
  594. {
  595. if (!flipper_http_save_wifi(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password))
  596. {
  597. FURI_LOG_E(TAG, "Failed to save wifi settings");
  598. }
  599. }
  600. // switch to the settings view
  601. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
  602. }
  603. static void text_updated_dictionary(void *context)
  604. {
  605. FlipLibraryApp *app = (FlipLibraryApp *)context;
  606. if (!app)
  607. {
  608. FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
  609. return;
  610. }
  611. // store the entered text
  612. strncpy(app->uart_text_input_buffer_dictionary, app->uart_text_input_temp_buffer_dictionary, app->uart_text_input_buffer_size_dictionary);
  613. // Ensure null-termination
  614. app->uart_text_input_buffer_dictionary[app->uart_text_input_buffer_size_dictionary - 1] = '\0';
  615. // switch to the dictionary view
  616. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewDictionaryRun);
  617. }
  618. static uint32_t callback_to_submenu(void *context)
  619. {
  620. if (!context)
  621. {
  622. FURI_LOG_E(TAG, "Context is NULL");
  623. return VIEW_NONE;
  624. }
  625. UNUSED(context);
  626. random_facts_index = 0;
  627. sent_random_fact_request = false;
  628. random_fact_request_success = false;
  629. random_fact_request_success_all = false;
  630. random_fact = NULL;
  631. return FlipLibraryViewSubmenuMain;
  632. }
  633. static uint32_t callback_to_wifi_settings(void *context)
  634. {
  635. if (!context)
  636. {
  637. FURI_LOG_E(TAG, "Context is NULL");
  638. return VIEW_NONE;
  639. }
  640. UNUSED(context);
  641. return FlipLibraryViewSettings;
  642. }
  643. static uint32_t callback_to_random_facts(void *context)
  644. {
  645. if (!context)
  646. {
  647. FURI_LOG_E(TAG, "Context is NULL");
  648. return VIEW_NONE;
  649. }
  650. UNUSED(context);
  651. return FlipLibraryViewRandomFacts;
  652. }
  653. static void settings_item_selected(void *context, uint32_t index)
  654. {
  655. FlipLibraryApp *app = (FlipLibraryApp *)context;
  656. if (!app)
  657. {
  658. FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
  659. return;
  660. }
  661. switch (index)
  662. {
  663. case 0: // Input SSID
  664. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputSSID);
  665. break;
  666. case 1: // Input Password
  667. view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputPassword);
  668. break;
  669. default:
  670. FURI_LOG_E(TAG, "Unknown configuration item index");
  671. break;
  672. }
  673. }
  674. /**
  675. * @brief Navigation callback for exiting the application
  676. * @param context The context - unused
  677. * @return next view id (VIEW_NONE to exit the app)
  678. */
  679. static uint32_t callback_exit_app(void *context)
  680. {
  681. // Exit the application
  682. if (!context)
  683. {
  684. FURI_LOG_E(TAG, "Context is NULL");
  685. return VIEW_NONE;
  686. }
  687. UNUSED(context);
  688. return VIEW_NONE; // Return VIEW_NONE to exit the app
  689. }
  690. #endif // FLIP_LIBRARY_CALLBACK_H