use tui::widgets::ListState;
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct Comment {
pub id: u64,
pub creator_id: u64,
pub post_id: u64,
pub content: String,
pub removed: bool,
pub published: String,
pub deleted: bool,
pub ap_id: String,
pub local: bool,
pub path: String,
pub distinguished: bool,
pub language_id: u64,
}
impl Comment {
pub const fn new() -> Self {
Self {
id: 0,
creator_id: 0,
post_id: 0,
content: String::new(),
removed: false,
published: String::new(),
deleted: false,
ap_id: String::new(),
local: false,
path: String::new(),
distinguished: false,
language_id: 0,
}
}
pub const fn id(&self) -> u64 {
self.id
}
pub const fn creator_id(&self) -> u64 {
self.creator_id
}
pub const fn post_id(&self) -> u64 {
self.post_id
}
pub fn content(&self) -> &str {
self.content.as_str()
}
pub const fn deleted(&self) -> bool {
self.deleted
}
pub fn ap_id(&self) -> &str {
self.ap_id.as_str()
}
pub const fn local(&self) -> bool {
self.local
}
pub fn path(&self) -> &str {
self.path.as_str()
}
pub const fn distinguished(&self) -> bool {
self.distinguished
}
pub const fn language_id(&self) -> u64 {
self.language_id
}
}
impl Default for Comment {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug)]
pub struct CommentList {
pub items: Vec<Comment>,
pub state: ListState,
}
impl CommentList {
pub fn new(items: Vec<Comment>) -> Self {
Self {
items,
state: ListState::default(),
}
}
pub fn items(&self) -> &[Comment] {
self.items.as_ref()
}
pub fn state(&self) -> &ListState {
&self.state
}
pub fn state_mut(&mut self) -> &mut ListState {
&mut self.state
}
pub fn current(&self) -> Option<&Comment> {
match self.state.selected() {
Some(i) => Some(&self.items[i]),
None => None,
}
}
pub fn deselect(&mut self) {
self.state.select(None);
}
pub fn next(&mut self) {
let len = self.items.len();
let i = match self.state.selected() {
Some(i) => (i + 1) % len,
None => 0,
};
self.state.select(Some(i));
}
pub fn previous(&mut self) {
let len = self.items.len();
let last = len - 1;
let i = match self.state.selected() {
Some(i) => {
if i == 0 {
last
} else {
i.saturating_sub(1)
}
}
None => last,
};
self.state.select(Some(i));
}
}
impl From<Vec<Comment>> for CommentList {
fn from(val: Vec<Comment>) -> Self {
Self::new(val)
}
}
impl AsRef<CommentList> for CommentList {
fn as_ref(&self) -> &Self {
self
}
}
impl AsMut<CommentList> for CommentList {
fn as_mut(&mut self) -> &mut Self {
self
}
}