2022-07-16 00:14:02 +10:00
|
|
|
use cacao::color::Color;
|
2021-02-08 18:37:25 +11:00
|
|
|
use cacao::layout::{Layout, LayoutConstraint};
|
|
|
|
use cacao::text::{Font, Label, LineBreakMode};
|
|
|
|
use cacao::view::{View, ViewDelegate};
|
|
|
|
|
|
|
|
use crate::storage::{Todo, TodoStatus};
|
|
|
|
|
|
|
|
/// This view is used as a row in our `TodosListView`. It displays information/status for a provided
|
|
|
|
/// `Todo`, and gets cached and reused as necessary. It should not store anything long-term.
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct TodoViewRow {
|
|
|
|
pub title: Label,
|
2022-08-18 09:11:45 +10:00
|
|
|
pub status: Label,
|
2021-02-08 18:37:25 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TodoViewRow {
|
|
|
|
/// Called when this view is being presented, and configures itself with the given todo.
|
|
|
|
pub fn configure_with(&mut self, todo: &Todo) {
|
|
|
|
self.title.set_text(&todo.title);
|
|
|
|
|
|
|
|
match todo.status {
|
|
|
|
TodoStatus::Incomplete => {
|
2021-02-13 12:57:06 +11:00
|
|
|
self.status.set_text_color(Color::SystemRed);
|
2021-02-08 18:37:25 +11:00
|
|
|
self.status.set_text("Incomplete");
|
|
|
|
},
|
|
|
|
|
|
|
|
TodoStatus::Complete => {
|
2021-02-13 12:57:06 +11:00
|
|
|
self.status.set_text_color(Color::SystemBlue);
|
2021-02-08 18:37:25 +11:00
|
|
|
self.status.set_text("Complete");
|
2022-08-18 09:11:45 +10:00
|
|
|
},
|
2021-02-08 18:37:25 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ViewDelegate for TodoViewRow {
|
|
|
|
const NAME: &'static str = "TodoViewRow";
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-08 18:37:25 +11:00
|
|
|
/// Called when the view is first created; handles setup of layout and associated styling that
|
|
|
|
/// doesn't change.
|
|
|
|
fn did_load(&mut self, view: View) {
|
|
|
|
view.add_subview(&self.title);
|
|
|
|
view.add_subview(&self.status);
|
|
|
|
|
|
|
|
self.title.set_line_break_mode(LineBreakMode::TruncateMiddle);
|
|
|
|
|
|
|
|
let font = Font::system(10.);
|
|
|
|
self.status.set_font(&font);
|
|
|
|
|
|
|
|
LayoutConstraint::activate(&[
|
|
|
|
self.title.top.constraint_equal_to(&view.top).offset(16.),
|
|
|
|
self.title.leading.constraint_equal_to(&view.leading).offset(16.),
|
|
|
|
self.title.trailing.constraint_equal_to(&view.trailing).offset(-16.),
|
|
|
|
self.status.top.constraint_equal_to(&self.title.bottom).offset(8.),
|
|
|
|
self.status.leading.constraint_equal_to(&view.leading).offset(16.),
|
|
|
|
self.status.trailing.constraint_equal_to(&view.trailing).offset(-16.),
|
2022-08-18 09:11:45 +10:00
|
|
|
self.status.bottom.constraint_equal_to(&view.bottom).offset(-16.),
|
2021-02-08 18:37:25 +11:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|