Fix to display correct days in calendar view. (#124)

<img width="624" alt="image"
src="https://github.com/user-attachments/assets/3e6528a0-d649-42a9-9c82-f1e06539ec46">
<img width="617" alt="image"
src="https://github.com/user-attachments/assets/370dfa05-e8b1-4aa6-8eda-070d6f4a2661">
This commit is contained in:
Jason Lee 2024-08-09 10:16:31 +08:00 committed by GitHub
parent 5d55c5c31e
commit 97dd09fbda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 51 deletions

View file

@ -236,11 +236,11 @@ impl Calendar {
/// Returns the days of the month in a 2D vector to render on calendar. /// Returns the days of the month in a 2D vector to render on calendar.
fn days(&self) -> Vec<Vec<NaiveDate>> { fn days(&self) -> Vec<Vec<NaiveDate>> {
days_in_month( (0..self.number_of_months)
self.current_year, .flat_map(|offset| {
self.current_month as u32, days_in_month(self.current_year, self.current_month as u32 + offset as u32)
self.number_of_months as u32, })
) .collect()
} }
fn has_prev_year_page(&self) -> bool { fn has_prev_year_page(&self) -> bool {
@ -353,7 +353,7 @@ impl Calendar {
}) })
.when(secondary_active, |this| { .when(secondary_active, |this| {
this.bg(if muted { this.bg(if muted {
cx.theme().accent.opacity(0.3) cx.theme().accent.opacity(0.5)
} else { } else {
cx.theme().accent cx.theme().accent
}) })
@ -382,7 +382,7 @@ impl Calendar {
let (_, month) = self.offset_year_month(offset_month); let (_, month) = self.offset_year_month(offset_month);
let day = d.day(); let day = d.day();
let is_current_month = d.month() == month; let is_current_month = d.month() == month;
let is_active = self.date.is_active(d); let is_active = self.date.is_active(d) && is_current_month;
let is_in_range = self.date.is_in_range(d); let is_in_range = self.date.is_in_range(d);
let date = *d; let date = *d;
@ -508,7 +508,7 @@ impl Calendar {
}) })
.when(multiple_months, |this| { .when(multiple_months, |this| {
this.child(h_flex().flex_1().justify_around().children( this.child(h_flex().flex_1().justify_around().children(
(0..self.number_of_months).into_iter().map(|n| { (0..self.number_of_months).map(|n| {
h_flex() h_flex()
.justify_center() .justify_center()
.gap_3() .gap_3()
@ -546,7 +546,6 @@ impl Calendar {
h_flex().gap_4().justify_between().text_sm().children( h_flex().gap_4().justify_between().text_sm().children(
self.days() self.days()
.chunks(5) .chunks(5)
.into_iter()
.enumerate() .enumerate()
.map(|(offset_month, days)| { .map(|(offset_month, days)| {
v_flex() v_flex()

View file

@ -28,7 +28,7 @@ impl NaiveDateExt for chrono::NaiveDate {
} }
} }
pub(crate) fn days_in_month(year: i32, month: u32, number_of_months: u32) -> Vec<Vec<NaiveDate>> { pub(crate) fn days_in_month(year: i32, month: u32) -> Vec<Vec<NaiveDate>> {
let mut year = year; let mut year = year;
let mut month = month; let mut month = month;
if month > 12 { if month > 12 {
@ -44,31 +44,14 @@ pub(crate) fn days_in_month(year: i32, month: u32, number_of_months: u32) -> Vec
let num_days = date.days_in_month(); let num_days = date.days_in_month();
let start_weekday = date.weekday().num_days_from_sunday(); let start_weekday = date.weekday().num_days_from_sunday();
let mut total_groups = number_of_months * 5;
if total_groups == 0 {
total_groups = 5;
}
// Get the days in the month, 2023-02 will returns // Get the days in the month, 2023-02 will returns
// "29|30|31| 1| 2| 3| 4", // "29|30|31| 1| 2| 3| 4",
// " 5| 6| 7| 8| 9|10|11", // " 5| 6| 7| 8| 9|10|11",
// "12|13|14|15|16|17|18", // "12|13|14|15|16|17|18",
// "19|20|21|22|23|24|25", // "19|20|21|22|23|24|25",
// "26|27|28| 1| 2| 3| 4", // "26|27|28| 1| 2| 3| 4",
//
// If the number_of_months is 2, then it will return
// "29|30|31| 1| 2| 3| 4",
// " 5| 6| 7| 8| 9|10|11",
// "12|13|14|15|16|17|18",
// "19|20|21|22|23|24|25",
// "26|27|28| 1| 2| 3| 4",
// " 5| 6| 7| 8| 9|10|11",
// "12|13|14|15|16|17|18",
// "19|20|21|22|23|24|25",
// "26|27|28| 1| 2| 3| 4",
// " 5| 6| 7| 8| 9|10|11",
let mut days = vec![]; let mut days = vec![];
for n in 0..total_groups as i32 { for n in 0..5 {
let mut week_days = vec![]; let mut week_days = vec![];
for weekday in 0..7 { for weekday in 0..7 {
let (mut y, mut m) = (year, month); let (mut y, mut m) = (year, month);
@ -131,8 +114,8 @@ mod tests {
#[test] #[test]
fn test_days() { fn test_days() {
#[track_caller] #[track_caller]
fn assert_case(date: NaiveDate, number_of_months: u32, expected: Vec<&str>) { fn assert_case(date: NaiveDate, expected: Vec<&str>) {
let out = days_in_month(date.year(), date.month(), number_of_months) let out = days_in_month(date.year(), date.month())
.iter() .iter()
.map(|week| { .map(|week| {
week.iter() week.iter()
@ -155,7 +138,6 @@ mod tests {
assert_case( assert_case(
NaiveDate::from_ymd_opt(2024, 8, 1).unwrap(), NaiveDate::from_ymd_opt(2024, 8, 1).unwrap(),
1,
vec![ vec![
"7-28|7-29|7-30|7-31| 1| 2| 3", "7-28|7-29|7-30|7-31| 1| 2| 3",
" 4| 5| 6| 7| 8| 9|10", " 4| 5| 6| 7| 8| 9|10",
@ -166,7 +148,6 @@ mod tests {
); );
assert_case( assert_case(
NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(), NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
1,
vec![ vec![
"2024-12-29|2024-12-30|2024-12-31| 1| 2| 3| 4", "2024-12-29|2024-12-30|2024-12-31| 1| 2| 3| 4",
" 5| 6| 7| 8| 9|10|11", " 5| 6| 7| 8| 9|10|11",
@ -178,7 +159,6 @@ mod tests {
assert_case( assert_case(
NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(), NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
1,
vec![ vec![
"1-28|1-29|1-30|1-31| 1| 2| 3", "1-28|1-29|1-30|1-31| 1| 2| 3",
" 4| 5| 6| 7| 8| 9|10", " 4| 5| 6| 7| 8| 9|10",
@ -189,7 +169,6 @@ mod tests {
); );
assert_case( assert_case(
NaiveDate::from_ymd_opt(2023, 2, 20).unwrap(), NaiveDate::from_ymd_opt(2023, 2, 20).unwrap(),
1,
vec![ vec![
"1-29|1-30|1-31| 1| 2| 3| 4", "1-29|1-30|1-31| 1| 2| 3| 4",
" 5| 6| 7| 8| 9|10|11", " 5| 6| 7| 8| 9|10|11",
@ -198,22 +177,5 @@ mod tests {
"26|27|28|3-1|3-2|3-3|3-4", "26|27|28|3-1|3-2|3-3|3-4",
], ],
); );
assert_case(
NaiveDate::from_ymd_opt(2023, 2, 20).unwrap(),
2,
vec![
"1-29|1-30|1-31| 1| 2| 3| 4",
" 5| 6| 7| 8| 9|10|11",
"12|13|14|15|16|17|18",
"19|20|21|22|23|24|25",
"26|27|28|3-1|3-2|3-3|3-4",
" 5| 6| 7| 8| 9|10|11",
"12|13|14|15|16|17|18",
"19|20|21|22|23|24|25",
"26|27|28|4-1|4-2|4-3|4-4",
" 5| 6| 7| 8| 9|10|11",
],
);
} }
} }