11use std:: {
22 cell:: RefCell ,
3+ error:: Error ,
34 path:: { Path , PathBuf } ,
45 rc:: Rc ,
56} ;
@@ -13,7 +14,18 @@ use crate::{
1314 util:: path_exists,
1415} ;
1516
16- #[ derive( serde:: Serialize , Clone , Copy , Debug ) ]
17+ #[ derive( serde:: Serialize , serde:: Deserialize , Debug ) ]
18+ pub struct InternalState {
19+ pub is_bundled : bool ,
20+ }
21+
22+ impl Default for InternalState {
23+ fn default ( ) -> Self {
24+ Self { is_bundled : false }
25+ }
26+ }
27+
28+ #[ derive( serde:: Serialize , Clone , Copy , Debug , PartialEq , Eq ) ]
1729pub enum InstallStatus {
1830 Bundled ,
1931 Manual ,
@@ -55,6 +67,26 @@ pub struct InstalledNavigationDataCycleInfo {
5567 pub validity_period : String ,
5668}
5769
70+ pub fn get_internal_state ( ) -> Result < InternalState , Box < dyn Error > > {
71+ let config_path = Path :: new ( consts:: NAVIGATION_DATA_INTERNAL_CONFIG_LOCATION ) ;
72+ if !path_exists ( & config_path) {
73+ return Err ( "Internal config file does not exist" ) ?;
74+ }
75+
76+ let config_file = std:: fs:: File :: open ( config_path) ?;
77+ let internal_state: InternalState = serde_json:: from_reader ( config_file) ?;
78+
79+ Ok ( internal_state)
80+ }
81+
82+ pub fn set_internal_state ( internal_state : InternalState ) -> Result < ( ) , Box < dyn Error > > {
83+ let config_path = Path :: new ( consts:: NAVIGATION_DATA_INTERNAL_CONFIG_LOCATION ) ;
84+ let config_file = std:: fs:: File :: create ( config_path) ?;
85+ serde_json:: to_writer ( config_file, & internal_state) ?;
86+
87+ Ok ( ( ) )
88+ }
89+
5890pub fn start_network_request ( task : Rc < RefCell < Task > > ) {
5991 let request = NetworkHelper :: make_request ( "https://navdata.api.navigraph.com/info" , Method :: Get , None , None ) ;
6092 let request = match request {
@@ -67,6 +99,13 @@ pub fn start_network_request(task: Rc<RefCell<Task>>) {
6799 task. borrow_mut ( ) . associated_network_request = Some ( request) ;
68100}
69101
102+ pub fn get_installed_cycle_from_json ( path : & Path ) -> Result < InstalledNavigationDataCycleInfo , Box < dyn Error > > {
103+ let json_file = std:: fs:: File :: open ( path) ?;
104+ let installed_cycle_info: InstalledNavigationDataCycleInfo = serde_json:: from_reader ( json_file) ?;
105+
106+ Ok ( installed_cycle_info)
107+ }
108+
70109pub fn get_navigation_data_install_status ( task : Rc < RefCell < Task > > ) {
71110 let response_bytes = match task. borrow ( ) . associated_network_request . as_ref ( ) {
72111 Some ( request) => {
@@ -98,23 +137,26 @@ pub fn get_navigation_data_install_status(task: Rc<RefCell<Task>>) {
98137 } ;
99138
100139 // figure out install status
101- let found_downloaded = path_exists ( Path :: new ( consts:: NAVIGATION_DATA_DOWNLOADED_LOCATION ) ) ;
140+ let found_downloaded = path_exists ( Path :: new ( consts:: NAVIGATION_DATA_WORK_LOCATION ) ) ;
102141
103- let found_bundled = path_exists ( Path :: new ( consts:: NAVIGATION_DATA_DEFAULT_LOCATION ) ) ;
142+ let found_bundled = get_internal_state ( )
143+ . map ( |internal_state| internal_state. is_bundled )
144+ . unwrap_or ( false ) ;
104145
105- let status = if found_downloaded {
106- InstallStatus :: Manual
107- } else if found_bundled {
146+ // Check bundled first, as downloaded and bundled are both possible
147+ let status = if found_bundled {
108148 InstallStatus :: Bundled
149+ } else if found_downloaded {
150+ InstallStatus :: Manual
109151 } else {
110152 InstallStatus :: None
111153 } ;
112154
113155 // Open JSON
114- let json_path = match status {
115- InstallStatus :: Manual => Some ( PathBuf :: from ( consts:: NAVIGATION_DATA_DOWNLOADED_LOCATION ) . join ( "cycle.json" ) ) ,
116- InstallStatus :: Bundled => Some ( PathBuf :: from ( consts :: NAVIGATION_DATA_DEFAULT_LOCATION ) . join ( "cycle.json" ) ) ,
117- InstallStatus :: None => None ,
156+ let json_path = if status != InstallStatus :: None {
157+ Some ( PathBuf :: from ( consts:: NAVIGATION_DATA_WORK_LOCATION ) . join ( "cycle.json" ) )
158+ } else {
159+ None
118160 } ;
119161
120162 let installed_cycle_info = match json_path {
@@ -154,10 +196,10 @@ pub fn get_navigation_data_install_status(task: Rc<RefCell<Task>>) {
154196 Some ( installed_cycle_info) => Some ( installed_cycle_info. cycle . clone ( ) ) ,
155197 None => None ,
156198 } ,
157- install_path : match status {
158- InstallStatus :: Manual => Some ( consts:: NAVIGATION_DATA_DOWNLOADED_LOCATION . to_string ( ) ) ,
159- InstallStatus :: Bundled => Some ( consts :: NAVIGATION_DATA_DEFAULT_LOCATION . to_string ( ) ) ,
160- InstallStatus :: None => None ,
199+ install_path : if status == InstallStatus :: Manual {
200+ Some ( consts:: NAVIGATION_DATA_WORK_LOCATION . to_string ( ) )
201+ } else {
202+ None
161203 } ,
162204 validity_period : match & installed_cycle_info {
163205 Some ( installed_cycle_info) => Some ( installed_cycle_info. validity_period . clone ( ) ) ,
0 commit comments