implement toggle of calendar files in sidebar
This commit is contained in:
parent
a3c8e60872
commit
08a699ca9d
52
src/app.rs
52
src/app.rs
@ -1,6 +1,6 @@
|
||||
|
||||
use crate::model::{
|
||||
events::EventsCollection,
|
||||
events::{EventsCollection, CalendarFile},
|
||||
ical_bridge::load_calendar,
|
||||
};
|
||||
use crate::ui::{
|
||||
@ -16,18 +16,9 @@ use crate::tracepoints;
|
||||
use chrono::{NaiveDate, Months, Utc, Days};
|
||||
use std::path;
|
||||
use iced::{
|
||||
Alignment,
|
||||
Task,
|
||||
Element,
|
||||
Length,
|
||||
widget::{
|
||||
column,
|
||||
container,
|
||||
Column,
|
||||
Container,
|
||||
text,
|
||||
pane_grid::{self, PaneGrid}
|
||||
},
|
||||
checkbox, column, container, pane_grid::{self, PaneGrid}, text, Column, Container
|
||||
}, Alignment, Element, Length, Task
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
@ -41,6 +32,8 @@ pub enum Message {
|
||||
ViewModeSelected(ViewMode),
|
||||
ToggleSidebar,
|
||||
Resized(pane_grid::ResizeEvent),
|
||||
UpdateEvents,
|
||||
ToggleCalendar(usize),
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
@ -60,7 +53,7 @@ pub struct CalendarApp {
|
||||
main_pane: pane_grid::Pane,
|
||||
split_value: f32,
|
||||
events: EventsCollection,
|
||||
calendars: std::vec::Vec<String>,
|
||||
calendars: std::vec::Vec<CalendarFile>,
|
||||
}
|
||||
|
||||
impl Default for CalendarApp {
|
||||
@ -84,7 +77,7 @@ impl CalendarApp {
|
||||
pub fn with_files(calendar_paths: std::vec::Vec<path::PathBuf>) -> Self {
|
||||
let view_date = Utc::now().date_naive();
|
||||
let calendars = calendar_paths.iter()
|
||||
.map(|path| {std::string::String::from(path.file_name().unwrap().to_str().unwrap_or("<>"))})
|
||||
.map(|file_path| {CalendarFile::from_file(file_path.to_path_buf())})
|
||||
.collect();
|
||||
let mut app = CalendarApp {
|
||||
view_date,
|
||||
@ -92,12 +85,19 @@ impl CalendarApp {
|
||||
calendars,
|
||||
..CalendarApp::default()
|
||||
};
|
||||
for calendar_path in calendar_paths {
|
||||
load_calendar(&calendar_path, &mut app.events);
|
||||
}
|
||||
app.update_events();
|
||||
app
|
||||
}
|
||||
|
||||
fn update_events(&mut self) {
|
||||
self.events.clear();
|
||||
for calendar_file in &self.calendars {
|
||||
if calendar_file.enabled {
|
||||
load_calendar(&calendar_file.file_path, &mut self.events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, message: Message) -> Task<Message> {
|
||||
match message {
|
||||
Message::PrevWeek => {
|
||||
@ -128,6 +128,17 @@ impl CalendarApp {
|
||||
self.split_value = ratio;
|
||||
self.grid_state.resize(split, ratio);
|
||||
},
|
||||
Message::UpdateEvents => {
|
||||
self.update_events();
|
||||
},
|
||||
Message::ToggleCalendar(index) => {
|
||||
for calendar_file in &mut self.calendars {
|
||||
if calendar_file.index == index {
|
||||
calendar_file.enabled = ! calendar_file.enabled;
|
||||
}
|
||||
}
|
||||
return Task::done(Message::UpdateEvents)
|
||||
},
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
@ -169,8 +180,11 @@ impl CalendarApp {
|
||||
//container(self.calendars.iter().map(|name| {text(name.as_str())}))
|
||||
let mut col = column!("Calendars:")
|
||||
.align_x(Alignment::Start);
|
||||
for calendar_name in &self.calendars {
|
||||
col = col.push(text(calendar_name.as_str()));
|
||||
for calendar_file in &self.calendars {
|
||||
col = col.push(
|
||||
checkbox(calendar_file.get_name().as_str(), calendar_file.enabled)
|
||||
.on_toggle(|enable| {Message::ToggleCalendar(calendar_file.index)})
|
||||
);
|
||||
}
|
||||
container(col)
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
use chrono::NaiveDate;
|
||||
use std::path::{self};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::vec::Vec;
|
||||
use std::default::Default;
|
||||
use std::string::String;
|
||||
@ -45,11 +47,14 @@ pub struct EventsCollection {
|
||||
impl EventsCollection {
|
||||
pub fn new() -> Self {
|
||||
EventsCollection {
|
||||
// TODO: hard-coded events
|
||||
events: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.events.clear();
|
||||
}
|
||||
|
||||
pub fn create(self: &mut Self, begin: NaiveDate, end: NaiveDate, text: &str, ev_type: &EventType) {
|
||||
self.events.push(Event {text: text.to_string(), ev_type: ev_type.clone(), begin, end});
|
||||
}
|
||||
@ -113,3 +118,36 @@ impl Default for EventsCollection {
|
||||
EventsCollection::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CalendarFile {
|
||||
pub file_path: path::PathBuf,
|
||||
pub name: String,
|
||||
pub enabled: bool,
|
||||
pub index: usize,
|
||||
}
|
||||
|
||||
impl CalendarFile {
|
||||
pub fn from_file(file_path: path::PathBuf) -> Self {
|
||||
static COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||
let name = if let Some(oss_name) = file_path.file_name() {
|
||||
if let Some(str) = oss_name.to_str() {
|
||||
String::from(str)
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
CalendarFile {
|
||||
file_path,
|
||||
name,
|
||||
enabled: true,
|
||||
index: COUNTER.fetch_add(1, Ordering::Relaxed),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> &String {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
@ -44,9 +44,9 @@ impl Controls {
|
||||
row![
|
||||
button(text("=")).on_press(Message::ToggleSidebar).style(secondary),
|
||||
row![
|
||||
button(text("Week")).on_press(Message::ViewModeSelected(ViewMode::Week)).style(self.get_mode_style(ViewMode::Week)),
|
||||
button(text("Month")).on_press(Message::ViewModeSelected(ViewMode::Month)).style(self.get_mode_style(ViewMode::Month)),
|
||||
button(text("Year")).on_press(Message::ViewModeSelected(ViewMode::Year)).style(self.get_mode_style(ViewMode::Year)),
|
||||
button(text("W")).on_press(Message::ViewModeSelected(ViewMode::Week)).style(self.get_mode_style(ViewMode::Week)),
|
||||
button(text("M")).on_press(Message::ViewModeSelected(ViewMode::Month)).style(self.get_mode_style(ViewMode::Month)),
|
||||
button(text("Y")).on_press(Message::ViewModeSelected(ViewMode::Year)).style(self.get_mode_style(ViewMode::Year)),
|
||||
],
|
||||
row![
|
||||
button(text("<")).on_press(self.get_msg_prev()).style(secondary),
|
||||
|
Loading…
x
Reference in New Issue
Block a user