flip_social_feed.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef FLIP_SOCIAL_FEED_H
  2. #define FLIP_SOCIAL_FEED_H
  3. static FlipSocialApp *app_instance = NULL;
  4. #define MAX_TOKENS 512 // Adjust based on expected JSON tokens
  5. typedef struct
  6. {
  7. char *usernames[128];
  8. char *messages[128];
  9. bool is_flipped[128];
  10. uint32_t ids[128];
  11. size_t count;
  12. size_t index;
  13. } FlipSocialFeed;
  14. #define MAX_FEED_ITEMS 128
  15. #define MAX_LINE_LENGTH 30
  16. // temporary FlipSocialFeed object
  17. static FlipSocialFeed flip_social_feed = {
  18. .usernames = {"JBlanked", "FlipperKing", "FlipperQueen"},
  19. .messages = {"Welcome. This is a temp message. Either the feed didn't load or there was a server error.", "I am the Chosen Flipper.", "No one can flip like me."},
  20. .is_flipped = {false, false, true},
  21. .ids = {0, 1, 2},
  22. .count = 3,
  23. .index = 0};
  24. bool flip_social_get_feed()
  25. {
  26. // Get the feed from the server
  27. if (app_instance->login_username_logged_out == NULL)
  28. {
  29. FURI_LOG_E(TAG, "Username is NULL");
  30. return false;
  31. }
  32. char command[256];
  33. snprintf(command, 128, "https://www.flipsocial.net/api/feed/20/%s/", app_instance->login_username_logged_out);
  34. bool success = flipper_http_get_request_with_headers(command, "{\"Content-Type\":\"application/json\"}");
  35. if (!success)
  36. {
  37. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  38. return false;
  39. }
  40. fhttp.state = RECEIVING;
  41. return true;
  42. }
  43. bool flip_social_parse_json_feed()
  44. {
  45. if (fhttp.received_data == NULL)
  46. {
  47. FURI_LOG_E(TAG, "No data received.");
  48. return false;
  49. }
  50. // Remove newlines
  51. char *pos = fhttp.received_data;
  52. while ((pos = strchr(pos, '\n')) != NULL)
  53. {
  54. *pos = ' ';
  55. }
  56. // Initialize feed count
  57. flip_social_feed.count = 0;
  58. // Iterate through the feed array
  59. for (int i = 0; i < MAX_FEED_ITEMS; i++)
  60. {
  61. // Parse each item in the array
  62. char *item = get_json_array_value("feed", i, fhttp.received_data, MAX_TOKENS);
  63. if (item == NULL)
  64. {
  65. break;
  66. }
  67. // Extract individual fields from the JSON object
  68. char *username = get_json_value("username", item, MAX_TOKENS);
  69. char *message = get_json_value("message", item, MAX_TOKENS);
  70. char *flipped = get_json_value("flipped", item, MAX_TOKENS);
  71. char *id = get_json_value("id", item, MAX_TOKENS);
  72. if (username == NULL || message == NULL || flipped == NULL || id == NULL)
  73. {
  74. FURI_LOG_E(TAG, "Failed to parse item fields.");
  75. free(item);
  76. continue;
  77. }
  78. // Store parsed values
  79. flip_social_feed.usernames[i] = username;
  80. flip_social_feed.messages[i] = message;
  81. flip_social_feed.is_flipped[i] = strstr(flipped, "true") != NULL;
  82. flip_social_feed.ids[i] = atoi(id);
  83. flip_social_feed.count++;
  84. free(item);
  85. }
  86. return true;
  87. }
  88. bool flip_social_board_is_active(Canvas *canvas)
  89. {
  90. if (fhttp.state == INACTIVE)
  91. {
  92. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  93. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  94. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  95. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  96. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  97. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  98. return false;
  99. }
  100. return true;
  101. }
  102. void flip_social_handle_error(Canvas *canvas)
  103. {
  104. if (fhttp.received_data != NULL)
  105. {
  106. if (strstr(fhttp.received_data, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  107. {
  108. canvas_clear(canvas);
  109. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  110. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  111. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  112. }
  113. else if (strstr(fhttp.received_data, "[ERROR] Failed to connect to Wifi.") != NULL)
  114. {
  115. canvas_clear(canvas);
  116. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  117. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  118. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  119. }
  120. else
  121. {
  122. canvas_draw_str(canvas, 0, 42, "Failed...");
  123. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  124. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  125. }
  126. }
  127. else
  128. {
  129. canvas_draw_str(canvas, 0, 42, "Failed...");
  130. canvas_draw_str(canvas, 0, 52, "Update your credentials.");
  131. canvas_draw_str(canvas, 0, 62, "Press BACK to return.");
  132. }
  133. }
  134. #endif // FLIP_SOCIAL_FEED_H