mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
map: update view with new info
This commit is contained in:
@@ -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<Model>) -> 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<Model>) -> 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<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> 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<B: Backend>(terminal: &mut Terminal<B>, model: Arc<Model>) -> 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());
|
||||
})?;
|
||||
|
||||
@@ -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<String>, infos: Vec<NodeInfo>) {
|
||||
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<String>) {
|
||||
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<NodeInfo>) -> 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<NodeInfo>) {
|
||||
for info in infos {
|
||||
self.infos.push(info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user