app.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. use crate::{id_list::IdList, info_list::InfoList, node_info::NodeInfo};
  2. //use smol::Timer;
  3. //use std::{collections::HashMap, time::Duration};
  4. // make a structure to be able to modify and read them
  5. // protect using a mutex
  6. // arc reference
  7. #[derive(Clone)]
  8. pub struct App {
  9. pub id_list: IdList,
  10. pub info_list: InfoList,
  11. }
  12. impl App {
  13. pub fn new() -> App {
  14. let infos = vec![
  15. NodeInfo {
  16. id: "0385048034sodisofjhosd1111q3434".to_string(),
  17. connections: 10,
  18. is_active: true,
  19. last_message: "hey how are you?".to_string(),
  20. },
  21. NodeInfo {
  22. id: "09w30we9wsnfksdfkdjflsjkdfjdfsd".to_string(),
  23. connections: 5,
  24. is_active: false,
  25. last_message: "lmao".to_string(),
  26. },
  27. NodeInfo {
  28. id: "038043325alsdlasjfrsdfsdfsdjsdf".to_string(),
  29. connections: 7,
  30. is_active: true,
  31. last_message: "gm".to_string(),
  32. },
  33. NodeInfo {
  34. id: "04985034953ldflsdjflsdjflsdjfii".to_string(),
  35. connections: 2,
  36. is_active: true,
  37. last_message: "hihi".to_string(),
  38. },
  39. NodeInfo {
  40. id: "09850249352asdjapsdikalskasdkas".to_string(),
  41. connections: 10,
  42. is_active: true,
  43. last_message: "wtf".to_string(),
  44. },
  45. ];
  46. let info_list = InfoList::new(infos.clone());
  47. let ids = vec![
  48. infos[0].id.clone(),
  49. infos[1].id.clone(),
  50. infos[2].id.clone(),
  51. infos[3].id.clone(),
  52. infos[4].id.clone(),
  53. ];
  54. let id_list = IdList::new(ids);
  55. //let infos = Vec::new();
  56. //let ids = Vec::new();
  57. //let info_list = InfoList::new(infos);
  58. //let id_list = IdList::new(ids);
  59. App { id_list, info_list }
  60. }
  61. // TODO: implement this
  62. //async fn sleep(self, dur: Duration) {
  63. // Timer::after(dur).await;
  64. //}
  65. pub async fn update(mut self, node_vec: Vec<NodeInfo>) -> App {
  66. let ids = vec![node_vec[0].id.clone()];
  67. for id in ids {
  68. self.id_list.node_id.push(id);
  69. }
  70. let id_list = self.id_list;
  71. for info in node_vec {
  72. self.info_list.infos.push(info);
  73. }
  74. let info_list = self.info_list;
  75. App { id_list, info_list }
  76. }
  77. }
  78. impl Default for App {
  79. fn default() -> Self {
  80. Self::new()
  81. }
  82. }