Skip to content

Commit 09a56d9

Browse files
author
cht
committed
异步完成
1 parent 82676a0 commit 09a56d9

3 files changed

Lines changed: 64 additions & 46 deletions

File tree

src/subscribe/appsub.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::state::subscribe_state;
44
use crate::spider;
55
use crate::state::{MyBackend, IFEXIT};
66
use std::io;
7+
use tokio::sync::mpsc::Receiver;
78
use tui::widgets::ListState;
89
use tui::Terminal;
910
pub(super) enum InputMode {
@@ -35,6 +36,8 @@ pub struct AppSub {
3536
// subscribes's information
3637
pub informations: Vec<Vec<spider::Information>>,
3738
pub subscription: Vec<String>,
39+
pub receiver: Option<Receiver<Vec<Vec<String>>>>,
40+
pub popinfomation: String,
3841
}
3942
impl AppSub {
4043
pub fn next(&mut self) {
@@ -138,6 +141,8 @@ impl Default for AppSub {
138141
show_popup: false,
139142
informations: vec![vec![]],
140143
subscription: Vec::new(),
144+
receiver: None,
145+
popinfomation: "Settings, e to edit, s to save".to_string(),
141146
}
142147
}
143148
}

src/subscribe/render.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,7 @@ pub(super) fn ui<B: Backend>(f: &mut Frame<B>, app: &mut AppSub) {
151151
.split(area);
152152

153153
f.render_widget(Clear, area); //this clears out the background
154-
let (msg, style) = (
155-
vec![Span::raw("Settings, e to edit, s to save")],
156-
Style::default(),
157-
);
154+
let (msg, style) = (vec![Span::raw(app.popinfomation.clone())], Style::default());
158155
let mut text = Text::from(Spans::from(msg));
159156
text.patch_style(style);
160157
let title = Paragraph::new(text);

src/subscribe/state.rs

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,52 @@ use crossterm::event::{self, Event, KeyCode};
66
use std::{env, io, process::Command};
77
use tui::widgets::ListState;
88
pub(super) async fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
9-
if let Event::Key(key) = event::read()? {
9+
if app.receiver.is_some() {
10+
if let Ok(list) = app.receiver.as_mut().unwrap().try_recv() {
11+
if !list.is_empty() {
12+
let mut storge: String = "[\n\n".to_string();
13+
let mut subs: Vec<Vec<String>> = Vec::new();
14+
let mut information: Vec<Vec<spider::Information>> = Vec::new();
15+
let mut state: Vec<ListState> = Vec::new();
16+
for lista in list {
17+
let mut ainformation: Vec<spider::Information> = Vec::new();
18+
//let mut asub: Vec<String> = Vec::new();
19+
storge.push_str("[\n\n");
20+
if !lista.is_empty() {
21+
for alist in lista {
22+
let inform = spider::Information::new(alist.to_string());
23+
ainformation.push(inform.clone());
24+
storge.push_str(&inform.get_the_json_node());
25+
}
26+
storge.pop();
27+
storge.pop();
28+
storge.push_str("\n ],");
29+
}
30+
state.push(ListState::default());
31+
subs.push(
32+
ainformation
33+
.iter()
34+
.map(|ainfor| spider::remove_quotation(ainfor.ps.clone()))
35+
.collect(),
36+
);
37+
information.push(ainformation);
38+
}
39+
app.state = state;
40+
app.subs = subs;
41+
app.informations = information;
42+
storge.pop();
43+
storge.push_str("\n]");
44+
utils::create_json_file(utils::Save::Storage, storge)
45+
.unwrap_or_else(|err| panic!("err {}", err));
46+
app.subsindex = 0;
47+
app.state[0].select(Some(0));
48+
app.stateoflist = true;
49+
}
50+
app.receiver = None;
51+
app.popinfomation = "Settings, e to edit, s to save".to_string();
52+
app.input_mode = InputMode::Popup;
53+
}
54+
} else if let Event::Key(key) = event::read()? {
1055
match app.input_mode {
1156
InputMode::Normal => match key.code {
1257
KeyCode::Char('e') => {
@@ -123,48 +168,19 @@ pub(super) async fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
123168
utils::create_json_file(utils::Save::Subscribes, subscribe_json)
124169
.unwrap_or_else(|err| panic!("{}", err));
125170
// .collect();
126-
let get_list = spider::get_the_key(app.subscription.clone()).await;
127-
if let Ok(list) = get_list {
128-
if !list.is_empty() {
129-
let mut storge: String = "[\n\n".to_string();
130-
let mut subs: Vec<Vec<String>> = Vec::new();
131-
let mut information: Vec<Vec<spider::Information>> = Vec::new();
132-
let mut state: Vec<ListState> = Vec::new();
133-
for lista in list {
134-
let mut ainformation: Vec<spider::Information> = Vec::new();
135-
//let mut asub: Vec<String> = Vec::new();
136-
storge.push_str("[\n\n");
137-
if !lista.is_empty() {
138-
for alist in lista {
139-
let inform = spider::Information::new(alist.to_string());
140-
ainformation.push(inform.clone());
141-
storge.push_str(&inform.get_the_json_node());
142-
}
143-
storge.pop();
144-
storge.pop();
145-
storge.push_str("\n ],");
146-
}
147-
state.push(ListState::default());
148-
subs.push(
149-
ainformation
150-
.iter()
151-
.map(|ainfor| spider::remove_quotation(ainfor.ps.clone()))
152-
.collect(),
153-
);
154-
information.push(ainformation);
155-
}
156-
app.state = state;
157-
app.subs = subs;
158-
app.informations = information;
159-
storge.pop();
160-
storge.push_str("\n]");
161-
utils::create_json_file(utils::Save::Storage, storge)
162-
.unwrap_or_else(|err| panic!("err {}", err));
163-
app.subsindex = 0;
164-
app.state[0].select(Some(0));
165-
app.stateoflist = true;
171+
let (sync_io_tx, sync_io_rx) =
172+
tokio::sync::mpsc::channel::<Vec<Vec<String>>>(100);
173+
app.receiver = Some(sync_io_rx);
174+
let input = app.subscription.clone();
175+
app.popinfomation = "Waiting for a moment".to_string();
176+
tokio::spawn(async move {
177+
let get_list = spider::get_the_key(input).await;
178+
if let Ok(list) = get_list {
179+
sync_io_tx.send(list).await.unwrap();
180+
} else {
181+
sync_io_tx.send(vec![]).await.unwrap();
166182
}
167-
}
183+
});
168184
}
169185
_ => {}
170186
},

0 commit comments

Comments
 (0)