| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819 |
- #ifndef FLIP_LIBRARY_CALLBACK_H
- #define FLIP_LIBRARY_CALLBACK_H
- static uint32_t random_facts_index = 0;
- static bool sent_random_fact_request = false;
- static bool random_fact_request_success = false;
- static bool random_fact_request_success_all = false;
- char* random_fact = NULL;
- static FlipLibraryApp* app_instance = NULL;
- #define MAX_TOKENS 512 // Adjust based on expected JSON size
- // Parse JSON to find the "text" key
- char* flip_library_parse_random_fact() {
- return get_json_value("text", fhttp.last_response, 128);
- }
- char* flip_library_parse_cat_fact() {
- return get_json_value("fact", fhttp.last_response, 128);
- }
- char* flip_library_parse_dog_fact() {
- return get_json_array_value("facts", 0, fhttp.last_response, 128);
- }
- char* flip_library_parse_quote() {
- // remove [ and ] from the start and end of the string
- char* response = fhttp.last_response;
- if(response[0] == '[') {
- response++;
- }
- if(response[strlen(response) - 1] == ']') {
- response[strlen(response) - 1] = '\0';
- }
- // remove white space from both sides
- while(response[0] == ' ') {
- response++;
- }
- while(response[strlen(response) - 1] == ' ') {
- response[strlen(response) - 1] = '\0';
- }
- return get_json_value("q", response, 128);
- }
- char* flip_library_parse_dictionary() {
- return get_json_value("definition", fhttp.last_response, 16);
- }
- static void flip_library_request_error(Canvas* canvas) {
- if(fhttp.last_response == NULL) {
- if(fhttp.last_response != NULL) {
- if(strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") !=
- NULL) {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- } else if(strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL) {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- } else {
- canvas_clear(canvas);
- FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
- canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
- }
- } else {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
- canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- } else {
- canvas_clear(canvas);
- canvas_draw_str(canvas, 0, 10, "Failed to receive data.");
- canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
- }
- }
- static void flip_library_draw_fact(char* message, Widget** widget) {
- if(app_instance == NULL) {
- FURI_LOG_E(TAG, "App instance is NULL");
- return;
- }
- widget_reset(*widget);
- uint32_t fact_length = strlen(message); // Length of the message
- uint32_t i = 0; // Index tracker
- uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
- char* formatted_message; // Buffer to hold the final formatted message
- if(!easy_flipper_set_buffer(&formatted_message, fact_length * 2 + 1)) {
- return;
- }
- while(i < fact_length) {
- uint32_t max_line_length = 29; // Maximum characters per line
- uint32_t remaining_length = fact_length - i; // Remaining characters
- uint32_t line_length = (remaining_length < max_line_length) ? remaining_length :
- max_line_length;
- // Temporary buffer to hold the current line
- char fact_line[30];
- strncpy(fact_line, message + i, line_length);
- fact_line[line_length] = '\0';
- // Check if the line ends in the middle of a word and adjust accordingly
- if(line_length == 29 && message[i + line_length] != '\0' &&
- message[i + line_length] != ' ') {
- // Find the last space within the 30-character segment
- char* last_space = strrchr(fact_line, ' ');
- if(last_space != NULL) {
- // Adjust the line length to avoid cutting the word
- line_length = last_space - fact_line;
- fact_line[line_length] = '\0'; // Null-terminate at the space
- }
- }
- // Manually copy the fixed line into the formatted_message buffer
- for(uint32_t j = 0; j < line_length; j++) {
- formatted_message[formatted_index++] = fact_line[j];
- }
- // Add a newline character for line spacing
- formatted_message[formatted_index++] = '\n';
- // Move i forward to the start of the next word
- i += line_length;
- // Skip spaces at the beginning of the next line
- while(message[i] == ' ') {
- i++;
- }
- }
- // Add the formatted message to the widget
- widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
- }
- // Callback for drawing the main screen
- static void view_draw_callback_random_facts(Canvas* canvas, void* model) {
- if(!canvas || !app_instance) {
- return;
- }
- UNUSED(model);
- canvas_set_font(canvas, FontSecondary);
- if(fhttp.state == INACTIVE) {
- canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
- canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
- canvas_draw_str(canvas, 0, 32, "If your board is connected,");
- canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
- canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
- canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
- return;
- }
- // Cats
- if(random_facts_index == FlipLibrarySubmenuIndexRandomFactsCats) {
- canvas_draw_str(canvas, 0, 7, "Random Cat Fact");
- canvas_draw_str(canvas, 0, 15, "Loading...");
- if(!sent_random_fact_request) {
- sent_random_fact_request = true;
- random_fact_request_success = flipper_http_get_request_with_headers(
- "https://catfact.ninja/fact", "{\"Content-Type\":\"application/json\"}");
- if(!random_fact_request_success) {
- FURI_LOG_E(TAG, "Failed to send request");
- flip_library_request_error(canvas);
- return;
- }
- fhttp.state = RECEIVING;
- } else {
- if(fhttp.state == RECEIVING) {
- canvas_draw_str(canvas, 0, 22, "Receiving...");
- return;
- }
- // check status
- else if(fhttp.state == ISSUE || !random_fact_request_success) {
- flip_library_request_error(canvas);
- } else if(
- fhttp.state == IDLE && fhttp.last_response != NULL &&
- !random_fact_request_success_all) {
- canvas_draw_str(canvas, 0, 22, "Processing...");
- // success
- // check status
- // unnecessary check
- if(fhttp.state == ISSUE || fhttp.last_response == NULL) {
- flip_library_request_error(canvas);
- FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
- return;
- } else if(!random_fact_request_success_all) {
- random_fact = flip_library_parse_cat_fact();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- // Mark success
- random_fact_request_success_all = true;
- // draw random facts
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- }
- }
- // likely redundant but just in case
- else if(fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL) {
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- } else // handle weird scenarios
- {
- // if received data isnt NULL
- if(fhttp.last_response != NULL) {
- // parse json to find the text key
- random_fact = flip_library_parse_cat_fact();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- }
- }
- }
- }
- // Dogs
- else if(random_facts_index == FlipLibrarySubmenuIndexRandomFactsDogs) {
- canvas_draw_str(canvas, 0, 7, "Random Dog Fact");
- canvas_draw_str(canvas, 0, 15, "Loading...");
- if(!sent_random_fact_request) {
- sent_random_fact_request = true;
- random_fact_request_success = flipper_http_get_request_with_headers(
- "https://dog-api.kinduff.com/api/facts",
- "{\"Content-Type\":\"application/json\"}");
- if(!random_fact_request_success) {
- FURI_LOG_E(TAG, "Failed to send request");
- flip_library_request_error(canvas);
- return;
- }
- fhttp.state = RECEIVING;
- } else {
- if(fhttp.state == RECEIVING) {
- canvas_draw_str(canvas, 0, 22, "Receiving...");
- return;
- }
- // check status
- else if(fhttp.state == ISSUE || !random_fact_request_success) {
- flip_library_request_error(canvas);
- } else if(
- fhttp.state == IDLE && fhttp.last_response != NULL &&
- !random_fact_request_success_all) {
- canvas_draw_str(canvas, 0, 22, "Processing...");
- // success
- // check status
- // unnecessary check
- if(fhttp.state == ISSUE || fhttp.last_response == NULL) {
- flip_library_request_error(canvas);
- FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
- return;
- } else if(!random_fact_request_success_all) {
- random_fact = flip_library_parse_dog_fact();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- // Mark success
- random_fact_request_success_all = true;
- // draw random facts
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- }
- }
- // likely redundant but just in case
- else if(fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL) {
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- } else // handle weird scenarios
- {
- // if received data isnt NULL
- if(fhttp.last_response != NULL) {
- // parse json to find the text key
- random_fact = flip_library_parse_cat_fact();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- }
- }
- }
- }
- // Quotes
- else if(random_facts_index == FlipLibrarySubmenuIndexRandomFactsQuotes) {
- canvas_draw_str(canvas, 0, 7, "Random Quote");
- canvas_draw_str(canvas, 0, 15, "Loading...");
- if(!sent_random_fact_request) {
- sent_random_fact_request = true;
- random_fact_request_success =
- flipper_http_get_request("https://zenquotes.io/api/random");
- if(!random_fact_request_success) {
- FURI_LOG_E(TAG, "Failed to send request");
- flip_library_request_error(canvas);
- return;
- }
- fhttp.state = RECEIVING;
- } else {
- if(fhttp.state == RECEIVING) {
- canvas_draw_str(canvas, 0, 22, "Receiving...");
- return;
- }
- // check status
- else if(fhttp.state == ISSUE || !random_fact_request_success) {
- flip_library_request_error(canvas);
- } else if(
- fhttp.state == IDLE && fhttp.last_response != NULL &&
- !random_fact_request_success_all) {
- canvas_draw_str(canvas, 0, 22, "Processing...");
- // success
- // check status
- // unnecessary check
- if(fhttp.state == ISSUE || fhttp.last_response == NULL) {
- flip_library_request_error(canvas);
- FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
- return;
- } else if(!random_fact_request_success_all) {
- random_fact = flip_library_parse_quote();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- // Mark success
- random_fact_request_success_all = true;
- // draw random facts
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- }
- }
- // likely redundant but just in case
- else if(fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL) {
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- } else // handle weird scenarios
- {
- // if received data isnt NULL
- if(fhttp.last_response != NULL) {
- // parse json to find the text key
- random_fact = flip_library_parse_quote();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- }
- }
- }
- }
- // All Random Facts
- else if(random_facts_index == FlipLibrarySubmenuIndexRandomFactsAll) {
- canvas_draw_str(canvas, 0, 10, "Random Fact");
- canvas_set_font(canvas, FontSecondary);
- canvas_draw_str(canvas, 0, 20, "Loading...");
- if(!sent_random_fact_request) {
- sent_random_fact_request = true;
- random_fact_request_success =
- flipper_http_get_request("https://uselessfacts.jsph.pl/api/v2/facts/random");
- if(!random_fact_request_success) {
- FURI_LOG_E(TAG, "Failed to send request");
- return;
- }
- fhttp.state = RECEIVING;
- } else {
- // check status
- if(fhttp.state == RECEIVING) {
- canvas_draw_str(canvas, 0, 30, "Receiving...");
- return;
- }
- // check status
- else if(fhttp.state == ISSUE || !random_fact_request_success) {
- flip_library_request_error(canvas);
- return;
- } else if(
- fhttp.state == IDLE && fhttp.last_response != NULL &&
- !random_fact_request_success_all) {
- canvas_draw_str(canvas, 0, 30, "Processing...");
- // success
- // check status
- if(fhttp.state == ISSUE || fhttp.last_response == NULL) {
- flip_library_request_error(canvas);
- FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
- return;
- }
- // parse json to find the text key
- random_fact = flip_library_parse_random_fact();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- // Mark success
- random_fact_request_success_all = true;
- // draw random facts
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- }
- // likely redundant but just in case
- else if(fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL) {
- // draw random facts
- flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
- } else // handle weird scenarios
- {
- // if received data isnt NULL
- if(fhttp.last_response != NULL) {
- // parse json to find the text key
- random_fact = flip_library_parse_random_fact();
- if(random_fact == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- }
- }
- }
- } else {
- canvas_draw_str(canvas, 0, 7, "Random Fact");
- }
- }
- static void view_draw_callback_dictionary_run(Canvas* canvas, void* model) {
- if(!canvas || !app_instance || app_instance->uart_text_input_buffer_dictionary == NULL) {
- return;
- }
- UNUSED(model);
- canvas_set_font(canvas, FontSecondary);
- if(fhttp.state == INACTIVE) {
- canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
- canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
- canvas_draw_str(canvas, 0, 32, "If your board is connected,");
- canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
- canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
- canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
- return;
- }
- canvas_draw_str(canvas, 0, 10, "Defining, please wait...");
- if(!sent_random_fact_request) {
- sent_random_fact_request = true;
- char payload[128];
- snprintf(
- payload,
- sizeof(payload),
- "{\"word\":\"%s\"}",
- app_instance->uart_text_input_buffer_dictionary);
- random_fact_request_success = flipper_http_post_request_with_headers(
- "https://www.flipsocial.net/api/define/",
- "{\"Content-Type\":\"application/json\"}",
- payload);
- if(!random_fact_request_success) {
- FURI_LOG_E(TAG, "Failed to send request");
- return;
- }
- fhttp.state = RECEIVING;
- } else {
- // check status
- if(fhttp.state == RECEIVING) {
- canvas_draw_str(canvas, 0, 20, "Receiving...");
- return;
- }
- // check status
- else if(fhttp.state == ISSUE || !random_fact_request_success) {
- flip_library_request_error(canvas);
- return;
- } else if(
- fhttp.state == IDLE && fhttp.last_response != NULL &&
- !random_fact_request_success_all) {
- canvas_draw_str(canvas, 0, 20, "Processing...");
- // success
- // check status
- if(fhttp.state == ISSUE || fhttp.last_response == NULL) {
- flip_library_request_error(canvas);
- FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
- return;
- }
- // parse json to find the text key
- char* definition = flip_library_parse_dictionary();
- if(definition == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- // Mark success
- random_fact_request_success_all = true;
- // draw random facts
- flip_library_draw_fact(definition, &app_instance->widget_dictionary);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewDictionaryWidget);
- }
- // likely redundant but just in case
- else if(fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL) {
- // draw random facts
- flip_library_draw_fact(random_fact, &app_instance->widget_dictionary);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewDictionaryWidget);
- } else // handle weird scenarios
- {
- // if received data isnt NULL
- if(fhttp.last_response != NULL) {
- // parse json to find the text key
- char* definition = flip_library_parse_dictionary();
- if(definition == NULL) {
- flip_library_request_error(canvas);
- fhttp.state = ISSUE;
- return;
- }
- // draw random facts
- flip_library_draw_fact(definition, &app_instance->widget_dictionary);
- // go to random facts widget
- view_dispatcher_switch_to_view(
- app_instance->view_dispatcher, FlipLibraryViewDictionaryWidget);
- free(definition);
- return;
- }
- }
- }
- }
- // Input callback for the view (async input handling)
- bool view_input_callback_random_facts(InputEvent* event, void* context) {
- if(!event || !context) {
- return false;
- }
- FlipLibraryApp* app = (FlipLibraryApp*)context;
- if(event->type == InputTypePress && event->key == InputKeyBack) {
- // Exit the app when the back button is pressed
- view_dispatcher_stop(app->view_dispatcher);
- return true;
- }
- return false;
- }
- static void callback_submenu_choices(void* context, uint32_t index) {
- FlipLibraryApp* app = (FlipLibraryApp*)context;
- if(!app) {
- FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
- return;
- }
- switch(index) {
- case FlipLibrarySubmenuIndexRandomFacts:
- random_facts_index = 0;
- sent_random_fact_request = false;
- random_fact = NULL;
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFacts);
- break;
- case FlipLibrarySubmenuIndexAbout:
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewAbout);
- break;
- case FlipLibrarySubmenuIndexSettings:
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
- break;
- case FlipLibrarySubmenuIndexDictionary:
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewDictionaryTextInput);
- break;
- case FlipLibrarySubmenuIndexRandomFactsCats:
- random_facts_index = FlipLibrarySubmenuIndexRandomFactsCats;
- sent_random_fact_request = false;
- random_fact = NULL;
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
- break;
- case FlipLibrarySubmenuIndexRandomFactsDogs:
- random_facts_index = FlipLibrarySubmenuIndexRandomFactsDogs;
- sent_random_fact_request = false;
- random_fact = NULL;
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
- break;
- case FlipLibrarySubmenuIndexRandomFactsQuotes:
- random_facts_index = FlipLibrarySubmenuIndexRandomFactsQuotes;
- sent_random_fact_request = false;
- random_fact = NULL;
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
- break;
- case FlipLibrarySubmenuIndexRandomFactsAll:
- random_facts_index = FlipLibrarySubmenuIndexRandomFactsAll;
- sent_random_fact_request = false;
- random_fact = NULL;
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
- break;
- default:
- break;
- }
- }
- static void text_updated_ssid(void* context) {
- FlipLibraryApp* app = (FlipLibraryApp*)context;
- if(!app) {
- FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
- return;
- }
- // store the entered text
- strncpy(
- app->uart_text_input_buffer_ssid,
- app->uart_text_input_temp_buffer_ssid,
- app->uart_text_input_buffer_size_ssid);
- // Ensure null-termination
- app->uart_text_input_buffer_ssid[app->uart_text_input_buffer_size_ssid - 1] = '\0';
- // update the variable item text
- if(app->variable_item_ssid) {
- variable_item_set_current_value_text(
- app->variable_item_ssid, app->uart_text_input_buffer_ssid);
- }
- // save settings
- save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
- // save wifi settings to devboard
- if(strlen(app->uart_text_input_buffer_ssid) > 0 &&
- strlen(app->uart_text_input_buffer_password) > 0) {
- if(!flipper_http_save_wifi(
- app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password)) {
- FURI_LOG_E(TAG, "Failed to save wifi settings");
- }
- }
- // switch to the settings view
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
- }
- static void text_updated_password(void* context) {
- FlipLibraryApp* app = (FlipLibraryApp*)context;
- if(!app) {
- FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
- return;
- }
- // store the entered text
- strncpy(
- app->uart_text_input_buffer_password,
- app->uart_text_input_temp_buffer_password,
- app->uart_text_input_buffer_size_password);
- // Ensure null-termination
- app->uart_text_input_buffer_password[app->uart_text_input_buffer_size_password - 1] = '\0';
- // update the variable item text
- if(app->variable_item_password) {
- variable_item_set_current_value_text(
- app->variable_item_password, app->uart_text_input_buffer_password);
- }
- // save settings
- save_settings(app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password);
- // save wifi settings to devboard
- if(strlen(app->uart_text_input_buffer_ssid) > 0 &&
- strlen(app->uart_text_input_buffer_password) > 0) {
- if(!flipper_http_save_wifi(
- app->uart_text_input_buffer_ssid, app->uart_text_input_buffer_password)) {
- FURI_LOG_E(TAG, "Failed to save wifi settings");
- }
- }
- // switch to the settings view
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewSettings);
- }
- static void text_updated_dictionary(void* context) {
- FlipLibraryApp* app = (FlipLibraryApp*)context;
- if(!app) {
- FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
- return;
- }
- // store the entered text
- strncpy(
- app->uart_text_input_buffer_dictionary,
- app->uart_text_input_temp_buffer_dictionary,
- app->uart_text_input_buffer_size_dictionary);
- // Ensure null-termination
- app->uart_text_input_buffer_dictionary[app->uart_text_input_buffer_size_dictionary - 1] = '\0';
- // switch to the dictionary view
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewDictionaryRun);
- }
- static uint32_t callback_to_submenu(void* context) {
- if(!context) {
- FURI_LOG_E(TAG, "Context is NULL");
- return VIEW_NONE;
- }
- UNUSED(context);
- random_facts_index = 0;
- sent_random_fact_request = false;
- random_fact_request_success = false;
- random_fact_request_success_all = false;
- random_fact = NULL;
- return FlipLibraryViewSubmenuMain;
- }
- static uint32_t callback_to_wifi_settings(void* context) {
- if(!context) {
- FURI_LOG_E(TAG, "Context is NULL");
- return VIEW_NONE;
- }
- UNUSED(context);
- return FlipLibraryViewSettings;
- }
- static uint32_t callback_to_random_facts(void* context) {
- if(!context) {
- FURI_LOG_E(TAG, "Context is NULL");
- return VIEW_NONE;
- }
- UNUSED(context);
- return FlipLibraryViewRandomFacts;
- }
- static void settings_item_selected(void* context, uint32_t index) {
- FlipLibraryApp* app = (FlipLibraryApp*)context;
- if(!app) {
- FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
- return;
- }
- switch(index) {
- case 0: // Input SSID
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputSSID);
- break;
- case 1: // Input Password
- view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewTextInputPassword);
- break;
- default:
- FURI_LOG_E(TAG, "Unknown configuration item index");
- break;
- }
- }
- /**
- * @brief Navigation callback for exiting the application
- * @param context The context - unused
- * @return next view id (VIEW_NONE to exit the app)
- */
- static uint32_t callback_exit_app(void* context) {
- // Exit the application
- if(!context) {
- FURI_LOG_E(TAG, "Context is NULL");
- return VIEW_NONE;
- }
- UNUSED(context);
- return VIEW_NONE; // Return VIEW_NONE to exit the app
- }
- #endif // FLIP_LIBRARY_CALLBACK_H
|