Explorar el Código

Update PreSavedMessages and feed flipping

jblanked hace 1 año
padre
commit
9ec3d89f6f

+ 1 - 1
alloc/flip_social_alloc.c

@@ -20,7 +20,7 @@ FlipSocialApp *flip_social_app_alloc()
         return NULL;
     }
     flip_social_loader_init(app->view_loader);
-    if (!easy_flipper_set_widget(&app->widget_result, FlipSocialViewWidgetResult, "Error, try again.", flip_social_callback_to_submenu_logged_out, &app->view_dispatcher))
+    if (!easy_flipper_set_widget(&app->widget_result, FlipSocialViewWidgetResult, "", flip_social_callback_to_submenu_logged_out, &app->view_dispatcher))
     {
         return NULL;
     }

+ 73 - 37
callback/flip_social_callback.c

@@ -57,13 +57,14 @@ static bool pre_saved_messages_alloc(void)
         }
         submenu_reset(app_instance->submenu_compose);
         submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app_instance);
-        // load the playlist
+
+        // Load the playlist
         if (load_playlist(&app_instance->pre_saved_messages))
         {
             // Update the playlist submenu
             for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
             {
-                if (app_instance->pre_saved_messages.messages[i])
+                if (app_instance->pre_saved_messages.messages[i][0] != '\0') // Check if the string is not empty
                 {
                     submenu_add_item(app_instance->submenu_compose, app_instance->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
                 }
@@ -72,6 +73,7 @@ static bool pre_saved_messages_alloc(void)
     }
     return true;
 }
+
 static void free_pre_saved_messages(void)
 {
     if (app_instance->submenu_compose)
@@ -80,14 +82,14 @@ static void free_pre_saved_messages(void)
         app_instance->submenu_compose = NULL;
         view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInCompose);
     }
-    for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
-    {
-        if (app_instance->pre_saved_messages.messages[i])
-        {
-            free(app_instance->pre_saved_messages.messages[i]);
-            app_instance->pre_saved_messages.messages[i] = NULL;
-        }
-    }
+    // for (uint32_t i = 0; i < app_instance->pre_saved_messages.count; i++)
+    // {
+    //     if (app_instance->pre_saved_messages.messages[i])
+    //     {
+    //         free(app_instance->pre_saved_messages.messages[i]);
+    //         app_instance->pre_saved_messages.messages[i] = NULL;
+    //     }
+    // }
 }
 
 static void flip_social_free_friends(void)
@@ -703,30 +705,48 @@ static void compose_dialog_callback(DialogExResult result, void *context)
     FlipSocialApp *app = (FlipSocialApp *)context;
     if (result == DialogExResultLeft) // Delete
     {
-        // delete message
-        app->pre_saved_messages.messages[app_instance->pre_saved_messages.index] = NULL;
+        // Ensure index is within bounds
+        if (app_instance->pre_saved_messages.index >= app_instance->pre_saved_messages.count)
+        {
+            FURI_LOG_E(TAG, "Invalid index for deletion: %zu", app_instance->pre_saved_messages.index);
+            return;
+        }
 
-        for (uint32_t i = app->pre_saved_messages.index; i < app->pre_saved_messages.count - 1; i++)
+        // Shift messages to remove the selected message
+        for (size_t i = app_instance->pre_saved_messages.index; i < app_instance->pre_saved_messages.count - 1; i++)
         {
-            app->pre_saved_messages.messages[i] = app->pre_saved_messages.messages[i + 1];
+            strncpy(app_instance->pre_saved_messages.messages[i],
+                    app_instance->pre_saved_messages.messages[i + 1],
+                    MAX_MESSAGE_LENGTH);
         }
-        app->pre_saved_messages.count--;
 
-        // add the item to the submenu
-        submenu_reset(app_instance->submenu_compose);
+        // Clear the last message after shifting
+        memset(app_instance->pre_saved_messages.messages[app_instance->pre_saved_messages.count - 1], 0, MAX_MESSAGE_LENGTH);
+        app_instance->pre_saved_messages.count--;
 
+        // Reset and rebuild the submenu
+        submenu_reset(app_instance->submenu_compose);
         submenu_add_item(app_instance->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
 
-        for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
+        for (size_t i = 0; i < app_instance->pre_saved_messages.count; i++)
         {
-            submenu_add_item(app_instance->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
+            submenu_add_item(app_instance->submenu_compose,
+                             app_instance->pre_saved_messages.messages[i],
+                             FlipSocialSubemnuComposeIndexStartIndex + i,
+                             flip_social_callback_submenu_choices,
+                             app);
         }
 
-        // save playlist
+        // Save the updated playlist
         save_playlist(&app_instance->pre_saved_messages);
+
+        // Switch back to the compose view
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
+
+        // Free the dialog resources
         flip_social_free_compose_dialog();
     }
+
     else if (result == DialogExResultRight) // Post
     {
         // post the message
@@ -793,7 +813,8 @@ static void feed_dialog_callback(DialogExResult result, void *context)
             flip_feed_info->index--;
         }
         // switch view, free dialog, re-alloc dialog, switch back to dialog
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
+        flip_social_free_feed_dialog();
         // load feed item
         if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
         {
@@ -819,7 +840,8 @@ static void feed_dialog_callback(DialogExResult result, void *context)
             flip_feed_info->index++;
         }
         // switch view, free dialog, re-alloc dialog, switch back to dialog
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
+        flip_social_free_feed_dialog();
         // load feed item
         if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
         {
@@ -881,7 +903,8 @@ static void feed_dialog_callback(DialogExResult result, void *context)
             // }
         }
         // switch view, free dialog, re-alloc dialog, switch back to dialog
-        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInSettings);
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewWidgetResult);
+        flip_social_free_feed_dialog();
         if (feed_dialog_alloc())
         {
             view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewFeedDialog);
@@ -889,8 +912,8 @@ static void feed_dialog_callback(DialogExResult result, void *context)
         else
         {
             FURI_LOG_E(TAG, "Failed to allocate feed dialog");
-            fhttp.state = ISSUE;
         }
+        furi_delay_ms(1000);
         flipper_http_deinit();
     }
 }
@@ -1817,7 +1840,7 @@ void flip_social_logged_in_compose_pre_save_updated(void *context)
         return;
     }
 
-    // check if the message is empty or if adding in the message would exceed the MAX_PRE_SAVED_MESSAGES
+    // Check if the message is empty or if adding the message would exceed MAX_PRE_SAVED_MESSAGES
     if (app->compose_pre_save_logged_in_temp_buffer_size == 0 || app->pre_saved_messages.count >= MAX_PRE_SAVED_MESSAGES)
     {
         FURI_LOG_E(TAG, "Message is empty or would exceed the maximum number of pre-saved messages");
@@ -1825,28 +1848,41 @@ void flip_social_logged_in_compose_pre_save_updated(void *context)
         return;
     }
 
-    // Store the entered message
-    strncpy(app->compose_pre_save_logged_in, app->compose_pre_save_logged_in_temp_buffer, app->compose_pre_save_logged_in_temp_buffer_size);
+    // Copy the entered message into the next available slot
+    strncpy(
+        app->pre_saved_messages.messages[app->pre_saved_messages.count],
+        app->compose_pre_save_logged_in_temp_buffer,
+        MAX_MESSAGE_LENGTH - 1);
 
     // Ensure null-termination
-    app->compose_pre_save_logged_in[app->compose_pre_save_logged_in_temp_buffer_size - 1] = '\0';
-
-    // add the item to the submenu
-    submenu_reset(app->submenu_compose);
+    app->pre_saved_messages.messages[app->pre_saved_messages.count][MAX_MESSAGE_LENGTH - 1] = '\0';
 
-    // loop through the items and add them to the submenu
-    app->pre_saved_messages.messages[app->pre_saved_messages.count] = app->compose_pre_save_logged_in;
+    // Increment the count
     app->pre_saved_messages.count++;
 
-    submenu_add_item(app->submenu_compose, "Add Pre-Save", FlipSocialSubmenuComposeIndexAddPreSave, flip_social_callback_submenu_choices, app);
-    for (uint32_t i = 0; i < app->pre_saved_messages.count; i++)
+    // Rebuild the submenu
+    submenu_reset(app->submenu_compose);
+    submenu_add_item(
+        app->submenu_compose,
+        "Add Pre-Save",
+        FlipSocialSubmenuComposeIndexAddPreSave,
+        flip_social_callback_submenu_choices,
+        app);
+
+    for (size_t i = 0; i < app->pre_saved_messages.count; i++)
     {
-        submenu_add_item(app->submenu_compose, app->pre_saved_messages.messages[i], FlipSocialSubemnuComposeIndexStartIndex + i, flip_social_callback_submenu_choices, app);
+        submenu_add_item(
+            app->submenu_compose,
+            app->pre_saved_messages.messages[i],
+            FlipSocialSubemnuComposeIndexStartIndex + i,
+            flip_social_callback_submenu_choices,
+            app);
     }
 
-    // save playlist
+    // Save the updated playlist
     save_playlist(&app->pre_saved_messages);
 
+    // Switch back to the compose view
     view_dispatcher_switch_to_view(app->view_dispatcher, FlipSocialViewLoggedInCompose);
 }
 

+ 0 - 2
feed/flip_social_feed.c

@@ -163,8 +163,6 @@ bool flip_social_load_feed_post(int post_id)
             free(data_cstr);
             return false;
         }
-        flip_feed_item->username = malloc(MAX_USER_LENGTH);
-        flip_feed_item->message = malloc(MAX_MESSAGE_LENGTH);
     }
 
     // Extract individual fields from the JSON object

+ 3 - 3
flip_social.h

@@ -58,7 +58,7 @@ typedef enum
 // Define the ScriptPlaylist structure
 typedef struct
 {
-    char *messages[MAX_PRE_SAVED_MESSAGES];
+    char messages[MAX_PRE_SAVED_MESSAGES][MAX_MESSAGE_LENGTH];
     size_t count;
     size_t index;
 } PreSavedPlaylist;
@@ -66,8 +66,8 @@ typedef struct
 // Define a FlipSocialFeed individual item
 typedef struct
 {
-    char *username;
-    char *message;
+    char username[MAX_USER_LENGTH];
+    char message[MAX_MESSAGE_LENGTH];
     bool is_flipped;
     int id;
     int flips;

+ 24 - 43
flip_storage/flip_social_storage.c

@@ -8,6 +8,7 @@ void save_playlist(const PreSavedPlaylist *playlist)
         FURI_LOG_E(TAG, "Playlist is NULL");
         return;
     }
+
     // Create the directory for saving settings
     char directory_path[128];
     snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social");
@@ -29,7 +30,7 @@ void save_playlist(const PreSavedPlaylist *playlist)
     // Write each playlist message on a separate line
     for (size_t i = 0; i < playlist->count; ++i)
     {
-        // Add a newline character after each message
+        // Write the message
         if (storage_file_write(file, playlist->messages[i], strlen(playlist->messages[i])) != strlen(playlist->messages[i]))
         {
             FURI_LOG_E(TAG, "Failed to write playlist message %zu", i);
@@ -42,34 +43,23 @@ void save_playlist(const PreSavedPlaylist *playlist)
         }
     }
 
+    // Close the file and storage
     storage_file_close(file);
     storage_file_free(file);
     furi_record_close(RECORD_STORAGE);
 }
 
-// Function to load the playlist
 bool load_playlist(PreSavedPlaylist *playlist)
 {
-    // Ensure playlist is not NULL
     if (!playlist)
     {
         FURI_LOG_E(TAG, "Playlist is NULL");
         return false;
     }
 
-    // Ensure playlist->messages is not NULL and allocate memory for each message
-    for (size_t i = 0; i < MAX_PRE_SAVED_MESSAGES; ++i)
-    {
-        if (!playlist->messages[i]) // Check if memory is already allocated
-        {
-            playlist->messages[i] = (char *)malloc(MAX_MESSAGE_LENGTH * sizeof(char));
-            if (!playlist->messages[i])
-            {
-                FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
-                return false; // Return false on memory allocation failure
-            }
-        }
-    }
+    // Clear existing data in the playlist
+    memset(playlist->messages, 0, sizeof(playlist->messages));
+    playlist->count = 0;
 
     // Open the storage
     Storage *storage = furi_record_open(RECORD_STORAGE);
@@ -80,29 +70,24 @@ bool load_playlist(PreSavedPlaylist *playlist)
         FURI_LOG_E(TAG, "Failed to open pre-saved messages file for reading: %s", PRE_SAVED_MESSAGES_PATH);
         storage_file_free(file);
         furi_record_close(RECORD_STORAGE);
-        return false; // Return false if the file does not exist
+        return false;
     }
 
-    // Initialize the playlist count
-    playlist->count = 0;
-
-    // Read the file byte by byte to simulate reading lines
+    // Read the file line by line
+    char line[MAX_MESSAGE_LENGTH] = {0};
+    size_t line_pos = 0;
     char ch;
-    size_t message_pos = 0;
-    bool message_started = false;
 
-    while (storage_file_read(file, &ch, 1) == 1) // Read one character at a time
+    while (storage_file_read(file, &ch, 1) == 1)
     {
-        message_started = true;
-
-        if (ch == '\n' || message_pos >= (MAX_MESSAGE_LENGTH - 1)) // End of line or message is too long
+        if (ch == '\n' || line_pos >= (MAX_MESSAGE_LENGTH - 1)) // End of line or max length reached
         {
-            playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the message
-            playlist->count++;                                       // Move to the next message
-            message_pos = 0;                                         // Reset for the next message
-            message_started = false;
+            line[line_pos] = '\0'; // Null-terminate the line
+            strncpy(playlist->messages[playlist->count], line, MAX_MESSAGE_LENGTH);
+            playlist->count++;
+            line_pos = 0;
 
-            // Ensure the playlist count does not exceed the maximum
+            // Ensure playlist count does not exceed maximum allowed
             if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
             {
                 FURI_LOG_W(TAG, "Reached maximum playlist messages");
@@ -111,21 +96,16 @@ bool load_playlist(PreSavedPlaylist *playlist)
         }
         else
         {
-            playlist->messages[playlist->count][message_pos++] = ch; // Add character to current message
+            line[line_pos++] = ch;
         }
     }
 
-    // Handle the case where the last message does not end with a newline
-    if (message_started && message_pos > 0)
+    // Handle the last line if it does not end with a newline
+    if (line_pos > 0)
     {
-        playlist->messages[playlist->count][message_pos] = '\0'; // Null-terminate the last message
-        playlist->count++;                                       // Increment the count for the last message
-
-        // Ensure the playlist count does not exceed the maximum
-        if (playlist->count >= MAX_PRE_SAVED_MESSAGES)
-        {
-            FURI_LOG_W(TAG, "Reached maximum playlist messages");
-        }
+        line[line_pos] = '\0'; // Null-terminate the last line
+        strncpy(playlist->messages[playlist->count], line, MAX_MESSAGE_LENGTH);
+        playlist->count++;
     }
 
     // Close the file and storage
@@ -135,6 +115,7 @@ bool load_playlist(PreSavedPlaylist *playlist)
 
     return true;
 }
+
 void save_settings(
     const char *ssid,
     const char *password,