[FL-2212] File validators and archive fixes #972

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Nikolay Minaylov
2022-01-29 12:39:10 +03:00
committed by GitHub
parent 84410c83b5
commit 6264ee8c3b
24 changed files with 194 additions and 52 deletions

View File

@@ -25,4 +25,5 @@ struct ArchiveApp {
ArchiveBrowserView* browser;
TextInput* text_input;
char text_store[MAX_NAME_LEN];
char file_extension[MAX_EXT_LEN + 1];
};

View File

@@ -272,7 +272,6 @@ void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
with_view_model(
browser->view, (ArchiveBrowserViewModel * model) {
model->last_idx = model->idx;
model->last_offset = model->list_offset;
model->idx = 0;
model->depth = CLAMP(model->depth + 1, MAX_DEPTH, 0);
return false;

View File

@@ -31,6 +31,40 @@ uint16_t archive_favorites_count(void* context) {
return lines;
}
static bool archive_favourites_rescan() {
string_t buffer;
string_init(buffer);
FileWorker* file_worker = file_worker_alloc(true);
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
while(1) {
if(!file_worker_read_until(file_worker, buffer, '\n')) {
break;
}
if(!string_size(buffer)) {
break;
}
bool file_exists = false;
file_worker_is_file_exist(file_worker, string_get_cstr(buffer), &file_exists);
if(file_exists) {
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
}
}
}
string_clear(buffer);
file_worker_close(file_worker);
file_worker_remove(file_worker, ARCHIVE_FAV_PATH);
file_worker_rename(file_worker, ARCHIVE_FAV_TEMP_PATH, ARCHIVE_FAV_PATH);
file_worker_free(file_worker);
return result;
}
bool archive_favorites_read(void* context) {
furi_assert(context);
@@ -41,6 +75,8 @@ bool archive_favorites_read(void* context) {
FileInfo file_info;
string_init(buffer);
bool need_refresh = false;
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
if(result) {
@@ -52,13 +88,24 @@ bool archive_favorites_read(void* context) {
break;
}
archive_add_item(browser, &file_info, string_get_cstr(buffer));
bool file_exists = false;
file_worker_is_file_exist(file_worker, string_get_cstr(buffer), &file_exists);
if(file_exists)
archive_add_item(browser, &file_info, string_get_cstr(buffer));
else
need_refresh = true;
string_reset(buffer);
}
}
string_clear(buffer);
file_worker_close(file_worker);
file_worker_free(file_worker);
if(need_refresh) {
archive_favourites_rescan();
}
return result;
}

View File

@@ -30,6 +30,14 @@ void archive_trim_file_path(char* name, bool ext) {
}
}
void archive_get_file_extension(char* name, char* ext) {
char* dot = strrchr(name, '.');
if(dot == NULL)
*ext = '\0';
else
strncpy(ext, dot, MAX_EXT_LEN);
}
void set_file_type(ArchiveFile_t* file, FileInfo* file_info) {
furi_assert(file);
furi_assert(file_info);

View File

@@ -50,6 +50,7 @@ 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_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_empty(void* context, const char* path);
bool archive_read_dir(void* context, const char* path);

View File

@@ -18,6 +18,7 @@ void archive_scene_rename_on_enter(void* context) {
ArchiveFile_t* current = archive_get_current_file(archive->browser);
strlcpy(archive->text_store, string_get_cstr(current->name), MAX_NAME_LEN);
archive_get_file_extension(archive->text_store, archive->file_extension);
archive_trim_file_path(archive->text_store, true);
text_input_set_header_text(text_input, "Rename:");
@@ -30,6 +31,10 @@ void archive_scene_rename_on_enter(void* context) {
MAX_TEXT_INPUT_LEN,
false);
ValidatorIsFile* validator_is_file =
validator_is_file_alloc_init(archive_get_path(archive->browser), archive->file_extension);
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewTextInput);
}
@@ -74,6 +79,11 @@ bool archive_scene_rename_on_event(void* context, SceneManagerEvent event) {
void archive_scene_rename_on_exit(void* context) {
ArchiveApp* archive = (ArchiveApp*)context;
// Clear view
void* validator_context = text_input_get_validator_callback_context(archive->text_input);
text_input_set_validator(archive->text_input, NULL, NULL);
validator_is_file_free(validator_context);
text_input_reset(archive->text_input);
}

View File

@@ -11,6 +11,7 @@
#define MAX_LEN_PX 110
#define MAX_NAME_LEN 255
#define MAX_EXT_LEN 6
#define FRAME_HEIGHT 12
#define MENU_ITEMS 4
#define MAX_DEPTH 32