Просмотр исходного кода

use the app's fhttp + make/use new loading_task

jblanked 9 месяцев назад
Родитель
Сommit
c59c154bd8
8 измененных файлов с 398 добавлено и 211 удалено
  1. 33 15
      alloc/alloc.c
  2. 2 1
      alloc/alloc.h
  3. 8 8
      app.c
  4. 297 178
      callback/callback.c
  5. 11 2
      callback/callback.h
  6. 0 5
      feed/feed.c
  7. 45 1
      free/free.c
  8. 2 1
      free/free.h

+ 33 - 15
alloc/alloc.c

@@ -381,7 +381,6 @@ FlipSocialApp *alloc_flip_social_app()
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedOutSubmenu);
         }
     }
-
     return app;
 }
 
@@ -830,19 +829,18 @@ static bool flip_social_feed_input_callback(InputEvent *event, void *context)
 
             save_char("series_index", new_series_index);
 
-            FlipperHTTP *fhttp = flipper_http_alloc();
-            if (!fhttp)
+            free_flipper_http();
+            if (!alloc_flipper_http())
             {
-                FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
                 return false;
             }
-            if (!feed_load_initial_feed(fhttp, flip_feed_info->series_index))
+            if (!feed_load_initial_feed(app_instance->fhttp, flip_feed_info->series_index))
             {
                 FURI_LOG_E(TAG, "Failed to load initial feed");
-                flipper_http_free(fhttp);
+                free_flipper_http();
                 return false;
             }
-            flipper_http_free(fhttp);
+            free_flipper_http();
             // switch view, free dialog, re-alloc dialog, switch back to dialog
             view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewWidgetResult);
             free_feed_view();
@@ -908,16 +906,15 @@ static bool flip_social_feed_input_callback(InputEvent *event, void *context)
             FURI_LOG_E(TAG, "Username is NULL");
             return false;
         }
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (!fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
             return false;
         }
         alloc_headers();
         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_request(fhttp, POST, "https://www.jblanked.com/flipper/api/feed/flip/", auth_headers, payload))
+        if (flipper_http_request(app_instance->fhttp, POST, "https://www.jblanked.com/flipper/api/feed/flip/", auth_headers, payload))
         {
             // save feed item
             char new_save[512];
@@ -928,7 +925,7 @@ static bool flip_social_feed_input_callback(InputEvent *event, void *context)
             if (!flip_social_save_post(id, new_save))
             {
                 FURI_LOG_E(TAG, "Failed to save the feed post");
-                flipper_http_free(fhttp);
+                free_flipper_http();
                 return false;
             }
         }
@@ -939,8 +936,8 @@ static bool flip_social_feed_input_callback(InputEvent *event, void *context)
         if (!feed_load_post(flip_feed_info->ids[flip_feed_info->index]))
         {
             FURI_LOG_E(TAG, "Failed to load nexy feed post");
-            fhttp->state = ISSUE;
-            flipper_http_free(fhttp);
+            app_instance->fhttp->state = ISSUE;
+            free_flipper_http();
             return false;
         }
         if (alloc_feed_view())
@@ -951,7 +948,7 @@ static bool flip_social_feed_input_callback(InputEvent *event, void *context)
         {
             FURI_LOG_E(TAG, "Failed to allocate feed dialog");
         }
-        flipper_http_free(fhttp);
+        free_flipper_http();
     }
     return false;
 }
@@ -1328,3 +1325,24 @@ bool alloc_variable_item_list(uint32_t view_id)
     }
     return false;
 }
+
+bool alloc_flipper_http()
+{
+    if (!app_instance)
+    {
+        FURI_LOG_E(TAG, "App instance is NULL");
+        return false;
+    }
+    if (!app_instance->fhttp)
+    {
+        app_instance->fhttp = flipper_http_alloc();
+        if (!app_instance->fhttp)
+        {
+            FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
+            return false;
+        }
+        return true;
+    }
+    FURI_LOG_I(TAG, "FlipperHTTP already allocated");
+    return false;
+}

+ 2 - 1
alloc/alloc.h

@@ -15,4 +15,5 @@ char *alloc_format_message(const char *user_message);
 bool alloc_text_input(uint32_t view_id);
 bool alloc_about_widget(bool is_logged_in);
 bool alloc_variable_item_list(uint32_t view_id);
-bool alloc_submenu(uint32_t view_id);
+bool alloc_submenu(uint32_t view_id);
+bool alloc_flipper_http();

+ 8 - 8
app.c

@@ -22,23 +22,23 @@ int32_t main_flip_social(void *p)
     }
 
     // check if board is connected (Derek Jamison)
-    FlipperHTTP *fhttp = flipper_http_alloc();
-    if (!fhttp)
+    app_instance->fhttp = flipper_http_alloc();
+    if (!app_instance->fhttp)
     {
         easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
         return -1;
     }
 
-    if (!flipper_http_send_command(fhttp, HTTP_CMD_PING))
+    if (!flipper_http_send_command(app_instance->fhttp, HTTP_CMD_PING))
     {
         FURI_LOG_E(TAG, "Failed to ping the device");
-        flipper_http_free(fhttp);
+        flipper_http_free(app_instance->fhttp);
         return -1;
     }
 
     // Try to wait for pong response.
     uint32_t counter = 10;
-    while (fhttp->state == INACTIVE && --counter > 0)
+    while (app_instance->fhttp->state == INACTIVE && --counter > 0)
     {
         FURI_LOG_D(TAG, "Waiting for PONG");
         furi_delay_ms(100);
@@ -63,16 +63,16 @@ int32_t main_flip_social(void *p)
             strcmp(is_notifications, "on") == 0 &&
             strcmp(is_logged_in, "true") == 0)
         {
-            callback_home_notification();
+            callback_home_notification(app_instance->fhttp);
         }
 
-        // if (update_is_ready(fhttp, true))
+        // if (update_is_ready(app_instance->fhttp, true))
         // {
         //     easy_flipper_dialog("Update Status", "Complete.\nRestart your Flipper Zero.");
         // }
     }
 
-    flipper_http_free(fhttp);
+    flipper_http_free(app_instance->fhttp);
 
     // Run the view dispatcher
     view_dispatcher_run(app_instance->view_dispatcher);

+ 297 - 178
callback/callback.c

@@ -8,6 +8,74 @@
 #include <free/free.h>
 #include <alloc/alloc.h>
 
+void callback_loading_task(FlipperHTTP *fhttp,
+                           LoadingCallback http_request,
+                           LoadingCallback parse_response,
+                           uint32_t success_view_id,
+                           uint32_t failure_view_id,
+                           ViewDispatcher **view_dispatcher)
+{
+    if (!fhttp)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to get context.");
+        return;
+    }
+    if (fhttp->state == INACTIVE)
+    {
+        view_dispatcher_switch_to_view(*view_dispatcher, failure_view_id);
+        return;
+    }
+    Loading *loading;
+    int32_t loading_view_id = 987654321; // Random ID
+
+    loading = loading_alloc();
+    if (!loading)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to allocate loading");
+        view_dispatcher_switch_to_view(*view_dispatcher, failure_view_id);
+        return;
+    }
+
+    view_dispatcher_add_view(*view_dispatcher, loading_view_id, loading_get_view(loading));
+
+    // Switch to the loading view
+    view_dispatcher_switch_to_view(*view_dispatcher, loading_view_id);
+
+    // Make the request
+    if (http_request(fhttp)) // start the async request
+    {
+        furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
+        fhttp->state = RECEIVING;
+    }
+    else
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to send request");
+        view_dispatcher_switch_to_view(*view_dispatcher, failure_view_id);
+        view_dispatcher_remove_view(*view_dispatcher, loading_view_id);
+        loading_free(loading);
+        return;
+    }
+    while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
+    {
+        // Wait for the request to be received
+        furi_delay_ms(100);
+    }
+    furi_timer_stop(fhttp->get_timeout_timer);
+    if (!parse_response(fhttp)) // parse the JSON before switching to the view (synchonous)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to parse the JSON...");
+        view_dispatcher_switch_to_view(*view_dispatcher, failure_view_id);
+        view_dispatcher_remove_view(*view_dispatcher, loading_view_id);
+        loading_free(loading);
+        return;
+    }
+
+    // Switch to the success view
+    view_dispatcher_switch_to_view(*view_dispatcher, success_view_id);
+    view_dispatcher_remove_view(*view_dispatcher, loading_view_id);
+    loading_free(loading);
+}
+
 static bool flip_social_login_fetch(DataLoaderModel *model)
 {
     UNUSED(model);
@@ -374,35 +442,37 @@ void explore_dialog_callback(DialogExResult result, void *context)
     FlipSocialApp *app = (FlipSocialApp *)context;
     if (result == DialogExResultLeft) // Remove
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            // 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]);
-            alloc_headers();
-            flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/remove-friend/", auth_headers, remove_payload);
-            view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
-            free_explore_dialog();
-            furi_delay_ms(1000);
-            flipper_http_free(fhttp);
+            return;
         }
+        // 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]);
+        alloc_headers();
+        flipper_http_request(app->fhttp, POST, "https://www.jblanked.com/flipper/api/user/remove-friend/", auth_headers, remove_payload);
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
+        free_explore_dialog();
+        furi_delay_ms(1000);
+        free_flipper_http();
     }
     else if (result == DialogExResultRight)
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            // 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]);
-            alloc_headers();
-            flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/add-friend/", auth_headers, add_payload);
-            view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
-            free_explore_dialog();
-            furi_delay_ms(1000);
-            flipper_http_free(fhttp);
+            return;
         }
+        // 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]);
+        alloc_headers();
+        flipper_http_request(app->fhttp, POST, "https://www.jblanked.com/flipper/api/user/add-friend/", auth_headers, add_payload);
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
+        free_explore_dialog();
+        furi_delay_ms(1000);
+        free_flipper_http();
     }
 }
 static void friends_dialog_callback(DialogExResult result, void *context)
@@ -411,19 +481,20 @@ static void friends_dialog_callback(DialogExResult result, void *context)
     FlipSocialApp *app = (FlipSocialApp *)context;
     if (result == DialogExResultLeft) // Remove
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            // 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]);
-            alloc_headers();
-            flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/api/user/remove-friend/", auth_headers, remove_payload);
-            view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
-            free_friends_dialog();
-            furi_delay_ms(1000);
-            flipper_http_free(fhttp);
+            return;
         }
+        // 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]);
+        alloc_headers();
+        flipper_http_request(app->fhttp, POST, "https://www.jblanked.com/flipper/api/user/remove-friend/", auth_headers, remove_payload);
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
+        free_friends_dialog();
+        furi_delay_ms(1000);
+        free_flipper_http();
     }
 }
 void callback_message_dialog(DialogExResult result, void *context)
@@ -536,10 +607,9 @@ static void compose_dialog_callback(DialogExResult result, void *context)
     {
         // post the message
         // send selected_message
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (!fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
             return;
         }
         if (selected_message && app_instance->login_username_logged_in)
@@ -547,7 +617,7 @@ static void compose_dialog_callback(DialogExResult result, void *context)
             if (strlen(selected_message) > MAX_MESSAGE_LENGTH)
             {
                 FURI_LOG_E(TAG, "Message is too long");
-                flipper_http_free(fhttp);
+                free_flipper_http();
                 return;
             }
             // Send the selected_message
@@ -556,39 +626,42 @@ static void compose_dialog_callback(DialogExResult result, void *context)
                      app_instance->login_username_logged_in, selected_message);
 
             if (!flipper_http_request(
-                    fhttp,
+                    app->fhttp,
                     POST,
                     "https://www.jblanked.com/flipper/api/feed/post/",
                     auth_headers,
                     command))
             {
                 FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
-                flipper_http_free(fhttp);
+                free_flipper_http();
                 return;
             }
 
-            fhttp->state = RECEIVING;
-            furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
+            app->fhttp->state = RECEIVING;
+            furi_timer_start(app->fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
         }
         else
         {
-            FURI_LOG_E(TAG, "Message or username is NULL");
-            flipper_http_free(fhttp);
+            FURI_LOG_E(TAG, "Selected message or username is NULL");
+            free_flipper_http();
             return;
         }
-        while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
+        while (app->fhttp->state == RECEIVING && furi_timer_is_running(app->fhttp->get_timeout_timer) > 0)
         {
             furi_delay_ms(100);
         }
-        if (feed_load_initial_feed(fhttp, 1))
+        if (feed_load_initial_feed(app->fhttp, 1))
         {
             free_compose_dialog();
         }
         else
         {
-            FURI_LOG_E(TAG, "Failed to load the initial feed");
-            flipper_http_free(fhttp);
+            FURI_LOG_E(TAG, "Failed to load feed");
+            free_flipper_http();
+            return;
         }
+        furi_timer_stop(app->fhttp->get_timeout_timer);
+        free_flipper_http();
     }
 }
 
@@ -721,12 +794,18 @@ void callback_submenu_choices(void *context, uint32_t index)
             FURI_LOG_E(TAG, "Failed to allocate submenu");
             return;
         }
-        // flipper_http_loading_task(
-        //     messages_get_message_users,        // get the message users
-        //     messages_parse_json_message_users, // parse the message users
-        //     FlipSocialViewSubmenu,                // switch to the messages submenu if successful
-        //     FlipSocialViewLoggedInSubmenu,        // switch back to the main submenu if failed
-        //     &app->view_dispatcher);               // view dispatcher
+        if (!alloc_flipper_http())
+        {
+            return;
+        }
+        callback_loading_task(
+            app->fhttp,
+            messages_get_message_users,        // get the message users
+            messages_parse_json_message_users, // parse the message users
+            FlipSocialViewSubmenu,             // switch to the messages submenu if successful
+            FlipSocialViewLoggedInSubmenu,     // switch back to the main submenu if failed
+            &app->view_dispatcher);            // view dispatcher
+        free_flipper_http();
         break;
     case FlipSocialSubmenuLoggedInIndexMessagesNewMessage:
         // they need to search for the user to send a message
@@ -740,18 +819,17 @@ void callback_submenu_choices(void *context, uint32_t index)
         break;
     case FlipSocialSubmenuLoggedInIndexFeed:
         free_all(true, true, context);
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (!fhttp)
+        if (!alloc_flipper_http())
         {
-            FURI_LOG_E(TAG, "Failed to allocate FlipperHTTP");
             return;
         }
-        if (!feed_load_initial_feed(fhttp, 1))
+        if (!feed_load_initial_feed(app->fhttp, 1))
         {
             FURI_LOG_E(TAG, "Failed to load the initial feed");
+            free_flipper_http();
             return;
         }
-        flipper_http_free(fhttp);
+        free_flipper_http();
         break;
     case FlipSocialSubmenuExploreIndex:
         free_all(true, true, context);
@@ -980,13 +1058,20 @@ void callback_submenu_choices(void *context, uint32_t index)
                 return;
             }
             flip_social_message_users->index = index - FlipSocialSubmenuLoggedInIndexMessagesUsersStart;
-            // flipper_http_loading_task(
-            //     messages_get_messages_with_user,    // get the messages with the selected user
-            //     messages_parse_json_messages,       // parse the messages
-            //     FlipSocialViewMessagesDialog,          // switch to the messages process if successful
-            //     FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
-            //     &app->view_dispatcher                  // view dispatcher
-            // );
+            free_flipper_http();
+            if (!alloc_flipper_http())
+            {
+                return;
+            }
+            callback_loading_task(
+                app->fhttp,
+                messages_get_messages_with_user,       // get the messages with the selected user
+                messages_parse_json_messages,          // parse the messages
+                FlipSocialViewMessagesDialog,          // switch to the messages process if successful
+                FlipSocialViewLoggedInMessagesSubmenu, // switch back to the messages submenu if failed
+                &app->view_dispatcher                  // view dispatcher
+            );
+            free_flipper_http();
         }
 
         // handle the messages user choices selection
@@ -1047,17 +1132,18 @@ void callback_logged_out_wifi_settings_ssid_updated(void *context)
     // update the wifi settings
     if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_out, app->wifi_password_logged_out))
-            {
-                FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
-                FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-            }
-            furi_delay_ms(500);
-            flipper_http_free(fhttp);
+            return;
+        }
+        if (!flipper_http_save_wifi(app->fhttp, app->wifi_ssid_logged_out, app->wifi_password_logged_out))
+        {
+            FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
+            FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
         }
+        furi_delay_ms(500);
+        free_flipper_http();
     }
 
     // Save the settings
@@ -1098,17 +1184,18 @@ void callback_logged_out_wifi_settings_password_updated(void *context)
     // update the wifi settings
     if (strlen(app->wifi_ssid_logged_out) > 0 && strlen(app->wifi_password_logged_out) > 0)
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_out, app->wifi_password_logged_out))
-            {
-                FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
-                FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-            }
-            furi_delay_ms(500);
-            flipper_http_free(fhttp);
+            return;
         }
+        if (!flipper_http_save_wifi(app->fhttp, app->wifi_ssid_logged_out, app->wifi_password_logged_out))
+        {
+            FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
+            FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
+        }
+        furi_delay_ms(500);
+        free_flipper_http();
     }
 
     // Save the settings
@@ -1268,11 +1355,6 @@ void callback_logged_out_login_item_selected(void *context, uint32_t index)
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
         break;
     case 2: // Login Button
-        // if (!flipper_http_init(flipper_http_rx_callback, app_instance))
-        // {
-        //     FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
-        //     return;
-        // }
         flip_social_login_switch_to_view(app);
         break;
     default:
@@ -1409,11 +1491,6 @@ void callback_logged_out_register_item_selected(void *context, uint32_t index)
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
         break;
     case 3: // Register button
-        // if (!flipper_http_init(flipper_http_rx_callback, app_instance))
-        // {
-        //     FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
-        //     return;
-        // }
         flip_social_register_switch_to_view(app);
         break;
     default:
@@ -1457,17 +1534,18 @@ void callback_logged_in_wifi_settings_ssid_updated(void *context)
     // update the wifi settings
     if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_in, app->wifi_password_logged_in))
-            {
-                FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
-                FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-            }
-            furi_delay_ms(500);
-            flipper_http_free(fhttp);
+            return;
+        }
+        if (!flipper_http_save_wifi(app->fhttp, app->wifi_ssid_logged_in, app->wifi_password_logged_in))
+        {
+            FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
+            FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
         }
+        furi_delay_ms(500);
+        free_flipper_http();
     }
 
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
@@ -1509,17 +1587,18 @@ void callback_logged_in_wifi_settings_password_updated(void *context)
     // update the wifi settings
     if (strlen(app->wifi_ssid_logged_in) > 0 && strlen(app->wifi_password_logged_in) > 0)
     {
-        FlipperHTTP *fhttp = flipper_http_alloc();
-        if (fhttp)
+        free_flipper_http();
+        if (!alloc_flipper_http())
         {
-            if (!flipper_http_save_wifi(fhttp, app->wifi_ssid_logged_in, app->wifi_password_logged_in))
-            {
-                FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
-                FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
-            }
-            furi_delay_ms(500);
-            flipper_http_free(fhttp);
+            return;
+        }
+        if (!flipper_http_save_wifi(app->fhttp, app->wifi_ssid_logged_in, app->wifi_password_logged_in))
+        {
+            FURI_LOG_E(TAG, "Failed to save wifi settings via UART");
+            FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
         }
+        furi_delay_ms(500);
+        free_flipper_http();
     }
 
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewVariableItemList);
@@ -1542,7 +1621,6 @@ void callback_logged_in_wifi_settings_item_selected(void *context, uint32_t inde
     switch (index)
     {
     case 0: // Input SSID
-        // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsSSIDInput);
         free_all(false, false, app);
         if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsSSIDInput))
         {
@@ -1552,7 +1630,6 @@ void callback_logged_in_wifi_settings_item_selected(void *context, uint32_t inde
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
         break;
     case 1: // Input Password
-        // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInWifiSettingsPasswordInput);
         free_all(false, false, app);
         if (!alloc_text_input(FlipSocialViewLoggedInWifiSettingsPasswordInput))
         {
@@ -1680,19 +1757,21 @@ void callback_logged_in_profile_change_password_updated(void *context)
     }
 
     // send post request to change password
-    FlipperHTTP *fhttp = flipper_http_alloc();
-    if (fhttp)
+    free_flipper_http();
+    if (!alloc_flipper_http())
     {
-        alloc_headers();
-        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_request(fhttp, POST, "https://www.jblanked.com/flipper/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_free(fhttp);
+        return;
     }
+    alloc_headers();
+    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_request(app->fhttp, POST, "https://www.jblanked.com/flipper/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");
+    }
+    free_flipper_http();
+
     // 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);
 
@@ -1722,20 +1801,22 @@ void callback_logged_in_profile_change_bio_updated(void *context)
     }
 
     // send post request to change bio
-    FlipperHTTP *fhttp = flipper_http_alloc();
-    if (fhttp)
+    free_flipper_http();
+    if (!alloc_flipper_http())
     {
-        alloc_headers();
-        char payload[256];
-        snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
-        if (!flipper_http_request(fhttp, POST, "https://www.jblanked.com/flipper/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");
-        }
-        furi_delay_ms(500);
-        flipper_http_free(fhttp);
+        return;
+    }
+    alloc_headers();
+    char payload[256];
+    snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"bio\":\"%s\"}", app->login_username_logged_out, app->change_bio_logged_in);
+    if (!flipper_http_request(app->fhttp, POST, "https://www.jblanked.com/flipper/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");
     }
+    furi_delay_ms(500);
+    free_flipper_http();
+
     // Save the settings
     save_settings(app->wifi_ssid_logged_out, app->wifi_password_logged_out, app->login_username_logged_out, app->login_username_logged_in, app->login_password_logged_out, app->change_password_logged_in, app->change_bio_logged_in, app->is_logged_in);
 
@@ -1763,7 +1844,6 @@ void callback_logged_in_profile_item_selected(void *context, uint32_t index)
         // do nothing since username cannot be changed
         break;
     case 1: // Change Password
-        // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangePasswordInput);
         free_text_input();
         if (!alloc_text_input(FlipSocialViewLoggedInChangePasswordInput))
         {
@@ -1773,7 +1853,6 @@ void callback_logged_in_profile_item_selected(void *context, uint32_t index)
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewTextInput);
         break;
     case 2: // Change Bio
-        // view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInChangeBioInput);
         free_text_input();
         if (!alloc_text_input(FlipSocialViewLoggedInChangeBioInput))
         {
@@ -1789,17 +1868,18 @@ void callback_logged_in_profile_item_selected(void *context, uint32_t index)
             FURI_LOG_E(TAG, "Failed to allocate submenu for friends");
             return;
         }
-        // if (!flipper_http_init(flipper_http_rx_callback, app))
-        // {
-        //     FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
-        //     return;
-        // }
-        // flipper_http_loading_task(
-        //     friends_fetch,
-        //     friends_parse_json,
-        //     FlipSocialViewSubmenu,
-        //     FlipSocialViewVariableItemList,
-        //     &app->view_dispatcher);
+        if (!alloc_flipper_http())
+        {
+            return;
+        }
+        callback_loading_task(
+            app->fhttp,
+            friends_fetch,
+            friends_parse_json,
+            FlipSocialViewSubmenu,
+            FlipSocialViewVariableItemList,
+            &app->view_dispatcher);
+        free_flipper_http();
         break;
     default:
         FURI_LOG_E(TAG, "Unknown configuration item index");
@@ -1836,10 +1916,9 @@ void callback_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
-    FlipperHTTP *fhttp = flipper_http_alloc();
-    if (!fhttp)
+    free_flipper_http();
+    if (!alloc_flipper_http())
     {
-        FURI_LOG_E(TAG, "Failed to initialize HTTP");
         return;
     }
     alloc_headers();
@@ -1848,13 +1927,13 @@ void callback_logged_in_messages_user_choice_message_updated(void *context)
     snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/messages/%s/post/", app->login_username_logged_in);
     snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_explore->usernames[flip_social_explore->index], app->message_user_choice_logged_in);
 
-    if (!flipper_http_request(fhttp, POST, url, auth_headers, payload)) // start the async request
+    if (!flipper_http_request(app->fhttp, POST, url, auth_headers, payload)) // start the async request
     {
         FURI_LOG_E(TAG, "Failed to send post request to send message");
         FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
     }
     furi_delay_ms(1000);
-    flipper_http_free(fhttp);
+    free_flipper_http();
     // add user to the top of the list if not already there
     for (int i = 0; i < flip_social_message_users->count; i++)
     {
@@ -1910,10 +1989,9 @@ void callback_logged_in_messages_new_message_updated(void *context)
     // Ensure null-termination
     app->messages_new_message_logged_in[app->messages_new_message_logged_in_temp_buffer_size - 1] = '\0';
 
-    FlipperHTTP *fhttp = flipper_http_alloc();
-    if (!fhttp)
+    free_flipper_http();
+    if (!alloc_flipper_http())
     {
-        FURI_LOG_E(TAG, "Failed to initialize HTTP");
         return;
     }
 
@@ -1925,7 +2003,7 @@ void callback_logged_in_messages_new_message_updated(void *context)
         char payload[256];
         snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/messages/%s/post/", app->login_username_logged_in);
         snprintf(payload, sizeof(payload), "{\"receiver\":\"%s\",\"content\":\"%s\"}", flip_social_message_users->usernames[flip_social_message_users->index], app->messages_new_message_logged_in);
-        if (!flipper_http_request(fhttp, POST, url, auth_headers, payload))
+        if (!flipper_http_request(app->fhttp, POST, url, auth_headers, payload))
         {
             FURI_LOG_E(TAG, "Failed to send post request to send message");
             FURI_LOG_E(TAG, "Make sure the Flipper is connected to the Wifi Dev Board");
@@ -1933,12 +2011,12 @@ void callback_logged_in_messages_new_message_updated(void *context)
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewSubmenu);
             return false;
         }
-        fhttp->state = RECEIVING;
+        app->fhttp->state = RECEIVING;
         return true;
     }
     bool parse_message_to_user()
     {
-        while (fhttp->state != IDLE)
+        while (app->fhttp->state != IDLE)
         {
             furi_delay_ms(10);
         }
@@ -1947,14 +2025,14 @@ void callback_logged_in_messages_new_message_updated(void *context)
 
     // well, we got a freeze here, so let's use the loading task to switch views and force refresh
     flipper_http_loading_task(
-        fhttp,
+        app->fhttp,
         send_message_to_user,
         parse_message_to_user,
         FlipSocialViewSubmenu,
         FlipSocialViewLoggedInMessagesNewMessageInput,
         &app->view_dispatcher);
 
-    flipper_http_free(fhttp);
+    free_flipper_http();
 }
 
 void callback_logged_in_explore_updated(void *context)
@@ -1987,13 +2065,18 @@ void callback_logged_in_explore_updated(void *context)
         FURI_LOG_E(TAG, "Failed to allocate submenu for explore");
         return;
     }
-
-    // flipper_http_loading_task(
-    //     explore_fetch,        // get the explore users
-    //     explore_parse_json, // parse the explore users
-    //     FlipSocialViewSubmenu,          // switch to the explore submenu if successful
-    //     FlipSocialViewLoggedInSubmenu,  // switch back to the main submenu if failed
-    //     &app->view_dispatcher);         // view dispatcher
+    if (!alloc_flipper_http())
+    {
+        return;
+    }
+    callback_loading_task(
+        app->fhttp,
+        explore_fetch,                 // get the explore users
+        explore_parse_json,            // parse the explore users
+        FlipSocialViewSubmenu,         // switch to the explore submenu if successful
+        FlipSocialViewLoggedInSubmenu, // switch back to the main submenu if failed
+        &app->view_dispatcher);        // view dispatcher
+    free_flipper_http();
 }
 
 void callback_logged_in_message_users_updated(void *context)
@@ -2029,12 +2112,19 @@ void callback_logged_in_message_users_updated(void *context)
     }
 
     // get users
-    // flipper_http_loading_task(
-    //     explore_fetch_2,                             // get the explore users
-    //     messages_parse_json_message_user_choices, // parse the explore users
-    //     FlipSocialViewSubmenu,                       // switch to the explore submenu if successful
-    //     FlipSocialViewLoggedInSubmenu,               // switch back to the main submenu if failed
-    //     &app->view_dispatcher);                      // view dispatcher
+    free_flipper_http();
+    if (!alloc_flipper_http())
+    {
+        return;
+    }
+    callback_loading_task(
+        app->fhttp,
+        explore_fetch_2,                          // get the explore users
+        messages_parse_json_message_user_choices, // parse the explore users
+        FlipSocialViewSubmenu,                    // switch to the explore submenu if successful
+        FlipSocialViewLoggedInSubmenu,            // switch back to the main submenu if failed
+        &app->view_dispatcher);                   // view dispatcher
+    free_flipper_http();
 }
 
 bool callback_get_home_notification(FlipperHTTP *fhttp)
@@ -2230,8 +2320,37 @@ bool callback_parse_home_notification(FlipperHTTP *fhttp)
 }
 
 // home notification
-bool callback_home_notification()
+bool callback_home_notification(FlipperHTTP *fhttp)
 {
-    // return flipper_http_process_response_async(callback_get_home_notification, callback_parse_home_notification);
-    return true; // for now
+    if (!fhttp)
+    {
+        FURI_LOG_E(TAG, "FlipperHTTP is NULL");
+        return false;
+    }
+
+    // Make the request
+    if (!callback_get_home_notification(fhttp)) // start the async request
+    {
+        FURI_LOG_E(TAG, "Failed to send get request to home notification");
+        return false;
+    }
+
+    furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
+    fhttp->state = RECEIVING;
+
+    while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
+    {
+        // Wait for the request to be received
+        furi_delay_ms(100);
+    }
+
+    furi_timer_stop(fhttp->get_timeout_timer);
+
+    if (!callback_parse_home_notification(fhttp)) // parse the JSON before switching to the view (synchonous)
+    {
+        FURI_LOG_E(HTTP_TAG, "Failed to parse the home notification...");
+        return false;
+    }
+
+    return true;
 }

+ 11 - 2
callback/callback.h

@@ -1,8 +1,17 @@
 #ifndef FLIP_SOCIAL_CALLBACK_H
 #define FLIP_SOCIAL_CALLBACK_H
-
 #include <flip_social.h>
 
+// replica of flipper_http_load_task but allows FlipperHTTP to be passed in
+typedef bool (*LoadingCallback)(FlipperHTTP *);
+
+void callback_loading_task(FlipperHTTP *fhttp,
+                           LoadingCallback http_request,
+                           LoadingCallback parse_response,
+                           uint32_t success_view_id,
+                           uint32_t failure_view_id,
+                           ViewDispatcher **view_dispatcher);
+
 /**
  * @brief Navigation callback to go back to the submenu Logged out.
  * @param context The context - unused
@@ -246,5 +255,5 @@ void callback_logged_in_message_users_updated(void *context);
 
 void callback_message_dialog(DialogExResult result, void *context);
 //
-bool callback_home_notification();
+bool callback_home_notification(FlipperHTTP *fhttp);
 #endif

+ 0 - 5
feed/feed.c

@@ -48,11 +48,6 @@ bool feed_fetch(FlipperHTTP *fhttp, int series_index)
 
 FlipSocialFeedMini *feed_parse_json(FlipperHTTP *fhttp)
 {
-    if (!app_instance)
-    {
-        FURI_LOG_E(TAG, "FlipSocialApp is NULL");
-        return NULL;
-    }
     if (!fhttp)
     {
         FURI_LOG_E(TAG, "FlipperHTTP is NULL");

+ 45 - 1
free/free.c

@@ -28,15 +28,21 @@ void free_all(bool should_free_variable_item_list, bool should_free_submenu, voi
 
     if (went_to_friends)
     {
-        // flipper_http_deinit();
         went_to_friends = false;
     }
 
     // free Derek's loader
     loader_view_free(app);
+
+    // free flipper_http
+    free_flipper_http();
 }
 void free_text_input()
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (app_instance->text_input)
     {
         uart_text_input_free(app_instance->text_input);
@@ -46,6 +52,10 @@ void free_text_input()
 }
 void free_explore_dialog()
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (app_instance->dialog_explore)
     {
         dialog_ex_free(app_instance->dialog_explore);
@@ -55,6 +65,10 @@ void free_explore_dialog()
 }
 void free_friends_dialog()
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (app_instance->dialog_friends)
     {
         dialog_ex_free(app_instance->dialog_friends);
@@ -64,6 +78,10 @@ void free_friends_dialog()
 }
 void free_messages_dialog()
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (app_instance->dialog_messages)
     {
         dialog_ex_free(app_instance->dialog_messages);
@@ -74,6 +92,10 @@ void free_messages_dialog()
 }
 void free_compose_dialog()
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (app_instance->dialog_compose)
     {
         dialog_ex_free(app_instance->dialog_compose);
@@ -83,6 +105,10 @@ void free_compose_dialog()
 }
 void free_feed_view()
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (app_instance->view_feed)
     {
         view_free(app_instance->view_feed);
@@ -93,6 +119,10 @@ void free_feed_view()
 
 void free_about_widget(bool is_logged_in)
 {
+    if (!app_instance)
+    {
+        return;
+    }
     if (is_logged_in && app_instance->widget_logged_in_about)
     {
         widget_free(app_instance->widget_logged_in_about);
@@ -178,4 +208,18 @@ void free_explore(void)
         free(flip_social_explore);
         flip_social_explore = NULL;
     }
+}
+
+void free_flipper_http()
+{
+    if (!app_instance)
+    {
+        FURI_LOG_E(TAG, "FlipSocialApp is NULL");
+        return;
+    }
+    if (app_instance->fhttp)
+    {
+        flipper_http_free(app_instance->fhttp);
+        app_instance->fhttp = NULL;
+    }
 }

+ 2 - 1
free/free.h

@@ -14,4 +14,5 @@ void free_variable_item_list(void);
 void free_submenu(void);
 void free_message_users();
 void free_messages();
-void free_explore();
+void free_explore();
+void free_flipper_http();