archive_browser.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "archive_browser.h"
  2. #include "math.h"
  3. void archive_update_offset(ArchiveBrowserView* browser) {
  4. furi_assert(browser);
  5. with_view_model(
  6. browser->view, (ArchiveBrowserViewModel * model) {
  7. size_t array_size = files_array_size(model->files);
  8. uint16_t bounds = array_size > 3 ? 2 : array_size;
  9. if(array_size > 3 && model->idx >= array_size - 1) {
  10. model->list_offset = model->idx - 3;
  11. } else if(model->last_offset && model->last_offset != model->list_offset) {
  12. model->list_offset = model->last_offset;
  13. model->last_offset = !model->last_offset;
  14. } else if(model->list_offset < model->idx - bounds) {
  15. model->list_offset = CLAMP(model->idx - 2, array_size - bounds, 0);
  16. } else if(model->list_offset > model->idx - bounds) {
  17. model->list_offset = CLAMP(model->idx - 1, array_size - bounds, 0);
  18. }
  19. return true;
  20. });
  21. }
  22. void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
  23. furi_assert(browser);
  24. furi_assert(target);
  25. archive_get_filenames(browser, string_get_cstr(browser->path));
  26. if(!archive_file_array_size(browser) && !archive_get_depth(browser)) {
  27. archive_switch_tab(browser, DEFAULT_TAB_DIR);
  28. } else {
  29. with_view_model(
  30. browser->view, (ArchiveBrowserViewModel * model) {
  31. uint16_t idx = 0;
  32. while(idx < files_array_size(model->files)) {
  33. ArchiveFile_t* current = files_array_get(model->files, idx);
  34. if(!string_search(current->name, target)) {
  35. model->idx = idx;
  36. break;
  37. }
  38. ++idx;
  39. }
  40. return false;
  41. });
  42. archive_update_offset(browser);
  43. }
  44. }
  45. size_t archive_file_array_size(ArchiveBrowserView* browser) {
  46. uint16_t size = 0;
  47. with_view_model(
  48. browser->view, (ArchiveBrowserViewModel * model) {
  49. size = files_array_size(model->files);
  50. return false;
  51. });
  52. return size;
  53. }
  54. void archive_file_array_rm_selected(ArchiveBrowserView* browser) {
  55. with_view_model(
  56. browser->view, (ArchiveBrowserViewModel * model) {
  57. files_array_remove_v(model->files, model->idx, model->idx + 1);
  58. model->idx = CLAMP(model->idx, files_array_size(model->files) - 1, 0);
  59. return false;
  60. });
  61. if(!archive_file_array_size(browser) && !archive_get_depth(browser)) {
  62. archive_switch_tab(browser, DEFAULT_TAB_DIR);
  63. }
  64. archive_update_offset(browser);
  65. }
  66. void archive_file_array_rm_all(ArchiveBrowserView* browser) {
  67. with_view_model(
  68. browser->view, (ArchiveBrowserViewModel * model) {
  69. files_array_clean(model->files);
  70. return false;
  71. });
  72. }
  73. ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) {
  74. ArchiveFile_t* selected;
  75. with_view_model(
  76. browser->view, (ArchiveBrowserViewModel * model) {
  77. selected = files_array_size(model->files) ? files_array_get(model->files, model->idx) :
  78. NULL;
  79. return false;
  80. });
  81. return selected;
  82. }
  83. ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
  84. ArchiveTabEnum tab_id;
  85. with_view_model(
  86. browser->view, (ArchiveBrowserViewModel * model) {
  87. tab_id = model->tab_idx;
  88. return false;
  89. });
  90. return tab_id;
  91. }
  92. uint8_t archive_get_depth(ArchiveBrowserView* browser) {
  93. uint8_t depth;
  94. with_view_model(
  95. browser->view, (ArchiveBrowserViewModel * model) {
  96. depth = model->depth;
  97. return false;
  98. });
  99. return depth;
  100. }
  101. const char* archive_get_path(ArchiveBrowserView* browser) {
  102. return string_get_cstr(browser->path);
  103. }
  104. const char* archive_get_name(ArchiveBrowserView* browser) {
  105. ArchiveFile_t* selected = archive_get_current_file(browser);
  106. return string_get_cstr(selected->name);
  107. }
  108. void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
  109. with_view_model(
  110. browser->view, (ArchiveBrowserViewModel * model) {
  111. model->tab_idx = tab;
  112. return false;
  113. });
  114. }
  115. void archive_set_last_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
  116. with_view_model(
  117. browser->view, (ArchiveBrowserViewModel * model) {
  118. model->last_tab = model->tab_idx;
  119. return false;
  120. });
  121. }
  122. void archive_add_item(ArchiveBrowserView* browser, FileInfo* file_info, const char* name) {
  123. furi_assert(browser);
  124. furi_assert(file_info);
  125. furi_assert(name);
  126. ArchiveFile_t item;
  127. if(filter_by_extension(file_info, get_tab_ext(archive_get_tab(browser)), name)) {
  128. ArchiveFile_t_init(&item);
  129. string_init_set_str(item.name, name);
  130. set_file_type(&item, file_info);
  131. with_view_model(
  132. browser->view, (ArchiveBrowserViewModel * model) {
  133. files_array_push_back(model->files, item);
  134. return false;
  135. });
  136. ArchiveFile_t_clear(&item);
  137. }
  138. }
  139. void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
  140. furi_assert(browser);
  141. with_view_model(
  142. browser->view, (ArchiveBrowserViewModel * model) {
  143. model->menu = show;
  144. model->menu_idx = 0;
  145. if(show) {
  146. ArchiveFile_t* selected = files_array_get(model->files, model->idx);
  147. selected->fav = archive_is_favorite(
  148. "%s/%s", string_get_cstr(browser->path), string_get_cstr(selected->name));
  149. }
  150. return true;
  151. });
  152. }
  153. void archive_switch_dir(ArchiveBrowserView* browser, const char* path) {
  154. furi_assert(browser);
  155. furi_assert(path);
  156. string_set(browser->path, path);
  157. archive_get_filenames(browser, string_get_cstr(browser->path));
  158. archive_update_offset(browser);
  159. }
  160. void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
  161. furi_assert(browser);
  162. ArchiveTabEnum tab = archive_get_tab(browser);
  163. if(key == InputKeyLeft) {
  164. tab = ((tab - 1) + ArchiveTabTotal) % ArchiveTabTotal;
  165. } else if(key == InputKeyRight) {
  166. tab = (tab + 1) % ArchiveTabTotal;
  167. }
  168. archive_set_tab(browser, tab);
  169. if((tab != ArchiveTabFavorites &&
  170. !archive_dir_empty(browser, archive_get_default_path(tab))) ||
  171. (tab == ArchiveTabFavorites && !archive_favorites_count(browser))) {
  172. if(tab != ArchiveTabBrowser) {
  173. archive_switch_tab(browser, key);
  174. }
  175. } else {
  176. with_view_model(
  177. browser->view, (ArchiveBrowserViewModel * model) {
  178. if(model->last_tab != model->tab_idx) {
  179. model->idx = 0;
  180. model->depth = 0;
  181. }
  182. return false;
  183. });
  184. archive_switch_dir(browser, archive_get_default_path(tab));
  185. }
  186. archive_set_last_tab(browser, tab);
  187. }
  188. void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
  189. furi_assert(browser);
  190. furi_assert(name);
  191. with_view_model(
  192. browser->view, (ArchiveBrowserViewModel * model) {
  193. model->last_idx = model->idx;
  194. model->last_offset = model->list_offset;
  195. model->idx = 0;
  196. model->depth = CLAMP(model->depth + 1, MAX_DEPTH, 0);
  197. return false;
  198. });
  199. string_cat(browser->path, "/");
  200. string_cat(browser->path, name);
  201. archive_switch_dir(browser, string_get_cstr(browser->path));
  202. }
  203. void archive_leave_dir(ArchiveBrowserView* browser) {
  204. furi_assert(browser);
  205. const char* path = archive_get_path(browser);
  206. char* last_char_ptr = strrchr(path, '/');
  207. if(last_char_ptr) {
  208. size_t pos = last_char_ptr - path;
  209. string_left(browser->path, pos);
  210. }
  211. with_view_model(
  212. browser->view, (ArchiveBrowserViewModel * model) {
  213. model->depth = CLAMP(model->depth - 1, MAX_DEPTH, 0);
  214. model->idx = model->last_idx;
  215. return false;
  216. });
  217. archive_switch_dir(browser, path);
  218. }