archive_browser.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_swap(ArchiveBrowserView* browser, int8_t d) {
  69. with_view_model(
  70. browser->view, (ArchiveBrowserViewModel * model) {
  71. size_t array_size = files_array_size(model->files) - 1;
  72. uint8_t swap_idx = CLAMP(model->idx + d, array_size, 0);
  73. if(model->idx == 0 && d < 0) {
  74. swap_idx = array_size;
  75. } else if(model->idx == array_size && d > 0) {
  76. swap_idx = 0;
  77. }
  78. files_array_swap_at(model->files, model->idx, swap_idx);
  79. return false;
  80. });
  81. }
  82. void archive_file_array_rm_all(ArchiveBrowserView* browser) {
  83. with_view_model(
  84. browser->view, (ArchiveBrowserViewModel * model) {
  85. files_array_clean(model->files);
  86. return false;
  87. });
  88. }
  89. ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) {
  90. ArchiveFile_t* selected;
  91. with_view_model(
  92. browser->view, (ArchiveBrowserViewModel * model) {
  93. selected = files_array_size(model->files) ? files_array_get(model->files, model->idx) :
  94. NULL;
  95. return false;
  96. });
  97. return selected;
  98. }
  99. ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) {
  100. ArchiveFile_t* selected;
  101. idx = CLAMP(idx, archive_file_array_size(browser), 0);
  102. with_view_model(
  103. browser->view, (ArchiveBrowserViewModel * model) {
  104. selected = files_array_size(model->files) ? files_array_get(model->files, idx) : NULL;
  105. return false;
  106. });
  107. return selected;
  108. }
  109. ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
  110. ArchiveTabEnum tab_id;
  111. with_view_model(
  112. browser->view, (ArchiveBrowserViewModel * model) {
  113. tab_id = model->tab_idx;
  114. return false;
  115. });
  116. return tab_id;
  117. }
  118. uint8_t archive_get_depth(ArchiveBrowserView* browser) {
  119. uint8_t depth;
  120. with_view_model(
  121. browser->view, (ArchiveBrowserViewModel * model) {
  122. depth = model->depth;
  123. return false;
  124. });
  125. return depth;
  126. }
  127. const char* archive_get_path(ArchiveBrowserView* browser) {
  128. return string_get_cstr(browser->path);
  129. }
  130. const char* archive_get_name(ArchiveBrowserView* browser) {
  131. ArchiveFile_t* selected = archive_get_current_file(browser);
  132. return string_get_cstr(selected->name);
  133. }
  134. void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
  135. with_view_model(
  136. browser->view, (ArchiveBrowserViewModel * model) {
  137. model->tab_idx = tab;
  138. return false;
  139. });
  140. }
  141. void archive_set_last_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
  142. with_view_model(
  143. browser->view, (ArchiveBrowserViewModel * model) {
  144. model->last_tab = model->tab_idx;
  145. return false;
  146. });
  147. }
  148. void archive_add_item(ArchiveBrowserView* browser, FileInfo* file_info, const char* name) {
  149. furi_assert(browser);
  150. furi_assert(file_info);
  151. furi_assert(name);
  152. ArchiveFile_t item;
  153. if(filter_by_extension(file_info, get_tab_ext(archive_get_tab(browser)), name)) {
  154. ArchiveFile_t_init(&item);
  155. string_init_set_str(item.name, name);
  156. set_file_type(&item, file_info);
  157. with_view_model(
  158. browser->view, (ArchiveBrowserViewModel * model) {
  159. files_array_push_back(model->files, item);
  160. return false;
  161. });
  162. ArchiveFile_t_clear(&item);
  163. }
  164. }
  165. void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
  166. furi_assert(browser);
  167. with_view_model(
  168. browser->view, (ArchiveBrowserViewModel * model) {
  169. model->menu = show;
  170. model->menu_idx = 0;
  171. if(show) {
  172. ArchiveFile_t* selected = files_array_get(model->files, model->idx);
  173. selected->fav = archive_is_favorite(
  174. "%s/%s", string_get_cstr(browser->path), string_get_cstr(selected->name));
  175. }
  176. return true;
  177. });
  178. }
  179. void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active) {
  180. with_view_model(
  181. browser->view, (ArchiveBrowserViewModel * model) {
  182. model->move_fav = active;
  183. return true;
  184. });
  185. }
  186. void archive_switch_dir(ArchiveBrowserView* browser, const char* path) {
  187. furi_assert(browser);
  188. furi_assert(path);
  189. string_set(browser->path, path);
  190. archive_get_filenames(browser, string_get_cstr(browser->path));
  191. archive_update_offset(browser);
  192. }
  193. void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
  194. furi_assert(browser);
  195. ArchiveTabEnum tab = archive_get_tab(browser);
  196. if(key == InputKeyLeft) {
  197. tab = ((tab - 1) + ArchiveTabTotal) % ArchiveTabTotal;
  198. } else if(key == InputKeyRight) {
  199. tab = (tab + 1) % ArchiveTabTotal;
  200. }
  201. archive_set_tab(browser, tab);
  202. if((tab != ArchiveTabFavorites &&
  203. !archive_dir_empty(browser, archive_get_default_path(tab))) ||
  204. (tab == ArchiveTabFavorites && !archive_favorites_count(browser))) {
  205. if(tab != ArchiveTabBrowser) {
  206. archive_switch_tab(browser, key);
  207. }
  208. } else {
  209. with_view_model(
  210. browser->view, (ArchiveBrowserViewModel * model) {
  211. if(model->last_tab != model->tab_idx) {
  212. model->idx = 0;
  213. model->depth = 0;
  214. }
  215. return false;
  216. });
  217. archive_switch_dir(browser, archive_get_default_path(tab));
  218. }
  219. archive_set_last_tab(browser, tab);
  220. }
  221. void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
  222. furi_assert(browser);
  223. furi_assert(name);
  224. with_view_model(
  225. browser->view, (ArchiveBrowserViewModel * model) {
  226. model->last_idx = model->idx;
  227. model->last_offset = model->list_offset;
  228. model->idx = 0;
  229. model->depth = CLAMP(model->depth + 1, MAX_DEPTH, 0);
  230. return false;
  231. });
  232. string_cat(browser->path, "/");
  233. string_cat(browser->path, name);
  234. archive_switch_dir(browser, string_get_cstr(browser->path));
  235. }
  236. void archive_leave_dir(ArchiveBrowserView* browser) {
  237. furi_assert(browser);
  238. const char* path = archive_get_path(browser);
  239. char* last_char_ptr = strrchr(path, '/');
  240. if(last_char_ptr) {
  241. size_t pos = last_char_ptr - path;
  242. string_left(browser->path, pos);
  243. }
  244. with_view_model(
  245. browser->view, (ArchiveBrowserViewModel * model) {
  246. model->depth = CLAMP(model->depth - 1, MAX_DEPTH, 0);
  247. model->idx = model->last_idx;
  248. return false;
  249. });
  250. archive_switch_dir(browser, path);
  251. }