From 4bfae91acb61d31d6b199fd4fc111bab61190531 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Mon, 14 Feb 2022 10:15:42 +0100 Subject: [PATCH] map: update view with new info --- bin/map/src/main.rs | 48 ++++++++++++++++++++------------------------- bin/map/src/view.rs | 25 ++++++++++++++++++----- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/bin/map/src/main.rs b/bin/map/src/main.rs index 5c360df49..82b810017 100644 --- a/bin/map/src/main.rs +++ b/bin/map/src/main.rs @@ -4,7 +4,7 @@ use darkfi::{ util::async_util, }; -use async_std::sync::{Arc, Mutex}; +use async_std::sync::Arc; use easy_parallel::Parallel; use log::debug; use serde_json::{json, Value}; @@ -171,18 +171,18 @@ async fn poll(client: Map, model: Arc) -> Result<()> { is_active: node1["is_active"].as_bool().unwrap(), last_message: node1["message"].to_string(), }, - NodeInfo { - id: node2["id"].to_string(), - connections: node2["connections"].as_u64().unwrap() as usize, - is_active: node2["is_active"].as_bool().unwrap(), - last_message: node2["message"].to_string(), - }, - NodeInfo { - id: node3["id"].to_string(), - connections: node3["connections"].as_u64().unwrap() as usize, - is_active: node3["is_active"].as_bool().unwrap(), - last_message: node3["message"].to_string(), - }, + //NodeInfo { + // id: node2["id"].to_string(), + // connections: node2["connections"].as_u64().unwrap() as usize, + // is_active: node2["is_active"].as_bool().unwrap(), + // last_message: node2["message"].to_string(), + //}, + //NodeInfo { + // id: node3["id"].to_string(), + // connections: node3["connections"].as_u64().unwrap() as usize, + // is_active: node3["is_active"].as_bool().unwrap(), + // last_message: node3["message"].to_string(), + //}, ]; for node in infos { @@ -195,16 +195,12 @@ async fn poll(client: Map, model: Arc) -> Result<()> { // write node id model.id_list.node_id.lock().await.push(node.clone().id); } - - //println!("MODEL INFO LIST: {:?}", model.info_list.infos.lock().await); - //println!("MODEL INFO INDEX: {:?}", model.info_list.index.lock().await); - //println!("MODEL ID LIST: {:?}", model.id_list.node_id.lock().await); } else { // TODO: error handling println!("Reply is an error"); } - async_util::sleep(1).await; + async_util::sleep(2).await; } } @@ -213,10 +209,6 @@ async fn render(terminal: &mut Terminal, model: Arc) -> io terminal.clear()?; - //println!("RENDER INFO LIST: {:?}", model.info_list.infos.lock().await); - //println!("RENDER INFO INDEX: {:?}", model.info_list.index.lock().await); - //println!("RENDER ID LIST: {:?}", model.id_list.node_id.lock().await); - let mut info_vec = Vec::new(); for info in model.info_list.infos.lock().await.clone() { @@ -232,18 +224,20 @@ async fn render(terminal: &mut Terminal, model: Arc) -> io let info_list = InfoListView::new(info_vec); - let mut view = View::new(id_list, info_list); + let mut view = View::new(id_list.clone(), info_list.clone()); view.id_list.state.select(Some(0)); view.info_list.index = 0; loop { - //println!("RENDER INFO LIST: {:?}", model.info_list.infos.lock().await); - //println!("RENDER INFO INDEX: {:?}", model.info_list.index.lock().await); - //println!("RENDER ID LIST: {:?}", model.id_list.node_id.lock().await); + let mut view = view.clone(); + view.update( + model.id_list.node_id.lock().await.clone(), + model.info_list.infos.lock().await.clone(), + ); - //async_util::sleep(1).await; + async_util::sleep(1).await; terminal.draw(|f| { ui::ui(f, view.clone()); })?; diff --git a/bin/map/src/view.rs b/bin/map/src/view.rs index 65d6a1b49..66baebea9 100644 --- a/bin/map/src/view.rs +++ b/bin/map/src/view.rs @@ -11,6 +11,11 @@ impl View { pub fn new(id_list: IdListView, info_list: InfoListView) -> View { View { id_list, info_list } } + + pub fn update(&mut self, node_id: Vec, infos: Vec) { + self.id_list.update(node_id); + self.info_list.update(infos); + } } #[derive(Clone)] @@ -54,6 +59,14 @@ impl IdListView { pub fn unselect(&mut self) { self.state.select(None); } + + pub fn update(&mut self, node_id: Vec) { + let index = 0; + for id in node_id { + self.state.select(Some(index)); + self.node_id.push(id) + } + } } #[derive(Clone)] @@ -64,11 +77,7 @@ pub struct InfoListView { impl InfoListView { pub fn new(infos: Vec) -> InfoListView { - let mut index = 0; - - for _info in infos.clone() { - index = index + 1 - } + let index = 0; InfoListView { index, infos } } @@ -84,4 +93,10 @@ impl InfoListView { self.index = self.infos.len() - 1; } } + + pub fn update(&mut self, infos: Vec) { + for info in infos { + self.infos.push(info); + } + } }