Newer
Older
use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
gui::{chat::Chat, selector::Selector, status::StatusBar, Element},
keyboard::KeyCode,
voltage::VoltMeter,
};
pub enum View {
MainMenu(Selector<&'static str, [&'static str; 3]>),
Contacts(Selector<String<30>, Vec<String<30>, MAX_CONTACTS>>),
ContactOptions(Selector<&'static str, [&'static str; 3]>, usize),
let menu = Selector::new(["Contacts", "Messages", "Settings"], id);
Self::MainMenu(menu)
}
pub fn new_contacts(id: usize, contacts: &ContactGroup) -> Self {
let mut contact_names: Vec<_, MAX_CONTACTS> =
contacts.contacts.iter().map(|c| c.name.clone()).collect();
// If we run out of space, we automatically hide the "Add" button
let _ = contact_names.push(String::try_from("Add New").unwrap());
Self::Contacts(Selector::new(contact_names, id))
}
pub fn new_contact_options(contact_id: usize) -> Self {
let opts = Selector::new(["View", "Edit", "Message"], 0);
Self::ContactOptions(opts, contact_id)
}
pub fn from_main_menu_id(id: usize, contacts: &ContactGroup) -> Option<Self> {
1 => Some(Self::Chat(Chat::new())),
_ => None,
}
}
pub fn to_main_menu_id(&self) -> usize {
match self {
View::MainMenu(_) => unreachable!(),
View::Contacts(_) => 0,
View::Chat(_) => 1,
}
}
}
impl Default for View {
fn default() -> Self {
}
}
impl Element for View {
fn render<E, DT: DrawTarget<Color = Rgb565, Error = E>>(
&mut self,
dt: &mut DT,
) -> Result<(), E> {
match self {
View::MainMenu(m) => m.render(dt),
View::Contacts(c) => c.render(dt),
View::Chat(c) => c.render(dt),
}
}
fn key_push(&mut self, k: KeyCode) {
match self {
View::MainMenu(m) => m.key_push(k),
View::Contacts(c) => c.key_push(k),
View::Chat(c) => c.key_push(k),
}
}
fn touchpad_scroll(&mut self, x: i8, y: i8) {
match self {
View::MainMenu(m) => m.touchpad_scroll(x, y),
View::Contacts(c) => c.touchpad_scroll(x, y),
// May belong in a DataStore struct
contacts: ContactGroup,
}
impl Controller {
pub fn new(volt_meter: VoltMeter) -> Self {
Self {
status: StatusBar::new(volt_meter),
}
}
}
impl Element for Controller {
fn render<E, DT: DrawTarget<Color = Rgb565, Error = E>>(
&mut self,
dt: &mut DT,
) -> Result<(), E> {
dt.clear(Rgb565::new(0, 32, 16))?;
self.status.render(dt)?;
Ok(())
}
fn key_push(&mut self, k: KeyCode) {
match (&self.cur_view, k) {
(View::MainMenu(m), KeyCode::Touchpad) => {
if let Some(new_view) = View::from_main_menu_id(m.selected(), &self.contacts) {
(View::Contacts(c), KeyCode::Touchpad) => {
// TODO need to handle "Add New" button
self.cur_view = View::new_contact_options(c.selected());
}
(View::ContactOptions(_, id), KeyCode::Back) => {
self.cur_view = View::new_contacts(*id, &self.contacts);
}
(_, KeyCode::Back) => {
let id = self.cur_view.to_main_menu_id();
}
}
fn touchpad_scroll(&mut self, x: i8, y: i8) {