Skip to content

Commit

Permalink
task(chat): Update what should be skipped in state.json (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flemmli97 authored Apr 15, 2024
1 parent 2323bd9 commit b5f4b59
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions common/src/state/chats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl Chat {
#[derive(Clone, Serialize, Debug, Default, Deserialize)]
pub struct Chats {
// All active chats from warp.
#[serde(skip)]
pub all: HashMap<Uuid, Chat>,
// Chat to display / interact with currently.
pub active: Option<Uuid>,
Expand All @@ -222,6 +223,8 @@ pub struct Chats {
pub in_sidebar: VecDeque<Uuid>,
// Favorite Chats
pub favorites: Vec<Uuid>,
// If there was a problem with loading state or state was deleted we readd all existing chats to the sidebar.
pub readd_sidebars: bool,
}

impl Chats {
Expand Down
16 changes: 16 additions & 0 deletions common/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct State {
friends: friends::Friends,
#[serde(skip)]
pub storage: storage::Storage,
#[serde(skip)]
pub scope_ids: scope_ids::ScopeIds,
pub settings: settings::Settings,
pub ui: ui::UI,
Expand Down Expand Up @@ -786,6 +787,8 @@ impl State {
return State::load_mock();
};

let mut success = true;

let mut state = {
match fs::read_to_string(&STATIC_ARGS.cache_path) {
Ok(contents) => match serde_json::from_str(&contents) {
Expand All @@ -794,11 +797,13 @@ impl State {
log::error!(
"state.json failed to deserialize: {e}. Initializing State with default values"
);
success = false;
State::default()
}
},
Err(_) => {
log::info!("state.json not found. Initializing State with default values");
success = false;
State::default()
}
}
Expand All @@ -807,6 +812,10 @@ impl State {
// protection in the future
state.initialized = false;

if !success {
state.chats.readd_sidebars = true;
}

if state.settings.font_scale() == 0.0 {
state.settings.set_font_scale(1.0);
}
Expand Down Expand Up @@ -879,6 +888,13 @@ impl State {
}
self.identities.extend(identities.drain());

if self.chats.readd_sidebars {
self.chats.readd_sidebars = false;
self.chats
.in_sidebar
.append(&mut self.chats.all.keys().cloned().collect())
}

self.initialized = true;
}
}
Expand Down
4 changes: 0 additions & 4 deletions common/src/state/scope_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Debug, Deserialize, Serialize)]
pub struct ScopeIds {
#[serde(skip)]
pub chatbar: Option<usize>,
// Would be nice in future if there is a way to access a shared state without subscribing
// This can then be removed
#[serde(skip)]
pub file_transfer: Option<usize>,
#[serde(skip)]
pub file_transfer_icon: Option<usize>,
#[serde(skip)]
pub pending_message_component: Option<usize>,
}

Expand Down
1 change: 1 addition & 0 deletions common/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn generate_mock() -> State {
active_media: None,
in_sidebar,
favorites: vec![],
readd_sidebars: false,
};
let friends = Friends {
all: HashSet::from_iter(identities.iter().map(|x| x.did_key())),
Expand Down

0 comments on commit b5f4b59

Please sign in to comment.