[FL-1823, FL-1824] Archive app: refactoring and UI improvements (#711)

* Archive app: skip empty app folders, file menu in favorites tab, looped tab switching
* refactoring
* cleanup
* better filepath trim
* fix excessive view updates, various small optimizations
* better list_offset calculation, favorites vargs)
* revert poor fix

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
its your bedtime
2021-09-21 13:56:33 +03:00
committed by GitHub
parent 0e1922db4d
commit 4c05f67686
14 changed files with 948 additions and 910 deletions

View File

@@ -0,0 +1,255 @@
#include "archive_browser.h"
#include "math.h"
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->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;
});
}
void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
furi_assert(browser);
furi_assert(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);
} else {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
uint16_t idx = 0;
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;
break;
}
++idx;
}
return false;
});
archive_update_offset(browser);
}
}
size_t archive_file_array_size(ArchiveBrowserView* browser) {
uint16_t size = 0;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
size = files_array_size(model->files);
return false;
});
return size;
}
void archive_file_array_rm_selected(ArchiveBrowserView* browser) {
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);
return false;
});
if(!archive_file_array_size(browser) && !archive_get_depth(browser)) {
archive_switch_tab(browser, DEFAULT_TAB_DIR);
}
archive_update_offset(browser);
}
void archive_file_array_rm_all(ArchiveBrowserView* browser) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_clean(model->files);
return false;
});
}
ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* 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;
return false;
});
return selected;
}
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
ArchiveTabEnum tab_id;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
tab_id = model->tab_idx;
return false;
});
return tab_id;
}
uint8_t archive_get_depth(ArchiveBrowserView* browser) {
uint8_t depth;
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
depth = model->depth;
return false;
});
return depth;
}
const char* archive_get_path(ArchiveBrowserView* browser) {
return string_get_cstr(browser->path);
}
const char* archive_get_name(ArchiveBrowserView* browser) {
ArchiveFile_t* selected = archive_get_current_file(browser);
return string_get_cstr(selected->name);
}
void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->tab_idx = tab;
return false;
});
}
void archive_set_last_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->last_tab = model->tab_idx;
return false;
});
}
void archive_add_item(ArchiveBrowserView* browser, FileInfo* file_info, const char* name) {
furi_assert(browser);
furi_assert(file_info);
furi_assert(name);
ArchiveFile_t item;
if(filter_by_extension(file_info, 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);
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
files_array_push_back(model->files, item);
return false;
});
ArchiveFile_t_clear(&item);
}
}
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/%s", string_get_cstr(browser->path), string_get_cstr(selected->name));
}
return true;
});
}
void archive_switch_dir(ArchiveBrowserView* browser, const char* path) {
furi_assert(browser);
furi_assert(path);
string_set(browser->path, path);
archive_get_filenames(browser, string_get_cstr(browser->path));
archive_update_offset(browser);
}
void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
furi_assert(browser);
ArchiveTabEnum tab = archive_get_tab(browser);
if(key == InputKeyLeft) {
tab = ((tab - 1) + ArchiveTabTotal) % ArchiveTabTotal;
} else if(key == InputKeyRight) {
tab = (tab + 1) % ArchiveTabTotal;
}
archive_set_tab(browser, tab);
if((tab != ArchiveTabFavorites &&
!archive_dir_empty(browser, archive_get_default_path(tab))) ||
(tab == ArchiveTabFavorites && !archive_favorites_count(browser))) {
archive_switch_tab(browser, key);
} else {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
if(model->last_tab != model->tab_idx) {
model->idx = 0;
model->depth = 0;
}
return false;
});
archive_switch_dir(browser, archive_get_default_path(tab));
}
archive_set_last_tab(browser, tab);
}
void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
furi_assert(browser);
furi_assert(name);
// update last index
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->last_idx[model->depth] =
CLAMP(model->idx, files_array_size(model->files) - 1, 0);
model->idx = 0;
model->depth = CLAMP(model->depth + 1, MAX_DEPTH, 0);
return false;
});
string_cat(browser->path, "/");
string_cat(browser->path, name);
archive_switch_dir(browser, string_get_cstr(browser->path));
}
void archive_leave_dir(ArchiveBrowserView* browser) {
furi_assert(browser);
const char* path = archive_get_path(browser);
char* last_char_ptr = strrchr(path, '/');
if(last_char_ptr) {
size_t pos = last_char_ptr - path;
string_left(browser->path, pos);
}
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->depth = CLAMP(model->depth - 1, MAX_DEPTH, 0);
model->idx = model->last_idx[model->depth];
return false;
});
archive_switch_dir(browser, path);
}

View File

@@ -0,0 +1,68 @@
#pragma once
#include "../archive_i.h"
#define DEFAULT_TAB_DIR InputKeyRight //default tab swith direction
static const char* tab_default_paths[] = {
[ArchiveTabFavorites] = "/any/favorites",
[ArchiveTabIButton] = "/any/ibutton",
[ArchiveTabNFC] = "/any/nfc",
[ArchiveTabSubGhz] = "/any/subghz/saved",
[ArchiveTabLFRFID] = "/any/lfrfid",
[ArchiveTabIrda] = "/any/irda",
[ArchiveTabBrowser] = "/any",
};
static const char* known_ext[] = {
[ArchiveFileTypeIButton] = ".ibtn",
[ArchiveFileTypeNFC] = ".nfc",
[ArchiveFileTypeSubGhz] = ".sub",
[ArchiveFileTypeLFRFID] = ".rfid",
[ArchiveFileTypeIrda] = ".ir",
};
static inline const char* get_tab_ext(ArchiveTabEnum tab) {
switch(tab) {
case ArchiveTabIButton:
return known_ext[ArchiveFileTypeIButton];
case ArchiveTabNFC:
return known_ext[ArchiveFileTypeNFC];
case ArchiveTabSubGhz:
return known_ext[ArchiveFileTypeSubGhz];
case ArchiveTabLFRFID:
return known_ext[ArchiveFileTypeLFRFID];
case ArchiveTabIrda:
return known_ext[ArchiveFileTypeIrda];
default:
return "*";
}
}
static inline const char* archive_get_default_path(ArchiveTabEnum tab) {
return tab_default_paths[tab];
}
inline bool is_known_app(ArchiveFileTypeEnum type) {
return (type != ArchiveFileTypeFolder && type != ArchiveFileTypeUnknown);
}
void archive_update_offset(ArchiveBrowserView* browser);
void archive_update_focus(ArchiveBrowserView* browser, const char* target);
size_t archive_file_array_size(ArchiveBrowserView* browser);
void archive_file_array_rm_selected(ArchiveBrowserView* browser);
void archive_file_array_rm_all(ArchiveBrowserView* browser);
ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser);
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser);
uint8_t archive_get_depth(ArchiveBrowserView* browser);
const char* archive_get_path(ArchiveBrowserView* browser);
const char* archive_get_name(ArchiveBrowserView* browser);
void archive_add_item(ArchiveBrowserView* browser, FileInfo* file_info, const char* name);
void archive_show_file_menu(ArchiveBrowserView* browser, bool show);
void archive_switch_tab(ArchiveBrowserView* browser, InputKey key);
void archive_enter_dir(ArchiveBrowserView* browser, string_t name);
void archive_leave_dir(ArchiveBrowserView* browser);

View File

@@ -1,18 +1,47 @@
#include "archive_favorites.h"
#include "archive_files.h"
#include "../views/archive_main_view.h"
#include "archive_browser.h"
uint16_t archive_favorites_count(void* context) {
furi_assert(context);
FileWorker* file_worker = file_worker_alloc(true);
string_t buffer;
string_init(buffer);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
uint16_t lines = 0;
if(result) {
while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) {
break;
}
if(!string_size(buffer)) {
break;
}
++lines;
}
}
string_clear(buffer);
file_worker_close(file_worker);
file_worker_free(file_worker);
return lines;
}
bool archive_favorites_read(void* context) {
furi_assert(context);
ArchiveMainView* archive_view = context;
ArchiveBrowserView* archive_view = context;
FileWorker* file_worker = file_worker_alloc(true);
string_t buffer;
FileInfo file_info;
string_init(buffer);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_ALWAYS);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
@@ -23,7 +52,7 @@ bool archive_favorites_read(void* context) {
break;
}
archive_view_add_item(archive_view, &file_info, string_get_cstr(buffer));
archive_add_item(archive_view, &file_info, string_get_cstr(buffer));
string_clean(buffer);
}
}
@@ -33,18 +62,19 @@ bool archive_favorites_read(void* context) {
return result;
}
bool archive_favorites_delete(const char* file_path, const char* name) {
furi_assert(file_path);
furi_assert(name);
bool archive_favorites_delete(const char* format, ...) {
va_list args;
va_start(args, format);
uint8_t len = vsnprintf(NULL, 0, format, args);
char filename[len + 1];
vsnprintf(filename, len + 1, format, args);
va_end(args);
FileWorker* file_worker = file_worker_alloc(true);
string_t path;
string_t buffer;
string_init(buffer);
string_init_printf(path, "%s/%s", file_path, name);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
@@ -55,17 +85,13 @@ bool archive_favorites_delete(const char* file_path, const char* name) {
break;
}
if(string_search(buffer, path)) {
string_t temp;
string_init_printf(temp, "%s\r\n", string_get_cstr(buffer));
archive_file_append(ARCHIVE_FAV_TEMP_PATH, temp);
string_clear(temp);
if(string_search_str(buffer, filename)) {
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\r\n", string_get_cstr(buffer));
}
}
}
string_clear(buffer);
string_clear(path);
file_worker_close(file_worker);
file_worker_remove(file_worker, ARCHIVE_FAV_PATH);
@@ -76,19 +102,20 @@ bool archive_favorites_delete(const char* file_path, const char* name) {
return result;
}
bool archive_is_favorite(const char* file_path, const char* name) {
furi_assert(file_path);
furi_assert(name);
bool archive_is_favorite(const char* format, ...) {
va_list args;
va_start(args, format);
uint8_t len = vsnprintf(NULL, 0, format, args);
char filename[len + 1];
vsnprintf(filename, len + 1, format, args);
va_end(args);
FileWorker* file_worker = file_worker_alloc(true);
string_t path;
string_t buffer;
string_init(buffer);
bool found = false;
string_init_printf(path, "%s/%s", file_path, name);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_ALWAYS);
bool found = false;
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
@@ -98,7 +125,7 @@ bool archive_is_favorite(const char* file_path, const char* name) {
if(!string_size(buffer)) {
break;
}
if(!string_search(buffer, path)) {
if(!string_search_str(buffer, filename)) {
found = true;
break;
}
@@ -106,7 +133,6 @@ bool archive_is_favorite(const char* file_path, const char* name) {
}
string_clear(buffer);
string_clear(path);
file_worker_close(file_worker);
file_worker_free(file_worker);
@@ -122,10 +148,8 @@ bool archive_favorites_rename(const char* file_path, const char* src, const char
string_t path;
string_t buffer;
string_t temp;
string_init(buffer);
string_init(temp);
string_init(path);
string_printf(path, "%s/%s", file_path, src);
@@ -139,14 +163,14 @@ bool archive_favorites_rename(const char* file_path, const char* src, const char
if(!string_size(buffer)) {
break;
}
string_printf(
temp, "%s\r\n", string_search(buffer, path) ? string_get_cstr(buffer) : dst);
archive_file_append(ARCHIVE_FAV_TEMP_PATH, temp);
string_clean(temp);
archive_file_append(
ARCHIVE_FAV_TEMP_PATH,
"%s\r\n",
string_search(buffer, path) ? string_get_cstr(buffer) : dst);
}
}
string_clear(temp);
string_clear(buffer);
string_clear(path);
@@ -159,13 +183,8 @@ bool archive_favorites_rename(const char* file_path, const char* src, const char
return result;
}
void archive_add_to_favorites(const char* file_path, const char* name) {
void archive_add_to_favorites(const char* file_path) {
furi_assert(file_path);
furi_assert(name);
string_t buffer_src;
string_init_printf(buffer_src, "%s/%s\r\n", file_path, name);
archive_file_append(ARCHIVE_FAV_PATH, buffer_src);
string_clear(buffer_src);
archive_file_append(ARCHIVE_FAV_PATH, "%s\r\n", file_path);
}

View File

@@ -4,8 +4,9 @@
#define ARCHIVE_FAV_PATH "/any/favorites.txt"
#define ARCHIVE_FAV_TEMP_PATH "/any/favorites.tmp"
uint16_t archive_favorites_count(void* context);
bool archive_favorites_read(void* context);
bool archive_favorites_delete(const char* file_path, const char* name);
bool archive_is_favorite(const char* file_path, const char* name);
bool archive_favorites_delete(const char* format, ...);
bool archive_is_favorite(const char* format, ...);
bool archive_favorites_rename(const char* file_path, const char* src, const char* dst);
void archive_add_to_favorites(const char* file_path, const char* name);
void archive_add_to_favorites(const char* file_path);

View File

@@ -1,6 +1,5 @@
#include "archive_files.h"
#include "archive_favorites.h"
#include "../archive_i.h"
#include "archive_browser.h"
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name) {
furi_assert(file_info);
@@ -20,14 +19,12 @@ bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* n
return result;
}
void archive_trim_file_ext(char* name) {
size_t str_len = strlen(name);
char* end = name + str_len;
while(end > name && *end != '.' && *end != '\\' && *end != '/') {
--end;
}
if((end > name && *end == '.') && (*(end - 1) != '\\' && *(end - 1) != '/')) {
*end = '\0';
void archive_trim_file_path(char* name, bool ext) {
char* slash = strrchr(name, '/') + 1;
if(strlen(slash)) strlcpy(name, slash, strlen(slash) + 1);
if(ext) {
char* dot = strrchr(name, '.');
if(strlen(dot)) *dot = '\0';
}
}
@@ -49,24 +46,24 @@ void set_file_type(ArchiveFile_t* file, FileInfo* file_info) {
}
}
bool archive_get_filenames(void* context, uint8_t tab_id, const char* path) {
bool archive_get_filenames(void* context, const char* path) {
furi_assert(context);
ArchiveMainView* main_view = context;
archive_file_array_clean(main_view);
bool res;
ArchiveBrowserView* browser = context;
archive_file_array_rm_all(browser);
if(tab_id != ArchiveTabFavorites) {
archive_read_dir(main_view, path);
if(archive_get_tab(browser) != ArchiveTabFavorites) {
res = archive_read_dir(browser, path);
} else {
archive_favorites_read(main_view);
res = archive_favorites_read(browser);
}
return true;
return res;
}
bool archive_read_dir(void* context, const char* path) {
bool archive_dir_empty(void* context, const char* path) { // can be simpler?
furi_assert(context);
ArchiveMainView* main_view = context;
FileInfo file_info;
Storage* fs_api = furi_record_open("storage");
File* directory = storage_file_alloc(fs_api);
@@ -78,17 +75,52 @@ bool archive_read_dir(void* context, const char* path) {
return false;
}
bool files_found = false;
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) {
files_found = name[0];
} else {
return false;
}
}
storage_dir_close(directory);
storage_file_free(directory);
uint16_t files_cnt = archive_file_array_size(main_view);
furi_record_close("storage");
return files_found;
}
bool archive_read_dir(void* context, const char* path) {
furi_assert(context);
ArchiveBrowserView* browser = context;
FileInfo file_info;
Storage* fs_api = furi_record_open("storage");
File* directory = storage_file_alloc(fs_api);
char name[MAX_NAME_LEN];
size_t files_cnt = 0;
if(!storage_dir_open(directory, path)) {
storage_dir_close(directory);
storage_file_free(directory);
return false;
}
while(1) {
if(!storage_dir_read(directory, &file_info, name, MAX_NAME_LEN)) {
break;
}
if(files_cnt > MAX_FILES) {
break;
} else if(storage_file_get_error(directory) == FSE_OK) {
archive_view_add_item(main_view, &file_info, name);
archive_add_item(browser, &file_info, name);
++files_cnt;
} else {
storage_dir_close(directory);
storage_file_free(directory);
@@ -103,9 +135,15 @@ bool archive_read_dir(void* context, const char* path) {
return true;
}
void archive_file_append(const char* path, string_t string) {
void archive_file_append(const char* path, const char* format, ...) {
furi_assert(path);
furi_assert(string);
va_list args;
va_start(args, format);
uint8_t len = vsnprintf(NULL, 0, format, args);
char cstr_buff[len + 1];
vsnprintf(cstr_buff, len + 1, format, args);
va_end(args);
FileWorker* file_worker = file_worker_alloc(false);
@@ -113,7 +151,7 @@ void archive_file_append(const char* path, string_t string) {
FURI_LOG_E("Archive", "Append open error");
}
if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
if(!file_worker_write(file_worker, cstr_buff, strlen(cstr_buff))) {
FURI_LOG_E("Archive", "Append write error");
}
@@ -125,19 +163,22 @@ void archive_delete_file(void* context, string_t path, string_t name) {
furi_assert(context);
furi_assert(path);
furi_assert(name);
ArchiveMainView* main_view = context;
FileWorker* file_worker = file_worker_alloc(false);
ArchiveBrowserView* browser = context;
FileWorker* file_worker = file_worker_alloc(true);
string_t full_path;
string_init(full_path);
string_printf(full_path, "%s/%s", string_get_cstr(path), string_get_cstr(name));
file_worker_remove(file_worker, string_get_cstr(full_path));
file_worker_free(file_worker);
string_clear(full_path);
string_init_printf(full_path, "%s/%s", string_get_cstr(path), string_get_cstr(name));
if(archive_is_favorite(string_get_cstr(path), string_get_cstr(name))) {
archive_favorites_delete(string_get_cstr(path), string_get_cstr(name));
bool res = file_worker_remove(file_worker, string_get_cstr(full_path));
file_worker_free(file_worker);
if(archive_is_favorite(string_get_cstr(full_path))) {
archive_favorites_delete(string_get_cstr(full_path));
}
archive_file_array_remove_selected(main_view);
if(res) {
archive_file_array_rm_selected(browser);
}
string_clear(full_path);
}

View File

@@ -49,8 +49,9 @@ ARRAY_DEF(
bool filter_by_extension(FileInfo* file_info, const char* tab_ext, const char* name);
void set_file_type(ArchiveFile_t* file, FileInfo* file_info);
void archive_trim_file_ext(char* name);
bool archive_get_filenames(void* context, uint8_t tab_id, const char* path);
void archive_trim_file_path(char* name, bool ext);
bool archive_get_filenames(void* context, const char* path);
bool archive_dir_empty(void* context, const char* path);
bool archive_read_dir(void* context, const char* path);
void archive_file_append(const char* path, string_t string);
void archive_file_append(const char* path, const char* format, ...);
void archive_delete_file(void* context, string_t path, string_t name);