Skip to content

Commit ac331b8

Browse files
authored
Merge pull request #389 from Dig03/file-browser-ordering
File browser ordering
2 parents 8789c37 + 38d0bc5 commit ac331b8

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

applications/main/archive/helpers/archive_browser.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ static void
6464
archive_add_file_item(browser, is_folder, furi_string_get_cstr(item_path));
6565
} else {
6666
with_view_model(
67-
browser->view, ArchiveBrowserViewModel * model, { model->list_loading = false; }, true);
67+
browser->view,
68+
ArchiveBrowserViewModel * model,
69+
{
70+
files_array_sort(model->files);
71+
model->list_loading = false;
72+
},
73+
true);
6874
}
6975
}
7076

applications/main/archive/helpers/archive_files.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <m-array.h>
44
#include <furi.h>
5+
#include <m-algo.h>
6+
#include <m-string.h>
57
#include <storage/storage.h>
68
#include "toolbox/path.h"
79

@@ -81,13 +83,26 @@ static void ArchiveFile_t_clear(ArchiveFile_t* obj) {
8183
furi_string_free(obj->custom_name);
8284
}
8385

84-
ARRAY_DEF(
85-
files_array,
86-
ArchiveFile_t,
87-
(INIT(API_2(ArchiveFile_t_init)),
88-
SET(API_6(ArchiveFile_t_set)),
89-
INIT_SET(API_6(ArchiveFile_t_init_set)),
90-
CLEAR(API_2(ArchiveFile_t_clear))))
86+
static int ArchiveFile_t_cmp(const ArchiveFile_t* a, const ArchiveFile_t* b) {
87+
if(a->type == ArchiveFileTypeFolder && b->type != ArchiveFileTypeFolder) {
88+
return -1;
89+
}
90+
91+
return furi_string_cmp(a->path, b->path);
92+
}
93+
94+
#define M_OPL_ArchiveFile_t() \
95+
(INIT(API_2(ArchiveFile_t_init)), \
96+
SET(API_6(ArchiveFile_t_set)), \
97+
INIT_SET(API_6(ArchiveFile_t_init_set)), \
98+
CLEAR(API_2(ArchiveFile_t_clear)), \
99+
CMP(API_6(ArchiveFile_t_cmp)), \
100+
SWAP(M_SWAP_DEFAULT), \
101+
EQUAL(API_6(M_EQUAL_DEFAULT)))
102+
103+
ARRAY_DEF(files_array, ArchiveFile_t)
104+
105+
ALGO_DEF(files_array, ARRAY_OPLIST(files_array, M_OPL_ArchiveFile_t()))
91106

92107
void archive_set_file_type(ArchiveFile_t* file, const char* path, bool is_folder, bool is_app);
93108
bool archive_get_items(void* context, const char* path);

applications/services/gui/modules/file_browser.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <core/common_defines.h>
66
#include <core/log.h>
77
#include "furi_hal_resources.h"
8+
#include "m-string.h"
9+
#include "m-algo.h"
810
#include <m-array.h>
911
#include <gui/elements.h>
1012
#include <furi.h>
@@ -71,13 +73,29 @@ static void BrowserItem_t_clear(BrowserItem_t* obj) {
7173
}
7274
}
7375

74-
ARRAY_DEF(
75-
items_array,
76-
BrowserItem_t,
77-
(INIT(API_2(BrowserItem_t_init)),
78-
SET(API_6(BrowserItem_t_set)),
79-
INIT_SET(API_6(BrowserItem_t_init_set)),
80-
CLEAR(API_2(BrowserItem_t_clear))))
76+
static int BrowserItem_t_cmp(const BrowserItem_t* a, const BrowserItem_t* b) {
77+
// Back indicator comes before everything, then folders, then all other files.
78+
if((a->type == BrowserItemTypeBack) ||
79+
(a->type == BrowserItemTypeFolder && b->type != BrowserItemTypeFolder &&
80+
b->type != BrowserItemTypeBack)) {
81+
return -1;
82+
}
83+
84+
return furi_string_cmp(a->path, b->path);
85+
}
86+
87+
#define M_OPL_BrowserItem_t() \
88+
(INIT(API_2(BrowserItem_t_init)), \
89+
SET(API_6(BrowserItem_t_set)), \
90+
INIT_SET(API_6(BrowserItem_t_init_set)), \
91+
CLEAR(API_2(BrowserItem_t_clear)), \
92+
CMP(API_6(BrowserItem_t_cmp)), \
93+
SWAP(M_SWAP_DEFAULT), \
94+
EQUAL(API_6(M_EQUAL_DEFAULT)))
95+
96+
ARRAY_DEF(items_array, BrowserItem_t)
97+
98+
ALGO_DEF(items_array, ARRAY_OPLIST(items_array, M_OPL_BrowserItem_t()))
8199

82100
struct FileBrowser {
83101
View* view;
@@ -388,7 +406,13 @@ static void
388406
}
389407
} else {
390408
with_view_model(
391-
browser->view, FileBrowserModel * model, { model->list_loading = false; }, true);
409+
browser->view,
410+
FileBrowserModel * model,
411+
{
412+
items_array_sort(model->items);
413+
model->list_loading = false;
414+
},
415+
true);
392416
}
393417
}
394418

0 commit comments

Comments
 (0)