|
|
@@ -2,6 +2,21 @@
|
|
|
|
|
|
static bool archive_get_filenames(ArchiveApp* archive);
|
|
|
|
|
|
+static bool is_favourite(ArchiveApp* archive, ArchiveFile_t* file) {
|
|
|
+ FS_Common_Api* common_api = &archive->fs_api->common;
|
|
|
+ FileInfo file_info;
|
|
|
+ FS_Error fr;
|
|
|
+ string_t path;
|
|
|
+
|
|
|
+ string_init_set(path, "favourites/");
|
|
|
+ string_cat(path, file->name);
|
|
|
+
|
|
|
+ fr = common_api->info(string_get_cstr(path), &file_info, NULL, 0);
|
|
|
+ FURI_LOG_I("FAV", "%d", fr);
|
|
|
+
|
|
|
+ return fr == 0 || fr == 2;
|
|
|
+}
|
|
|
+
|
|
|
static void update_offset(ArchiveApp* archive) {
|
|
|
furi_assert(archive);
|
|
|
|
|
|
@@ -9,10 +24,13 @@ static void update_offset(ArchiveApp* archive) {
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
size_t array_size = files_array_size(model->files);
|
|
|
uint16_t bounds = array_size > 3 ? 2 : array_size;
|
|
|
- if(model->list_offset < model->idx - bounds) {
|
|
|
- model->list_offset = CLAMP(model->list_offset + 1, array_size - (bounds + 2), 0);
|
|
|
+
|
|
|
+ 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->list_offset + 1, array_size - bounds, 0);
|
|
|
} else if(model->list_offset > model->idx - bounds) {
|
|
|
- model->list_offset = CLAMP(model->idx - 1, array_size - (bounds), 0);
|
|
|
+ model->list_offset = CLAMP(model->idx - 1, array_size - bounds, 0);
|
|
|
}
|
|
|
return true;
|
|
|
});
|
|
|
@@ -33,9 +51,9 @@ static void archive_update_last_idx(ArchiveApp* archive) {
|
|
|
static void archive_switch_dir(ArchiveApp* archive, const char* path) {
|
|
|
furi_assert(archive);
|
|
|
furi_assert(path);
|
|
|
-
|
|
|
string_set(archive->browser.path, path);
|
|
|
archive_get_filenames(archive);
|
|
|
+ update_offset(archive);
|
|
|
}
|
|
|
|
|
|
static void archive_switch_tab(ArchiveApp* archive) {
|
|
|
@@ -45,22 +63,22 @@ static void archive_switch_tab(ArchiveApp* archive) {
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
model->tab_idx = archive->browser.tab_id;
|
|
|
model->idx = 0;
|
|
|
+
|
|
|
return true;
|
|
|
});
|
|
|
|
|
|
archive->browser.depth = 0;
|
|
|
archive_switch_dir(archive, tab_default_paths[archive->browser.tab_id]);
|
|
|
-
|
|
|
- update_offset(archive);
|
|
|
}
|
|
|
|
|
|
static void archive_leave_dir(ArchiveApp* archive) {
|
|
|
furi_assert(archive);
|
|
|
|
|
|
- size_t last_char =
|
|
|
- string_search_rchar(archive->browser.path, '/', string_size(archive->browser.path));
|
|
|
- if(last_char) {
|
|
|
- string_right(archive->browser.path, last_char);
|
|
|
+ char* last_char_ptr = strrchr(string_get_cstr(archive->browser.path), '/');
|
|
|
+
|
|
|
+ if(last_char_ptr) {
|
|
|
+ size_t pos = last_char_ptr - string_get_cstr(archive->browser.path);
|
|
|
+ string_left(archive->browser.path, pos);
|
|
|
}
|
|
|
|
|
|
archive->browser.depth = CLAMP(archive->browser.depth - 1, MAX_DEPTH, 0);
|
|
|
@@ -68,12 +86,13 @@ static void archive_leave_dir(ArchiveApp* archive) {
|
|
|
with_view_model(
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
model->idx = archive->browser.last_idx[archive->browser.depth];
|
|
|
+ model->list_offset =
|
|
|
+ model->idx -
|
|
|
+ (files_array_size(model->files) > 3 ? 3 : files_array_size(model->files));
|
|
|
return true;
|
|
|
});
|
|
|
|
|
|
archive_switch_dir(archive, string_get_cstr(archive->browser.path));
|
|
|
-
|
|
|
- update_offset(archive);
|
|
|
}
|
|
|
|
|
|
static void archive_enter_dir(ArchiveApp* archive, string_t name) {
|
|
|
@@ -87,8 +106,6 @@ static void archive_enter_dir(ArchiveApp* archive, string_t name) {
|
|
|
string_cat(archive->browser.path, archive->browser.name);
|
|
|
|
|
|
archive_switch_dir(archive, string_get_cstr(archive->browser.path));
|
|
|
-
|
|
|
- update_offset(archive);
|
|
|
}
|
|
|
|
|
|
static bool filter_by_extension(ArchiveApp* archive, FileInfo* file_info, const char* name) {
|
|
|
@@ -103,6 +120,8 @@ static bool filter_by_extension(ArchiveApp* archive, FileInfo* file_info, const
|
|
|
result = true;
|
|
|
} else if(strstr(name, filter_ext_ptr) != NULL) {
|
|
|
result = true;
|
|
|
+ } else if(file_info->flags & FSF_DIRECTORY) {
|
|
|
+ result = true;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
@@ -208,6 +227,16 @@ static void archive_add_to_favourites(ArchiveApp* archive) {
|
|
|
furi_assert(archive);
|
|
|
|
|
|
FS_Common_Api* common_api = &archive->fs_api->common;
|
|
|
+ common_api->mkdir("favourites");
|
|
|
+
|
|
|
+ FS_File_Api* file_api = &archive->fs_api->file;
|
|
|
+ File src;
|
|
|
+ File dst;
|
|
|
+
|
|
|
+ bool fr;
|
|
|
+ uint16_t buffer[MAX_FILE_SIZE];
|
|
|
+ uint16_t bw = 0;
|
|
|
+ uint16_t br = 0;
|
|
|
|
|
|
string_t buffer_src;
|
|
|
string_t buffer_dst;
|
|
|
@@ -219,7 +248,20 @@ static void archive_add_to_favourites(ArchiveApp* archive) {
|
|
|
string_init_set_str(buffer_dst, "/favourites/");
|
|
|
string_cat(buffer_dst, archive->browser.name);
|
|
|
|
|
|
- common_api->rename(string_get_cstr(buffer_src), string_get_cstr(buffer_dst));
|
|
|
+ fr = file_api->open(&src, string_get_cstr(buffer_src), FSAM_READ, FSOM_OPEN_EXISTING);
|
|
|
+ FURI_LOG_I("FATFS", "OPEN: %d", fr);
|
|
|
+ fr = file_api->open(&dst, string_get_cstr(buffer_dst), FSAM_WRITE, FSOM_CREATE_ALWAYS);
|
|
|
+ FURI_LOG_I("FATFS", "CREATE: %d", fr);
|
|
|
+
|
|
|
+ for(;;) {
|
|
|
+ br = file_api->read(&src, &buffer, sizeof(buffer));
|
|
|
+ if(br == 0) break;
|
|
|
+ bw = file_api->write(&dst, &buffer, sizeof(buffer));
|
|
|
+ if(bw < br) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ file_api->close(&src);
|
|
|
+ file_api->close(&dst);
|
|
|
|
|
|
string_clear(buffer_src);
|
|
|
string_clear(buffer_dst);
|
|
|
@@ -295,8 +337,12 @@ static void archive_show_file_menu(ArchiveApp* archive) {
|
|
|
|
|
|
with_view_model(
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
+ ArchiveFile_t* selected;
|
|
|
+ selected = files_array_get(model->files, model->idx);
|
|
|
model->menu = true;
|
|
|
model->menu_idx = 0;
|
|
|
+ selected->fav = is_favourite(archive, selected);
|
|
|
+
|
|
|
return true;
|
|
|
});
|
|
|
}
|
|
|
@@ -321,29 +367,43 @@ static void archive_open_app(ArchiveApp* archive, const char* app_name, const ch
|
|
|
app_loader_start(app_name, args);
|
|
|
}
|
|
|
|
|
|
-static void archive_delete_file(ArchiveApp* archive, string_t name) {
|
|
|
+static void archive_delete_file(ArchiveApp* archive, ArchiveFile_t* file, bool fav, bool orig) {
|
|
|
furi_assert(archive);
|
|
|
- furi_assert(name);
|
|
|
+ furi_assert(file);
|
|
|
|
|
|
FS_Common_Api* common_api = &archive->fs_api->common;
|
|
|
-
|
|
|
string_t path;
|
|
|
- string_init_set(path, archive->browser.path);
|
|
|
- string_cat(path, "/");
|
|
|
- string_cat(path, name);
|
|
|
+ string_init(path);
|
|
|
+
|
|
|
+ if(!fav && !orig) {
|
|
|
+ string_set(path, archive->browser.path);
|
|
|
+ string_cat(path, "/");
|
|
|
+ string_cat(path, file->name);
|
|
|
+ common_api->remove(string_get_cstr(path));
|
|
|
+
|
|
|
+ } else { // remove from favorites
|
|
|
+ string_set(path, "favourites/");
|
|
|
+ string_cat(path, file->name);
|
|
|
+ common_api->remove(string_get_cstr(path));
|
|
|
+
|
|
|
+ if(orig) { // remove original file
|
|
|
+ string_set_str(path, get_default_path(file->type));
|
|
|
+ string_cat(path, "/");
|
|
|
+ string_cat(path, file->name);
|
|
|
+ common_api->remove(string_get_cstr(path));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- common_api->remove(string_get_cstr(path));
|
|
|
string_clear(path);
|
|
|
-
|
|
|
archive_get_filenames(archive);
|
|
|
|
|
|
- update_offset(archive);
|
|
|
-
|
|
|
with_view_model(
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
model->idx = CLAMP(model->idx, files_array_size(model->files) - 1, 0);
|
|
|
return true;
|
|
|
});
|
|
|
+
|
|
|
+ update_offset(archive);
|
|
|
}
|
|
|
|
|
|
static void archive_file_menu_callback(ArchiveApp* archive) {
|
|
|
@@ -361,7 +421,7 @@ static void archive_file_menu_callback(ArchiveApp* archive) {
|
|
|
|
|
|
switch(idx) {
|
|
|
case 0:
|
|
|
- if((selected->type != ArchiveFileTypeFolder && selected->type != ArchiveFileTypeUnknown)) {
|
|
|
+ if(is_known_app(selected->type)) {
|
|
|
string_t full_path;
|
|
|
string_init_set(full_path, archive->browser.path);
|
|
|
string_cat(full_path, "/");
|
|
|
@@ -374,19 +434,34 @@ static void archive_file_menu_callback(ArchiveApp* archive) {
|
|
|
}
|
|
|
break;
|
|
|
case 1:
|
|
|
-
|
|
|
- string_set(archive->browser.name, selected->name);
|
|
|
- archive_add_to_favourites(archive);
|
|
|
- archive_close_file_menu(archive);
|
|
|
+ if(is_known_app(selected->type)) {
|
|
|
+ if(!is_favourite(archive, selected)) {
|
|
|
+ string_set(archive->browser.name, selected->name);
|
|
|
+ archive_add_to_favourites(archive);
|
|
|
+ } else {
|
|
|
+ // delete from favourites
|
|
|
+ archive_delete_file(archive, selected, true, false);
|
|
|
+ }
|
|
|
+ archive_close_file_menu(archive);
|
|
|
+ }
|
|
|
break;
|
|
|
case 2:
|
|
|
// open rename view
|
|
|
- archive_enter_text_input(archive);
|
|
|
+ if(is_known_app(selected->type)) {
|
|
|
+ archive_enter_text_input(archive);
|
|
|
+ }
|
|
|
break;
|
|
|
case 3:
|
|
|
// confirmation?
|
|
|
- archive_delete_file(archive, selected->name);
|
|
|
+ if(is_favourite(archive, selected)) {
|
|
|
+ //delete both fav & original
|
|
|
+ archive_delete_file(archive, selected, true, true);
|
|
|
+ } else {
|
|
|
+ archive_delete_file(archive, selected, false, false);
|
|
|
+ }
|
|
|
+
|
|
|
archive_close_file_menu(archive);
|
|
|
+
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
@@ -405,9 +480,9 @@ static void menu_input_handler(ArchiveApp* archive, InputEvent* event) {
|
|
|
with_view_model(
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
if(event->key == InputKeyUp) {
|
|
|
- model->menu_idx = CLAMP(model->menu_idx - 1, MENU_ITEMS - 1, 0);
|
|
|
+ model->menu_idx = ((model->menu_idx - 1) + MENU_ITEMS) % MENU_ITEMS;
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
- model->menu_idx = CLAMP(model->menu_idx + 1, MENU_ITEMS - 1, 0);
|
|
|
+ model->menu_idx = (model->menu_idx + 1) % MENU_ITEMS;
|
|
|
}
|
|
|
return true;
|
|
|
});
|
|
|
@@ -437,14 +512,18 @@ static bool archive_view_input(InputEvent* event, void* context) {
|
|
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
if(event->key == InputKeyLeft) {
|
|
|
- archive->browser.tab_id = CLAMP(archive->browser.tab_id - 1, ArchiveTabTotal, 0);
|
|
|
- archive_switch_tab(archive);
|
|
|
- return true;
|
|
|
-
|
|
|
+ if(archive->browser.tab_id > 0) {
|
|
|
+ archive->browser.tab_id = CLAMP(archive->browser.tab_id - 1, ArchiveTabTotal, 0);
|
|
|
+ archive_switch_tab(archive);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
- archive->browser.tab_id = CLAMP(archive->browser.tab_id + 1, ArchiveTabTotal - 1, 0);
|
|
|
- archive_switch_tab(archive);
|
|
|
- return true;
|
|
|
+ if(archive->browser.tab_id < ArchiveTabTotal - 1) {
|
|
|
+ archive->browser.tab_id =
|
|
|
+ CLAMP(archive->browser.tab_id + 1, ArchiveTabTotal - 1, 0);
|
|
|
+ archive_switch_tab(archive);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
} else if(event->key == InputKeyBack) {
|
|
|
if(archive->browser.depth == 0) {
|
|
|
@@ -459,14 +538,15 @@ static bool archive_view_input(InputEvent* event, void* context) {
|
|
|
if(event->key == InputKeyUp || event->key == InputKeyDown) {
|
|
|
with_view_model(
|
|
|
archive->view_archive_main, (ArchiveViewModel * model) {
|
|
|
- size_t num_elements = files_array_size(model->files) - 1;
|
|
|
+ uint16_t num_elements = (uint16_t)files_array_size(model->files);
|
|
|
if((event->type == InputTypeShort || event->type == InputTypeRepeat)) {
|
|
|
if(event->key == InputKeyUp) {
|
|
|
- model->idx = CLAMP(model->idx - 1, num_elements, 0);
|
|
|
+ model->idx = ((model->idx - 1) + num_elements) % num_elements;
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
- model->idx = CLAMP(model->idx + 1, num_elements, 0);
|
|
|
+ model->idx = (model->idx + 1) % num_elements;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return true;
|
|
|
});
|
|
|
update_offset(archive);
|