generated from maddoxwerts/Dockerized-Rust
117 lines
3.3 KiB
Rust
117 lines
3.3 KiB
Rust
// Libraries
|
|
slint::include_modules!();
|
|
use super::Backend;
|
|
use slint::{ModelRc, SharedString, VecModel};
|
|
use std::io::Result;
|
|
use std::rc::Rc;
|
|
|
|
// Macros
|
|
macro_rules! map_err {
|
|
($expr:expr) => {
|
|
$expr.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
|
|
};
|
|
}
|
|
|
|
// Structures
|
|
pub struct App {
|
|
app: Rc<Prorater>,
|
|
backend: Rc<Backend>,
|
|
}
|
|
|
|
// Implementations
|
|
impl App {
|
|
// Constructors
|
|
/// Creates a new App
|
|
///
|
|
/// # Examples
|
|
/// ```rs
|
|
/// let app = App::new()?;
|
|
/// ```
|
|
pub fn new(backend: Rc<Backend>) -> Result<Self> {
|
|
// Creating our new app
|
|
let app = map_err!(Prorater::new())?;
|
|
|
|
// Setting the Membership Options
|
|
let memberships = {
|
|
let vec_model: VecModel<SharedString> = {
|
|
let result = VecModel::from(vec![SharedString::from("Select One...")]);
|
|
for i in backend.get_memberships() {
|
|
result.push(SharedString::from(i));
|
|
}
|
|
result
|
|
};
|
|
ModelRc::new(vec_model)
|
|
};
|
|
app.set_membership_names(memberships.clone());
|
|
|
|
// Turning the App into an Rc
|
|
let app = Rc::new(app);
|
|
|
|
// Returning Self
|
|
Ok(Self { app, backend })
|
|
}
|
|
|
|
// Functions
|
|
/// Starts the Application
|
|
///
|
|
/// # Examples
|
|
/// ```rs
|
|
/// app.start()?;
|
|
/// ```
|
|
pub fn start(&self) -> Result<()> {
|
|
// Creating an Rc of our App
|
|
let app = Rc::clone(&self.app);
|
|
let backend = Rc::clone(&self.backend);
|
|
|
|
// Setting the on_submit function
|
|
self.app.on_on_calculate(move || {
|
|
// Getting the prices of the stuff
|
|
let current_membership = app.get_current_membership();
|
|
let new_membership = app.get_new_membership();
|
|
let last_billing = app.get_last_billing();
|
|
|
|
// Are either of these a "Select One..."
|
|
if current_membership == "Select One..."
|
|
|| new_membership == "Select One..."
|
|
|| last_billing == "NULL"
|
|
{
|
|
return SharedString::from(
|
|
"Please fill out all fields before calculating the Prorate.",
|
|
);
|
|
}
|
|
|
|
// Making the Backend Calculate This
|
|
let calculation = backend
|
|
.calculate(
|
|
current_membership.into(),
|
|
new_membership.into(),
|
|
last_billing.into(),
|
|
)
|
|
.unwrap();
|
|
|
|
// Is there no cost difference?
|
|
if calculation.invalid {
|
|
return SharedString::from("No price difference, don't change anything.");
|
|
}
|
|
|
|
// Returning the Result
|
|
if calculation.reversed {
|
|
SharedString::from(format!(
|
|
"Retract the billing date to {} (-{} Days).",
|
|
calculation.new_date, calculation.days_pushed
|
|
))
|
|
} else {
|
|
SharedString::from(format!(
|
|
"Extend the billing date to {} (+{} Days).",
|
|
calculation.new_date, calculation.days_pushed
|
|
))
|
|
}
|
|
});
|
|
|
|
// Starting our application
|
|
map_err!(self.app.run())?;
|
|
|
|
// Ok!!
|
|
Ok(())
|
|
}
|
|
}
|