flip_social_explore.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "flip_social_explore.h"
  2. FlipSocialModel *flip_social_explore_alloc(void)
  3. {
  4. if (!app_instance)
  5. {
  6. return NULL;
  7. }
  8. if (!app_instance->submenu_explore)
  9. {
  10. if (!easy_flipper_set_submenu(&app_instance->submenu_explore, FlipSocialViewLoggedInExploreSubmenu, "Explore", flip_social_callback_to_submenu_logged_in, &app_instance->view_dispatcher))
  11. {
  12. return NULL;
  13. }
  14. }
  15. // Allocate memory for each username only if not already allocated
  16. FlipSocialModel *explore = malloc(sizeof(FlipSocialModel));
  17. if (explore == NULL)
  18. {
  19. FURI_LOG_E(TAG, "Failed to allocate memory for explore model.");
  20. return NULL;
  21. }
  22. for (size_t i = 0; i < MAX_EXPLORE_USERS; i++)
  23. {
  24. if (explore->usernames[i] == NULL)
  25. {
  26. explore->usernames[i] = malloc(MAX_USER_LENGTH);
  27. if (explore->usernames[i] == NULL)
  28. {
  29. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  30. return NULL; // Return false on memory allocation failure
  31. }
  32. }
  33. }
  34. return explore;
  35. }
  36. void flip_social_free_explore(void)
  37. {
  38. if (!app_instance)
  39. {
  40. return;
  41. }
  42. if (app_instance->submenu_explore)
  43. {
  44. submenu_free(app_instance->submenu_explore);
  45. app_instance->submenu_explore = NULL;
  46. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  47. }
  48. if (!flip_social_explore)
  49. {
  50. return;
  51. }
  52. free(flip_social_explore);
  53. flip_social_explore = NULL;
  54. }
  55. // for now we're just listing the current users
  56. // as the feed is upgraded, then we can port more to the explore view
  57. bool flip_social_get_explore(void)
  58. {
  59. if (fhttp.state == INACTIVE)
  60. {
  61. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  62. return false;
  63. }
  64. snprintf(
  65. fhttp.file_path,
  66. sizeof(fhttp.file_path),
  67. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/users.json");
  68. fhttp.save_received_data = true;
  69. auth_headers_alloc();
  70. if (!flipper_http_get_request_with_headers("https://www.flipsocial.net/api/user/users/", auth_headers))
  71. {
  72. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  73. fhttp.state = ISSUE;
  74. return false;
  75. }
  76. fhttp.state = RECEIVING;
  77. return true;
  78. }
  79. bool flip_social_parse_json_explore()
  80. {
  81. // load the received data from the saved file
  82. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  83. if (user_data == NULL)
  84. {
  85. FURI_LOG_E(TAG, "Failed to load received data from file.");
  86. return false;
  87. }
  88. char *data_cstr = (char *)furi_string_get_cstr(user_data);
  89. if (data_cstr == NULL)
  90. {
  91. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  92. furi_string_free(user_data);
  93. return false;
  94. }
  95. // Allocate memory for each username only if not already allocated
  96. flip_social_explore = flip_social_explore_alloc();
  97. if (flip_social_explore == NULL)
  98. {
  99. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  100. furi_string_free(user_data);
  101. free(data_cstr);
  102. return false;
  103. }
  104. // Initialize explore count
  105. flip_social_explore->count = 0;
  106. // set submenu
  107. submenu_reset(app_instance->submenu_explore);
  108. submenu_set_header(app_instance->submenu_explore, "Explore");
  109. // Parse the JSON array of usernames
  110. for (size_t i = 0; i < MAX_EXPLORE_USERS; i++)
  111. {
  112. char *username = get_json_array_value("users", i, data_cstr, MAX_TOKENS);
  113. if (username == NULL)
  114. {
  115. break;
  116. }
  117. snprintf(flip_social_explore->usernames[i], MAX_USER_LENGTH, "%s", username);
  118. submenu_add_item(app_instance->submenu_explore, flip_social_explore->usernames[i], FlipSocialSubmenuExploreIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  119. flip_social_explore->count++;
  120. free(username);
  121. }
  122. free(data_cstr);
  123. furi_string_free(user_data);
  124. return flip_social_explore->count > 0;
  125. }