[FL-2414, FL-2417] Archive: Unlimited file list and various fixes (#1089)

* Archive: unlimited file list and various fixes
* Recursive remove fix
This commit is contained in:
Nikolay Minaylov 2022-04-06 20:44:06 +03:00 committed by GitHub
parent 5a8961764e
commit b43dcbd74f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 322 additions and 134 deletions

View File

@ -39,6 +39,8 @@ bool archive_app_read_dir(void* context, const char* path) {
furi_assert(path); furi_assert(path);
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
archive_file_array_rm_all(browser);
ArchiveAppTypeEnum app = archive_get_app_type(path); ArchiveAppTypeEnum app = archive_get_app_type(path);
if(app == ArchiveAppTypeU2f) { if(app == ArchiveAppTypeU2f) {

View File

@ -3,25 +3,31 @@
#include "archive_browser.h" #include "archive_browser.h"
#include <math.h> #include <math.h>
bool archive_is_item_in_array(ArchiveBrowserViewModel* model, uint32_t idx) {
size_t array_size = files_array_size(model->files);
if((idx >= model->array_offset + array_size) || (idx < model->array_offset)) {
return false;
}
return true;
}
void archive_update_offset(ArchiveBrowserView* browser) { void archive_update_offset(ArchiveBrowserView* browser) {
furi_assert(browser); furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
size_t array_size = files_array_size(model->files); uint16_t bounds = model->item_cnt > 3 ? 2 : model->item_cnt;
uint16_t bounds = array_size > 3 ? 2 : array_size;
if(array_size > 3 && model->idx >= array_size - 1) { if(model->item_cnt > 3 && model->item_idx >= model->item_cnt - 1) {
model->list_offset = model->idx - 3; model->list_offset = model->item_idx - 3;
} else if( } else if(model->list_offset < model->item_idx - bounds) {
model->last_offset && model->last_offset != model->list_offset && model->list_offset = CLAMP(model->item_idx - 2, model->item_cnt - bounds, 0);
model->tab_idx == model->last_tab) { } else if(model->list_offset > model->item_idx - bounds) {
model->list_offset = model->last_offset; model->list_offset = CLAMP(model->item_idx - 1, model->item_cnt - bounds, 0);
model->last_offset = !model->last_offset;
} else if(model->list_offset < model->idx - bounds) {
model->list_offset = CLAMP(model->idx - 2, array_size - bounds, 0);
} else if(model->list_offset > model->idx - bounds) {
model->list_offset = CLAMP(model->idx - 1, array_size - bounds, 0);
} }
return true; return true;
}); });
} }
@ -32,8 +38,8 @@ void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
archive_get_filenames(browser, string_get_cstr(browser->path)); archive_get_filenames(browser, string_get_cstr(browser->path));
if(!archive_file_array_size(browser) && !archive_get_depth(browser)) { if(!archive_file_get_array_size(browser) && !archive_get_depth(browser)) {
archive_switch_tab(browser, DEFAULT_TAB_DIR); archive_switch_tab(browser, TAB_RIGHT);
} else { } else {
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
@ -41,7 +47,7 @@ void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
while(idx < files_array_size(model->files)) { while(idx < files_array_size(model->files)) {
ArchiveFile_t* current = files_array_get(model->files, idx); ArchiveFile_t* current = files_array_get(model->files, idx);
if(!string_search(current->name, target)) { if(!string_search(current->name, target)) {
model->idx = idx; model->item_idx = idx + model->array_offset;
break; break;
} }
++idx; ++idx;
@ -53,7 +59,9 @@ void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
} }
} }
size_t archive_file_array_size(ArchiveBrowserView* browser) { size_t archive_file_get_array_size(ArchiveBrowserView* browser) {
furi_assert(browser);
uint16_t size = 0; uint16_t size = 0;
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
@ -63,40 +71,60 @@ size_t archive_file_array_size(ArchiveBrowserView* browser) {
return size; return size;
} }
void archive_file_array_rm_selected(ArchiveBrowserView* browser) { void archive_set_item_count(ArchiveBrowserView* browser, uint32_t count) {
furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
files_array_remove_v(model->files, model->idx, model->idx + 1); model->item_cnt = count;
model->idx = CLAMP(model->idx, files_array_size(model->files) - 1, 0); return false;
});
}
void archive_file_array_rm_selected(ArchiveBrowserView* browser) {
furi_assert(browser);
uint32_t items_cnt = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_remove_v(
model->files,
model->item_idx - model->array_offset,
model->item_idx - model->array_offset + 1);
model->item_cnt--;
model->item_idx = CLAMP(model->item_idx, model->item_cnt - 1, 0);
items_cnt = model->item_cnt;
return false; return false;
}); });
if(!archive_file_array_size(browser) && !archive_get_depth(browser)) { if((items_cnt == 0) && (archive_get_depth(browser) == 0)) {
archive_switch_tab(browser, DEFAULT_TAB_DIR); archive_switch_tab(browser, TAB_RIGHT);
} }
archive_update_offset(browser); archive_update_offset(browser);
} }
void archive_file_array_swap(ArchiveBrowserView* browser, int8_t d) { void archive_file_array_swap(ArchiveBrowserView* browser, int8_t dir) {
furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
ArchiveFile_t temp; ArchiveFile_t temp;
size_t array_size = files_array_size(model->files) - 1; size_t array_size = files_array_size(model->files) - 1;
uint8_t swap_idx = CLAMP(model->idx + d, array_size, 0); uint8_t swap_idx = CLAMP(model->item_idx + dir, array_size, 0);
if(model->idx == 0 && d < 0) { if(model->item_idx == 0 && dir < 0) {
ArchiveFile_t_init(&temp); ArchiveFile_t_init(&temp);
files_array_pop_at(&temp, model->files, array_size); files_array_pop_at(&temp, model->files, array_size);
files_array_push_at(model->files, model->idx, temp); files_array_push_at(model->files, model->item_idx, temp);
ArchiveFile_t_clear(&temp); ArchiveFile_t_clear(&temp);
} else if(model->idx == array_size && d > 0) { } else if(model->item_idx == array_size && dir > 0) {
ArchiveFile_t_init(&temp); ArchiveFile_t_init(&temp);
files_array_pop_at(&temp, model->files, model->last_idx); files_array_pop_at(&temp, model->files, model->item_idx);
files_array_push_at(model->files, array_size, temp); files_array_push_at(model->files, array_size, temp);
ArchiveFile_t_clear(&temp); ArchiveFile_t_clear(&temp);
} else { } else {
files_array_swap_at(model->files, model->idx, swap_idx); files_array_swap_at(model->files, model->item_idx, swap_idx);
} }
return false; return false;
@ -104,6 +132,8 @@ void archive_file_array_swap(ArchiveBrowserView* browser, int8_t d) {
} }
void archive_file_array_rm_all(ArchiveBrowserView* browser) { void archive_file_array_rm_all(ArchiveBrowserView* browser) {
furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
files_array_reset(model->files); files_array_reset(model->files);
@ -111,23 +141,61 @@ void archive_file_array_rm_all(ArchiveBrowserView* browser) {
}); });
} }
bool archive_file_array_load(ArchiveBrowserView* browser, int8_t dir) {
furi_assert(browser);
int32_t offset_new = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
if(model->item_cnt > FILE_LIST_BUF_LEN) {
if(dir < 0) {
offset_new = model->item_idx - FILE_LIST_BUF_LEN / 4 * 3;
} else if(dir == 0) {
offset_new = model->item_idx - FILE_LIST_BUF_LEN / 4 * 2;
} else {
offset_new = model->item_idx - FILE_LIST_BUF_LEN / 4 * 1;
}
offset_new = CLAMP(offset_new, model->item_cnt - FILE_LIST_BUF_LEN, 0);
}
return false;
});
bool res = archive_dir_read_items(
browser, string_get_cstr(browser->path), offset_new, FILE_LIST_BUF_LEN);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->array_offset = offset_new;
model->list_loading = false;
return true;
});
return res;
}
ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) { ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) {
furi_assert(browser);
ArchiveFile_t* selected; ArchiveFile_t* selected;
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
selected = files_array_size(model->files) ? files_array_get(model->files, model->idx) : selected = files_array_size(model->files) ?
NULL; files_array_get(model->files, model->item_idx - model->array_offset) :
NULL;
return false; return false;
}); });
return selected; return selected;
} }
ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) { ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) {
furi_assert(browser);
ArchiveFile_t* selected; ArchiveFile_t* selected;
idx = CLAMP(idx, archive_file_array_size(browser), 0);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
idx = CLAMP(idx - model->array_offset, files_array_size(model->files), 0);
selected = files_array_size(model->files) ? files_array_get(model->files, idx) : NULL; selected = files_array_size(model->files) ? files_array_get(model->files, idx) : NULL;
return false; return false;
}); });
@ -135,6 +203,8 @@ ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) {
} }
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) { ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
furi_assert(browser);
ArchiveTabEnum tab_id; ArchiveTabEnum tab_id;
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
@ -145,10 +215,12 @@ ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
} }
uint8_t archive_get_depth(ArchiveBrowserView* browser) { uint8_t archive_get_depth(ArchiveBrowserView* browser) {
furi_assert(browser);
uint8_t depth; uint8_t depth;
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
depth = model->depth; depth = idx_last_array_size(model->idx_last);
return false; return false;
}); });
@ -165,6 +237,8 @@ const char* archive_get_name(ArchiveBrowserView* browser) {
} }
void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) { void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
model->tab_idx = tab; model->tab_idx = tab;
@ -172,6 +246,8 @@ void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
}); });
} }
void archive_set_last_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) { void archive_set_last_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
model->last_tab = model->tab_idx; model->last_tab = model->tab_idx;
@ -198,11 +274,12 @@ void archive_add_app_item(ArchiveBrowserView* browser, const char* name) {
ArchiveFile_t_init(&item); ArchiveFile_t_init(&item);
string_init_set_str(item.name, name); string_init_set_str(item.name, name);
set_file_type(&item, NULL, app_name + 1, true); archive_set_file_type(&item, NULL, app_name + 1, true);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
files_array_push_back(model->files, item); files_array_push_back(model->files, item);
model->item_cnt = files_array_size(model->files);
return false; return false;
}); });
ArchiveFile_t_clear(&item); ArchiveFile_t_clear(&item);
@ -216,10 +293,11 @@ void archive_add_file_item(ArchiveBrowserView* browser, FileInfo* file_info, con
ArchiveFile_t item; ArchiveFile_t item;
if(filter_by_extension(file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) { if(archive_filter_by_extension(
file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
ArchiveFile_t_init(&item); ArchiveFile_t_init(&item);
string_init_set_str(item.name, name); string_init_set_str(item.name, name);
set_file_type(&item, file_info, archive_get_path(browser), false); archive_set_file_type(&item, file_info, archive_get_path(browser), false);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
@ -234,12 +312,17 @@ void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
furi_assert(browser); furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
model->menu = show;
model->menu_idx = 0;
if(show) { if(show) {
ArchiveFile_t* selected = files_array_get(model->files, model->idx); if(archive_is_item_in_array(model, model->item_idx)) {
selected->fav = archive_is_favorite("%s", string_get_cstr(selected->name)); model->menu = true;
model->menu_idx = 0;
ArchiveFile_t* selected =
files_array_get(model->files, model->item_idx - model->array_offset);
selected->fav = archive_is_favorite("%s", string_get_cstr(selected->name));
}
} else {
model->menu = false;
model->menu_idx = 0;
} }
return true; return true;
@ -247,6 +330,8 @@ void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
} }
void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active) { void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active) {
furi_assert(browser);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
model->move_fav = active; model->move_fav = active;
@ -282,7 +367,8 @@ void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
} else if(strncmp(path, "/app:", 5) == 0) { } else if(strncmp(path, "/app:", 5) == 0) {
if(archive_app_is_available(browser, path)) tab_empty = false; if(archive_app_is_available(browser, path)) tab_empty = false;
} else { } else {
if(archive_dir_not_empty(browser, archive_get_default_path(tab))) tab_empty = false; uint32_t files_cnt = archive_dir_count_items(browser, archive_get_default_path(tab));
if(files_cnt > 0) tab_empty = false;
} }
if((tab_empty) && (tab != ArchiveTabBrowser)) { if((tab_empty) && (tab != ArchiveTabBrowser)) {
@ -291,8 +377,9 @@ void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
if(model->last_tab != model->tab_idx) { if(model->last_tab != model->tab_idx) {
model->idx = 0; model->item_idx = 0;
model->depth = 0; model->array_offset = 0;
idx_last_array_reset(model->idx_last);
} }
return false; return false;
}); });
@ -305,11 +392,13 @@ void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
furi_assert(browser); furi_assert(browser);
furi_assert(name); furi_assert(name);
archive_dir_count_items(browser, string_get_cstr(name));
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
model->last_idx = model->idx; idx_last_array_push_back(model->idx_last, model->item_idx);
model->idx = 0; model->array_offset = 0;
model->depth = CLAMP(model->depth + 1, MAX_DEPTH, 0); model->item_idx = 0;
return false; return false;
}); });
@ -329,10 +418,11 @@ void archive_leave_dir(ArchiveBrowserView* browser) {
string_left(browser->path, pos); string_left(browser->path, pos);
} }
archive_dir_count_items(browser, path);
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
model->depth = CLAMP(model->depth - 1, MAX_DEPTH, 0); idx_last_array_pop_back(&model->item_idx, model->idx_last);
model->idx = model->last_idx;
return false; return false;
}); });

View File

@ -2,7 +2,8 @@
#include "../archive_i.h" #include "../archive_i.h"
#define DEFAULT_TAB_DIR InputKeyRight //default tab swith direction #define TAB_RIGHT InputKeyRight //default tab swith direction
#define FILE_LIST_BUF_LEN 100
static const char* tab_default_paths[] = { static const char* tab_default_paths[] = {
[ArchiveTabFavorites] = "/any/favorites", [ArchiveTabFavorites] = "/any/favorites",
@ -56,14 +57,18 @@ inline bool archive_is_known_app(ArchiveFileTypeEnum type) {
return (type != ArchiveFileTypeFolder && type != ArchiveFileTypeUnknown); return (type != ArchiveFileTypeFolder && type != ArchiveFileTypeUnknown);
} }
bool archive_is_item_in_array(ArchiveBrowserViewModel* model, uint32_t idx);
void archive_update_offset(ArchiveBrowserView* browser); void archive_update_offset(ArchiveBrowserView* browser);
void archive_update_focus(ArchiveBrowserView* browser, const char* target); void archive_update_focus(ArchiveBrowserView* browser, const char* target);
size_t archive_file_array_size(ArchiveBrowserView* browser); bool archive_file_array_load(ArchiveBrowserView* browser, int8_t dir);
size_t archive_file_get_array_size(ArchiveBrowserView* browser);
void archive_file_array_rm_selected(ArchiveBrowserView* browser); void archive_file_array_rm_selected(ArchiveBrowserView* browser);
void archive_file_array_swap(ArchiveBrowserView* browser, int8_t d); void archive_file_array_swap(ArchiveBrowserView* browser, int8_t dir);
void archive_file_array_rm_all(ArchiveBrowserView* browser); void archive_file_array_rm_all(ArchiveBrowserView* browser);
void archive_set_item_count(ArchiveBrowserView* browser, uint32_t count);
ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser); ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser);
ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx); ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx);
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser); ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser);

View File

@ -84,6 +84,9 @@ bool archive_favorites_read(void* context) {
string_init(buffer); string_init(buffer);
bool need_refresh = false; bool need_refresh = false;
uint16_t file_count = 0;
archive_file_array_rm_all(browser);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING); bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
@ -99,6 +102,7 @@ bool archive_favorites_read(void* context) {
if(string_search(buffer, "/app:") == 0) { if(string_search(buffer, "/app:") == 0) {
if(archive_app_is_available(browser, string_get_cstr(buffer))) { if(archive_app_is_available(browser, string_get_cstr(buffer))) {
archive_add_app_item(browser, string_get_cstr(buffer)); archive_add_app_item(browser, string_get_cstr(buffer));
file_count++;
} else { } else {
need_refresh = true; need_refresh = true;
} }
@ -106,10 +110,12 @@ bool archive_favorites_read(void* context) {
bool file_exists = false; bool file_exists = false;
file_worker_is_file_exist(file_worker, string_get_cstr(buffer), &file_exists); file_worker_is_file_exist(file_worker, string_get_cstr(buffer), &file_exists);
if(file_exists) if(file_exists) {
archive_add_file_item(browser, &file_info, string_get_cstr(buffer)); archive_add_file_item(browser, &file_info, string_get_cstr(buffer));
else file_count++;
} else {
need_refresh = true; need_refresh = true;
}
} }
string_reset(buffer); string_reset(buffer);
@ -119,6 +125,8 @@ bool archive_favorites_read(void* context) {
file_worker_close(file_worker); file_worker_close(file_worker);
file_worker_free(file_worker); file_worker_free(file_worker);
archive_set_item_count(browser, file_count);
if(need_refresh) { if(need_refresh) {
archive_favourites_rescan(); archive_favourites_rescan();
} }
@ -257,7 +265,7 @@ void archive_favorites_save(void* context) {
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
FileWorker* file_worker = file_worker_alloc(true); FileWorker* file_worker = file_worker_alloc(true);
for(size_t i = 0; i < archive_file_array_size(browser); i++) { for(size_t i = 0; i < archive_file_get_array_size(browser); i++) {
ArchiveFile_t* item = archive_get_file_at(browser, i); ArchiveFile_t* item = archive_get_file_at(browser, i);
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->name)); archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->name));
} }

View File

@ -6,7 +6,7 @@
#define ASSETS_DIR "assets" #define ASSETS_DIR "assets"
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) { bool archive_filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) {
furi_assert(file_info); furi_assert(file_info);
furi_assert(tab_ext); furi_assert(tab_ext);
furi_assert(name); furi_assert(name);
@ -45,7 +45,7 @@ void archive_get_file_extension(char* name, char* ext) {
strncpy(ext, dot, MAX_EXT_LEN); strncpy(ext, dot, MAX_EXT_LEN);
} }
void set_file_type(ArchiveFile_t* file, FileInfo* file_info, const char* path, bool is_app) { void archive_set_file_type(ArchiveFile_t* file, FileInfo* file_info, const char* path, bool is_app) {
furi_assert(file); furi_assert(file);
file->is_app = is_app; file->is_app = is_app;
@ -83,21 +83,19 @@ bool archive_get_filenames(void* context, const char* path) {
bool res; bool res;
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
archive_file_array_rm_all(browser);
if(archive_get_tab(browser) == ArchiveTabFavorites) { if(archive_get_tab(browser) == ArchiveTabFavorites) {
res = archive_favorites_read(browser); res = archive_favorites_read(browser);
} else if(strncmp(path, "/app:", 5) == 0) { } else if(strncmp(path, "/app:", 5) == 0) {
res = archive_app_read_dir(browser, path); res = archive_app_read_dir(browser, path);
} else { } else {
res = archive_read_dir(browser, path); res = archive_file_array_load(browser, 0);
} }
return res; return res;
} }
bool archive_dir_not_empty(void* context, const char* path) { // can be simpler? uint32_t archive_dir_count_items(void* context, const char* path) {
furi_assert(context); furi_assert(context);
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
FileInfo file_info; FileInfo file_info;
@ -108,23 +106,19 @@ bool archive_dir_not_empty(void* context, const char* path) { // can be simpler?
if(!storage_dir_open(directory, path)) { if(!storage_dir_open(directory, path)) {
storage_dir_close(directory); storage_dir_close(directory);
storage_file_free(directory); storage_file_free(directory);
return false; return 0;
} }
bool files_found = false; uint32_t files_found = 0;
while(1) { while(1) {
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) { if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
break; break;
} }
if(files_found) { if((storage_file_get_error(directory) == FSE_OK) && (name[0])) {
break; if(archive_filter_by_extension(
} else if((storage_file_get_error(directory) == FSE_OK) && (name[0])) {
if(filter_by_extension(
&file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) { &file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
files_found = true; files_found++;
} }
} else {
return false;
} }
} }
storage_dir_close(directory); storage_dir_close(directory);
@ -132,10 +126,12 @@ bool archive_dir_not_empty(void* context, const char* path) { // can be simpler?
furi_record_close("storage"); furi_record_close("storage");
archive_set_item_count(browser, files_found);
return files_found; return files_found;
} }
bool archive_read_dir(void* context, const char* path) { uint32_t archive_dir_read_items(void* context, const char* path, uint32_t offset, uint32_t count) {
furi_assert(context); furi_assert(context);
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
@ -145,7 +141,6 @@ bool archive_read_dir(void* context, const char* path) {
char name[MAX_NAME_LEN]; char name[MAX_NAME_LEN];
snprintf(name, MAX_NAME_LEN, "%s/", path); snprintf(name, MAX_NAME_LEN, "%s/", path);
size_t path_len = strlen(name); size_t path_len = strlen(name);
size_t files_cnt = 0;
if(!storage_dir_open(directory, path)) { if(!storage_dir_open(directory, path)) {
storage_dir_close(directory); storage_dir_close(directory);
@ -153,28 +148,48 @@ bool archive_read_dir(void* context, const char* path) {
return false; return false;
} }
while(1) { // Skip items before offset
uint32_t items_cnt = 0;
while(items_cnt < offset) {
if(!storage_dir_read(directory, &file_info, &name[path_len], MAX_NAME_LEN)) {
break;
}
if(storage_file_get_error(directory) == FSE_OK) {
if(archive_filter_by_extension(
&file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
items_cnt++;
}
} else {
break;
}
}
if(items_cnt != offset) {
storage_dir_close(directory);
storage_file_free(directory);
furi_record_close("storage");
return false;
}
items_cnt = 0;
archive_file_array_rm_all(browser);
while(items_cnt < count) {
if(!storage_dir_read(directory, &file_info, &name[path_len], MAX_NAME_LEN - path_len)) { if(!storage_dir_read(directory, &file_info, &name[path_len], MAX_NAME_LEN - path_len)) {
break; break;
} }
if(files_cnt > MAX_FILES) { if(storage_file_get_error(directory) == FSE_OK) {
break;
} else if(storage_file_get_error(directory) == FSE_OK) {
archive_add_file_item(browser, &file_info, name); archive_add_file_item(browser, &file_info, name);
++files_cnt; items_cnt++;
} else { } else {
storage_dir_close(directory); break;
storage_file_free(directory);
return false;
} }
} }
storage_dir_close(directory); storage_dir_close(directory);
storage_file_free(directory); storage_file_free(directory);
furi_record_close("storage"); furi_record_close("storage");
return true; return (items_cnt == count);
} }
void archive_file_append(const char* path, const char* format, ...) { void archive_file_append(const char* path, const char* format, ...) {
@ -210,10 +225,20 @@ void archive_delete_file(void* context, const char* format, ...) {
va_end(args); va_end(args);
ArchiveBrowserView* browser = context; ArchiveBrowserView* browser = context;
FileWorker* file_worker = file_worker_alloc(true); Storage* fs_api = furi_record_open("storage");
bool res = file_worker_remove(file_worker, string_get_cstr(filename)); FileInfo fileinfo;
file_worker_free(file_worker); storage_common_stat(fs_api, string_get_cstr(filename), &fileinfo);
bool res = false;
if(fileinfo.flags & FSF_DIRECTORY) {
res = storage_simply_remove_recursive(fs_api, string_get_cstr(filename));
} else {
res = (storage_common_remove(fs_api, string_get_cstr(filename)) == FSE_OK);
}
furi_record_close("storage");
if(archive_is_favorite("%s", string_get_cstr(filename))) { if(archive_is_favorite("%s", string_get_cstr(filename))) {
archive_favorites_delete("%s", string_get_cstr(filename)); archive_favorites_delete("%s", string_get_cstr(filename));
@ -224,4 +249,4 @@ void archive_delete_file(void* context, const char* format, ...) {
} }
string_clear(filename); string_clear(filename);
} }

View File

@ -2,8 +2,6 @@
#include "file_worker.h" #include "file_worker.h"
#include <m-array.h> #include <m-array.h>
#define MAX_FILES 100 //temp
typedef enum { typedef enum {
ArchiveFileTypeIButton, ArchiveFileTypeIButton,
ArchiveFileTypeNFC, ArchiveFileTypeNFC,
@ -14,7 +12,7 @@ typedef enum {
ArchiveFileTypeU2f, ArchiveFileTypeU2f,
ArchiveFileTypeFolder, ArchiveFileTypeFolder,
ArchiveFileTypeUnknown, ArchiveFileTypeUnknown,
ArchiveFileTypesTotal, ArchiveFileTypeLoading,
} ArchiveFileTypeEnum; } ArchiveFileTypeEnum;
typedef struct { typedef struct {
@ -57,12 +55,12 @@ ARRAY_DEF(
INIT_SET(API_6(ArchiveFile_t_init_set)), INIT_SET(API_6(ArchiveFile_t_init_set)),
CLEAR(API_2(ArchiveFile_t_clear)))) CLEAR(API_2(ArchiveFile_t_clear))))
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name); bool archive_filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name);
void set_file_type(ArchiveFile_t* file, FileInfo* file_info, const char* path, bool is_app); void archive_set_file_type(ArchiveFile_t* file, FileInfo* file_info, const char* path, bool is_app);
void archive_trim_file_path(char* name, bool ext); void archive_trim_file_path(char* name, bool ext);
void archive_get_file_extension(char* name, char* ext); void archive_get_file_extension(char* name, char* ext);
bool archive_get_filenames(void* context, const char* path); bool archive_get_filenames(void* context, const char* path);
bool archive_dir_not_empty(void* context, const char* path); uint32_t archive_dir_count_items(void* context, const char* path);
bool archive_read_dir(void* context, const char* path); uint32_t archive_dir_read_items(void* context, const char* path, uint32_t offset, uint32_t count);
void archive_file_append(const char* path, const char* format, ...); void archive_file_append(const char* path, const char* format, ...);
void archive_delete_file(void* context, const char* format, ...); void archive_delete_file(void* context, const char* format, ...);

View File

@ -105,7 +105,9 @@ bool archive_scene_browser_on_event(void* context, SceneManagerEvent event) {
consumed = true; consumed = true;
break; break;
case ArchiveBrowserEventFileMenuDelete: case ArchiveBrowserEventFileMenuDelete:
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneDelete); if(archive_get_tab(browser) != ArchiveTabFavorites) {
scene_manager_next_scene(archive->scene_manager, ArchiveAppSceneDelete);
}
consumed = true; consumed = true;
break; break;
case ArchiveBrowserEventEnterDir: case ArchiveBrowserEventEnterDir:
@ -136,6 +138,14 @@ bool archive_scene_browser_on_event(void* context, SceneManagerEvent event) {
archive_favorites_save(archive->browser); archive_favorites_save(archive->browser);
consumed = true; consumed = true;
break; break;
case ArchiveBrowserEventLoadPrevItems:
archive_file_array_load(archive->browser, -1);
consumed = true;
break;
case ArchiveBrowserEventLoadNextItems:
archive_file_array_load(archive->browser, 1);
consumed = true;
break;
case ArchiveBrowserEventExit: case ArchiveBrowserEventExit:
if(archive_get_depth(browser)) { if(archive_get_depth(browser)) {

View File

@ -24,6 +24,7 @@ static const Icon* ArchiveItemIcons[] = {
[ArchiveFileTypeU2f] = &I_u2f_10px, [ArchiveFileTypeU2f] = &I_u2f_10px,
[ArchiveFileTypeFolder] = &I_dir_10px, [ArchiveFileTypeFolder] = &I_dir_10px,
[ArchiveFileTypeUnknown] = &I_unknown_10px, [ArchiveFileTypeUnknown] = &I_unknown_10px,
[ArchiveFileTypeLoading] = &I_unknown_10px, // TODO loading icon
}; };
void archive_browser_set_callback( void archive_browser_set_callback(
@ -49,7 +50,7 @@ static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
string_init_set_str(menu[2], "Rename"); string_init_set_str(menu[2], "Rename");
string_init_set_str(menu[3], "Delete"); string_init_set_str(menu[3], "Delete");
ArchiveFile_t* selected = files_array_get(model->files, model->idx); ArchiveFile_t* selected = files_array_get(model->files, model->item_idx - model->array_offset);
if(!archive_is_known_app(selected->type)) { if(!archive_is_known_app(selected->type)) {
string_set_str(menu[0], "---"); string_set_str(menu[0], "---");
@ -58,6 +59,7 @@ static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
} else { } else {
if(model->tab_idx == ArchiveTabFavorites) { if(model->tab_idx == ArchiveTabFavorites) {
string_set_str(menu[2], "Move"); string_set_str(menu[2], "Move");
string_set_str(menu[3], "---");
} else if(selected->is_app) { } else if(selected->is_app) {
string_set_str(menu[2], "---"); string_set_str(menu[2], "---");
} }
@ -100,36 +102,44 @@ static void draw_list(Canvas* canvas, ArchiveBrowserViewModel* model) {
furi_assert(model); furi_assert(model);
size_t array_size = files_array_size(model->files); size_t array_size = files_array_size(model->files);
bool scrollbar = array_size > 4; bool scrollbar = model->item_cnt > 4;
for(size_t i = 0; i < MIN(array_size, MENU_ITEMS); ++i) { for(size_t i = 0; i < MIN(model->item_cnt, MENU_ITEMS); ++i) {
string_t str_buff; string_t str_buff;
char cstr_buff[MAX_NAME_LEN]; char cstr_buff[MAX_NAME_LEN];
size_t idx = CLAMP(i + model->list_offset, array_size, 0); size_t idx = CLAMP(i + model->list_offset, model->item_cnt, 0);
uint8_t x_offset = (model->move_fav && model->idx == idx) ? MOVE_OFFSET : 0; uint8_t x_offset = (model->move_fav && model->item_idx == idx) ? MOVE_OFFSET : 0;
ArchiveFile_t* file = files_array_get(model->files, CLAMP(idx, array_size - 1, 0)); ArchiveFileTypeEnum file_type = ArchiveFileTypeLoading;
if(archive_is_item_in_array(model, idx)) {
ArchiveFile_t* file =
files_array_get(model->files, CLAMP(idx - model->array_offset, array_size - 1, 0));
strlcpy(cstr_buff, string_get_cstr(file->name), string_size(file->name) + 1);
archive_trim_file_path(cstr_buff, archive_is_known_app(file->type));
string_init_set_str(str_buff, cstr_buff);
file_type = file->type;
} else {
string_init_set_str(str_buff, "---");
}
strlcpy(cstr_buff, string_get_cstr(file->name), string_size(file->name) + 1);
archive_trim_file_path(cstr_buff, archive_is_known_app(file->type));
string_init_set_str(str_buff, cstr_buff);
elements_string_fit_width( elements_string_fit_width(
canvas, str_buff, (scrollbar ? MAX_LEN_PX - 6 : MAX_LEN_PX) - x_offset); canvas, str_buff, (scrollbar ? MAX_LEN_PX - 6 : MAX_LEN_PX) - x_offset);
if(model->idx == idx) { if(model->item_idx == idx) {
archive_draw_frame(canvas, i, scrollbar, model->move_fav); archive_draw_frame(canvas, i, scrollbar, model->move_fav);
} else { } else {
canvas_set_color(canvas, ColorBlack); canvas_set_color(canvas, ColorBlack);
} }
canvas_draw_icon( canvas_draw_icon(canvas, 2 + x_offset, 16 + i * FRAME_HEIGHT, ArchiveItemIcons[file_type]);
canvas, 2 + x_offset, 16 + i * FRAME_HEIGHT, ArchiveItemIcons[file->type]);
canvas_draw_str(canvas, 15 + x_offset, 24 + i * FRAME_HEIGHT, string_get_cstr(str_buff)); canvas_draw_str(canvas, 15 + x_offset, 24 + i * FRAME_HEIGHT, string_get_cstr(str_buff));
string_clear(str_buff); string_clear(str_buff);
} }
if(scrollbar) { if(scrollbar) {
elements_scrollbar_pos(canvas, 126, 15, 49, model->idx, array_size); elements_scrollbar_pos(canvas, 126, 15, 49, model->item_idx, model->item_cnt);
} }
if(model->menu) { if(model->menu) {
@ -173,13 +183,13 @@ static void archive_render_status_bar(Canvas* canvas, ArchiveBrowserViewModel* m
canvas_set_color(canvas, ColorBlack); canvas_set_color(canvas, ColorBlack);
} }
void archive_view_render(Canvas* canvas, void* model) { void archive_view_render(Canvas* canvas, void* mdl) {
ArchiveBrowserViewModel* m = model; ArchiveBrowserViewModel* model = mdl;
archive_render_status_bar(canvas, model); archive_render_status_bar(canvas, mdl);
if(files_array_size(m->files)) { if(model->item_cnt > 0) {
draw_list(canvas, m); draw_list(canvas, model);
} else { } else {
canvas_draw_str_aligned( canvas_draw_str_aligned(
canvas, GUI_DISPLAY_WIDTH / 2, 40, AlignCenter, AlignCenter, "Empty"); canvas, GUI_DISPLAY_WIDTH / 2, 40, AlignCenter, AlignCenter, "Empty");
@ -191,6 +201,26 @@ View* archive_browser_get_view(ArchiveBrowserView* browser) {
return browser->view; return browser->view;
} }
static bool is_file_list_load_required(ArchiveBrowserViewModel* model) {
size_t array_size = files_array_size(model->files);
if((model->list_loading) || (array_size >= model->item_cnt)) {
return false;
}
if((model->array_offset > 0) &&
(model->item_idx < (model->array_offset + FILE_LIST_BUF_LEN / 4))) {
return true;
}
if(((model->array_offset + array_size) < model->item_cnt) &&
(model->item_idx > (model->array_offset + array_size - FILE_LIST_BUF_LEN / 4))) {
return true;
}
return false;
}
bool archive_view_input(InputEvent* event, void* context) { bool archive_view_input(InputEvent* event, void* context) {
furi_assert(event); furi_assert(event);
furi_assert(context); furi_assert(context);
@ -247,22 +277,28 @@ bool archive_view_input(InputEvent* event, void* context) {
} }
} }
if(event->key == InputKeyUp || event->key == InputKeyDown) { if((event->key == InputKeyUp || event->key == InputKeyDown) &&
(event->type == InputTypeShort || event->type == InputTypeRepeat)) {
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
uint16_t num_elements = (uint16_t)files_array_size(model->files); if(event->key == InputKeyUp) {
if((event->type == InputTypeShort || event->type == InputTypeRepeat)) { model->item_idx =
if(event->key == InputKeyUp) { ((model->item_idx - 1) + model->item_cnt) % model->item_cnt;
model->idx = ((model->idx - 1) + num_elements) % num_elements; if(is_file_list_load_required(model)) {
if(move_fav_mode) { model->list_loading = true;
browser->callback(ArchiveBrowserEventFavMoveUp, browser->context); browser->callback(ArchiveBrowserEventLoadPrevItems, browser->context);
} }
} else if(event->key == InputKeyDown) { if(move_fav_mode) {
model->idx = (model->idx + 1) % num_elements; browser->callback(ArchiveBrowserEventFavMoveUp, browser->context);
if(move_fav_mode) { }
browser->callback( } else if(event->key == InputKeyDown) {
ArchiveBrowserEventFavMoveDown, browser->context); model->item_idx = (model->item_idx + 1) % model->item_cnt;
} if(is_file_list_load_required(model)) {
model->list_loading = true;
browser->callback(ArchiveBrowserEventLoadNextItems, browser->context);
}
if(move_fav_mode) {
browser->callback(ArchiveBrowserEventFavMoveDown, browser->context);
} }
} }
@ -317,6 +353,7 @@ ArchiveBrowserView* browser_alloc() {
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
files_array_init(model->files); files_array_init(model->files);
idx_last_array_init(model->idx_last);
return true; return true;
}); });
@ -329,6 +366,7 @@ void browser_free(ArchiveBrowserView* browser) {
with_view_model( with_view_model(
browser->view, (ArchiveBrowserViewModel * model) { browser->view, (ArchiveBrowserViewModel * model) {
files_array_clear(model->files); files_array_clear(model->files);
idx_last_array_clear(model->idx_last);
return false; return false;
}); });

View File

@ -14,7 +14,6 @@
#define MAX_EXT_LEN 6 #define MAX_EXT_LEN 6
#define FRAME_HEIGHT 12 #define FRAME_HEIGHT 12
#define MENU_ITEMS 4 #define MENU_ITEMS 4
#define MAX_DEPTH 32
#define MOVE_OFFSET 5 #define MOVE_OFFSET 5
typedef enum { typedef enum {
@ -37,12 +36,18 @@ typedef enum {
ArchiveBrowserEventFileMenuPin, ArchiveBrowserEventFileMenuPin,
ArchiveBrowserEventFileMenuAction, ArchiveBrowserEventFileMenuAction,
ArchiveBrowserEventFileMenuDelete, ArchiveBrowserEventFileMenuDelete,
ArchiveBrowserEventEnterDir, ArchiveBrowserEventEnterDir,
ArchiveBrowserEventFavMoveUp, ArchiveBrowserEventFavMoveUp,
ArchiveBrowserEventFavMoveDown, ArchiveBrowserEventFavMoveDown,
ArchiveBrowserEventEnterFavMove, ArchiveBrowserEventEnterFavMove,
ArchiveBrowserEventExitFavMove, ArchiveBrowserEventExitFavMove,
ArchiveBrowserEventSaveFavMove, ArchiveBrowserEventSaveFavMove,
ArchiveBrowserEventLoadPrevItems,
ArchiveBrowserEventLoadNextItems,
ArchiveBrowserEventExit, ArchiveBrowserEventExit,
} ArchiveBrowserEvent; } ArchiveBrowserEvent;
@ -71,21 +76,23 @@ struct ArchiveBrowserView {
string_t path; string_t path;
}; };
ARRAY_DEF(idx_last_array, int32_t)
typedef struct { typedef struct {
ArchiveTabEnum tab_idx; ArchiveTabEnum tab_idx;
ArchiveTabEnum last_tab; ArchiveTabEnum last_tab;
files_array_t files; files_array_t files;
idx_last_array_t idx_last;
uint8_t menu_idx; uint8_t menu_idx;
bool move_fav; bool move_fav;
bool menu; bool menu;
bool list_loading;
uint16_t idx; uint32_t item_cnt;
uint16_t last_idx; int32_t item_idx;
uint16_t list_offset; int32_t array_offset;
uint16_t last_offset; int32_t list_offset;
uint8_t depth;
} ArchiveBrowserViewModel; } ArchiveBrowserViewModel;
void archive_browser_set_callback( void archive_browser_set_callback(

View File

@ -2,6 +2,7 @@
#include <furi_hal_bt.h> #include <furi_hal_bt.h>
#include <furi_hal_random.h> #include <furi_hal_random.h>
#include <stm32wbxx_ll_cortex.h> #include <stm32wbxx_ll_cortex.h>
#include <stm32wbxx_ll_bus.h>
#include <furi.h> #include <furi.h>
#include <shci.h> #include <shci.h>
@ -182,6 +183,10 @@ static void crypto_enable() {
static void crypto_disable() { static void crypto_disable() {
CLEAR_BIT(AES1->CR, AES_CR_EN); CLEAR_BIT(AES1->CR, AES_CR_EN);
FURI_CRITICAL_ENTER();
LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_AES1);
LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_AES1);
FURI_CRITICAL_EXIT();
} }
static void crypto_key_init(uint32_t* key, uint32_t* iv) { static void crypto_key_init(uint32_t* key, uint32_t* iv) {