archive_browser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "archive_files.h"
  2. #include "archive_apps.h"
  3. #include "archive_browser.h"
  4. #include <math.h>
  5. void archive_update_offset(ArchiveBrowserView* browser) {
  6. furi_assert(browser);
  7. with_view_model(
  8. browser->view, (ArchiveBrowserViewModel * model) {
  9. size_t array_size = files_array_size(model->files);
  10. uint16_t bounds = array_size > 3 ? 2 : array_size;
  11. if(array_size > 3 && model->idx >= array_size - 1) {
  12. model->list_offset = model->idx - 3;
  13. } else if(
  14. model->last_offset && model->last_offset != model->list_offset &&
  15. model->tab_idx == model->last_tab) {
  16. model->list_offset = model->last_offset;
  17. model->last_offset = !model->last_offset;
  18. } else if(model->list_offset < model->idx - bounds) {
  19. model->list_offset = CLAMP(model->idx - 2, array_size - bounds, 0);
  20. } else if(model->list_offset > model->idx - bounds) {
  21. model->list_offset = CLAMP(model->idx - 1, array_size - bounds, 0);
  22. }
  23. return true;
  24. });
  25. }
  26. void archive_update_focus(ArchiveBrowserView* browser, const char* target) {
  27. furi_assert(browser);
  28. furi_assert(target);
  29. archive_get_filenames(browser, string_get_cstr(browser->path));
  30. if(!archive_file_array_size(browser) && !archive_get_depth(browser)) {
  31. archive_switch_tab(browser, DEFAULT_TAB_DIR);
  32. } else {
  33. with_view_model(
  34. browser->view, (ArchiveBrowserViewModel * model) {
  35. uint16_t idx = 0;
  36. while(idx < files_array_size(model->files)) {
  37. ArchiveFile_t* current = files_array_get(model->files, idx);
  38. if(!string_search(current->name, target)) {
  39. model->idx = idx;
  40. break;
  41. }
  42. ++idx;
  43. }
  44. return false;
  45. });
  46. archive_update_offset(browser);
  47. }
  48. }
  49. size_t archive_file_array_size(ArchiveBrowserView* browser) {
  50. uint16_t size = 0;
  51. with_view_model(
  52. browser->view, (ArchiveBrowserViewModel * model) {
  53. size = files_array_size(model->files);
  54. return false;
  55. });
  56. return size;
  57. }
  58. void archive_file_array_rm_selected(ArchiveBrowserView* browser) {
  59. with_view_model(
  60. browser->view, (ArchiveBrowserViewModel * model) {
  61. files_array_remove_v(model->files, model->idx, model->idx + 1);
  62. model->idx = CLAMP(model->idx, files_array_size(model->files) - 1, 0);
  63. return false;
  64. });
  65. if(!archive_file_array_size(browser) && !archive_get_depth(browser)) {
  66. archive_switch_tab(browser, DEFAULT_TAB_DIR);
  67. }
  68. archive_update_offset(browser);
  69. }
  70. void archive_file_array_swap(ArchiveBrowserView* browser, int8_t d) {
  71. with_view_model(
  72. browser->view, (ArchiveBrowserViewModel * model) {
  73. ArchiveFile_t temp;
  74. size_t array_size = files_array_size(model->files) - 1;
  75. uint8_t swap_idx = CLAMP(model->idx + d, array_size, 0);
  76. if(model->idx == 0 && d < 0) {
  77. ArchiveFile_t_init(&temp);
  78. files_array_pop_at(&temp, model->files, array_size);
  79. files_array_push_at(model->files, model->idx, temp);
  80. ArchiveFile_t_clear(&temp);
  81. } else if(model->idx == array_size && d > 0) {
  82. ArchiveFile_t_init(&temp);
  83. files_array_pop_at(&temp, model->files, model->last_idx);
  84. files_array_push_at(model->files, array_size, temp);
  85. ArchiveFile_t_clear(&temp);
  86. } else {
  87. files_array_swap_at(model->files, model->idx, swap_idx);
  88. }
  89. return false;
  90. });
  91. }
  92. void archive_file_array_rm_all(ArchiveBrowserView* browser) {
  93. with_view_model(
  94. browser->view, (ArchiveBrowserViewModel * model) {
  95. files_array_reset(model->files);
  96. return false;
  97. });
  98. }
  99. ArchiveFile_t* archive_get_current_file(ArchiveBrowserView* browser) {
  100. ArchiveFile_t* selected;
  101. with_view_model(
  102. browser->view, (ArchiveBrowserViewModel * model) {
  103. selected = files_array_size(model->files) ? files_array_get(model->files, model->idx) :
  104. NULL;
  105. return false;
  106. });
  107. return selected;
  108. }
  109. ArchiveFile_t* archive_get_file_at(ArchiveBrowserView* browser, size_t idx) {
  110. ArchiveFile_t* selected;
  111. idx = CLAMP(idx, archive_file_array_size(browser), 0);
  112. with_view_model(
  113. browser->view, (ArchiveBrowserViewModel * model) {
  114. selected = files_array_size(model->files) ? files_array_get(model->files, idx) : NULL;
  115. return false;
  116. });
  117. return selected;
  118. }
  119. ArchiveTabEnum archive_get_tab(ArchiveBrowserView* browser) {
  120. ArchiveTabEnum tab_id;
  121. with_view_model(
  122. browser->view, (ArchiveBrowserViewModel * model) {
  123. tab_id = model->tab_idx;
  124. return false;
  125. });
  126. return tab_id;
  127. }
  128. uint8_t archive_get_depth(ArchiveBrowserView* browser) {
  129. uint8_t depth;
  130. with_view_model(
  131. browser->view, (ArchiveBrowserViewModel * model) {
  132. depth = model->depth;
  133. return false;
  134. });
  135. return depth;
  136. }
  137. const char* archive_get_path(ArchiveBrowserView* browser) {
  138. return string_get_cstr(browser->path);
  139. }
  140. const char* archive_get_name(ArchiveBrowserView* browser) {
  141. ArchiveFile_t* selected = archive_get_current_file(browser);
  142. return string_get_cstr(selected->name);
  143. }
  144. void archive_set_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
  145. with_view_model(
  146. browser->view, (ArchiveBrowserViewModel * model) {
  147. model->tab_idx = tab;
  148. return false;
  149. });
  150. }
  151. void archive_set_last_tab(ArchiveBrowserView* browser, ArchiveTabEnum tab) {
  152. with_view_model(
  153. browser->view, (ArchiveBrowserViewModel * model) {
  154. model->last_tab = model->tab_idx;
  155. return false;
  156. });
  157. }
  158. void archive_add_app_item(ArchiveBrowserView* browser, const char* name) {
  159. furi_assert(browser);
  160. furi_assert(name);
  161. ArchiveFile_t item;
  162. string_t full_name;
  163. string_init_set(full_name, browser->path);
  164. string_cat_printf(full_name, "/%s", name);
  165. char* app_name = strchr(string_get_cstr(full_name), ':');
  166. if(app_name == NULL) {
  167. string_clear(full_name);
  168. return;
  169. }
  170. ArchiveFile_t_init(&item);
  171. string_init_set_str(item.name, name);
  172. set_file_type(&item, NULL, app_name + 1, true);
  173. with_view_model(
  174. browser->view, (ArchiveBrowserViewModel * model) {
  175. files_array_push_back(model->files, item);
  176. return false;
  177. });
  178. ArchiveFile_t_clear(&item);
  179. string_clear(full_name);
  180. }
  181. void archive_add_file_item(ArchiveBrowserView* browser, FileInfo* file_info, const char* name) {
  182. furi_assert(browser);
  183. furi_assert(file_info);
  184. furi_assert(name);
  185. ArchiveFile_t item;
  186. if(filter_by_extension(file_info, archive_get_tab_ext(archive_get_tab(browser)), name)) {
  187. ArchiveFile_t_init(&item);
  188. string_init_set_str(item.name, name);
  189. set_file_type(&item, file_info, archive_get_path(browser), false);
  190. with_view_model(
  191. browser->view, (ArchiveBrowserViewModel * model) {
  192. files_array_push_back(model->files, item);
  193. return false;
  194. });
  195. ArchiveFile_t_clear(&item);
  196. }
  197. }
  198. void archive_show_file_menu(ArchiveBrowserView* browser, bool show) {
  199. furi_assert(browser);
  200. with_view_model(
  201. browser->view, (ArchiveBrowserViewModel * model) {
  202. model->menu = show;
  203. model->menu_idx = 0;
  204. if(show) {
  205. ArchiveFile_t* selected = files_array_get(model->files, model->idx);
  206. selected->fav = archive_is_favorite("%s", string_get_cstr(selected->name));
  207. }
  208. return true;
  209. });
  210. }
  211. void archive_favorites_move_mode(ArchiveBrowserView* browser, bool active) {
  212. with_view_model(
  213. browser->view, (ArchiveBrowserViewModel * model) {
  214. model->move_fav = active;
  215. return true;
  216. });
  217. }
  218. void archive_switch_dir(ArchiveBrowserView* browser, const char* path) {
  219. furi_assert(browser);
  220. furi_assert(path);
  221. string_set(browser->path, path);
  222. archive_get_filenames(browser, string_get_cstr(browser->path));
  223. archive_update_offset(browser);
  224. }
  225. void archive_switch_tab(ArchiveBrowserView* browser, InputKey key) {
  226. furi_assert(browser);
  227. ArchiveTabEnum tab = archive_get_tab(browser);
  228. if(key == InputKeyLeft) {
  229. tab = ((tab - 1) + ArchiveTabTotal) % ArchiveTabTotal;
  230. } else if(key == InputKeyRight) {
  231. tab = (tab + 1) % ArchiveTabTotal;
  232. }
  233. archive_set_tab(browser, tab);
  234. const char* path = archive_get_default_path(tab);
  235. bool tab_empty = true;
  236. if(tab == ArchiveTabFavorites) {
  237. if(archive_favorites_count(browser) > 0) tab_empty = false;
  238. } else if(strncmp(path, "/app:", 5) == 0) {
  239. if(archive_app_is_available(browser, path)) tab_empty = false;
  240. } else {
  241. if(archive_dir_not_empty(browser, archive_get_default_path(tab))) tab_empty = false;
  242. }
  243. if((tab_empty) && (tab != ArchiveTabBrowser)) {
  244. archive_switch_tab(browser, key);
  245. } else {
  246. with_view_model(
  247. browser->view, (ArchiveBrowserViewModel * model) {
  248. if(model->last_tab != model->tab_idx) {
  249. model->idx = 0;
  250. model->depth = 0;
  251. }
  252. return false;
  253. });
  254. archive_switch_dir(browser, archive_get_default_path(tab));
  255. }
  256. archive_set_last_tab(browser, tab);
  257. }
  258. void archive_enter_dir(ArchiveBrowserView* browser, string_t name) {
  259. furi_assert(browser);
  260. furi_assert(name);
  261. with_view_model(
  262. browser->view, (ArchiveBrowserViewModel * model) {
  263. model->last_idx = model->idx;
  264. model->idx = 0;
  265. model->depth = CLAMP(model->depth + 1, MAX_DEPTH, 0);
  266. return false;
  267. });
  268. string_set(browser->path, name);
  269. archive_switch_dir(browser, string_get_cstr(browser->path));
  270. }
  271. void archive_leave_dir(ArchiveBrowserView* browser) {
  272. furi_assert(browser);
  273. const char* path = archive_get_path(browser);
  274. char* last_char_ptr = strrchr(path, '/');
  275. if(last_char_ptr) {
  276. size_t pos = last_char_ptr - path;
  277. string_left(browser->path, pos);
  278. }
  279. with_view_model(
  280. browser->view, (ArchiveBrowserViewModel * model) {
  281. model->depth = CLAMP(model->depth - 1, MAX_DEPTH, 0);
  282. model->idx = model->last_idx;
  283. return false;
  284. });
  285. archive_switch_dir(browser, path);
  286. }