Kaynağa Gözat

Init/Deinit FlipperHTTP when needed

jblanked 1 yıl önce
ebeveyn
işleme
d34a2a23e5

+ 0 - 7
alloc/flip_social_alloc.c

@@ -8,13 +8,6 @@ FlipSocialApp *flip_social_app_alloc()
     // Initialize gui
     Gui *gui = furi_record_open(RECORD_GUI);
 
-    // Initialize UART
-    if (!flipper_http_init(flipper_http_rx_callback, app))
-    {
-        FURI_LOG_E(TAG, "Failed to initialize UART");
-        return NULL;
-    }
-
     // Allocate ViewDispatcher
     if (!easy_flipper_set_view_dispatcher(&app->view_dispatcher, gui, app))
     {

+ 2 - 0
app.c

@@ -25,6 +25,8 @@ int32_t main_flip_social(void *p)
     // initialize the http
     if (flipper_http_init(flipper_http_rx_callback, app_instance))
     {
+        fhttp.state = INACTIVE; // set inactive for the ping
+
         if (!flipper_http_ping())
         {
             FURI_LOG_E(TAG, "Failed to ping the device");

+ 150 - 48
callback/flip_social_callback.c

@@ -166,7 +166,11 @@ static bool flip_social_login_fetch(DataLoaderModel *model)
     {
         return false;
     }
-
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
+    {
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+        return false;
+    }
     char buffer[256];
     snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->login_username_logged_out, app_instance->login_password_logged_out);
     auth_headers_alloc();
@@ -188,6 +192,7 @@ static char *flip_social_login_parse(DataLoaderModel *model)
     // read response
     if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
     {
+        flipper_http_deinit();
         app_instance->is_logged_in = "true";
 
         // set the logged_in_username and change_password_logged_in
@@ -208,10 +213,12 @@ static char *flip_social_login_parse(DataLoaderModel *model)
     }
     else if (strstr(fhttp.last_response, "User not found") != NULL)
     {
+        flipper_http_deinit();
         return "Account not found...";
     }
     else
     {
+        flipper_http_deinit();
         return "Failed to login...";
     }
 }
@@ -237,7 +244,11 @@ static bool flip_social_register_fetch(DataLoaderModel *model)
         FURI_LOG_E(TAG, "Passwords do not match");
         return false;
     }
-
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
+    {
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+        return false;
+    }
     char buffer[128];
     snprintf(buffer, sizeof(buffer), "{\"username\":\"%s\",\"password\":\"%s\"}", app_instance->register_username_logged_out, app_instance->register_password_logged_out);
 
@@ -260,6 +271,7 @@ static char *flip_social_register_parse(DataLoaderModel *model)
     // read response
     if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
     {
+        flipper_http_deinit();
         // set the login credentials
         if (app_instance->login_username_logged_out)
         {
@@ -289,14 +301,17 @@ static char *flip_social_register_parse(DataLoaderModel *model)
     }
     else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
     {
+        flipper_http_deinit();
         return "Please enter your credentials.\nPress BACK to return.";
     }
     else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
     {
+        flipper_http_deinit();
         return "Registration failed...\nUsername already exists.\nPress BACK to return.";
     }
     else
     {
+        flipper_http_deinit();
         return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
     }
 }
@@ -552,23 +567,31 @@ static void explore_dialog_callback(DialogExResult result, void *context)
     FlipSocialApp *app = (FlipSocialApp *)context;
     if (result == DialogExResultLeft) // Remove
     {
-        // remove friend
-        char remove_payload[128];
-        snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
-        auth_headers_alloc();
-        flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
-        flip_social_free_explore_dialog();
+        if (flipper_http_init(flipper_http_rx_callback, app_instance))
+        {
+            // remove friend
+            char remove_payload[128];
+            snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
+            auth_headers_alloc();
+            flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
+            view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
+            flip_social_free_explore_dialog();
+            flipper_http_deinit();
+        }
     }
     else if (result == DialogExResultRight)
     {
-        // add friend
-        char add_payload[128];
-        snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
-        auth_headers_alloc();
-        flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/add-friend/", auth_headers, add_payload);
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
-        flip_social_free_explore_dialog();
+        if (flipper_http_init(flipper_http_rx_callback, app_instance))
+        {
+            // add friend
+            char add_payload[128];
+            snprintf(add_payload, sizeof(add_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_explore->usernames[flip_social_explore->index]);
+            auth_headers_alloc();
+            flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/add-friend/", auth_headers, add_payload);
+            view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
+            flip_social_free_explore_dialog();
+            flipper_http_deinit();
+        }
     }
 }
 static void friends_dialog_callback(DialogExResult result, void *context)
@@ -577,13 +600,17 @@ static void friends_dialog_callback(DialogExResult result, void *context)
     FlipSocialApp *app = (FlipSocialApp *)context;
     if (result == DialogExResultLeft) // Remove
     {
-        // remove friend
-        char remove_payload[128];
-        snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
-        auth_headers_alloc();
-        flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
-        flip_social_free_friends_dialog();
+        if (flipper_http_init(flipper_http_rx_callback, app_instance))
+        {
+            // remove friend
+            char remove_payload[128];
+            snprintf(remove_payload, sizeof(remove_payload), "{\"username\":\"%s\",\"friend\":\"%s\"}", app_instance->login_username_logged_in, flip_social_friends->usernames[flip_social_friends->index]);
+            auth_headers_alloc();
+            flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/remove-friend/", auth_headers, remove_payload);
+            view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
+            flip_social_free_friends_dialog();
+            flipper_http_deinit();
+        }
     }
 }
 static void messages_dialog_callback(DialogExResult result, void *context)
@@ -704,11 +731,17 @@ static void compose_dialog_callback(DialogExResult result, void *context)
     {
         // post the message
         // send selected_message
+        if (!flipper_http_init(flipper_http_rx_callback, app_instance))
+        {
+            FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+            return;
+        }
         if (selected_message && app_instance->login_username_logged_in)
         {
             if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
             {
                 FURI_LOG_E(TAG, "Message is too long");
+                flipper_http_deinit();
                 return;
             }
             // Send the selected_message
@@ -721,7 +754,7 @@ static void compose_dialog_callback(DialogExResult result, void *context)
                     command))
             {
                 FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
-                fhttp.state = ISSUE;
+                flipper_http_deinit();
                 return;
             }
 
@@ -731,18 +764,22 @@ static void compose_dialog_callback(DialogExResult result, void *context)
         else
         {
             FURI_LOG_E(TAG, "Message or username is NULL");
+            flipper_http_deinit();
             return;
         }
         while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
         {
             furi_delay_ms(100);
         }
-        if (flip_social_load_initial_feed())
+        if (flip_social_load_initial_feed(false))
         {
             flip_social_free_compose_dialog();
         }
-        FURI_LOG_E(TAG, "Failed to load initial feed");
-        return;
+        else
+        {
+            FURI_LOG_E(TAG, "Failed to load the initial feed");
+            flipper_http_deinit();
+        }
     }
 }
 static void feed_dialog_callback(DialogExResult result, void *context)
@@ -822,6 +859,12 @@ static void feed_dialog_callback(DialogExResult result, void *context)
             FURI_LOG_E(TAG, "Username is NULL");
             return;
         }
+        if (!flipper_http_init(flipper_http_rx_callback, app_instance))
+        {
+            FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+            return;
+        }
+        auth_headers_alloc();
         char payload[256];
         snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"post_id\":\"%u\"}", app_instance->login_username_logged_in, flip_feed_item->id);
         if (flipper_http_post_request_with_headers("https://www.flipsocial.net/api/feed/flip/", auth_headers, payload))
@@ -847,8 +890,8 @@ static void feed_dialog_callback(DialogExResult result, void *context)
         {
             FURI_LOG_E(TAG, "Failed to allocate feed dialog");
             fhttp.state = ISSUE;
-            return;
         }
+        flipper_http_deinit();
     }
 }
 
@@ -980,6 +1023,12 @@ bool feed_dialog_alloc()
 }
 static bool flip_social_get_user_info()
 {
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
+    {
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+        fhttp.state = ISSUE;
+        return false;
+    }
     char url[256];
     snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/users/%s/extended/", flip_social_explore->usernames[flip_social_explore->index]);
     if (!flipper_http_get_request_with_headers(url, auth_headers))
@@ -1005,6 +1054,7 @@ static bool flip_social_parse_user_info()
     }
     char *bio = get_json_value("bio", fhttp.last_response, 32);
     char *friends = get_json_value("friends", fhttp.last_response, 32);
+    bool parse_success = false;
     if (bio && friends)
     {
         if (strlen(bio) != 0)
@@ -1016,9 +1066,11 @@ static bool flip_social_parse_user_info()
             snprintf(app_instance->explore_user_bio, MAX_MESSAGE_LENGTH, "%s friends", friends);
         }
         free(bio);
-        return true;
+        free(friends);
+        parse_success = true;
     }
-    return false;
+    flipper_http_deinit();
+    return parse_success;
 }
 
 /**
@@ -1074,7 +1126,7 @@ void flip_social_callback_submenu_choices(void *context, uint32_t index)
         break;
     case FlipSocialSubmenuLoggedInIndexFeed:
         free_flip_social_group();
-        if (!flip_social_load_initial_feed())
+        if (!flip_social_load_initial_feed(true))
         {
             FURI_LOG_E(TAG, "Failed to load the initial feed");
             return;
@@ -1832,14 +1884,17 @@ void flip_social_logged_in_profile_change_password_updated(void *context)
     }
 
     // send post request to change password
-    auth_headers_alloc();
-    char payload[256];
-    snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
-    if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
+    if (flipper_http_init(flipper_http_rx_callback, app))
     {
-        FURI_LOG_E(TAG, "Failed to send post request to change password");
-        FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-        return;
+        auth_headers_alloc();
+        char payload[256];
+        snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"old_password\":\"%s\",\"new_password\":\"%s\"}", app->login_username_logged_out, old_password, app->change_password_logged_in);
+        if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-password/", auth_headers, payload))
+        {
+            FURI_LOG_E(TAG, "Failed to send post request to change password");
+            FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
+        }
+        flipper_http_deinit();
     }
     // Save the settings
     save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
@@ -1869,14 +1924,17 @@ void flip_social_logged_in_profile_change_bio_updated(void *context)
     }
 
     // send post request to change bio
-    auth_headers_alloc();
-    char payload[256];
-    snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
-    if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-bio/", auth_headers, payload))
+    if (flipper_http_init(flipper_http_rx_callback, app))
     {
-        FURI_LOG_E(TAG, "Failed to send post request to change bio");
-        FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-        return;
+        auth_headers_alloc();
+        char payload[256];
+        snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
+        if (!flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/change-bio/", auth_headers, payload))
+        {
+            FURI_LOG_E(TAG, "Failed to send post request to change bio");
+            FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
+        }
+        flipper_http_deinit();
     }
     // Save the settings
     save_settings(app_instance->wifi_ssid_logged_out, app_instance->wifi_password_logged_out, app_instance->login_username_logged_out, app_instance->login_username_logged_in, app_instance->login_password_logged_out, app_instance->change_password_logged_in, app_instance->change_bio_logged_in, app_instance->is_logged_in);
@@ -1884,6 +1942,34 @@ void flip_social_logged_in_profile_change_bio_updated(void *context)
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInProfile);
 }
 
+static bool flip_social_fetch_friends(DataLoaderModel *model)
+{
+    UNUSED(model);
+    if (!flip_social_get_friends())
+    {
+        FURI_LOG_E(TAG, "Failed to get friends");
+        return false;
+    }
+    return true;
+}
+static char *flip_social_parse_friends(DataLoaderModel *model)
+{
+    UNUSED(model);
+    if (!flip_social_parse_json_friends())
+    {
+        FURI_LOG_E(TAG, "Failed to parse friends");
+        flipper_http_deinit(); // deinit the HTTP
+        return "Failed to parse friends";
+    }
+    view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInFriendsSubmenu);
+    flipper_http_deinit(); // deinit the HTTP
+    return "Friends loaded";
+}
+static void flip_social_friends_switch_to_view(FlipSocialApp *app)
+{
+    flip_social_generic_switch_to_view(app, "Friends", flip_social_fetch_friends, flip_social_parse_friends, 1, flip_social_callback_to_profile_logged_in, FlipSocialViewLoader);
+}
+
 /**
  * @brief Callback when a user selects a menu item in the profile (logged in) screen.
  * @param context The context - FlipSocialApp object.
@@ -1920,7 +2006,12 @@ void flip_social_text_input_logged_in_profile_item_selected(void *context, uint3
                 return;
             }
         }
-        flipper_http_loading_task(flip_social_get_friends, flip_social_parse_json_friends, FlipSocialViewLoggedInFriendsSubmenu, FlipSocialViewLoggedInProfile, &app->view_dispatcher);
+        if (!flipper_http_init(flipper_http_rx_callback, app))
+        {
+            FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+            return;
+        }
+        flip_social_friends_switch_to_view(app);
         break;
     default:
         FURI_LOG_E(TAG, "Unknown configuration item index");
@@ -1988,6 +2079,11 @@ void flip_social_logged_in_messages_user_choice_message_updated(void *context)
     app->message_user_choice_logged_in[app->message_user_choice_logged_in_temp_buffer_size - 1] = '\0';
 
     // send post request to send message
+    if (!flipper_http_init(flipper_http_rx_callback, app))
+    {
+        FURI_LOG_E(TAG, "Failed to initialize HTTP");
+        return;
+    }
     auth_headers_alloc();
     char url[128];
     char payload[256];
@@ -2009,10 +2105,10 @@ void flip_social_logged_in_messages_user_choice_message_updated(void *context)
     while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
     {
         // Wait for the request to be received
-        furi_delay_ms(10);
+        furi_delay_ms(100);
     }
     furi_timer_stop(fhttp.get_timeout_timer);
-
+    flipper_http_deinit();
     // add user to the message list
     strncpy(flip_social_message_users->usernames[flip_social_message_users->count], flip_social_explore->usernames[flip_social_explore->index], strlen(flip_social_explore->usernames[flip_social_explore->index]));
     flip_social_message_users->count++;
@@ -2051,6 +2147,11 @@ void flip_social_logged_in_messages_new_message_updated(void *context)
     app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
 
     // send post request to send message
+    if (!flipper_http_init(flipper_http_rx_callback, app))
+    {
+        FURI_LOG_E(TAG, "Failed to initialize HTTP");
+        return;
+    }
     auth_headers_alloc();
     char url[128];
     char payload[256];
@@ -2075,6 +2176,7 @@ void flip_social_logged_in_messages_new_message_updated(void *context)
         furi_delay_ms(10);
     }
     furi_timer_stop(fhttp.get_timeout_timer);
+    flipper_http_deinit();
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
 }
 

+ 7 - 4
explore/flip_social_explore.c

@@ -61,9 +61,9 @@ void flip_social_free_explore(void)
 // as the feed is upgraded, then we can port more to the explore view
 bool flip_social_get_explore(void)
 {
-    if (fhttp.state == INACTIVE)
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
     {
-        FURI_LOG_E(TAG, "HTTP state is INACTIVE");
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
         return false;
     }
     char *keyword = !app_instance->explore_logged_in || strlen(app_instance->explore_logged_in) == 0 ? "a" : app_instance->explore_logged_in;
@@ -88,9 +88,9 @@ bool flip_social_get_explore(void)
 }
 bool flip_social_get_explore_2(void)
 {
-    if (fhttp.state == INACTIVE)
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
     {
-        FURI_LOG_E(TAG, "HTTP state is INACTIVE");
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
         return false;
     }
     char *keyword = !app_instance->message_users_logged_in || strlen(app_instance->message_users_logged_in) == 0 ? "a" : app_instance->message_users_logged_in;
@@ -123,6 +123,9 @@ bool flip_social_parse_json_explore()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return false;
     }
+
+    flipper_http_deinit();
+
     char *data_cstr = (char *)furi_string_get_cstr(user_data);
     if (data_cstr == NULL)
     {

+ 6 - 8
feed/flip_social_feed.c

@@ -1,15 +1,15 @@
 #include "flip_social_feed.h"
 
-bool flip_social_get_feed()
+bool flip_social_get_feed(bool alloc_http)
 {
     if (!app_instance)
     {
         FURI_LOG_E(TAG, "FlipSocialApp is NULL");
         return false;
     }
-    if (fhttp.state == INACTIVE)
+    if (alloc_http && !flipper_http_init(flipper_http_rx_callback, app_instance))
     {
-        FURI_LOG_E(TAG, "HTTP state is INACTIVE");
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
         return false;
     }
     // Get the feed from the server
@@ -46,6 +46,7 @@ FlipSocialFeedMini *flip_social_parse_json_feed()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return NULL;
     }
+    flipper_http_deinit();
     char *data_cstr = (char *)furi_string_get_cstr(feed_data);
     if (data_cstr == NULL)
     {
@@ -208,7 +209,7 @@ bool flip_social_load_feed_post(int post_id)
     return true;
 }
 
-bool flip_social_load_initial_feed()
+bool flip_social_load_initial_feed(bool alloc_http)
 {
     if (fhttp.state == INACTIVE)
     {
@@ -221,7 +222,7 @@ bool flip_social_load_initial_feed()
         return false;
     }
     view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoading);
-    if (flip_social_get_feed()) // start the async request
+    if (flip_social_get_feed(alloc_http)) // start the async request
     {
         furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
         fhttp.state = RECEIVING;
@@ -247,7 +248,6 @@ bool flip_social_load_initial_feed()
     if (!flip_feed_info || flip_feed_info->count < 1)
     {
         FURI_LOG_E(TAG, "Failed to parse JSON feed");
-        fhttp.state = ISSUE;
         view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
         view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
         loading_free(app_instance->loading);
@@ -258,7 +258,6 @@ bool flip_social_load_initial_feed()
     if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
     {
         FURI_LOG_E(TAG, "Failed to load feed post");
-        fhttp.state = ISSUE;
         view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
         view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
         loading_free(app_instance->loading);
@@ -267,7 +266,6 @@ bool flip_social_load_initial_feed()
     if (!feed_dialog_alloc())
     {
         FURI_LOG_E(TAG, "Failed to allocate feed dialog");
-        fhttp.state = ISSUE;
         view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
         view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoading);
         loading_free(app_instance->loading);

+ 2 - 2
feed/flip_social_feed.h

@@ -4,8 +4,8 @@
 #include <callback/flip_social_callback.h>
 #include <flip_storage/flip_social_storage.h>
 
-bool flip_social_get_feed();
+bool flip_social_get_feed(bool alloc_http);
 bool flip_social_load_feed_post(int post_id);
-bool flip_social_load_initial_feed();
+bool flip_social_load_initial_feed(bool alloc_http);
 FlipSocialFeedMini *flip_social_parse_json_feed();
 #endif

+ 0 - 3
flip_social.c

@@ -264,9 +264,6 @@ void flip_social_app_free(FlipSocialApp *app)
     if (app->explore_user_bio)
         free(app->explore_user_bio);
 
-    // DeInit UART
-    flipper_http_deinit();
-
     // Free the app structure
     if (app_instance)
         free(app_instance);

+ 1 - 0
flipper_http/flipper_http.c

@@ -379,6 +379,7 @@ bool flipper_http_init(FlipperHTTP_Callback callback, void *context)
     }
 
     // FURI_LOG_I(HTTP_TAG, "UART initialized successfully.");
+    fhttp.state = IDLE; // set idle for easy use
     return true;
 }
 

+ 11 - 65
friends/flip_social_friends.c

@@ -28,11 +28,6 @@ bool flip_social_get_friends()
         FURI_LOG_E(TAG, "App instance is NULL");
         return false;
     }
-    if (fhttp.state == INACTIVE)
-    {
-        FURI_LOG_E(TAG, "HTTP state is INACTIVE");
-        return false;
-    }
     // will return true unless the devboard is not connected
     char url[100];
     snprintf(
@@ -84,13 +79,6 @@ bool flip_social_parse_json_friends()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return false;
     }
-    char *data_cstr = (char *)furi_string_get_cstr(friend_data);
-    if (data_cstr == NULL)
-    {
-        FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
-        furi_string_free(friend_data);
-        return false;
-    }
 
     // Allocate memory for each username only if not already allocated
     flip_social_friends = flip_social_friends_alloc();
@@ -98,17 +86,9 @@ bool flip_social_parse_json_friends()
     {
         FURI_LOG_E(TAG, "Failed to allocate memory for friends usernames.");
         furi_string_free(friend_data);
-        free(data_cstr);
         return false;
     }
 
-    // Remove newlines
-    char *pos = data_cstr;
-    while ((pos = strchr(pos, '\n')) != NULL)
-    {
-        *pos = ' ';
-    }
-
     // Initialize friends count
     flip_social_friends->count = 0;
 
@@ -117,54 +97,20 @@ bool flip_social_parse_json_friends()
     submenu_set_header(app_instance->submenu_friends, "Friends");
 
     // Extract the users array from the JSON
-    char *json_users = get_json_value("friends", data_cstr, 128);
-    if (json_users == NULL)
-    {
-        FURI_LOG_E(TAG, "Failed to parse friends array.");
-        furi_string_free(friend_data);
-        free(data_cstr);
-        return false;
-    }
-
-    // Manual tokenization for comma-separated values
-    char *start = json_users + 1; // Skip the opening bracket
-    char *end;
-    while ((end = strchr(start, ',')) != NULL && flip_social_friends->count < MAX_FRIENDS)
+    for (int i = 0; i < MAX_FRIENDS; i++)
     {
-        *end = '\0'; // Null-terminate the current token
-
-        // Remove quotes
-        if (*start == '"')
-            start++;
-        if (*(end - 1) == '"')
-            *(end - 1) = '\0';
-
-        // Copy username to pre-allocated memory
-        snprintf(flip_social_friends->usernames[flip_social_friends->count], MAX_USER_LENGTH, "%s", start);
-        submenu_add_item(app_instance->submenu_friends, flip_social_friends->usernames[flip_social_friends->count], FlipSocialSubmenuLoggedInIndexFriendsStart + flip_social_friends->count, flip_social_callback_submenu_choices, app_instance);
-        flip_social_friends->count++;
-        start = end + 1;
-    }
-
-    // Handle the last token
-    if (*start != '\0' && flip_social_friends->count < MAX_FRIENDS)
-    {
-        if (*start == '"')
-            start++;
-        if (*(start + strlen(start) - 1) == ']')
-            *(start + strlen(start) - 1) = '\0';
-        if (*(start + strlen(start) - 1) == '"')
-            *(start + strlen(start) - 1) = '\0';
-
-        snprintf(flip_social_friends->usernames[flip_social_friends->count], MAX_USER_LENGTH, "%s", start);
-        submenu_add_item(app_instance->submenu_friends, flip_social_friends->usernames[flip_social_friends->count], FlipSocialSubmenuLoggedInIndexFriendsStart + flip_social_friends->count, flip_social_callback_submenu_choices, app_instance);
+        char *friend = get_json_array_value("friends", i, (char *)furi_string_get_cstr(friend_data), 256);
+        if (friend == NULL)
+        {
+            FURI_LOG_E(TAG, "Failed to parse friend %d.", i);
+            furi_string_free(friend_data);
+            break;
+        }
+        snprintf(flip_social_friends->usernames[i], MAX_USER_LENGTH, "%s", friend);
+        submenu_add_item(app_instance->submenu_friends, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, flip_social_callback_submenu_choices, app_instance);
         flip_social_friends->count++;
+        free(friend);
     }
-
     furi_string_free(friend_data);
-    free(data_cstr);
-    free(json_users);
-    free(start);
-    free(end);
     return true;
 }

+ 9 - 4
messages/flip_social_messages.c

@@ -141,9 +141,9 @@ bool flip_social_get_message_users()
         FURI_LOG_E(TAG, "Username is NULL");
         return false;
     }
-    if (fhttp.state == INACTIVE)
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
     {
-        FURI_LOG_E(TAG, "HTTP state is INACTIVE");
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
         return false;
     }
     char command[128];
@@ -168,9 +168,9 @@ bool flip_social_get_message_users()
 // Get all the messages between the logged in user and the selected user
 bool flip_social_get_messages_with_user()
 {
-    if (fhttp.state == INACTIVE)
+    if (!flipper_http_init(flipper_http_rx_callback, app_instance))
     {
-        FURI_LOG_E(TAG, "HTTP state is INACTIVE");
+        FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
         return false;
     }
     if (app_instance->login_username_logged_out == NULL)
@@ -213,6 +213,7 @@ bool flip_social_parse_json_message_users()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return false;
     }
+    flipper_http_deinit();
     char *data_cstr = (char *)furi_string_get_cstr(message_data);
     if (data_cstr == NULL)
     {
@@ -299,6 +300,9 @@ bool flip_social_parse_json_message_user_choices()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return false;
     }
+
+    flipper_http_deinit();
+
     char *data_cstr = (char *)furi_string_get_cstr(user_data);
     if (data_cstr == NULL)
     {
@@ -385,6 +389,7 @@ bool flip_social_parse_json_messages()
         FURI_LOG_E(TAG, "Failed to load received data from file.");
         return false;
     }
+    flipper_http_deinit();
     char *data_cstr = (char *)furi_string_get_cstr(message_data);
     if (data_cstr == NULL)
     {