Archive: fix delete from favorites tab (#752)
* archive_delete_file vargs, fix wrong path when deleting item from favorites tab * use string_t for archive vargs funcs * favorites manual sorting Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -76,6 +76,23 @@ void archive_file_array_rm_selected(ArchiveBrowserView* browser) {
|
||||
archive_update_offset(browser);
|
||||
}
|
||||
|
||||
void archive_file_array_swap(ArchiveBrowserView* browser, int8_t d) {
|
||||
with_view_model(
|
||||
browser->view, (ArchiveBrowserViewModel * model) {
|
||||
size_t array_size = files_array_size(model->files) - 1;
|
||||
uint8_t swap_idx = CLAMP(model->idx + d, array_size, 0);
|
||||
|
||||
if(model->idx == 0 && d < 0) {
|
||||
swap_idx = array_size;
|
||||
} else if(model->idx == array_size && d > 0) {
|
||||
swap_idx = 0;
|
||||
}
|
||||
|
||||
files_array_swap_at(model->files, model->idx, swap_idx);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
void archive_file_array_rm_all(ArchiveBrowserView* browser) {
|
||||
with_view_model(
|
||||
browser->view, (ArchiveBrowserViewModel * model) {
|
||||
@@ -95,6 +112,18 @@ ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) {
|
||||
return selected;
|
||||
}
|
||||
|
||||
ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) {
|
||||
ArchiveFile_t* selected;
|
||||
idx = CLAMP(idx, archive_file_array_size(browser), 0);
|
||||
|
||||
with_view_model(
|
||||
browser->view, (ArchiveBrowserViewModel * model) {
|
||||
selected = files_array_size(model->files) ? files_array_get(model->files, idx) : NULL;
|
||||
return false;
|
||||
});
|
||||
return selected;
|
||||
}
|
||||
|
||||
ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
|
||||
ArchiveTabEnum tab_id;
|
||||
with_view_model(
|
||||
@@ -179,6 +208,14 @@ void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
|
||||
});
|
||||
}
|
||||
|
||||
void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active) {
|
||||
with_view_model(
|
||||
browser->view, (ArchiveBrowserViewModel * model) {
|
||||
model->move_fav = active;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
void archive_switch_dir(ArchiveBrowserView* browser, const char* path) {
|
||||
furi_assert(browser);
|
||||
furi_assert(path);
|
||||
|
@@ -52,9 +52,11 @@ 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_swap(ArchiveBrowserView* browser, int8_t d);
|
||||
void archive_file_array_rm_all(ArchiveBrowserView* browser);
|
||||
|
||||
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);
|
||||
uint8_t archive_get_depth(ArchiveBrowserView* browser);
|
||||
const char* archive_get_path(ArchiveBrowserView* browser);
|
||||
@@ -62,6 +64,7 @@ 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_favorites_move_mode(ArchiveBrowserView* browser, bool active);
|
||||
|
||||
void archive_switch_tab(ArchiveBrowserView* browser, InputKey key);
|
||||
void archive_enter_dir(ArchiveBrowserView* browser, string_t name);
|
||||
|
@@ -34,7 +34,7 @@ uint16_t archive_favorites_count(void* context) {
|
||||
bool archive_favorites_read(void* context) {
|
||||
furi_assert(context);
|
||||
|
||||
ArchiveBrowserView* archive_view = context;
|
||||
ArchiveBrowserView* browser = context;
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
string_t buffer;
|
||||
@@ -52,7 +52,7 @@ bool archive_favorites_read(void* context) {
|
||||
break;
|
||||
}
|
||||
|
||||
archive_add_item(archive_view, &file_info, string_get_cstr(buffer));
|
||||
archive_add_item(browser, &file_info, string_get_cstr(buffer));
|
||||
string_clean(buffer);
|
||||
}
|
||||
}
|
||||
@@ -63,17 +63,15 @@ bool archive_favorites_read(void* context) {
|
||||
}
|
||||
|
||||
bool archive_favorites_delete(const char* format, ...) {
|
||||
string_t buffer;
|
||||
string_t filename;
|
||||
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);
|
||||
string_init_vprintf(filename, format, args);
|
||||
va_end(args);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
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) {
|
||||
@@ -85,13 +83,14 @@ bool archive_favorites_delete(const char* format, ...) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(string_search_str(buffer, filename)) {
|
||||
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\r\n", string_get_cstr(buffer));
|
||||
if(string_search(buffer, filename)) {
|
||||
archive_file_append(ARCHIVE_FAV_TEMP_PATH, "%s\n", string_get_cstr(buffer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string_clear(buffer);
|
||||
string_clear(filename);
|
||||
|
||||
file_worker_close(file_worker);
|
||||
file_worker_remove(file_worker, ARCHIVE_FAV_PATH);
|
||||
@@ -103,16 +102,15 @@ bool archive_favorites_delete(const char* format, ...) {
|
||||
}
|
||||
|
||||
bool archive_is_favorite(const char* format, ...) {
|
||||
string_t buffer;
|
||||
string_t filename;
|
||||
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);
|
||||
string_init_vprintf(filename, format, args);
|
||||
va_end(args);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
string_t buffer;
|
||||
string_init(buffer);
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
bool found = false;
|
||||
bool result = file_worker_open(file_worker, ARCHIVE_FAV_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
@@ -125,7 +123,7 @@ bool archive_is_favorite(const char* format, ...) {
|
||||
if(!string_size(buffer)) {
|
||||
break;
|
||||
}
|
||||
if(!string_search_str(buffer, filename)) {
|
||||
if(!string_search(buffer, filename)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -133,6 +131,7 @@ bool archive_is_favorite(const char* format, ...) {
|
||||
}
|
||||
|
||||
string_clear(buffer);
|
||||
string_clear(filename);
|
||||
file_worker_close(file_worker);
|
||||
file_worker_free(file_worker);
|
||||
|
||||
@@ -166,7 +165,7 @@ bool archive_favorites_rename(const char* file_path, const char* src, const char
|
||||
|
||||
archive_file_append(
|
||||
ARCHIVE_FAV_TEMP_PATH,
|
||||
"%s\r\n",
|
||||
"%s\n",
|
||||
string_search(buffer, path) ? string_get_cstr(buffer) : dst);
|
||||
}
|
||||
}
|
||||
@@ -186,5 +185,22 @@ bool archive_favorites_rename(const char* file_path, const char* src, const char
|
||||
void archive_add_to_favorites(const char* file_path) {
|
||||
furi_assert(file_path);
|
||||
|
||||
archive_file_append(ARCHIVE_FAV_PATH, "%s\r\n", file_path);
|
||||
archive_file_append(ARCHIVE_FAV_PATH, "%s\n", file_path);
|
||||
}
|
||||
|
||||
void archive_favorites_save(void* context) {
|
||||
furi_assert(context);
|
||||
|
||||
ArchiveBrowserView* browser = context;
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
for(size_t i = 0; i < archive_file_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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
@@ -9,4 +9,5 @@ bool archive_favorites_read(void* context);
|
||||
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);
|
||||
void archive_add_to_favorites(const char* file_path);
|
||||
void archive_favorites_save(void* context);
|
||||
|
@@ -138,11 +138,10 @@ bool archive_read_dir(void* context, const char* path) {
|
||||
void archive_file_append(const char* path, const char* format, ...) {
|
||||
furi_assert(path);
|
||||
|
||||
string_t 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);
|
||||
string_init_vprintf(string, format, args);
|
||||
va_end(args);
|
||||
|
||||
FileWorker* file_worker = file_worker_alloc(false);
|
||||
@@ -151,7 +150,7 @@ void archive_file_append(const char* path, const char* format, ...) {
|
||||
FURI_LOG_E("Archive", "Append open error");
|
||||
}
|
||||
|
||||
if(!file_worker_write(file_worker, cstr_buff, strlen(cstr_buff))) {
|
||||
if(!file_worker_write(file_worker, string_get_cstr(string), string_size(string))) {
|
||||
FURI_LOG_E("Archive", "Append write error");
|
||||
}
|
||||
|
||||
@@ -159,26 +158,28 @@ void archive_file_append(const char* path, const char* format, ...) {
|
||||
file_worker_free(file_worker);
|
||||
}
|
||||
|
||||
void archive_delete_file(void* context, string_t path, string_t name) {
|
||||
void archive_delete_file(void* context, const char* format, ...) {
|
||||
furi_assert(context);
|
||||
furi_assert(path);
|
||||
furi_assert(name);
|
||||
|
||||
string_t filename;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
string_init_vprintf(filename, format, args);
|
||||
va_end(args);
|
||||
|
||||
ArchiveBrowserView* browser = context;
|
||||
FileWorker* file_worker = file_worker_alloc(true);
|
||||
|
||||
string_t full_path;
|
||||
string_init_printf(full_path, "%s/%s", string_get_cstr(path), string_get_cstr(name));
|
||||
|
||||
bool res = file_worker_remove(file_worker, string_get_cstr(full_path));
|
||||
bool res = file_worker_remove(file_worker, string_get_cstr(filename));
|
||||
file_worker_free(file_worker);
|
||||
|
||||
if(archive_is_favorite(string_get_cstr(full_path))) {
|
||||
archive_favorites_delete(string_get_cstr(full_path));
|
||||
if(archive_is_favorite("%s", string_get_cstr(filename))) {
|
||||
archive_favorites_delete("%s", string_get_cstr(filename));
|
||||
}
|
||||
|
||||
if(res) {
|
||||
archive_file_array_rm_selected(browser);
|
||||
}
|
||||
|
||||
string_clear(full_path);
|
||||
}
|
||||
string_clear(filename);
|
||||
}
|
@@ -54,4 +54,4 @@ 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, const char* format, ...);
|
||||
void archive_delete_file(void* context, string_t path, string_t name);
|
||||
void archive_delete_file(void* context, const char* format, ...);
|
Reference in New Issue
Block a user