context getters

This commit is contained in:
veeso
2021-07-08 15:43:23 +02:00
parent e6b44e1461
commit 37abe596c7
14 changed files with 168 additions and 97 deletions

View File

@@ -111,23 +111,24 @@ impl AuthActivity {
fn check_for_updates(&mut self) {
debug!("Check for updates...");
// Check version only if unset in the store
let ctx: &mut Context = self.context.as_mut().unwrap();
if !ctx.store.isset(STORE_KEY_LATEST_VERSION) {
let ctx: &mut Context = self.context_mut();
if !ctx.store().isset(STORE_KEY_LATEST_VERSION) {
debug!("Version is not set in storage");
if ctx.config_client.get_check_for_updates() {
if ctx.config().get_check_for_updates() {
debug!("Check for updates is enabled");
// Send request
match git::check_for_updates(env!("CARGO_PKG_VERSION")) {
Ok(Some(git::GithubTag { tag_name, body })) => {
// If some, store version and release notes
info!("Latest version is: {}", tag_name);
ctx.store.set_string(STORE_KEY_LATEST_VERSION, tag_name);
ctx.store.set_string(STORE_KEY_RELEASE_NOTES, body);
ctx.store_mut()
.set_string(STORE_KEY_LATEST_VERSION, tag_name);
ctx.store_mut().set_string(STORE_KEY_RELEASE_NOTES, body);
}
Ok(None) => {
info!("Latest version is: {} (current)", env!("CARGO_PKG_VERSION"));
// Just set flag as check
ctx.store.set(STORE_KEY_LATEST_VERSION);
ctx.store_mut().set(STORE_KEY_LATEST_VERSION);
}
Err(err) => {
// Report error
@@ -140,30 +141,28 @@ impl AuthActivity {
} else {
info!("Check for updates is disabled");
}
/*
let ctx: &mut Context = self.context.as_mut().unwrap();
// Set version into the store (or just a flag)
match github_tag.take() {
Some(git::GithubTag { tag_name, body }) => {
// If some store version and release notes
info!("Latest version is: {}", tag_name);
ctx.store.set_string(STORE_KEY_LATEST_VERSION, tag_name);
ctx.store.set_string(STORE_KEY_RELEASE_NOTES, body);
}
None => {
info!("Latest version is: {} (current)", env!("CARGO_PKG_VERSION"));
// Just set flag as check
ctx.store.set(STORE_KEY_LATEST_VERSION);
}
}*/
}
}
/// ### context
///
/// Returns a reference to context
fn context(&self) -> &Context {
self.context.as_ref().unwrap()
}
/// ### context_mut
///
/// Returns a mutable reference to context
fn context_mut(&mut self) -> &mut Context {
self.context.as_mut().unwrap()
}
/// ### theme
///
/// Returns a reference to theme
fn theme(&self) -> &Theme {
self.context.as_ref().unwrap().theme_provider.theme()
self.context().theme_provider().theme()
}
}
@@ -176,11 +175,11 @@ impl Activity for AuthActivity {
fn on_create(&mut self, mut context: Context) {
debug!("Initializing activity");
// Initialize file transfer params
context.ft_params = Some(FileTransferParams::default());
context.set_ftparams(FileTransferParams::default());
// Set context
self.context = Some(context);
// Clear terminal
self.context.as_mut().unwrap().clear_screen();
self.context_mut().clear_screen();
// Put raw mode on enabled
if let Err(err) = enable_raw_mode() {
error!("Failed to enter raw mode: {}", err);
@@ -197,7 +196,7 @@ impl Activity for AuthActivity {
self.view_recent_connections();
}
// Verify error state from context
if let Some(err) = self.context.as_mut().unwrap().error() {
if let Some(err) = self.context_mut().error() {
self.mount_error(err.as_str());
}
info!("Activity initialized");
@@ -213,7 +212,7 @@ impl Activity for AuthActivity {
return;
}
// Read one event
if let Ok(Some(event)) = self.context.as_ref().unwrap().input_hnd.read_event() {
if let Ok(Some(event)) = self.context().input_hnd().read_event() {
// Set redraw to true
self.redraw = true;
// Handle on resize

View File

@@ -331,19 +331,21 @@ impl Update for AuthActivity {
self.save_recent();
let (address, port, protocol, username, password) = self.get_input();
// Set file transfer params to context
let mut ft_params: &mut FileTransferParams =
&mut self.context.as_mut().unwrap().ft_params.as_mut().unwrap();
ft_params.address = address;
ft_params.port = port;
ft_params.protocol = protocol;
ft_params.username = match username.is_empty() {
true => None,
false => Some(username),
};
ft_params.password = match password.is_empty() {
true => None,
false => Some(password),
let params: FileTransferParams = FileTransferParams {
address,
port,
protocol,
username: match username.is_empty() {
true => None,
false => Some(username),
},
password: match password.is_empty() {
true => None,
false => Some(password),
},
entry_directory: None,
};
self.context_mut().set_ftparams(params);
// Set exit reason
self.exit_reason = Some(super::ExitReason::Connect);
// Return None

View File

@@ -109,12 +109,7 @@ impl AuthActivity {
)),
);
// Get default protocol
let default_protocol: FileTransferProtocol = self
.context
.as_ref()
.unwrap()
.config_client
.get_default_protocol();
let default_protocol: FileTransferProtocol = self.context().config().get_default_protocol();
// Protocol
self.view.mount(
super::COMPONENT_RADIO_PROTOCOL,
@@ -186,12 +181,11 @@ impl AuthActivity {
);
// Version notice
if let Some(version) = self
.context
.as_ref()
.unwrap()
.store
.context()
.store()
.get_string(super::STORE_KEY_LATEST_VERSION)
{
let version: String = version.to_string();
self.view.mount(
super::COMPONENT_TEXT_NEW_VERSION,
Box::new(Span::new(
@@ -199,7 +193,7 @@ impl AuthActivity {
.with_foreground(Color::Yellow)
.with_spans(vec![
TextSpan::from("termscp "),
TextSpanBuilder::new(version).underlined().bold().build(),
TextSpanBuilder::new(version.as_str()).underlined().bold().build(),
TextSpan::from(" is NOW available! Get it from <https://veeso.github.io/termscp/>; view release notes with <CTRL+R>"),
])
.build(),
@@ -242,7 +236,7 @@ impl AuthActivity {
/// Display view on canvas
pub(super) fn view(&mut self) {
let mut ctx: Context = self.context.take().unwrap();
let _ = ctx.terminal.draw(|f| {
let _ = ctx.terminal().draw(|f| {
// Check window size
let height: u16 = f.size().height;
self.check_minimum_window_size(height);
@@ -784,7 +778,7 @@ impl AuthActivity {
/// mount release notes text area
pub(super) fn mount_release_notes(&mut self) {
if let Some(ctx) = self.context.as_ref() {
if let Some(release_notes) = ctx.store.get_string(super::STORE_KEY_RELEASE_NOTES) {
if let Some(release_notes) = ctx.store().get_string(super::STORE_KEY_RELEASE_NOTES) {
// make spans
let spans: Vec<TextSpan> = release_notes.lines().map(TextSpan::from).collect();
self.view.mount(