[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:
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user