Skip to content

Commit

Permalink
rename modal and creator actions for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
aprxi committed Sep 1, 2024
1 parent a1f0a59 commit 1bf679b
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl UserProfileDbHandler {
settings: &JsonValue,
) -> Result<UserProfile, ApplicationError> {
// Simulate a 3-second delay
// sleep(Duration::from_secs(3)).await;
//sleep(Duration::from_secs(3)).await;
let timestamp = Timestamp::from_system_time().unwrap().as_millis();

let encryption_key_id = self.get_or_create_encryption_key().await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ async fn handle_modal_refresh(
.modal
.as_mut()
.expect("Modal should exist when in Modal mode");
let refresh_result = modal.refresh().await?;
let refresh_result = modal.poll_background_task().await?;
match refresh_result {
WindowEvent::Modal(ModalAction::Refresh) => {
// If the modal still needs refreshing, keep the processing state
WindowEvent::Modal(ModalAction::PollBackGroundTask) => {
// If the modal still needs polling, keep the processing state
app.is_processing = true;
Ok(WindowEvent::Modal(ModalAction::Refresh))
Ok(WindowEvent::Modal(ModalAction::PollBackGroundTask))
}
other_event => {
// Handle the event returned by refresh
Expand All @@ -196,7 +196,7 @@ async fn handle_window_event(
Ok(window_event)
}
WindowEvent::Modal(ref action) => match action {
ModalAction::Refresh => {
ModalAction::PollBackGroundTask => {
app.is_processing = true;
Ok(window_event)
}
Expand All @@ -210,7 +210,7 @@ async fn handle_window_event(
.await?;
// refresh at least once after opening modal
app.is_processing = true;
Ok(WindowEvent::Modal(ModalAction::Refresh))
Ok(WindowEvent::Modal(ModalAction::PollBackGroundTask))
}
ModalAction::Event(ref user_event) => {
handle_modal_user_event(app, user_event, db_conn).await?;
Expand Down
4 changes: 2 additions & 2 deletions lumni/src/apps/builtin/llm/prompt/src/tui/events/key_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl KeyEventHandler {
message
))?;
return Ok(WindowEvent::Modal(
ModalAction::WaitForKeyEvent,
ModalAction::UpdateUI,
));
}
_ => {
Expand All @@ -241,7 +241,7 @@ impl KeyEventHandler {
};
return Ok(new_window_event);
} else {
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
Ok(WindowEvent::Modal(ModalAction::UpdateUI))
}
}
_ => Ok(current_mode),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a> ConversationListModal<'a> {
}
}
}
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
Ok(WindowEvent::Modal(ModalAction::UpdateUI))
}

pub async fn handle_normal_mode_key_event(
Expand All @@ -51,7 +51,7 @@ impl<'a> ConversationListModal<'a> {
.await;
}
log::warn!("ThreadedChatSession is not available");
return Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent));
return Ok(WindowEvent::Modal(ModalAction::UpdateUI));
}
KeyCode::Down => {
self.move_selection_down();
Expand All @@ -62,7 +62,7 @@ impl<'a> ConversationListModal<'a> {
.await;
}
log::warn!("ThreadedChatSession is not available");
return Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent));
return Ok(WindowEvent::Modal(ModalAction::UpdateUI));
}
KeyCode::Enter => {
return Ok(WindowEvent::PromptWindow(None));
Expand Down Expand Up @@ -93,7 +93,7 @@ impl<'a> ConversationListModal<'a> {
_ => {}
}
// stay in the Modal window, waiting for next key event
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
Ok(WindowEvent::Modal(ModalAction::UpdateUI))
}

async fn save_edited_name(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,11 @@ impl ModalWindowTrait for FileBrowserModal<'_> {
Ok(WindowEvent::Modal(modal_action))
}

async fn refresh(&mut self) -> Result<WindowEvent, ApplicationError> {
self.file_browser.refresh().await?;
async fn poll_background_task(
&mut self,
) -> Result<WindowEvent, ApplicationError> {
self.file_browser.poll_background_task().await?;
self.update_selected_file_info().await?;
Ok(WindowEvent::Modal(ModalAction::Refresh))
Ok(WindowEvent::Modal(ModalAction::PollBackGroundTask))
}
}
17 changes: 9 additions & 8 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ pub enum ModalWindowType {
#[derive(Debug, Clone, PartialEq)]
pub enum ModalAction {
Open(ModalWindowType), // open the modal
Refresh, // modal needs to be refreshed to handle updates
WaitForKeyEvent, // wait for a key event
Close, // close the curren modal
PollBackGroundTask, // modal needs to be polled for background updates
UpdateUI, // update the UI of the modal once and wait for the next key event
Close, // close the curren modal
Event(UserEvent),
}

#[async_trait]
pub trait ModalWindowTrait: Send + Sync {
fn get_type(&self) -> ModalWindowType;
fn render_on_frame(&mut self, frame: &mut Frame, area: Rect);
async fn refresh(&mut self) -> Result<WindowEvent, ApplicationError> {
// handle_key_event can return WindowEvent::Modal(ModalAction::Refresh),
// which typically means a background process started, and can be monitored by calling refresh. Refresh can also return a WindowEvent with Action::Refresh. When background processes is completed, or none are running, it should return the default WaitForKeyEvent.
// Default implementation completes immediately
Ok(WindowEvent::Modal(ModalAction::WaitForKeyEvent))
async fn poll_background_task(
&mut self,
) -> Result<WindowEvent, ApplicationError> {
// handle_key_event can return WindowEvent::Modal(ModalAction::PollBackGroundTask),
// this means a background process started, and must be monitored by calling this method. The monitoring can stop when a regular UpdateUI is received
Ok(WindowEvent::Modal(ModalAction::UpdateUI))
}
async fn handle_key_event<'a>(
&'a mut self,
Expand Down
51 changes: 0 additions & 51 deletions lumni/src/apps/builtin/llm/prompt/src/tui/modals/settings/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::future::Future;

use super::*;

pub trait ListItem: Clone {
Expand Down Expand Up @@ -106,25 +104,6 @@ impl<T: ListItem> SettingsList<T> {
items.push(format!("Create new {}", T::item_type()));
items
}

pub async fn delete_selected_item<F, Fut>(
&mut self,
delete_fn: F,
) -> Result<(), ApplicationError>
where
F: FnOnce(T) -> Fut,
Fut: Future<Output = Result<(), ApplicationError>>,
{
if let Some(index) = self.selected_index.checked_sub(1) {
let item = self.items.remove(index);
delete_fn(item).await?;
if self.selected_index >= self.items.len() && !self.items.is_empty()
{
self.selected_index = self.items.len() - 1;
}
}
Ok(())
}
}

impl<T: ListItem> SettingsListTrait for SettingsList<T> {
Expand Down Expand Up @@ -182,33 +161,3 @@ impl ListItem for ProviderConfig {
"Provider"
}
}

pub type ProfileList = SettingsList<UserProfile>;
pub type ProviderList = SettingsList<ProviderConfig>;

impl ProfileList {
pub async fn delete_profile(
&mut self,
db_handler: &mut UserProfileDbHandler,
) -> Result<(), ApplicationError> {
self.delete_selected_item(|profile| async move {
db_handler.delete_profile(&profile).await
})
.await
}
}

impl ProviderList {
pub async fn delete_provider(
&mut self,
db_handler: &mut UserProfileDbHandler,
) -> Result<(), ApplicationError> {
self.delete_selected_item(|provider| async move {
if let Some(id) = provider.id {
db_handler.delete_provider_config(id).await?;
}
Ok(())
})
.await
}
}
Loading

0 comments on commit 1bf679b

Please sign in to comment.