flip_social_explore.h 4.4 KB

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