78 lines
1.8 KiB
Rust
78 lines
1.8 KiB
Rust
|
|
//! TUI state
|
||
|
|
|
||
|
|
use ratatui::widgets::TableState;
|
||
|
|
|
||
|
|
use crate::places::{
|
||
|
|
db_repository::DbPlacesRepository, models::Place, repository::PlacesRepository,
|
||
|
|
};
|
||
|
|
|
||
|
|
pub struct State {
|
||
|
|
pub height: u16,
|
||
|
|
pub page: u32,
|
||
|
|
pub mode: Mode,
|
||
|
|
places_repository: DbPlacesRepository,
|
||
|
|
pub places: Vec<Place>,
|
||
|
|
places_status: DataStatus,
|
||
|
|
pub selected_place: TableState,
|
||
|
|
pub quit: bool,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub enum Mode {
|
||
|
|
List,
|
||
|
|
Edit,
|
||
|
|
}
|
||
|
|
|
||
|
|
enum DataStatus {
|
||
|
|
Fresh,
|
||
|
|
Old,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl State {
|
||
|
|
pub fn new(places_repository: DbPlacesRepository, height: u16) -> Self {
|
||
|
|
Self {
|
||
|
|
height,
|
||
|
|
page: 0,
|
||
|
|
mode: Mode::List,
|
||
|
|
places_repository,
|
||
|
|
places_status: DataStatus::Old,
|
||
|
|
places: vec![],
|
||
|
|
selected_place: TableState::default(),
|
||
|
|
quit: false,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn next_page(&mut self) {
|
||
|
|
self.page += 1;
|
||
|
|
self.places_status = DataStatus::Old;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn prev_page(&mut self) {
|
||
|
|
self.page = self.page.saturating_sub(1);
|
||
|
|
self.places_status = DataStatus::Old;
|
||
|
|
}
|
||
|
|
|
||
|
|
pub async fn fetch_places(&mut self) {
|
||
|
|
if let DataStatus::Fresh = self.places_status {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
let limit = (self.height as u8).saturating_sub(3);
|
||
|
|
let offset = (limit as u32) * self.page;
|
||
|
|
match self
|
||
|
|
.places_repository
|
||
|
|
.get_places_paginated(offset, limit)
|
||
|
|
.await
|
||
|
|
{
|
||
|
|
Ok(places) => {
|
||
|
|
self.places = places;
|
||
|
|
if !self.places.is_empty() {
|
||
|
|
self.selected_place.select(Some(0));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Err(err) => {
|
||
|
|
tracing::error!("{err}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
self.places_status = DataStatus::Fresh;
|
||
|
|
}
|
||
|
|
}
|