archive_browser.c 8.1 KB

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