@@ -6,7 +6,52 @@ use crossterm::event::{self, Event, KeyCode};
66use std:: { env, io, process:: Command } ;
77use tui:: widgets:: ListState ;
88pub ( 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