Skip to content

Commit

Permalink
Reintroduced dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
EMcNugget committed Aug 30, 2024
1 parent 4fc311f commit ad5569a
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 123 deletions.
145 changes: 104 additions & 41 deletions src-tauri/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
use crate::contraction::write_contractions;
use crate::settings::read_settings;
use crate::structs::{Alert, FindComposite};
use crate::util::{get_vatis_path, read_json_file, write_json_file};
use crate::util::{read_json_file, write_json_file};
use log::{error, info};
use serde_json::{self, Value};
use tauri::AppHandle;
use sysinfo::System;
use tauri::{AppHandle, Manager};

// Nested hell, needs a refactor
fn find_composite(
data: &Value,
profile: &str,
facility: &str,
atis_type: Option<&str>,
) -> Option<FindComposite> {
fn find_atis_type(atis_type: Option<&str>) -> &str {
match atis_type {
Some("dep") => "Departure",
Some("arr") => "Arrival",
Some("combined") => "Combined",
_ => "Combined",
}
}
let atis_type_str = match atis_type {
Some("dep") => "Departure",
Some("arr") => "Arrival",
Some("combined") => "Combined",
_ => "Combined",
};

let atis_type_str = find_atis_type(atis_type);
let profiles = data.get("profiles").and_then(|p| p.as_array()).unwrap();

if let Some(profiles) = data.get("profiles").and_then(|p| p.as_array()) {
if profile == "No Profile" {
for (profile_index, profile) in profiles.iter().enumerate() {
if let Some(composites) = profile.get("composites").and_then(|c| c.as_array()) {
for (composite_index, composite) in composites.iter().enumerate() {
if let Some(identifier) = composite.get("identifier").and_then(|id| id.as_str())
{
if identifier == facility {
if let Some(atis) = composite.get("atisType").and_then(|a| a.as_str()) {
if atis == atis_type_str {
return Some(FindComposite {
profile_index,
composite_index,
});
}
}
}
}
}
}
}
} else {
for (profile_index, prof) in profiles.iter().enumerate() {
if let Some(name) = prof.get("name").and_then(|n| n.as_str()) {
if name == profile {
Expand Down Expand Up @@ -52,26 +70,6 @@ fn find_composite(
}
}
}

for (profile_index, profile) in profiles.iter().enumerate() {
if let Some(composites) = profile.get("composites").and_then(|c| c.as_array()) {
for (composite_index, composite) in composites.iter().enumerate() {
if let Some(identifier) = composite.get("identifier").and_then(|id| id.as_str())
{
if identifier == facility {
if let Some(atis) = composite.get("atisType").and_then(|a| a.as_str()) {
if atis == atis_type_str {
return Some(FindComposite {
profile_index,
composite_index,
});
}
}
}
}
}
}
}
}
None
}
Expand Down Expand Up @@ -151,22 +149,87 @@ pub fn write_atis(facility: String, atis: Value, app_handle: AppHandle) -> Resul
Ok(_) => {
let data = &format!("Successfully wrote ATIS for {}", &facility);
info!("{}", data);
if alert.message == *data {
} else {
alert.message.push_str(data);
}
alert.message.push_str(data);
}
Err(e) => {
let data = &format!("Error writing ATIS: {}", e);
error!("{}", data);
if alert.message == *data {
} else {
alert.message.push_str(data);
}
alert.message.push_str(data);
alert.alert_type = "error".to_string();
}
}
}

Ok(alert)
}

#[tauri::command]
pub fn is_vatis_running() -> bool {
let s = System::new_all();
let is_running = s.processes().values().any(|p| p.name() == "vATIS.exe");
is_running
}

pub fn get_vatis_path(app_handle: &AppHandle) -> String {
let settings = read_settings(app_handle.clone()).unwrap();
let mut app_data_path = app_handle.path().app_local_data_dir().unwrap();
app_data_path.pop();
if settings.custom_path {
return settings.file_path.clone();
}
format!("{}\\vATIS-4.0", app_data_path.to_str().unwrap())
}

#[tauri::command]
pub fn open_vatis(app_handle: AppHandle, custom_path: Option<&str>) -> Result<(), String> {
let s = System::new_all();
let is_running = s.processes().values().any(|p| p.name() == "vATIS.exe");

let mut app_data_path = app_handle.path().app_local_data_dir().unwrap();
app_data_path.pop();
let file_path = format!(
"{}\\vATIS-4.0\\Application\\vATIS.exe",
app_data_path.to_str().unwrap()
);

if !is_running {
let path = custom_path.unwrap_or(&file_path);
std::process::Command::new(path).spawn().map_err(|e| {
error!("Failed to open vATIS: {}", e);
e.to_string()
})?;
}

Ok(())
}

#[tauri::command]
pub fn get_profiles(app_handle: AppHandle) -> Result<Vec<String>, String> {
let file_path = format!("{}\\AppConfig.json", get_vatis_path(&app_handle));
let data = read_json_file(&file_path).unwrap();
let mut profiles = Vec::new();
for profile in data["profiles"].as_array().unwrap() {
profiles.push(profile["name"].as_str().unwrap().to_string());
}
Ok(profiles)
}

#[tauri::command]
pub fn get_airports_in_profile(app_handle: AppHandle, profile: &str) -> Vec<String> {
let file_path = format!("{}\\AppConfig.json", get_vatis_path(&app_handle));
let data = read_json_file(&file_path).unwrap();
let mut airports = Vec::new();

for p in data["profiles"].as_array().unwrap() {
if p["name"].as_str().unwrap() == profile {
for composite in p["composites"].as_array().unwrap() {
let identifier = composite["identifier"].as_str().unwrap().to_string();
if !airports.contains(&identifier) {
airports.push(identifier);
}
}
}
}

return airports;
}
8 changes: 7 additions & 1 deletion src-tauri/src/audio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use log::info;
use rodio::{source::Source, Decoder, OutputStream};
use tauri::AppHandle;
use tauri::{AppHandle, Manager, UserAttentionType};

use crate::util::get_resource;

Expand All @@ -14,4 +14,10 @@ pub fn play_audio(app_handle: &AppHandle) -> () {
std::thread::sleep(std::time::Duration::from_secs(2));

info!("ATIS Update audio played");

app_handle
.get_webview_window("main")
.unwrap()
.request_user_attention(Some(UserAttentionType::Informational))
.unwrap();
}
6 changes: 0 additions & 6 deletions src-tauri/src/lib.rs

This file was deleted.

23 changes: 16 additions & 7 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ use log::info;
use tauri::{AppHandle, Listener};
use tauri_plugin_log::{Target, TargetKind};

pub mod app;
pub mod audio;
pub mod contraction;
pub mod settings;
pub mod structs;
pub mod util;

fn setup(app_handle: &AppHandle) {
info!(
"D-ATIS to vATIS started version {}",
app_handle.config().version.as_ref().unwrap()
);

d_atis_to_vatis::settings::check_settings_file(&app_handle);
crate::settings::check_settings_file(&app_handle);
}

fn main() {
Expand All @@ -21,7 +28,7 @@ fn main() {
setup(&handle);

app.listen("new-codes", move |_event| {
d_atis_to_vatis::audio::play_audio(&handle);
crate::audio::play_audio(&handle);
});

Ok(())
Expand All @@ -41,11 +48,13 @@ fn main() {
.build(),
)
.invoke_handler(tauri::generate_handler![
d_atis_to_vatis::settings::write_settings,
d_atis_to_vatis::settings::read_settings,
d_atis_to_vatis::app::write_atis,
d_atis_to_vatis::util::is_vatis_running,
d_atis_to_vatis::util::open_vatis,
crate::settings::write_settings,
crate::settings::read_settings,
crate::app::write_atis,
crate::app::is_vatis_running,
crate::app::open_vatis,
crate::app::get_profiles,
crate::app::get_airports_in_profile
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
44 changes: 0 additions & 44 deletions src-tauri/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use log::error;
use serde_json::Value;
use std::{
fs::File,
io::{BufReader, Write},
};
use sysinfo::System;
use tauri::{path::BaseDirectory, AppHandle, Manager};

use crate::settings::read_settings;

pub fn read_json_file(file_path: &str) -> Result<Value, anyhow::Error> {
let file = File::open(file_path)?;

Expand Down Expand Up @@ -40,43 +36,3 @@ pub fn get_resource(app_handle: &AppHandle, file_name: &str) -> Result<File, any

Ok(file?)
}

#[tauri::command]
pub fn is_vatis_running() -> bool {
let s = System::new_all();
let is_running = s.processes().values().any(|p| p.name() == "vATIS.exe");
is_running
}

pub fn get_vatis_path(app_handle: &AppHandle) -> String {
let settings = read_settings(app_handle.clone()).unwrap();
let mut app_data_path = app_handle.path().app_local_data_dir().unwrap();
app_data_path.pop();
if settings.custom_path {
return settings.file_path.clone();
}
format!("{}\\vATIS-4.0", app_data_path.to_str().unwrap())
}

#[tauri::command]
pub fn open_vatis(app_handle: AppHandle, custom_path: Option<&str>) -> Result<(), String> {
let s = System::new_all();
let is_running = s.processes().values().any(|p| p.name() == "vATIS.exe");

let mut app_data_path = app_handle.path().app_local_data_dir().unwrap();
app_data_path.pop();
let file_path = format!(
"{}\\vATIS-4.0\\Application\\vATIS.exe",
app_data_path.to_str().unwrap()
);

if !is_running {
let path = custom_path.unwrap_or(&file_path);
std::process::Command::new(path).spawn().map_err(|e| {
error!("Failed to open vATIS: {}", e);
e.to_string()
})?;
}

Ok(())
}
Loading

0 comments on commit ad5569a

Please sign in to comment.