Skip to content
Snippets Groups Projects
Commit a730921c authored by Stephen D's avatar Stephen D
Browse files

contact menus

parent 413684db
No related branches found
No related tags found
No related merge requests found
use heapless::{String, Vec};
pub const MAX_CONTACTS: usize = 20;
#[derive(Debug, Clone)]
pub struct Contact {
callsign: String<10>,
......@@ -9,7 +11,7 @@ pub struct Contact {
#[derive(Debug)]
pub struct ContactGroup {
pub contacts: Vec<Contact, 20>,
pub contacts: Vec<Contact, MAX_CONTACTS>,
}
impl Default for ContactGroup {
......
......@@ -2,7 +2,7 @@ use embedded_graphics::{pixelcolor::Rgb565, prelude::DrawTarget};
use heapless::{String, Vec};
use crate::{
contact::{Contact, ContactGroup},
contact::{ContactGroup, MAX_CONTACTS},
gui::{chat::Chat, selector::Selector, status::StatusBar, Element},
keyboard::KeyCode,
voltage::VoltMeter,
......@@ -10,28 +10,35 @@ use crate::{
pub enum View {
MainMenu(Selector<&'static str, [&'static str; 3]>),
Contacts(Selector<String<30>, Vec<String<30>, 3>>),
Contacts(Selector<String<30>, Vec<String<30>, MAX_CONTACTS>>),
ContactOptions(Selector<&'static str, [&'static str; 3]>, usize),
Chat(Chat),
}
impl View {
pub fn main_menu_with_id(id: usize) -> Self {
pub fn new_main_menu(id: usize) -> Self {
let menu = Selector::new(["Contacts", "Messages", "Settings"], id);
Self::MainMenu(menu)
}
pub fn from_main_menu_id(id: usize) -> Option<Self> {
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> {
match id {
0 => {
let group = ContactGroup::default();
let contact_names: Vec<_, 3> =
group.contacts.iter().map(|c| c.name.clone()).collect();
Some(Self::Contacts(Selector::new(
contact_names.try_into().unwrap(),
0,
)))
}
0 => Some(Self::new_contacts(0, contacts)),
1 => Some(Self::Chat(Chat::new())),
_ => None,
}
......@@ -41,6 +48,7 @@ impl View {
match self {
View::MainMenu(_) => unreachable!(),
View::Contacts(_) => 0,
View::ContactOptions(_, _) => unreachable!(),
View::Chat(_) => 1,
}
}
......@@ -48,7 +56,7 @@ impl View {
impl Default for View {
fn default() -> Self {
Self::main_menu_with_id(0)
Self::new_main_menu(0)
}
}
......@@ -60,6 +68,7 @@ impl Element for View {
match self {
View::MainMenu(m) => m.render(dt),
View::Contacts(c) => c.render(dt),
View::ContactOptions(c, _) => c.render(dt),
View::Chat(c) => c.render(dt),
}
}
......@@ -68,6 +77,7 @@ impl Element for View {
match self {
View::MainMenu(m) => m.key_push(k),
View::Contacts(c) => c.key_push(k),
View::ContactOptions(c, _) => c.key_push(k),
View::Chat(c) => c.key_push(k),
}
}
......@@ -76,6 +86,7 @@ impl Element for View {
match self {
View::MainMenu(m) => m.touchpad_scroll(x, y),
View::Contacts(c) => c.touchpad_scroll(x, y),
View::ContactOptions(c, _) => c.touchpad_scroll(x, y),
View::Chat(c) => c.touchpad_scroll(x, y),
}
}
......@@ -84,6 +95,9 @@ impl Element for View {
pub struct Controller {
status: StatusBar,
cur_view: View,
// May belong in a DataStore struct
contacts: ContactGroup,
}
impl Controller {
......@@ -91,6 +105,8 @@ impl Controller {
Self {
status: StatusBar::new(volt_meter),
cur_view: View::default(),
contacts: ContactGroup::default(),
}
}
}
......@@ -112,15 +128,25 @@ impl Element for Controller {
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()) {
if let Some(new_view) = View::from_main_menu_id(m.selected(), &self.contacts) {
self.cur_view = new_view;
}
}
(View::Contacts(c), KeyCode::Touchpad) => {
// TODO need to handle "Add New" button
self.cur_view = View::new_contact_options(c.selected());
}
(View::MainMenu(_), KeyCode::Back) => {}
(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();
self.cur_view = View::main_menu_with_id(id);
self.cur_view = View::new_main_menu(id);
}
(_, k) => self.cur_view.key_push(k),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment