@@ -22,7 +22,7 @@ use iced::{
2222} ;
2323use iced:: { event, window} ;
2424
25- use log:: info;
25+ use log:: { info, warn } ;
2626use objc2:: rc:: Retained ;
2727use objc2_app_kit:: NSRunningApplication ;
2828use rayon:: iter:: { IntoParallelRefIterator , ParallelIterator } ;
@@ -31,6 +31,7 @@ use tray_icon::TrayIcon;
3131use std:: fmt:: Debug ;
3232use std:: fs;
3333use std:: ops:: Bound ;
34+ use std:: str:: FromStr ;
3435use std:: time:: Duration ;
3536use std:: { collections:: BTreeMap , path:: Path } ;
3637
@@ -109,6 +110,7 @@ pub struct Tile {
109110 pub focus_id : u32 ,
110111 pub query : String ,
111112 pub current_mode : String ,
113+ pub update_available : bool ,
112114 query_lc : String ,
113115 results : Vec < App > ,
114116 options : AppIndex ,
@@ -171,6 +173,7 @@ impl Tile {
171173 Subscription :: run ( handle_hotkeys) ,
172174 keyboard,
173175 Subscription :: run ( handle_recipient) ,
176+ Subscription :: run ( check_version) ,
174177 Subscription :: run ( handle_hot_reloading) ,
175178 Subscription :: run ( handle_clipboard_history) ,
176179 window:: close_events ( ) . map ( Message :: HideWindow ) ,
@@ -393,3 +396,51 @@ fn handle_recipient() -> impl futures::Stream<Item = Message> {
393396 }
394397 } )
395398}
399+
400+ fn check_version ( ) -> impl futures:: Stream < Item = Message > {
401+ stream:: channel ( 100 , async |mut output| {
402+ let current_version = format ! ( "\" {}\" " , option_env!( "APP_VERSION" ) . unwrap_or( "" ) ) ;
403+
404+ if current_version. is_empty ( ) {
405+ println ! ( "empty version" ) ;
406+ return ;
407+ }
408+
409+ let req = minreq:: Request :: new (
410+ minreq:: Method :: Get ,
411+ "https://api.github.com/repos/unsecretised/rustcast/releases/latest" ,
412+ )
413+ . with_header ( "User-Agent" , "rustcast-update-checker" )
414+ . with_header ( "Accept" , "application/vnd.github+json" )
415+ . with_header ( "X-GitHub-Api-Version" , "2022-11-28" ) ;
416+
417+ loop {
418+ let resp = req
419+ . clone ( )
420+ . send ( )
421+ . and_then ( |x| x. as_str ( ) . map ( serde_json:: Value :: from_str) ) ;
422+
423+ info ! ( "Made a req for latest version" ) ;
424+
425+ if let Ok ( Ok ( val) ) = resp {
426+ let new_ver = val
427+ . get ( "name" )
428+ . map ( |x| x. to_string ( ) )
429+ . unwrap_or ( "" . to_string ( ) ) ;
430+
431+ // new_ver is in the format "\"v0.0.0\""
432+ // note that it is encapsulated in double quotes
433+ if new_ver. trim ( ) != current_version
434+ && !new_ver. is_empty ( )
435+ && new_ver. starts_with ( "\" v" )
436+ {
437+ info ! ( "new version available: {new_ver}" ) ;
438+ output. send ( Message :: UpdateAvailable ) . await . ok ( ) ;
439+ }
440+ } else {
441+ warn ! ( "Error getting resp" ) ;
442+ }
443+ tokio:: time:: sleep ( Duration :: from_secs ( 60 ) ) . await ;
444+ }
445+ } )
446+ }
0 commit comments