flip_social_explore.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef FLIP_SOCIAL_EXPLORE_H
  2. #define FLIP_SOCIAL_EXPLORE_H
  3. static FlipSocialModel* flip_social_explore_alloc() {
  4. // Allocate memory for each username only if not already allocated
  5. FlipSocialModel* explore = malloc(sizeof(FlipSocialModel));
  6. if(explore == NULL) {
  7. FURI_LOG_E(TAG, "Failed to allocate memory for explore model.");
  8. return NULL;
  9. }
  10. for(size_t i = 0; i < MAX_EXPLORE_USERS; i++) {
  11. if(explore->usernames[i] == NULL) {
  12. explore->usernames[i] = malloc(MAX_USER_LENGTH);
  13. if(explore->usernames[i] == NULL) {
  14. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  15. return NULL; // Return false on memory allocation failure
  16. }
  17. }
  18. }
  19. return explore;
  20. }
  21. static void flip_social_free_explore() {
  22. if(!flip_social_explore) {
  23. FURI_LOG_E(TAG, "Explore model is NULL");
  24. return;
  25. }
  26. for(int i = 0; i < flip_social_explore->count; i++) {
  27. free(flip_social_explore->usernames[i]);
  28. }
  29. }
  30. // for now we're just listing the current users
  31. // as the feed is upgraded, then we can port more to the explore view
  32. static bool flip_social_get_explore() {
  33. // will return true unless the devboard is not connected
  34. bool success = flipper_http_get_request_with_headers(
  35. "https://www.flipsocial.net/api/user/users/", jsmn("Content-Type", "application/json"));
  36. if(!success) {
  37. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  38. return false;
  39. }
  40. fhttp.state = RECEIVING;
  41. return true;
  42. }
  43. static bool flip_social_parse_json_explore() {
  44. if(fhttp.received_data == NULL) {
  45. FURI_LOG_E(TAG, "No data received.");
  46. return false;
  47. }
  48. // Allocate memory for each username only if not already allocated
  49. flip_social_explore = flip_social_explore_alloc();
  50. if(flip_social_explore == NULL) {
  51. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  52. return false;
  53. }
  54. // Remove newlines
  55. char* pos = fhttp.received_data;
  56. while((pos = strchr(pos, '\n')) != NULL) {
  57. *pos = ' ';
  58. }
  59. // Initialize explore count
  60. flip_social_explore->count = 0;
  61. // Extract the users array from the JSON
  62. char* json_users = get_json_value("users", fhttp.received_data, MAX_TOKENS);
  63. if(json_users == NULL) {
  64. FURI_LOG_E(TAG, "Failed to parse users array.");
  65. return false;
  66. }
  67. // Manual tokenization for comma-separated values
  68. char* start = json_users + 1; // Skip the opening bracket
  69. char* end;
  70. while((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS) {
  71. *end = '\0'; // Null-terminate the current token
  72. // Remove quotes
  73. if(*start == '"') start++;
  74. if(*(end - 1) == '"') *(end - 1) = '\0';
  75. // Copy username to pre-allocated memory
  76. strncpy(
  77. flip_social_explore->usernames[flip_social_explore->count],
  78. start,
  79. MAX_USER_LENGTH - 1);
  80. flip_social_explore->usernames[flip_social_explore->count][MAX_USER_LENGTH - 1] =
  81. '\0'; // Ensure null termination
  82. flip_social_explore->count++;
  83. start = end + 1;
  84. }
  85. // Handle the last token
  86. if(*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS) {
  87. if(*start == '"') start++;
  88. if(*(start + strlen(start) - 1) == ']') *(start + strlen(start) - 1) = '\0';
  89. if(*(start + strlen(start) - 1) == '"') *(start + strlen(start) - 1) = '\0';
  90. strncpy(
  91. flip_social_explore->usernames[flip_social_explore->count],
  92. start,
  93. MAX_USER_LENGTH - 1);
  94. flip_social_explore->usernames[flip_social_explore->count][MAX_USER_LENGTH - 1] =
  95. '\0'; // Ensure null termination
  96. flip_social_explore->count++;
  97. }
  98. // Add submenu items for the users
  99. submenu_reset(app_instance->submenu_explore);
  100. submenu_set_header(app_instance->submenu_explore, "Explore");
  101. for(int i = 0; i < flip_social_explore->count; i++) {
  102. submenu_add_item(
  103. app_instance->submenu_explore,
  104. flip_social_explore->usernames[i],
  105. FlipSocialSubmenuExploreIndexStartIndex + i,
  106. flip_social_callback_submenu_choices,
  107. app_instance);
  108. }
  109. // Free the json_users
  110. free(json_users);
  111. free(start);
  112. free(end);
  113. return true;
  114. }
  115. #endif // FLIP_SOCIAL_EXPLORE_H