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

contact deletion

parent 33bd949b
No related branches found
No related tags found
1 merge request!2Storage
...@@ -237,8 +237,7 @@ impl Element for Controller { ...@@ -237,8 +237,7 @@ impl Element for Controller {
(View::ContactView(cv, contact_id, _), k) => { (View::ContactView(cv, contact_id, _), k) => {
if cv.key_push(k) { if cv.key_push(k) {
// TODO delete contact self.storage.delete_contact(*contact_id);
// self.storage.contacts((.contacts.remove(*contact_id);
self.cur_view = View::new_contacts(*contact_id, &self.storage.contacts()); self.cur_view = View::new_contacts(*contact_id, &self.storage.contacts());
} }
......
...@@ -40,12 +40,27 @@ impl Storage { ...@@ -40,12 +40,27 @@ impl Storage {
if contact.id >= usize::from(len) { if contact.id >= usize::from(len) {
assert!(usize::from(len) < MAX_CONTACTS); assert!(usize::from(len) < MAX_CONTACTS);
self.contact_header.num_contacts += 1; self.contact_header.num_contacts += 1;
self.contact_header.write(self); let ch = self.contact_header;
ch.write(self);
} }
contact.write(self); contact.write(self);
} }
pub fn delete_contact(&mut self, contact_id: usize) {
assert!(contact_id < self.contacts_len());
// Shift every contact down
for mut c in self.contacts().skip(contact_id + 1) {
c.id -= 1;
c.write(self);
}
self.contact_header.num_contacts -= 1;
let ch = self.contact_header;
ch.write(self);
}
pub fn contacts(&self) -> ContactIter { pub fn contacts(&self) -> ContactIter {
ContactIter::new(self.contact_header.num_contacts.into()) ContactIter::new(self.contact_header.num_contacts.into())
} }
...@@ -66,7 +81,7 @@ impl Storage { ...@@ -66,7 +81,7 @@ impl Storage {
} }
} }
fn write_data(&self, start_addr: usize, buf: &[u8]) { fn write_data(&mut self, start_addr: usize, buf: &[u8]) {
let start_sector = start_addr / SECTOR_SIZE; let start_sector = start_addr / SECTOR_SIZE;
let num_sectors = buf.len() / SECTOR_SIZE + 1; let num_sectors = buf.len() / SECTOR_SIZE + 1;
let diff = start_addr % SECTOR_SIZE; let diff = start_addr % SECTOR_SIZE;
...@@ -98,7 +113,7 @@ impl Storage { ...@@ -98,7 +113,7 @@ impl Storage {
buf buf
} }
fn write_sector(&self, start_addr: usize, buf: &[u8; 4096]) { fn write_sector(&mut self, start_addr: usize, buf: &[u8; 4096]) {
let addr: u32 = (start_addr - FLASH_START).try_into().unwrap(); let addr: u32 = (start_addr - FLASH_START).try_into().unwrap();
let sector_size = u32::try_from(SECTOR_SIZE).unwrap(); let sector_size = u32::try_from(SECTOR_SIZE).unwrap();
assert!(addr % sector_size == 0); assert!(addr % sector_size == 0);
...@@ -111,6 +126,7 @@ impl Storage { ...@@ -111,6 +126,7 @@ impl Storage {
// Up to 100 contacts // Up to 100 contacts
// 2 byte CRC checksum // 2 byte CRC checksum
#[derive(Copy, Clone)]
struct ContactHeader { struct ContactHeader {
num_contacts: u8, num_contacts: u8,
} }
...@@ -131,7 +147,7 @@ impl ContactHeader { ...@@ -131,7 +147,7 @@ impl ContactHeader {
} }
} }
fn write(&self, storage: &Storage) { fn write(&self, storage: &mut Storage) {
let mut buf = [0; CONTACT_HEADER_SIZE]; let mut buf = [0; CONTACT_HEADER_SIZE];
buf[0] = self.num_contacts; buf[0] = self.num_contacts;
let checksum = X25.checksum(&[self.num_contacts]); let checksum = X25.checksum(&[self.num_contacts]);
...@@ -186,7 +202,7 @@ impl Contact { ...@@ -186,7 +202,7 @@ impl Contact {
}) })
} }
fn write(&self, storage: &Storage) { fn write(&self, storage: &mut Storage) {
let start_addr = CONTACT_HEADER_START + CONTACT_HEADER_SIZE + self.id * CONTACT_LENGTH; let start_addr = CONTACT_HEADER_START + CONTACT_HEADER_SIZE + self.id * CONTACT_LENGTH;
let mut buf = [0; CONTACT_LENGTH]; let mut buf = [0; CONTACT_LENGTH];
......
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