-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a84a037
commit 70eac00
Showing
7 changed files
with
205 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,75 @@ | ||
use ratatui::widgets::ListState; | ||
use ratatui::widgets::{List, ListItem, ListState}; | ||
use sqlx::SqlitePool; | ||
|
||
use crate::models::Message; | ||
use crate::{db, models::Message, AppResult}; | ||
|
||
// TODO: make all attrs private | ||
// TODO: create common StatefulList trait and implement it for ConversationList and MessageList | ||
#[derive(Default)] | ||
pub struct Chat { | ||
pub messages: Vec<Message>, | ||
messages: Vec<Message>, | ||
pub state: ListState, | ||
sqlite: SqlitePool, | ||
} | ||
|
||
impl Chat { | ||
pub fn new(sqlite: SqlitePool) -> Self { | ||
Self { | ||
messages: vec![], | ||
state: Default::default(), | ||
sqlite, | ||
} | ||
} | ||
|
||
pub fn currently_selected(&self) -> Option<Message> { | ||
let selected_index = self.state.selected()?; | ||
self.messages.get(selected_index).cloned() | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.unselect(); | ||
self.messages = vec![]; | ||
} | ||
|
||
pub fn unselect(&mut self) { | ||
self.state.select(None); | ||
} | ||
|
||
pub fn push(&mut self, message: Message) { | ||
self.messages.push(message); | ||
} | ||
|
||
pub fn pop(&mut self) { | ||
self.messages.pop(); | ||
} | ||
|
||
pub fn last(&self) -> Option<&Message> { | ||
self.messages.last() | ||
} | ||
|
||
pub fn up(&mut self) { | ||
self.state.scroll_up_by(1); | ||
} | ||
|
||
pub fn down(&mut self) { | ||
self.state.scroll_down_by(1); | ||
} | ||
|
||
pub async fn load_messages(&mut self, conversation_id: u32) -> AppResult<()> { | ||
let messages = db::get_messages(self.sqlite.clone(), conversation_id).await?; | ||
self.messages = messages; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn as_list_widget<F, T>(&self, f: F) -> List<'static> | ||
where | ||
F: Fn(&Message) -> T, | ||
T: Into<ListItem<'static>>, | ||
{ | ||
let items = self | ||
.messages | ||
.iter() | ||
.map(|elem| f(elem).into()) | ||
.collect::<Vec<ListItem>>(); | ||
|
||
List::new(items) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,59 @@ | ||
use ratatui::widgets::ListState; | ||
use ratatui::widgets::{List, ListItem, ListState}; | ||
use sqlx::SqlitePool; | ||
|
||
use crate::models::Conversation; | ||
|
||
// TODO: make all attrs private | ||
// TODO: create common StatefulList trait and implement it for Conversations and MessageList | ||
#[derive(Default)] | ||
pub struct Conversations { | ||
pub conversations: Vec<Conversation>, | ||
conversations: Vec<Conversation>, | ||
pub state: ListState, | ||
_sqlite: SqlitePool, | ||
} | ||
|
||
impl Conversations { | ||
pub fn new(sqlite: SqlitePool) -> Self { | ||
Self { | ||
conversations: vec![], | ||
state: Default::default(), | ||
_sqlite: sqlite, | ||
} | ||
} | ||
|
||
pub fn set_conversations(&mut self, conversations: Vec<Conversation>) { | ||
self.conversations = conversations; | ||
} | ||
|
||
pub fn currently_selected(&self) -> Option<Conversation> { | ||
let selected_index = self.state.selected()?; | ||
self.conversations.get(selected_index).cloned() | ||
} | ||
|
||
pub fn unselect(&mut self) { | ||
self.state.select(None); | ||
} | ||
|
||
pub fn push(&mut self, conversation: Conversation) { | ||
self.conversations.push(conversation); | ||
} | ||
|
||
pub fn up(&mut self) { | ||
self.state.scroll_up_by(1); | ||
} | ||
|
||
pub fn down(&mut self) { | ||
self.state.scroll_down_by(1); | ||
} | ||
|
||
pub fn as_list_widget<F, T>(&self, f: F) -> List<'static> | ||
where | ||
F: Fn(&Conversation) -> T, | ||
T: Into<ListItem<'static>>, | ||
{ | ||
let items = self | ||
.conversations | ||
.iter() | ||
.map(|elem| f(elem).into()) | ||
.collect::<Vec<ListItem>>(); | ||
|
||
List::new(items) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.