Просмотр исходного кода

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

* Archive: unlimited file list and various fixes
* Recursive remove fix
Nikolay Minaylov 3 лет назад
Родитель
Сommit
b43dcbd74f

+ 2 - 0
applications/archive/helpers/archive_apps.c

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

+ 139 - 49
applications/archive/helpers/archive_browser.c

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

+ 8 - 3
applications/archive/helpers/archive_browser.h

@@ -2,7 +2,8 @@
 
 #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[] = {
     [ArchiveTabFavorites] = "/any/favorites",
@@ -56,14 +57,18 @@ inline bool archive_is_known_app(ArchiveFileTypeEnum type) {
     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_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_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_set_item_count(ArchiveBrowserView* browser, uint32_t count);
+
 ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser);
 ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx);
 ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser);

+ 11 - 3
applications/archive/helpers/archive_favorites.c

@@ -84,6 +84,9 @@ bool archive_favorites_read(void* context) {
     string_init(buffer);
 
     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);
 
@@ -99,6 +102,7 @@ bool archive_favorites_read(void* context) {
             if(string_search(buffer, "/app:") == 0) {
                 if(archive_app_is_available(browser, string_get_cstr(buffer))) {
                     archive_add_app_item(browser, string_get_cstr(buffer));
+                    file_count++;
                 } else {
                     need_refresh = true;
                 }
@@ -106,10 +110,12 @@ bool archive_favorites_read(void* context) {
                 bool file_exists = false;
                 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));
-                else
+                    file_count++;
+                } else {
                     need_refresh = true;
+                }
             }
 
             string_reset(buffer);
@@ -119,6 +125,8 @@ bool archive_favorites_read(void* context) {
     file_worker_close(file_worker);
     file_worker_free(file_worker);
 
+    archive_set_item_count(browser, file_count);
+
     if(need_refresh) {
         archive_favourites_rescan();
     }
@@ -257,7 +265,7 @@ void archive_favorites_save(void* context) {
     ArchiveBrowserView* browser = context;
     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);
         archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(item->name));
     }

+ 56 - 31
applications/archive/helpers/archive_files.c

@@ -6,7 +6,7 @@
 
 #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(tab_ext);
     furi_assert(name);
@@ -45,7 +45,7 @@ void archive_get_file_extension(char* name, char* ext) {
         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);
 
     file->is_app = is_app;
@@ -83,21 +83,19 @@ bool archive_get_filenames(void* context, const char* path) {
 
     bool res;
     ArchiveBrowserView* browser = context;
-    archive_file_array_rm_all(browser);
 
     if(archive_get_tab(browser) == ArchiveTabFavorites) {
         res = archive_favorites_read(browser);
     } else if(strncmp(path, "/app:", 5) == 0) {
         res = archive_app_read_dir(browser, path);
     } else {
-        res = archive_read_dir(browser, path);
+        res = archive_file_array_load(browser, 0);
     }
     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);
-
     ArchiveBrowserView* browser = context;
 
     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)) {
         storage_dir_close(directory);
         storage_file_free(directory);
-        return false;
+        return 0;
     }
 
-    bool files_found = false;
+    uint32_t files_found = 0;
     while(1) {
         if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
             break;
         }
-        if(files_found) {
-            break;
-        } else if((storage_file_get_error(directory) == FSE_OK) && (name[0])) {
-            if(filter_by_extension(
+        if((storage_file_get_error(directory) == FSE_OK) && (name[0])) {
+            if(archive_filter_by_extension(
                    &file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
-                files_found = true;
+                files_found++;
             }
-        } else {
-            return false;
         }
     }
     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");
 
+    archive_set_item_count(browser, 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);
 
     ArchiveBrowserView* browser = context;
@@ -145,7 +141,6 @@ bool archive_read_dir(void* context, const char* path) {
     char name[MAX_NAME_LEN];
     snprintf(name, MAX_NAME_LEN, "%s/", path);
     size_t path_len = strlen(name);
-    size_t files_cnt = 0;
 
     if(!storage_dir_open(directory, path)) {
         storage_dir_close(directory);
@@ -153,28 +148,48 @@ bool archive_read_dir(void* context, const char* path) {
         return false;
     }
 
-    while(1) {
-        if(!storage_dir_read(directory, &file_info, &name[path_len], MAX_NAME_LEN - path_len)) {
+    // 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;
+    }
 
-        if(files_cnt > MAX_FILES) {
+    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)) {
             break;
-        } else if(storage_file_get_error(directory) == FSE_OK) {
+        }
+
+        if(storage_file_get_error(directory) == FSE_OK) {
             archive_add_file_item(browser, &file_info, name);
-            ++files_cnt;
+            items_cnt++;
         } else {
-            storage_dir_close(directory);
-            storage_file_free(directory);
-            return false;
+            break;
         }
     }
     storage_dir_close(directory);
     storage_file_free(directory);
-
     furi_record_close("storage");
 
-    return true;
+    return (items_cnt == count);
 }
 
 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);
 
     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));
-    file_worker_free(file_worker);
+    FileInfo fileinfo;
+    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))) {
         archive_favorites_delete("%s", string_get_cstr(filename));
@@ -224,4 +249,4 @@ void archive_delete_file(void* context, const char* format, ...) {
     }
 
     string_clear(filename);
-}
+}

+ 6 - 8
applications/archive/helpers/archive_files.h

@@ -2,8 +2,6 @@
 #include "file_worker.h"
 #include <m-array.h>
 
-#define MAX_FILES 100 //temp
-
 typedef enum {
     ArchiveFileTypeIButton,
     ArchiveFileTypeNFC,
@@ -14,7 +12,7 @@ typedef enum {
     ArchiveFileTypeU2f,
     ArchiveFileTypeFolder,
     ArchiveFileTypeUnknown,
-    ArchiveFileTypesTotal,
+    ArchiveFileTypeLoading,
 } ArchiveFileTypeEnum;
 
 typedef struct {
@@ -57,12 +55,12 @@ ARRAY_DEF(
      INIT_SET(API_6(ArchiveFile_t_init_set)),
      CLEAR(API_2(ArchiveFile_t_clear))))
 
-bool 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);
+bool archive_filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name);
+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_get_file_extension(char* name, char* ext);
 bool archive_get_filenames(void* context, const char* path);
-bool archive_dir_not_empty(void* context, const char* path);
-bool archive_read_dir(void* context, const char* path);
+uint32_t archive_dir_count_items(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_delete_file(void* context, const char* format, ...);
+void archive_delete_file(void* context, const char* format, ...);

+ 11 - 1
applications/archive/scenes/archive_scene_browser.c

@@ -105,7 +105,9 @@ bool archive_scene_browser_on_event(void* context, SceneManagerEvent event) {
             consumed = true;
             break;
         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;
             break;
         case ArchiveBrowserEventEnterDir:
@@ -136,6 +138,14 @@ bool archive_scene_browser_on_event(void* context, SceneManagerEvent event) {
             archive_favorites_save(archive->browser);
             consumed = true;
             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:
             if(archive_get_depth(browser)) {

+ 71 - 33
applications/archive/views/archive_browser_view.c

@@ -24,6 +24,7 @@ static const Icon* ArchiveItemIcons[] = {
     [ArchiveFileTypeU2f] = &I_u2f_10px,
     [ArchiveFileTypeFolder] = &I_dir_10px,
     [ArchiveFileTypeUnknown] = &I_unknown_10px,
+    [ArchiveFileTypeLoading] = &I_unknown_10px, // TODO loading icon
 };
 
 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[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)) {
         string_set_str(menu[0], "---");
@@ -58,6 +59,7 @@ static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
     } else {
         if(model->tab_idx == ArchiveTabFavorites) {
             string_set_str(menu[2], "Move");
+            string_set_str(menu[3], "---");
         } else if(selected->is_app) {
             string_set_str(menu[2], "---");
         }
@@ -100,36 +102,44 @@ static void draw_list(Canvas* canvas, ArchiveBrowserViewModel* model) {
     furi_assert(model);
 
     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;
         char cstr_buff[MAX_NAME_LEN];
-        size_t idx = CLAMP(i + model->list_offset, array_size, 0);
-        uint8_t x_offset = (model->move_fav && model->idx == idx) ? MOVE_OFFSET : 0;
-
-        ArchiveFile_t* file = files_array_get(model->files, CLAMP(idx, array_size - 1, 0));
+        size_t idx = CLAMP(i + model->list_offset, model->item_cnt, 0);
+        uint8_t x_offset = (model->move_fav && model->item_idx == idx) ? MOVE_OFFSET : 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(
             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);
         } else {
             canvas_set_color(canvas, ColorBlack);
         }
 
-        canvas_draw_icon(
-            canvas, 2 + x_offset, 16 + i * FRAME_HEIGHT, ArchiveItemIcons[file->type]);
+        canvas_draw_icon(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));
+
         string_clear(str_buff);
     }
 
     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) {
@@ -173,13 +183,13 @@ static void archive_render_status_bar(Canvas* canvas, ArchiveBrowserViewModel* m
     canvas_set_color(canvas, ColorBlack);
 }
 
-void archive_view_render(Canvas* canvas, void* model) {
-    ArchiveBrowserViewModel* m = model;
+void archive_view_render(Canvas* canvas, void* mdl) {
+    ArchiveBrowserViewModel* model = mdl;
 
-    archive_render_status_bar(canvas, model);
+    archive_render_status_bar(canvas, mdl);
 
-    if(files_array_size(m->files)) {
-        draw_list(canvas, m);
+    if(model->item_cnt > 0) {
+        draw_list(canvas, model);
     } else {
         canvas_draw_str_aligned(
             canvas, GUI_DISPLAY_WIDTH / 2, 40, AlignCenter, AlignCenter, "Empty");
@@ -191,6 +201,26 @@ View* archive_browser_get_view(ArchiveBrowserView* browser) {
     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) {
     furi_assert(event);
     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(
                 browser->view, (ArchiveBrowserViewModel * model) {
-                    uint16_t num_elements = (uint16_t)files_array_size(model->files);
-                    if((event->type == InputTypeShort || event->type == InputTypeRepeat)) {
-                        if(event->key == InputKeyUp) {
-                            model->idx = ((model->idx - 1) + num_elements) % num_elements;
-                            if(move_fav_mode) {
-                                browser->callback(ArchiveBrowserEventFavMoveUp, browser->context);
-                            }
-                        } else if(event->key == InputKeyDown) {
-                            model->idx = (model->idx + 1) % num_elements;
-                            if(move_fav_mode) {
-                                browser->callback(
-                                    ArchiveBrowserEventFavMoveDown, browser->context);
-                            }
+                    if(event->key == InputKeyUp) {
+                        model->item_idx =
+                            ((model->item_idx - 1) + model->item_cnt) % model->item_cnt;
+                        if(is_file_list_load_required(model)) {
+                            model->list_loading = true;
+                            browser->callback(ArchiveBrowserEventLoadPrevItems, browser->context);
+                        }
+                        if(move_fav_mode) {
+                            browser->callback(ArchiveBrowserEventFavMoveUp, browser->context);
+                        }
+                    } else if(event->key == InputKeyDown) {
+                        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(
         browser->view, (ArchiveBrowserViewModel * model) {
             files_array_init(model->files);
+            idx_last_array_init(model->idx_last);
             return true;
         });
 
@@ -329,6 +366,7 @@ void browser_free(ArchiveBrowserView* browser) {
     with_view_model(
         browser->view, (ArchiveBrowserViewModel * model) {
             files_array_clear(model->files);
+            idx_last_array_clear(model->idx_last);
             return false;
         });
 

+ 14 - 7
applications/archive/views/archive_browser_view.h

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

+ 5 - 0
firmware/targets/f7/furi_hal/furi_hal_crypto.c

@@ -2,6 +2,7 @@
 #include <furi_hal_bt.h>
 #include <furi_hal_random.h>
 #include <stm32wbxx_ll_cortex.h>
+#include <stm32wbxx_ll_bus.h>
 #include <furi.h>
 #include <shci.h>
 
@@ -182,6 +183,10 @@ static void crypto_enable() {
 
 static void crypto_disable() {
     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) {