From 403453c3fcd085ce9af2cd05f3bf6c1212a7ca11 Mon Sep 17 00:00:00 2001 From: LiquidFenrir Date: Fri, 11 May 2018 15:53:38 +0200 Subject: [PATCH] add a loading bar (#159) - when loading icons - when downloading via QR - when loading list in the browser - when loading preview/BGM in the browser - when downloading from the browser --- include/draw.h | 1 + include/remote.h | 3 ++- source/camera.c | 2 +- source/draw.c | 22 ++++++++++++++++++++-- source/loading.c | 3 +++ source/remote.c | 21 ++++++++++++--------- 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/include/draw.h b/include/draw.h index 1845deb..d2b2395 100644 --- a/include/draw.h +++ b/include/draw.h @@ -93,6 +93,7 @@ bool draw_confirm(const char* conf_msg, Entry_List_s* list); void draw_preview(ssize_t previewID, int preview_offset); void draw_install(InstallType type); +void draw_loading_bar(u32 current, u32 max, InstallType type); void draw_base_interface(void); void draw_grid_interface(Entry_List_s* list, Instructions_s instructions); diff --git a/include/remote.h b/include/remote.h index d6b1514..afc5865 100644 --- a/include/remote.h +++ b/include/remote.h @@ -28,6 +28,7 @@ #define REMOTE_H #include "common.h" +#include "draw.h" #define THEMEPLAZA_BASE_URL "https://themeplaza.eu" #define THEMEPLAZA_API_URL "/api/anemone/v1" @@ -50,6 +51,6 @@ #define CACHE_PATH_FORMAT "/3ds/" APP_TITLE "/cache/%" JSON_INTEGER_FORMAT bool themeplaza_browser(EntryMode mode); -u32 http_get(const char *url, char ** filename, char ** buf); +u32 http_get(const char *url, char ** filename, char ** buf, InstallType install_type); #endif diff --git a/source/camera.c b/source/camera.c index a38bfea..1d7c355 100644 --- a/source/camera.c +++ b/source/camera.c @@ -206,7 +206,7 @@ void update_qr(qr_data *data) draw_install(INSTALL_DOWNLOAD); char * zip_buf = NULL; char * filename = NULL; - u32 zip_size = http_get((char*)scan_data.payload, &filename, &zip_buf); + u32 zip_size = http_get((char*)scan_data.payload, &filename, &zip_buf, INSTALL_DOWNLOAD); if(zip_size != 0) { diff --git a/source/draw.c b/source/draw.c index 01aef7e..72d3b84 100644 --- a/source/draw.c +++ b/source/draw.c @@ -182,9 +182,8 @@ void draw_preview(ssize_t previewID, int preview_offset) pp2d_draw_texture_part(previewID, 0, 0, 40 + preview_offset, 240, 320, 240); } -void draw_install(InstallType type) +static void draw_install_handler(InstallType type) { - draw_base_interface(); switch(type) { case INSTALL_LOADING_THEMES: @@ -238,6 +237,25 @@ void draw_install(InstallType type) default: break; } +} + +void draw_install(InstallType type) +{ + draw_base_interface(); + draw_install_handler(type); + pp2d_end_draw(); +} + +void draw_loading_bar(u32 current, u32 max, InstallType type) +{ + draw_base_interface(); + draw_install_handler(type); + pp2d_draw_on(GFX_BOTTOM, GFX_LEFT); + double percent = 100*((double)current/(double)max); + u32 width = (u32)percent; + width *= 2; + pp2d_draw_rectangle(60-1, 110-1, 200+2, 20+2, COLOR_CURSOR); + pp2d_draw_rectangle(60, 110, width, 20, COLOR_ACCENT); pp2d_end_draw(); } diff --git a/source/loading.c b/source/loading.c index 2db4f6d..3530ff7 100644 --- a/source/loading.c +++ b/source/loading.c @@ -230,6 +230,9 @@ void load_icons_first(Entry_List_s * list, bool silent) for(int i = starti; i < endi; i++, id++) { + if(!silent) + draw_loading_bar(i - starti, endi-starti, INSTALL_LOADING_ICONS); + int offset = i; if(offset < 0) offset += list->entries_count; diff --git a/source/remote.c b/source/remote.c index cc6039c..f94c6a4 100644 --- a/source/remote.c +++ b/source/remote.c @@ -26,7 +26,6 @@ #include "remote.h" #include "loading.h" -#include "draw.h" #include "fs.h" #include "unicode.h" #include "music.h" @@ -115,7 +114,7 @@ static void load_remote_smdh(Entry_s * entry, size_t textureID, bool ignore_cach smdh_buf = NULL; char * api_url = NULL; asprintf(&api_url, THEMEPLAZA_SMDH_FORMAT, entry->tp_download_id); - smdh_size = http_get(api_url, NULL, &smdh_buf); + smdh_size = http_get(api_url, NULL, &smdh_buf, INSTALL_NONE); free(api_url); smdh = (Icon_s *)smdh_buf; } @@ -164,7 +163,7 @@ static void load_remote_smdh(Entry_s * entry, size_t textureID, bool ignore_cach free(smdh_buf); } -static void load_remote_entries(Entry_List_s * list, json_t *ids_array, bool ignore_cache) +static void load_remote_entries(Entry_List_s * list, json_t *ids_array, bool ignore_cache, InstallType type) { list->entries_count = json_array_size(ids_array); free(list->entries); @@ -177,6 +176,7 @@ static void load_remote_entries(Entry_List_s * list, json_t *ids_array, bool ign json_t * id = NULL; json_array_foreach(ids_array, i, id) { + draw_loading_bar(i, list->entries_count, type); size_t offset = i; Entry_s * current_entry = &list->entries[offset]; current_entry->tp_download_id = json_integer_value(id); @@ -209,7 +209,7 @@ static void load_remote_list(Entry_List_s * list, json_int_t page, EntryMode mod char * page_json = NULL; char * api_url = NULL; asprintf(&api_url, THEMEPLAZA_PAGE_FORMAT, page, mode+1, list->tp_search); - u32 json_len = http_get(api_url, NULL, &page_json); + u32 json_len = http_get(api_url, NULL, &page_json, INSTALL_NONE); free(api_url); if(json_len) @@ -232,7 +232,7 @@ static void load_remote_list(Entry_List_s * list, json_int_t page, EntryMode mod if(json_is_integer(value) && !strcmp(key, THEMEPLAZA_JSON_PAGE_COUNT)) list->tp_page_count = json_integer_value(value); else if(json_is_array(value) && !strcmp(key, THEMEPLAZA_JSON_PAGE_IDS)) - load_remote_entries(list, value, ignore_cache); + load_remote_entries(list, value, ignore_cache, loading_screen); else if(json_is_string(value) && !strcmp(key, THEMEPLAZA_JSON_ERROR_MESSAGE) && !strcmp(json_string_value(value), THEMEPLAZA_JSON_ERROR_MESSAGE_NOT_FOUND)) throw_error("No results for this search.", ERROR_LEVEL_WARNING); } @@ -270,7 +270,7 @@ static bool load_remote_preview(Entry_s * entry, int * preview_offset) draw_install(INSTALL_LOADING_REMOTE_PREVIEW); - preview_size = http_get(preview_url, NULL, &preview_png); + preview_size = http_get(preview_url, NULL, &preview_png, INSTALL_LOADING_REMOTE_PREVIEW); free(preview_url); } @@ -343,7 +343,7 @@ static void load_remote_bgm(Entry_s * entry) draw_install(INSTALL_LOADING_REMOTE_BGM); - bgm_size = http_get(bgm_url, NULL, &bgm_ogg); + bgm_size = http_get(bgm_url, NULL, &bgm_ogg, INSTALL_LOADING_REMOTE_BGM); free(bgm_url); u16 path[0x107] = {0}; @@ -366,7 +366,7 @@ static void download_remote_entry(Entry_s * entry, EntryMode mode) char * zip_buf = NULL; char * filename = NULL; draw_install(INSTALL_DOWNLOAD); - u32 zip_size = http_get(download_url, &filename, &zip_buf); + u32 zip_size = http_get(download_url, &filename, &zip_buf, INSTALL_DOWNLOAD); free(download_url); char path_to_file[0x107] = {0}; @@ -770,7 +770,7 @@ bool themeplaza_browser(EntryMode mode) return downloaded; } -u32 http_get(const char *url, char ** filename, char ** buf) +u32 http_get(const char *url, char ** filename, char ** buf, InstallType install_type) { Result ret; httpcContext context; @@ -894,6 +894,9 @@ u32 http_get(const char *url, char ** filename, char ** buf) ret = httpcDownloadData(&context, (*(u8**)buf) + size, 0x1000, &read_size); size += read_size; + if(content_size && install_type != INSTALL_NONE) + draw_loading_bar(size, content_size, install_type); + if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) { last_buf = *buf;