소스 검색

map: update view with new info

lunar-mining 4 년 전
부모
커밋
4bfae91acb
2개의 변경된 파일41개의 추가작업 그리고 32개의 파일을 삭제
  1. 21 27
      bin/map/src/main.rs
  2. 20 5
      bin/map/src/view.rs

+ 21 - 27
bin/map/src/main.rs

@@ -4,7 +4,7 @@ use darkfi::{
     util::async_util,
     util::async_util,
 };
 };
 
 
-use async_std::sync::{Arc, Mutex};
+use async_std::sync::Arc;
 use easy_parallel::Parallel;
 use easy_parallel::Parallel;
 use log::debug;
 use log::debug;
 use serde_json::{json, Value};
 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(),
                     is_active: node1["is_active"].as_bool().unwrap(),
                     last_message: node1["message"].to_string(),
                     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 {
             for node in infos {
@@ -195,16 +195,12 @@ async fn poll(client: Map, model: Arc<Model>) -> Result<()> {
                 // write node id
                 // write node id
                 model.id_list.node_id.lock().await.push(node.clone().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 {
         } else {
             // TODO: error handling
             // TODO: error handling
             println!("Reply is an error");
             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()?;
     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();
     let mut info_vec = Vec::new();
 
 
     for info in model.info_list.infos.lock().await.clone() {
     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 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.id_list.state.select(Some(0));
 
 
     view.info_list.index = 0;
     view.info_list.index = 0;
 
 
     loop {
     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| {
         terminal.draw(|f| {
             ui::ui(f, view.clone());
             ui::ui(f, view.clone());
         })?;
         })?;

+ 20 - 5
bin/map/src/view.rs

@@ -11,6 +11,11 @@ impl View {
     pub fn new(id_list: IdListView, info_list: InfoListView) -> View {
     pub fn new(id_list: IdListView, info_list: InfoListView) -> View {
         View { id_list, info_list }
         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)]
 #[derive(Clone)]
@@ -54,6 +59,14 @@ impl IdListView {
     pub fn unselect(&mut self) {
     pub fn unselect(&mut self) {
         self.state.select(None);
         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)]
 #[derive(Clone)]
@@ -64,11 +77,7 @@ pub struct InfoListView {
 
 
 impl InfoListView {
 impl InfoListView {
     pub fn new(infos: Vec<NodeInfo>) -> 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 }
         InfoListView { index, infos }
     }
     }
@@ -84,4 +93,10 @@ impl InfoListView {
             self.index = self.infos.len() - 1;
             self.index = self.infos.len() - 1;
         }
         }
     }
     }
+
+    pub fn update(&mut self, infos: Vec<NodeInfo>) {
+        for info in infos {
+            self.infos.push(info);
+        }
+    }
 }
 }