src/exit.rs

84c4617332578aaf4e9ee68a0a2d7d6258e48243
youtube-tui/src/exit.rs history · blame · raw

85 lines · 2779 bytes

1 symbol in this file
  • exit function, line 18
 1use std::{collections::HashSet, error::Error, fs};
 2use tui_additions::framework::Framework;
 3
 4use crate::{
 5    config::MainConfig,
 6    global::{
 7        functions::paths,
 8        structs::*,
 9        traits::{Collection, CollectionNoId},
10    },
11    CACHED_BEFORE,
12};
13
14/// function to run when the app ends
15// exit tasks:
16//  - move thumbnails of videos in watch history to `~/.local/share/youtube-tui/watch_history/thumbnails`
17//  - remove `~/.cache`
18pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
19    let limits = framework.data.global.remove::<MainConfig>().unwrap().limits;
20    let mut watchhistory = framework.data.global.remove::<WatchHistory>().unwrap();
21    watchhistory.trim(limits.watch_history);
22    // watchhistory.exit_move();
23    let _ = watchhistory.save();
24    let subscriptions = framework.data.global.get::<Subscriptions>().unwrap();
25    // subscriptions.exit_move();
26    let _ = subscriptions.save();
27    // framework.data.global.get::<Library>().unwrap().exit_move();
28    let searchhistory = framework.data.global.get_mut::<SearchHistory>().unwrap();
29    searchhistory.trim(limits.search_history);
30    let _ = searchhistory.save();
31    let mut commandhistory = framework.data.global.remove::<CommandHistory>().unwrap();
32    commandhistory.trim(limits.commands_history);
33    let _ = commandhistory.save();
34    let library = framework.data.global.get_mut::<Library>().unwrap();
35    let _ = library.save();
36
37    let cached_after: HashSet<String> = HashSet::from_iter(
38        library
39            .items()
40            .iter()
41            .filter_map(|item| item.id())
42            .map(str::to_string)
43            .chain(
44                watchhistory
45                    .items()
46                    .iter()
47                    .filter_map(|item| item.id())
48                    .map(str::to_string),
49            ),
50    );
51
52    let cached_before = CACHED_BEFORE.get().unwrap();
53
54    let data = paths::data_dir();
55    let info_path = data.join("info");
56    let thumbnail_path = data.join("thumbnails");
57
58    // remove cache that no longer exists
59    for deleted in cached_before
60        .iter()
61        .filter(|id| !cached_after.contains(*id))
62    {
63        let _ = fs::remove_file(info_path.join(deleted).with_extension("json"));
64        let _ = fs::remove_file(thumbnail_path.join(deleted));
65    }
66
67    for image_id in LocalStore::list_downloaded_images() {
68        if !cached_after.contains(image_id) {
69            let _ = fs::remove_file(thumbnail_path.join(image_id));
70        }
71    }
72
73    LocalStore::save_only(&cached_after);
74
75    // let home_dir = home_dir().unwrap();
76    // let cache_path = home_dir.join(".cache/youtube-tui/");
77
78    /*
79    if cache_path.exists() {
80        fs::remove_dir_all(cache_path)?;
81    }
82    */
83
84    Ok(())
85}