ab: read respectively and sync when set

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2022-11-28 18:16:29 +08:00
parent 5616b20879
commit 6f7eb17c48
6 changed files with 165 additions and 112 deletions

View File

@@ -641,45 +641,11 @@ pub fn main_peer_has_password(id: String) -> bool {
peer_has_password(id)
}
pub fn main_get_recent_peers() -> String {
if !config::APP_DIR.read().unwrap().is_empty() {
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers()
.drain(..)
.map(|(id, _, p)| {
HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
])
})
.collect();
serde_json::ser::to_string(&peers).unwrap_or("".to_owned())
} else {
String::new()
}
}
pub fn main_load_recent_peers() {
if !config::APP_DIR.read().unwrap().is_empty() {
let peers: Vec<HashMap<&str, String>> = PeerConfig::peers()
.drain(..)
.map(|(id, _, p)| {
HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
])
})
.map(|(id, _, p)| peer_to_map(id, p))
.collect();
if let Some(s) = flutter::GLOBAL_EVENT_STREAM
.read()
@@ -705,16 +671,7 @@ pub fn main_load_fav_peers() {
.into_iter()
.filter_map(|(id, _, p)| {
if favs.contains(&id) {
Some(HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
]))
Some(peer_to_map(id, p))
} else {
None
}

View File

@@ -685,6 +685,19 @@ pub fn discover() {
});
}
pub fn peer_to_map(id: String, p: PeerConfig) -> HashMap<&'static str, String> {
HashMap::<&str, String>::from_iter([
("id", id),
("username", p.info.username.clone()),
("hostname", p.info.hostname.clone()),
("platform", p.info.platform.clone()),
(
"alias",
p.options.get("alias").unwrap_or(&"".to_owned()).to_owned(),
),
])
}
#[inline]
pub fn get_lan_peers() -> Vec<HashMap<&'static str, String>> {
config::LanPeers::load()