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

Start API login and flip social info

jblanked 1 год назад
Родитель
Сommit
589c482d14
3 измененных файлов с 392 добавлено и 1 удалено
  1. 153 0
      callback/callback.c
  2. 236 0
      flip_storage/storage.c
  3. 3 1
      flip_storage/storage.h

+ 153 - 0
callback/callback.c

@@ -330,6 +330,51 @@ static bool alloc_variable_item_list(void *context, uint32_t view_id)
                 variable_item_set_current_value_index(app->variable_item_game_fps, index);
                 variable_item_set_current_value_index(app->variable_item_game_fps, index);
             }
             }
             break;
             break;
+        case FlipWorldSubmenuIndexUserSettings:
+            if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, NULL, callback_to_settings, &app->view_dispatcher, app))
+            {
+                FURI_LOG_E(TAG, "Failed to allocate variable item list");
+                return false;
+            }
+
+            if (!app->variable_item_list)
+            {
+                FURI_LOG_E(TAG, "Variable item list is NULL");
+                return false;
+            }
+
+            // if logged in, show profile info, otherwise show login/register
+            if (is_logged_in_to_flip_social())
+            {
+                if (!app->variable_item_user_username)
+                {
+                    app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
+                    variable_item_set_current_value_text(app->variable_item_user_username, "");
+                }
+                if (!app->variable_item_user_password)
+                {
+                    app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
+                    variable_item_set_current_value_text(app->variable_item_user_password, "");
+                }
+                variable_item_set_current_value_text(app->variable_item_user_username, furi_string_get_cstr(flip_social_info("login_username_logged_in")));
+                variable_item_set_current_value_text(app->variable_item_user_password, "");
+            }
+            else
+            {
+                if (!app->variable_item_user_username)
+                {
+                    app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
+                    variable_item_set_current_value_text(app->variable_item_user_username, "");
+                }
+                if (!app->variable_item_user_password)
+                {
+                    app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
+                    variable_item_set_current_value_text(app->variable_item_user_password, "");
+                }
+                variable_item_list_add(app->variable_item_list, "Login", 0, NULL, NULL);
+                variable_item_list_add(app->variable_item_list, "Register", 0, NULL, NULL);
+            }
+            break;
         }
         }
     }
     }
     return true;
     return true;
@@ -590,6 +635,100 @@ static void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
 {
 {
     flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 2, callback_to_submenu, FlipWorldViewLoader);
     flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 2, callback_to_submenu, FlipWorldViewLoader);
 }
 }
+static bool flip_social_login_fetch(DataLoaderModel *model)
+{
+    UNUSED(model);
+    if (model->request_index == 0)
+    {
+        if (!app_instance)
+        {
+            FURI_LOG_E(TAG, "app_instance is NULL");
+            return false;
+        }
+        free_text_input_view(app_instance);
+        if (!alloc_text_input_view(app_instance, "Username"))
+        {
+            FURI_LOG_E(TAG, "Failed to allocate text input view");
+            return false;
+        }
+        // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
+        return true;
+    }
+    else if (model->request_index == 1)
+    {
+        if (!app_instance)
+        {
+            FURI_LOG_E(TAG, "app_instance is NULL");
+            return false;
+        }
+        free_text_input_view(app_instance);
+        if (!alloc_text_input_view(app_instance, "Password"))
+        {
+            FURI_LOG_E(TAG, "Failed to allocate text input view");
+            return false;
+        }
+        // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewTextInput);
+        return true;
+    }
+    else if (model->request_index == 2)
+    {
+        if (!app_instance)
+        {
+            FURI_LOG_E(TAG, "app_instance is NULL");
+            return false;
+        }
+        return true;
+    }
+    return false;
+}
+
+static char *flip_social_login_parse(DataLoaderModel *model)
+{
+    if (model->request_index == 0)
+    {
+        return "Enter your username...";
+    }
+    else if (model->request_index == 1)
+    {
+        return "Enter your password...";
+    }
+    else if (model->request_index == 2)
+    {
+        if (!app_instance)
+        {
+            FURI_LOG_E(TAG, "app_instance is NULL");
+            return "Failed to login...";
+        }
+        if (!fhttp.last_response)
+        {
+            flipper_http_deinit();
+            return "Failed to login...";
+        }
+        // read response
+        if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
+        {
+            flipper_http_deinit();
+            save_char("is_logged_in", "true");
+            return "Login successful!";
+        }
+        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...";
+        }
+    }
+    return NULL;
+}
+
+void flip_social_login_switch_to_view(FlipWorldApp *app)
+{
+    flip_world_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 3, callback_to_settings, FlipWorldViewTextInput);
+}
 
 
 void callback_submenu_choices(void *context, uint32_t index)
 void callback_submenu_choices(void *context, uint32_t index)
 {
 {
@@ -653,6 +792,20 @@ void callback_submenu_choices(void *context, uint32_t index)
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
         break;
         break;
     case FlipWorldSubmenuIndexUserSettings:
     case FlipWorldSubmenuIndexUserSettings:
+        free_all_views(app, true, false);
+        // if (!flipper_http_init(flipper_http_rx_callback, app))
+        // {
+        //     FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
+        //     return;
+        // }
+        // app_instance = malloc(sizeof(FlipWorldApp));
+        // if (!app_instance)
+        // {
+        //     FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
+        //     return;
+        // }
+        // memcpy(app_instance, app, sizeof(FlipWorldApp));
+        // flip_social_login_switch_to_view(app);
         easy_flipper_dialog("User Settings", "Coming soon...");
         easy_flipper_dialog("User Settings", "Coming soon...");
         break;
         break;
     default:
     default:

+ 236 - 0
flip_storage/storage.c

@@ -444,3 +444,239 @@ bool save_world_names(const FuriString *json)
 
 
     return true;
     return true;
 }
 }
+
+static bool load_flip_social_settings(
+    char *ssid,
+    size_t ssid_size,
+    char *password,
+    size_t password_size,
+    char *login_username_logged_out,
+    size_t username_out_size,
+    char *login_username_logged_in,
+    size_t username_in_size,
+    char *login_password_logged_out,
+    size_t password_out_size,
+    char *change_password_logged_in,
+    size_t change_password_size,
+    char *change_bio_logged_in,
+    size_t change_bio_size,
+    char *is_logged_in,
+    size_t is_logged_in_size)
+{
+    Storage *storage = furi_record_open(RECORD_STORAGE);
+    File *file = storage_file_alloc(storage);
+
+    if (!storage_file_open(file, SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING))
+    {
+        FURI_LOG_E(TAG, "Failed to open settings file for reading: %s", SETTINGS_PATH);
+        storage_file_free(file);
+        furi_record_close(RECORD_STORAGE);
+        return false; // Return false if the file does not exist
+    }
+
+    // Load the ssid
+    size_t ssid_length;
+    if (storage_file_read(file, &ssid_length, sizeof(size_t)) != sizeof(size_t) || ssid_length > ssid_size ||
+        storage_file_read(file, ssid, ssid_length) != ssid_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read SSID");
+        storage_file_close(file);
+        storage_file_free(file);
+        furi_record_close(RECORD_STORAGE);
+        return false;
+    }
+    else
+    {
+        ssid[ssid_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the password
+    size_t password_length;
+    if (storage_file_read(file, &password_length, sizeof(size_t)) != sizeof(size_t) || password_length > password_size ||
+        storage_file_read(file, password, password_length) != password_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read password");
+        storage_file_close(file);
+        storage_file_free(file);
+        furi_record_close(RECORD_STORAGE);
+        return false;
+    }
+    else
+    {
+        password[password_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the login_username_logged_out
+    size_t username_out_length;
+    if (storage_file_read(file, &username_out_length, sizeof(size_t)) != sizeof(size_t) || username_out_length > username_out_size ||
+        storage_file_read(file, login_username_logged_out, username_out_length) != username_out_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read login_username_logged_out");
+        // storage_file_close(file);
+        // storage_file_free(file);
+        // furi_record_close(RECORD_STORAGE);
+        // return false;
+    }
+    else
+    {
+        login_username_logged_out[username_out_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the login_username_logged_in
+    size_t username_in_length;
+    if (storage_file_read(file, &username_in_length, sizeof(size_t)) != sizeof(size_t) || username_in_length > username_in_size ||
+        storage_file_read(file, login_username_logged_in, username_in_length) != username_in_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read login_username_logged_in");
+        // storage_file_close(file);
+        // storage_file_free(file);
+        // furi_record_close(RECORD_STORAGE);
+        // return false;
+    }
+    else
+    {
+        login_username_logged_in[username_in_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the login_password_logged_out
+    size_t password_out_length;
+    if (storage_file_read(file, &password_out_length, sizeof(size_t)) != sizeof(size_t) || password_out_length > password_out_size ||
+        storage_file_read(file, login_password_logged_out, password_out_length) != password_out_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read login_password_logged_out");
+        // storage_file_close(file);
+        // storage_file_free(file);
+        // furi_record_close(RECORD_STORAGE);
+        // return false;
+    }
+    else
+    {
+        login_password_logged_out[password_out_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the change_password_logged_in
+    size_t change_password_length;
+    if (storage_file_read(file, &change_password_length, sizeof(size_t)) != sizeof(size_t) || change_password_length > change_password_size ||
+        storage_file_read(file, change_password_logged_in, change_password_length) != change_password_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read change_password_logged_in");
+        // storage_file_close(file);
+        // storage_file_free(file);
+        // furi_record_close(RECORD_STORAGE);
+        //  return false;
+    }
+    else
+    {
+        change_password_logged_in[change_password_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the is_logged_in
+    size_t is_logged_in_length;
+    if (storage_file_read(file, &is_logged_in_length, sizeof(size_t)) != sizeof(size_t) || is_logged_in_length > is_logged_in_size ||
+        storage_file_read(file, is_logged_in, is_logged_in_length) != is_logged_in_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read is_logged_in");
+        // storage_file_close(file);
+        // storage_file_free(file);
+        // furi_record_close(RECORD_STORAGE);
+        //  return false;
+    }
+    else
+    {
+        is_logged_in[is_logged_in_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    // Load the change_bio_logged_in
+    size_t change_bio_length;
+    if (storage_file_read(file, &change_bio_length, sizeof(size_t)) != sizeof(size_t) || change_bio_length > change_bio_size ||
+        storage_file_read(file, change_bio_logged_in, change_bio_length) != change_bio_length)
+    {
+        FURI_LOG_E(TAG, "Failed to read change_bio_logged_in");
+        // storage_file_close(file);
+        // storage_file_free(file);
+        // furi_record_close(RECORD_STORAGE);
+        //  return false;
+    }
+    else
+    {
+        change_bio_logged_in[change_bio_length - 1] = '\0'; // Ensure null-termination
+    }
+
+    storage_file_close(file);
+    storage_file_free(file);
+    furi_record_close(RECORD_STORAGE);
+
+    return true;
+}
+
+FuriString *flip_social_info(char *key)
+{
+    char ssid[64];
+    char password[64];
+    char login_username_logged_out[64];
+    char login_username_logged_in[64];
+    char login_password_logged_out[64];
+    char change_password_logged_in[64];
+    char change_bio_logged_in[64];
+    char is_logged_in[64];
+    FuriString *result = furi_string_alloc();
+    if (!result)
+    {
+        FURI_LOG_E(TAG, "Failed to allocate FuriString");
+        return NULL;
+    }
+    if (!load_flip_social_settings(ssid, sizeof(ssid), password, sizeof(password), login_username_logged_out, sizeof(login_username_logged_out), login_username_logged_in, sizeof(login_username_logged_in), login_password_logged_out, sizeof(login_password_logged_out), change_password_logged_in, sizeof(change_password_logged_in), change_bio_logged_in, sizeof(change_bio_logged_in), is_logged_in, sizeof(is_logged_in)))
+    {
+        FURI_LOG_E(TAG, "Failed to load flip social settings");
+        return NULL;
+    }
+    if (strcmp(key, "ssid") == 0)
+    {
+        furi_string_set_str(result, ssid);
+    }
+    else if (strcmp(key, "password") == 0)
+    {
+        furi_string_set_str(result, password);
+    }
+    else if (strcmp(key, "login_username_logged_out") == 0)
+    {
+        furi_string_set_str(result, login_username_logged_out);
+    }
+    else if (strcmp(key, "login_username_logged_in") == 0)
+    {
+        furi_string_set_str(result, login_username_logged_in);
+    }
+    else if (strcmp(key, "login_password_logged_out") == 0)
+    {
+        furi_string_set_str(result, login_password_logged_out);
+    }
+    else if (strcmp(key, "change_password_logged_in") == 0)
+    {
+        furi_string_set_str(result, change_password_logged_in);
+    }
+    else if (strcmp(key, "change_bio_logged_in") == 0)
+    {
+        furi_string_set_str(result, change_bio_logged_in);
+    }
+    else if (strcmp(key, "is_logged_in") == 0)
+    {
+        furi_string_set_str(result, is_logged_in);
+    }
+    FURI_LOG_E(TAG, "Invalid key");
+    furi_string_free(result);
+    return NULL;
+}
+
+bool is_logged_in_to_flip_social()
+{
+    // load flip social settings and check if logged in
+    FuriString *is_logged_in = flip_social_info("is_logged_in");
+    if (!is_logged_in)
+    {
+        FURI_LOG_E(TAG, "Failed to load is_logged_in");
+        return false;
+    }
+    bool logged_in = furi_string_cmp(is_logged_in, "true") == 0;
+    free(is_logged_in);
+    return logged_in;
+}

+ 3 - 1
flip_storage/storage.h

@@ -41,4 +41,6 @@ FuriString *load_furi_world(
 bool world_exists(
 bool world_exists(
     const char *name);
     const char *name);
 
 
-bool save_world_names(const FuriString *json);
+bool save_world_names(const FuriString *json);
+FuriString *flip_social_info(char *key);
+bool is_logged_in_to_flip_social();