فهرست منبع

FlipLibrary - v1.2

- Improved memory allocation.
- Added in Dog Facts.
- Added in Random Quotes.
jblanked 1 سال پیش
والد
کامیت
2142ce15bb
7فایلهای تغییر یافته به همراه908 افزوده شده و 202 حذف شده
  1. BIN
      .DS_Store
  2. 9 1
      CHANGELOG.md
  3. 234 13
      flip_library_callback.h
  4. 9 5
      flip_library_e.h
  5. 0 5
      flip_library_free.h
  6. 4 2
      flip_library_i.h
  7. 652 176
      flipper_http.h

BIN
.DS_Store


+ 9 - 1
CHANGELOG.md

@@ -1,2 +1,10 @@
+## v1.2
+- Improved memory allocation.
+- Added in Dog Facts.
+- Added in Random Quotes.
+
+## v1.1
+- Update for app catalog.
+
 ## v1.0
 ## v1.0
-- Initial Release
+- Initial Release.

+ 234 - 13
flip_library_callback.h

@@ -12,22 +12,51 @@ static FlipLibraryApp *app_instance = NULL;
 // Parse JSON to find the "text" key
 // Parse JSON to find the "text" key
 char *flip_library_parse_random_fact()
 char *flip_library_parse_random_fact()
 {
 {
-    return get_json_value("text", fhttp.received_data, 128);
+    return get_json_value("text", fhttp.last_response, 128);
 }
 }
 
 
 char *flip_library_parse_cat_fact()
 char *flip_library_parse_cat_fact()
 {
 {
-    return get_json_value("fact", fhttp.received_data, 128);
+    return get_json_value("fact", fhttp.last_response, 128);
+}
+
+char *flip_library_parse_dog_fact()
+{
+    return get_json_array_value("facts", 0, fhttp.last_response, 128);
+}
+
+char *flip_library_parse_quote()
+{
+    // remove [ and ] from the start and end of the string
+    char *response = fhttp.last_response;
+    if (response[0] == '[')
+    {
+        response++;
+    }
+    if (response[strlen(response) - 1] == ']')
+    {
+        response[strlen(response) - 1] = '\0';
+    }
+    // remove white space from both sides
+    while (response[0] == ' ')
+    {
+        response++;
+    }
+    while (response[strlen(response) - 1] == ' ')
+    {
+        response[strlen(response) - 1] = '\0';
+    }
+    return get_json_value("q", response, 128);
 }
 }
 
 
 char *flip_library_parse_dictionary()
 char *flip_library_parse_dictionary()
 {
 {
-    return get_json_value("definition", fhttp.received_data, 16);
+    return get_json_value("definition", fhttp.last_response, 16);
 }
 }
 
 
 static void flip_library_request_error(Canvas *canvas)
 static void flip_library_request_error(Canvas *canvas)
 {
 {
-    if (fhttp.received_data == NULL)
+    if (fhttp.last_response == NULL)
     {
     {
         if (fhttp.last_response != NULL)
         if (fhttp.last_response != NULL)
         {
         {
@@ -161,6 +190,7 @@ static void view_draw_callback_random_facts(Canvas *canvas, void *model)
         canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
         canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
         return;
         return;
     }
     }
+    // Cats
     if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsCats)
     if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsCats)
     {
     {
         canvas_draw_str(canvas, 0, 7, "Random Cat Fact");
         canvas_draw_str(canvas, 0, 7, "Random Cat Fact");
@@ -190,13 +220,13 @@ static void view_draw_callback_random_facts(Canvas *canvas, void *model)
             {
             {
                 flip_library_request_error(canvas);
                 flip_library_request_error(canvas);
             }
             }
-            else if (fhttp.state == IDLE && fhttp.received_data != NULL && !random_fact_request_success_all)
+            else if (fhttp.state == IDLE && fhttp.last_response != NULL && !random_fact_request_success_all)
             {
             {
                 canvas_draw_str(canvas, 0, 22, "Processing...");
                 canvas_draw_str(canvas, 0, 22, "Processing...");
                 // success
                 // success
                 // check status
                 // check status
                 // unnecessary check
                 // unnecessary check
-                if (fhttp.state == ISSUE || fhttp.received_data == NULL)
+                if (fhttp.state == ISSUE || fhttp.last_response == NULL)
                 {
                 {
                     flip_library_request_error(canvas);
                     flip_library_request_error(canvas);
                     FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
                     FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
@@ -234,7 +264,7 @@ static void view_draw_callback_random_facts(Canvas *canvas, void *model)
             else // handle weird scenarios
             else // handle weird scenarios
             {
             {
                 // if received data isnt NULL
                 // if received data isnt NULL
-                if (fhttp.received_data != NULL)
+                if (fhttp.last_response != NULL)
                 {
                 {
                     // parse json to find the text key
                     // parse json to find the text key
                     random_fact = flip_library_parse_cat_fact();
                     random_fact = flip_library_parse_cat_fact();
@@ -249,6 +279,185 @@ static void view_draw_callback_random_facts(Canvas *canvas, void *model)
             }
             }
         }
         }
     }
     }
+    // Dogs
+    else if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsDogs)
+    {
+        canvas_draw_str(canvas, 0, 7, "Random Dog Fact");
+        canvas_draw_str(canvas, 0, 15, "Loading...");
+
+        if (!sent_random_fact_request)
+        {
+            sent_random_fact_request = true;
+            random_fact_request_success = flipper_http_get_request_with_headers("https://dog-api.kinduff.com/api/facts", "{\"Content-Type\":\"application/json\"}");
+            if (!random_fact_request_success)
+            {
+                FURI_LOG_E(TAG, "Failed to send request");
+                flip_library_request_error(canvas);
+                return;
+            }
+            fhttp.state = RECEIVING;
+        }
+        else
+        {
+            if (fhttp.state == RECEIVING)
+            {
+                canvas_draw_str(canvas, 0, 22, "Receiving...");
+                return;
+            }
+            // check status
+            else if (fhttp.state == ISSUE || !random_fact_request_success)
+            {
+                flip_library_request_error(canvas);
+            }
+            else if (fhttp.state == IDLE && fhttp.last_response != NULL && !random_fact_request_success_all)
+            {
+                canvas_draw_str(canvas, 0, 22, "Processing...");
+                // success
+                // check status
+                // unnecessary check
+                if (fhttp.state == ISSUE || fhttp.last_response == NULL)
+                {
+                    flip_library_request_error(canvas);
+                    FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
+                    return;
+                }
+                else if (!random_fact_request_success_all)
+                {
+                    random_fact = flip_library_parse_dog_fact();
+
+                    if (random_fact == NULL)
+                    {
+                        flip_library_request_error(canvas);
+                        fhttp.state = ISSUE;
+                        return;
+                    }
+
+                    // Mark success
+                    random_fact_request_success_all = true;
+
+                    // draw random facts
+                    flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
+
+                    // go to random facts widget
+                    view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
+                }
+            }
+            // likely redundant but just in case
+            else if (fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL)
+            {
+                flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
+
+                // go to random facts widget
+                view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
+            }
+            else // handle weird scenarios
+            {
+                // if received data isnt NULL
+                if (fhttp.last_response != NULL)
+                {
+                    // parse json to find the text key
+                    random_fact = flip_library_parse_cat_fact();
+
+                    if (random_fact == NULL)
+                    {
+                        flip_library_request_error(canvas);
+                        fhttp.state = ISSUE;
+                        return;
+                    }
+                }
+            }
+        }
+    }
+    // Quotes
+    else if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsQuotes)
+    {
+        canvas_draw_str(canvas, 0, 7, "Random Quote");
+        canvas_draw_str(canvas, 0, 15, "Loading...");
+
+        if (!sent_random_fact_request)
+        {
+            sent_random_fact_request = true;
+            random_fact_request_success = flipper_http_get_request("https://zenquotes.io/api/random");
+            if (!random_fact_request_success)
+            {
+                FURI_LOG_E(TAG, "Failed to send request");
+                flip_library_request_error(canvas);
+                return;
+            }
+            fhttp.state = RECEIVING;
+        }
+        else
+        {
+            if (fhttp.state == RECEIVING)
+            {
+                canvas_draw_str(canvas, 0, 22, "Receiving...");
+                return;
+            }
+            // check status
+            else if (fhttp.state == ISSUE || !random_fact_request_success)
+            {
+                flip_library_request_error(canvas);
+            }
+            else if (fhttp.state == IDLE && fhttp.last_response != NULL && !random_fact_request_success_all)
+            {
+                canvas_draw_str(canvas, 0, 22, "Processing...");
+                // success
+                // check status
+                // unnecessary check
+                if (fhttp.state == ISSUE || fhttp.last_response == NULL)
+                {
+                    flip_library_request_error(canvas);
+                    FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
+                    return;
+                }
+                else if (!random_fact_request_success_all)
+                {
+                    random_fact = flip_library_parse_quote();
+
+                    if (random_fact == NULL)
+                    {
+                        flip_library_request_error(canvas);
+                        fhttp.state = ISSUE;
+                        return;
+                    }
+
+                    // Mark success
+                    random_fact_request_success_all = true;
+
+                    // draw random facts
+                    flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
+
+                    // go to random facts widget
+                    view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
+                }
+            }
+            // likely redundant but just in case
+            else if (fhttp.state == IDLE && random_fact_request_success_all && random_fact != NULL)
+            {
+                flip_library_draw_fact(random_fact, &app_instance->widget_random_fact);
+
+                // go to random facts widget
+                view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipLibraryViewRandomFactWidget);
+            }
+            else // handle weird scenarios
+            {
+                // if received data isnt NULL
+                if (fhttp.last_response != NULL)
+                {
+                    // parse json to find the text key
+                    random_fact = flip_library_parse_quote();
+
+                    if (random_fact == NULL)
+                    {
+                        flip_library_request_error(canvas);
+                        fhttp.state = ISSUE;
+                        return;
+                    }
+                }
+            }
+        }
+    }
+    // All Random Facts
     else if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsAll)
     else if (random_facts_index == FlipLibrarySubmenuIndexRandomFactsAll)
     {
     {
         canvas_draw_str(canvas, 0, 10, "Random Fact");
         canvas_draw_str(canvas, 0, 10, "Random Fact");
@@ -281,12 +490,12 @@ static void view_draw_callback_random_facts(Canvas *canvas, void *model)
                 flip_library_request_error(canvas);
                 flip_library_request_error(canvas);
                 return;
                 return;
             }
             }
-            else if (fhttp.state == IDLE && fhttp.received_data != NULL && !random_fact_request_success_all)
+            else if (fhttp.state == IDLE && fhttp.last_response != NULL && !random_fact_request_success_all)
             {
             {
                 canvas_draw_str(canvas, 0, 30, "Processing...");
                 canvas_draw_str(canvas, 0, 30, "Processing...");
                 // success
                 // success
                 // check status
                 // check status
-                if (fhttp.state == ISSUE || fhttp.received_data == NULL)
+                if (fhttp.state == ISSUE || fhttp.last_response == NULL)
                 {
                 {
                     flip_library_request_error(canvas);
                     flip_library_request_error(canvas);
                     FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
                     FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
@@ -324,7 +533,7 @@ static void view_draw_callback_random_facts(Canvas *canvas, void *model)
             else // handle weird scenarios
             else // handle weird scenarios
             {
             {
                 // if received data isnt NULL
                 // if received data isnt NULL
-                if (fhttp.received_data != NULL)
+                if (fhttp.last_response != NULL)
                 {
                 {
                     // parse json to find the text key
                     // parse json to find the text key
                     random_fact = flip_library_parse_random_fact();
                     random_fact = flip_library_parse_random_fact();
@@ -398,12 +607,12 @@ static void view_draw_callback_dictionary_run(Canvas *canvas, void *model)
             flip_library_request_error(canvas);
             flip_library_request_error(canvas);
             return;
             return;
         }
         }
-        else if (fhttp.state == IDLE && fhttp.received_data != NULL && !random_fact_request_success_all)
+        else if (fhttp.state == IDLE && fhttp.last_response != NULL && !random_fact_request_success_all)
         {
         {
             canvas_draw_str(canvas, 0, 20, "Processing...");
             canvas_draw_str(canvas, 0, 20, "Processing...");
             // success
             // success
             // check status
             // check status
-            if (fhttp.state == ISSUE || fhttp.received_data == NULL)
+            if (fhttp.state == ISSUE || fhttp.last_response == NULL)
             {
             {
                 flip_library_request_error(canvas);
                 flip_library_request_error(canvas);
                 FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
                 FURI_LOG_E(TAG, "HTTP request failed or received data is NULL");
@@ -441,7 +650,7 @@ static void view_draw_callback_dictionary_run(Canvas *canvas, void *model)
         else // handle weird scenarios
         else // handle weird scenarios
         {
         {
             // if received data isnt NULL
             // if received data isnt NULL
-            if (fhttp.received_data != NULL)
+            if (fhttp.last_response != NULL)
             {
             {
                 // parse json to find the text key
                 // parse json to find the text key
                 char *definition = flip_library_parse_dictionary();
                 char *definition = flip_library_parse_dictionary();
@@ -515,6 +724,18 @@ static void callback_submenu_choices(void *context, uint32_t index)
         random_fact = NULL;
         random_fact = NULL;
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
         break;
         break;
+    case FlipLibrarySubmenuIndexRandomFactsDogs:
+        random_facts_index = FlipLibrarySubmenuIndexRandomFactsDogs;
+        sent_random_fact_request = false;
+        random_fact = NULL;
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
+        break;
+    case FlipLibrarySubmenuIndexRandomFactsQuotes:
+        random_facts_index = FlipLibrarySubmenuIndexRandomFactsQuotes;
+        sent_random_fact_request = false;
+        random_fact = NULL;
+        view_dispatcher_switch_to_view(app->view_dispatcher, FlipLibraryViewRandomFactsRun);
+        break;
     case FlipLibrarySubmenuIndexRandomFactsAll:
     case FlipLibrarySubmenuIndexRandomFactsAll:
         random_facts_index = FlipLibrarySubmenuIndexRandomFactsAll;
         random_facts_index = FlipLibrarySubmenuIndexRandomFactsAll;
         sent_random_fact_request = false;
         sent_random_fact_request = false;

+ 9 - 5
flip_library_e.h

@@ -23,8 +23,10 @@ typedef enum
     FlipLibrarySubmenuIndexAbout,       // Click to view the about screen
     FlipLibrarySubmenuIndexAbout,       // Click to view the about screen
     FlipLibrarySubmenuIndexSettings,    // Click to view the WiFi settings
     FlipLibrarySubmenuIndexSettings,    // Click to view the WiFi settings
     //
     //
-    FlipLibrarySubmenuIndexRandomFactsCats, // Click to view the random facts (cats)
-    FlipLibrarySubmenuIndexRandomFactsAll,  // Click to view the random facts (all)
+    FlipLibrarySubmenuIndexRandomFactsCats,   // Click to view the random facts (cats)
+    FlipLibrarySubmenuIndexRandomFactsDogs,   // Click to view the random facts (dogs)
+    FlipLibrarySubmenuIndexRandomFactsQuotes, // Click to view the random facts (quotes)
+    FlipLibrarySubmenuIndexRandomFactsAll,    // Click to view the random facts (all)
 } FlipLibrarySubmenuIndex;
 } FlipLibrarySubmenuIndex;
 
 
 // Define a single view for our FlipLibrary application
 // Define a single view for our FlipLibrary application
@@ -43,10 +45,12 @@ typedef enum
     FlipLibraryViewDictionaryRun = 16,
     FlipLibraryViewDictionaryRun = 16,
     //
     //
     FlipLibraryViewRandomFactsCats = 17,
     FlipLibraryViewRandomFactsCats = 17,
-    FlipLibraryViewRandomFactsAll = 18,
+    FlipLibraryViewRandomFactsDogs = 18,
+    FlipLibraryViewRandomFactsQuotes = 19,
+    FlipLibraryViewRandomFactsAll = 20,
     //
     //
-    FlipLibraryViewRandomFactWidget = 19, // The text box that displays the random fact
-    FlipLibraryViewDictionaryWidget = 20, // The text box that displays the dictionary
+    FlipLibraryViewRandomFactWidget = 21, // The text box that displays the random fact
+    FlipLibraryViewDictionaryWidget = 22, // The text box that displays the dictionary
 } FlipLibraryView;
 } FlipLibraryView;
 
 
 // Each screen will have its own view
 // Each screen will have its own view

+ 0 - 5
flip_library_free.h

@@ -9,11 +9,6 @@ static void flip_library_app_free(FlipLibraryApp *app)
         FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
         FURI_LOG_E(TAG, "FlipLibraryApp is NULL");
         return;
         return;
     }
     }
-    // disconnect wifi
-    if (!flipper_http_disconnect_wifi())
-    {
-        FURI_LOG_E(TAG, "Failed to disconnect from wifi");
-    }
 
 
     // Free View(s)
     // Free View(s)
     if (app->view_random_facts)
     if (app->view_random_facts)

+ 4 - 2
flip_library_i.h

@@ -60,7 +60,7 @@ static FlipLibraryApp *flip_library_app_alloc()
     }
     }
 
 
     // Widget
     // Widget
-    if (!easy_flipper_set_widget(&app->widget, FlipLibraryViewAbout, "FlipLibrary v1.0\n-----\nDictionary, random facts, and\nmore.\n-----\nwww.github.com/jblanked", callback_to_submenu, &app->view_dispatcher))
+    if (!easy_flipper_set_widget(&app->widget, FlipLibraryViewAbout, "FlipLibrary v1.2\n-----\nDictionary, random facts, and\nmore.\n-----\nwww.github.com/jblanked", callback_to_submenu, &app->view_dispatcher))
     {
     {
         return NULL;
         return NULL;
     }
     }
@@ -99,7 +99,7 @@ static FlipLibraryApp *flip_library_app_alloc()
     variable_item_set_current_value_text(app->variable_item_password, "");
     variable_item_set_current_value_text(app->variable_item_password, "");
 
 
     // Submenu
     // Submenu
-    if (!easy_flipper_set_submenu(&app->submenu_main, FlipLibraryViewSubmenuMain, "FlipLibrary v1.0", callback_exit_app, &app->view_dispatcher))
+    if (!easy_flipper_set_submenu(&app->submenu_main, FlipLibraryViewSubmenuMain, "FlipLibrary v1.2", callback_exit_app, &app->view_dispatcher))
     {
     {
         return NULL;
         return NULL;
     }
     }
@@ -114,6 +114,8 @@ static FlipLibraryApp *flip_library_app_alloc()
     submenu_add_item(app->submenu_main, "WiFi", FlipLibrarySubmenuIndexSettings, callback_submenu_choices, app);
     submenu_add_item(app->submenu_main, "WiFi", FlipLibrarySubmenuIndexSettings, callback_submenu_choices, app);
     //
     //
     submenu_add_item(app->submenu_random_facts, "Cats", FlipLibrarySubmenuIndexRandomFactsCats, callback_submenu_choices, app);
     submenu_add_item(app->submenu_random_facts, "Cats", FlipLibrarySubmenuIndexRandomFactsCats, callback_submenu_choices, app);
+    submenu_add_item(app->submenu_random_facts, "Dogs", FlipLibrarySubmenuIndexRandomFactsDogs, callback_submenu_choices, app);
+    submenu_add_item(app->submenu_random_facts, "Quotes", FlipLibrarySubmenuIndexRandomFactsQuotes, callback_submenu_choices, app);
     submenu_add_item(app->submenu_random_facts, "Random", FlipLibrarySubmenuIndexRandomFactsAll, callback_submenu_choices, app);
     submenu_add_item(app->submenu_random_facts, "Random", FlipLibrarySubmenuIndexRandomFactsAll, callback_submenu_choices, app);
 
 
     // load settings
     // load settings

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 652 - 176
flipper_http.h


برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است