From fcd33cb8da3a477480aae31afe353c91012b17f3 Mon Sep 17 00:00:00 2001 From: Dig Date: Tue, 6 Sep 2022 11:58:28 +0100 Subject: [PATCH 1/3] File Browser Ordering prototype --- .../main/archive/helpers/archive_browser.c | 8 ++++++- .../main/archive/helpers/archive_files.h | 18 ++++++++++++++- .../services/gui/modules/file_browser.c | 23 +++++++++++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/applications/main/archive/helpers/archive_browser.c b/applications/main/archive/helpers/archive_browser.c index 9689454ba..2ebc099b8 100644 --- a/applications/main/archive/helpers/archive_browser.c +++ b/applications/main/archive/helpers/archive_browser.c @@ -64,7 +64,13 @@ static void archive_add_file_item(browser, is_folder, furi_string_get_cstr(item_path)); } else { with_view_model( - browser->view, ArchiveBrowserViewModel * model, { model->list_loading = false; }, true); + browser->view, + ArchiveBrowserViewModel * model, + { + files_array_special_sort(model->files, ArchiveFile_t_cmp); + model->list_loading = false; + }, + true); } } diff --git a/applications/main/archive/helpers/archive_files.h b/applications/main/archive/helpers/archive_files.h index 1822befa3..2fd322ede 100644 --- a/applications/main/archive/helpers/archive_files.h +++ b/applications/main/archive/helpers/archive_files.h @@ -76,13 +76,29 @@ static void ArchiveFile_t_clear(ArchiveFile_t* obj) { furi_string_free(obj->custom_name); } +static int ArchiveFile_t_cmp(const ArchiveFile_t* a, const ArchiveFile_t* b) { + if(a->type == ArchiveFileTypeFolder && b->type != ArchiveFileTypeFolder) { + return -1; + } + + return string_cmp(a->path, b->path); +} + +static void ArchiveFile_t_swap(ArchiveFile_t* a, ArchiveFile_t* b) { + ArchiveFile_t tmp = *a; + *a = *b; + *b = tmp; +} + ARRAY_DEF( files_array, ArchiveFile_t, (INIT(API_2(ArchiveFile_t_init)), SET(API_6(ArchiveFile_t_set)), INIT_SET(API_6(ArchiveFile_t_init_set)), - CLEAR(API_2(ArchiveFile_t_clear)))) + CLEAR(API_2(ArchiveFile_t_clear)), + CMP(API_6(ArchiveFile_t_cmp)), + SWAP(API_6(ArchiveFile_t_swap)))) void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app); bool archive_get_items(void* context, const char* path); diff --git a/applications/services/gui/modules/file_browser.c b/applications/services/gui/modules/file_browser.c index 60e78b01c..faf7d71fc 100644 --- a/applications/services/gui/modules/file_browser.c +++ b/applications/services/gui/modules/file_browser.c @@ -71,13 +71,26 @@ static void BrowserItem_t_clear(BrowserItem_t* obj) { } } +static int BrowserItem_t_cmp(const BrowserItem_t* a, const BrowserItem_t* b) { + // Back indicator comes before everything, then folders, then all other files. + if((a->type == BrowserItemTypeBack) || + (a->type == BrowserItemTypeFolder && b->type != BrowserItemTypeFolder && + b->type != BrowserItemTypeBack)) { + return -1; + } + + return string_cmp(a->path, b->path); +} + ARRAY_DEF( items_array, BrowserItem_t, (INIT(API_2(BrowserItem_t_init)), SET(API_6(BrowserItem_t_set)), INIT_SET(API_6(BrowserItem_t_init_set)), - CLEAR(API_2(BrowserItem_t_clear)))) + CLEAR(API_2(BrowserItem_t_clear)), + CMP(API_6(BrowserItem_t_cmp)), + SWAP(M_SWAP_DEFAULT))) struct FileBrowser { View* view; @@ -388,7 +401,13 @@ static void } } else { with_view_model( - browser->view, FileBrowserModel * model, { model->list_loading = false; }, true); + browser->view, + FileBrowserModel * model, + { + items_array_special_sort(model->items, BrowserItem_t_cmp); + model->list_loading = false; + }, + true); } } From 1f9ad813569ecdac54cdec97252ca9dba9f8ed97 Mon Sep 17 00:00:00 2001 From: Dig Date: Tue, 6 Sep 2022 16:42:04 +0100 Subject: [PATCH 2/3] convert impl to use m-algo and *_sort funcs --- .../main/archive/helpers/archive_browser.c | 2 +- .../main/archive/helpers/archive_files.h | 27 +++++++++---------- .../services/gui/modules/file_browser.c | 25 ++++++++++------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/applications/main/archive/helpers/archive_browser.c b/applications/main/archive/helpers/archive_browser.c index 2ebc099b8..95cc8f7ab 100644 --- a/applications/main/archive/helpers/archive_browser.c +++ b/applications/main/archive/helpers/archive_browser.c @@ -67,7 +67,7 @@ static void browser->view, ArchiveBrowserViewModel * model, { - files_array_special_sort(model->files, ArchiveFile_t_cmp); + files_array_sort(model->files); model->list_loading = false; }, true); diff --git a/applications/main/archive/helpers/archive_files.h b/applications/main/archive/helpers/archive_files.h index 2fd322ede..72f6be45f 100644 --- a/applications/main/archive/helpers/archive_files.h +++ b/applications/main/archive/helpers/archive_files.h @@ -2,6 +2,8 @@ #include #include +#include +#include #include #define FAP_MANIFEST_MAX_ICON_SIZE 32 @@ -84,21 +86,18 @@ static int ArchiveFile_t_cmp(const ArchiveFile_t* a, const ArchiveFile_t* b) { return string_cmp(a->path, b->path); } -static void ArchiveFile_t_swap(ArchiveFile_t* a, ArchiveFile_t* b) { - ArchiveFile_t tmp = *a; - *a = *b; - *b = tmp; -} +#define M_OPL_ArchiveFile_t() \ + (INIT(API_2(ArchiveFile_t_init)), \ + SET(API_6(ArchiveFile_t_set)), \ + INIT_SET(API_6(ArchiveFile_t_init_set)), \ + CLEAR(API_2(ArchiveFile_t_clear)), \ + CMP(API_6(ArchiveFile_t_cmp)), \ + SWAP(M_SWAP_DEFAULT), \ + EQUAL(API_6(M_EQUAL_DEFAULT))) -ARRAY_DEF( - files_array, - ArchiveFile_t, - (INIT(API_2(ArchiveFile_t_init)), - SET(API_6(ArchiveFile_t_set)), - INIT_SET(API_6(ArchiveFile_t_init_set)), - CLEAR(API_2(ArchiveFile_t_clear)), - CMP(API_6(ArchiveFile_t_cmp)), - SWAP(API_6(ArchiveFile_t_swap)))) +ARRAY_DEF(files_array, ArchiveFile_t) + +ALGO_DEF(files_array, ARRAY_OPLIST(files_array, M_OPL_ArchiveFile_t())) void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app); bool archive_get_items(void* context, const char* path); diff --git a/applications/services/gui/modules/file_browser.c b/applications/services/gui/modules/file_browser.c index faf7d71fc..57e49c604 100644 --- a/applications/services/gui/modules/file_browser.c +++ b/applications/services/gui/modules/file_browser.c @@ -5,6 +5,8 @@ #include #include #include "furi_hal_resources.h" +#include "m-string.h" +#include "m-algo.h" #include #include #include @@ -82,15 +84,18 @@ static int BrowserItem_t_cmp(const BrowserItem_t* a, const BrowserItem_t* b) { return string_cmp(a->path, b->path); } -ARRAY_DEF( - items_array, - BrowserItem_t, - (INIT(API_2(BrowserItem_t_init)), - SET(API_6(BrowserItem_t_set)), - INIT_SET(API_6(BrowserItem_t_init_set)), - CLEAR(API_2(BrowserItem_t_clear)), - CMP(API_6(BrowserItem_t_cmp)), - SWAP(M_SWAP_DEFAULT))) +#define M_OPL_BrowserItem_t() \ + (INIT(API_2(BrowserItem_t_init)), \ + SET(API_6(BrowserItem_t_set)), \ + INIT_SET(API_6(BrowserItem_t_init_set)), \ + CLEAR(API_2(BrowserItem_t_clear)), \ + CMP(API_6(BrowserItem_t_cmp)), \ + SWAP(M_SWAP_DEFAULT), \ + EQUAL(API_6(M_EQUAL_DEFAULT))) + +ARRAY_DEF(items_array, BrowserItem_t) + +ALGO_DEF(items_array, ARRAY_OPLIST(items_array, M_OPL_BrowserItem_t())) struct FileBrowser { View* view; @@ -404,7 +409,7 @@ static void browser->view, FileBrowserModel * model, { - items_array_special_sort(model->items, BrowserItem_t_cmp); + items_array_sort(model->items); model->list_loading = false; }, true); From 38d0bc5a5f3b58ab790bd58b89504c26c93c7cbc Mon Sep 17 00:00:00 2001 From: Dig Date: Sat, 22 Oct 2022 16:39:11 +0100 Subject: [PATCH 3/3] patch for updated string type --- applications/main/archive/helpers/archive_files.h | 2 +- applications/services/gui/modules/file_browser.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/applications/main/archive/helpers/archive_files.h b/applications/main/archive/helpers/archive_files.h index 72f6be45f..28701e09d 100644 --- a/applications/main/archive/helpers/archive_files.h +++ b/applications/main/archive/helpers/archive_files.h @@ -83,7 +83,7 @@ static int ArchiveFile_t_cmp(const ArchiveFile_t* a, const ArchiveFile_t* b) { return -1; } - return string_cmp(a->path, b->path); + return furi_string_cmp(a->path, b->path); } #define M_OPL_ArchiveFile_t() \ diff --git a/applications/services/gui/modules/file_browser.c b/applications/services/gui/modules/file_browser.c index 57e49c604..fa119f128 100644 --- a/applications/services/gui/modules/file_browser.c +++ b/applications/services/gui/modules/file_browser.c @@ -81,7 +81,7 @@ static int BrowserItem_t_cmp(const BrowserItem_t* a, const BrowserItem_t* b) { return -1; } - return string_cmp(a->path, b->path); + return furi_string_cmp(a->path, b->path); } #define M_OPL_BrowserItem_t() \