|
@@ -1,30 +1,60 @@
|
|
|
use crate::entities::{DateFilterCondition, DateFilterPB};
|
|
|
use crate::services::cell::{AnyCellData, CellData, CellFilterOperation};
|
|
|
use crate::services::field::{DateTimestamp, DateTypeOptionPB};
|
|
|
+use chrono::NaiveDateTime;
|
|
|
use flowy_error::FlowyResult;
|
|
|
|
|
|
impl DateFilterPB {
|
|
|
- pub fn is_visible<T: Into<i64>>(&self, cell_timestamp: T) -> bool {
|
|
|
- if self.start.is_none() {
|
|
|
- return false;
|
|
|
- }
|
|
|
- let cell_timestamp = cell_timestamp.into();
|
|
|
- let start_timestamp = *self.start.as_ref().unwrap();
|
|
|
- // We assume that the cell_timestamp doesn't contain hours, just day.
|
|
|
- match self.condition {
|
|
|
- DateFilterCondition::DateIs => cell_timestamp == start_timestamp,
|
|
|
- DateFilterCondition::DateBefore => cell_timestamp < start_timestamp,
|
|
|
- DateFilterCondition::DateAfter => cell_timestamp > start_timestamp,
|
|
|
- DateFilterCondition::DateOnOrBefore => cell_timestamp <= start_timestamp,
|
|
|
- DateFilterCondition::DateOnOrAfter => cell_timestamp >= start_timestamp,
|
|
|
- DateFilterCondition::DateWithIn => {
|
|
|
- if let Some(end_timestamp) = self.end.as_ref() {
|
|
|
- cell_timestamp >= start_timestamp && cell_timestamp <= *end_timestamp
|
|
|
- } else {
|
|
|
- false
|
|
|
+ pub fn is_visible<T: Into<Option<i64>>>(&self, cell_timestamp: T) -> bool {
|
|
|
+ match cell_timestamp.into() {
|
|
|
+ None => DateFilterCondition::DateIsEmpty == self.condition,
|
|
|
+ Some(timestamp) => {
|
|
|
+ match self.condition {
|
|
|
+ DateFilterCondition::DateIsNotEmpty => {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ DateFilterCondition::DateIsEmpty => {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ _ => {}
|
|
|
+ }
|
|
|
+
|
|
|
+ let cell_time = NaiveDateTime::from_timestamp(timestamp, 0);
|
|
|
+ let cell_date = cell_time.date();
|
|
|
+ match self.timestamp {
|
|
|
+ None => {
|
|
|
+ if self.start.is_none() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if self.end.is_none() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ let start_time = NaiveDateTime::from_timestamp(*self.start.as_ref().unwrap(), 0);
|
|
|
+ let start_date = start_time.date();
|
|
|
+
|
|
|
+ let end_time = NaiveDateTime::from_timestamp(*self.end.as_ref().unwrap(), 0);
|
|
|
+ let end_date = end_time.date();
|
|
|
+
|
|
|
+ cell_date >= start_date && cell_date <= end_date
|
|
|
+ }
|
|
|
+ Some(timestamp) => {
|
|
|
+ let expected_timestamp = NaiveDateTime::from_timestamp(timestamp, 0);
|
|
|
+ let expected_date = expected_timestamp.date();
|
|
|
+
|
|
|
+ // We assume that the cell_timestamp doesn't contain hours, just day.
|
|
|
+ match self.condition {
|
|
|
+ DateFilterCondition::DateIs => cell_date == expected_date,
|
|
|
+ DateFilterCondition::DateBefore => cell_date < expected_date,
|
|
|
+ DateFilterCondition::DateAfter => cell_date > expected_date,
|
|
|
+ DateFilterCondition::DateOnOrBefore => cell_date <= expected_date,
|
|
|
+ DateFilterCondition::DateOnOrAfter => cell_date >= expected_date,
|
|
|
+ _ => true,
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- DateFilterCondition::DateIsEmpty => cell_timestamp == 0_i64,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -49,11 +79,12 @@ mod tests {
|
|
|
fn date_filter_is_test() {
|
|
|
let filter = DateFilterPB {
|
|
|
condition: DateFilterCondition::DateIs,
|
|
|
- start: Some(123),
|
|
|
+ timestamp: Some(1668387885),
|
|
|
end: None,
|
|
|
+ start: None,
|
|
|
};
|
|
|
|
|
|
- for (val, visible) in vec![(123, true), (12, false)] {
|
|
|
+ for (val, visible) in vec![(1668387885, true), (1647251762, false)] {
|
|
|
assert_eq!(filter.is_visible(val as i64), visible);
|
|
|
}
|
|
|
}
|
|
@@ -61,23 +92,26 @@ mod tests {
|
|
|
fn date_filter_before_test() {
|
|
|
let filter = DateFilterPB {
|
|
|
condition: DateFilterCondition::DateBefore,
|
|
|
- start: Some(123),
|
|
|
+ timestamp: Some(1668387885),
|
|
|
+ start: None,
|
|
|
end: None,
|
|
|
};
|
|
|
|
|
|
- for (val, visible) in vec![(123, false), (122, true)] {
|
|
|
- assert_eq!(filter.is_visible(val as i64), visible);
|
|
|
+ for (val, visible, msg) in vec![(1668387884, false, "1"), (1647251762, true, "2")] {
|
|
|
+ assert_eq!(filter.is_visible(val as i64), visible, "{}", msg);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
#[test]
|
|
|
fn date_filter_before_or_on_test() {
|
|
|
let filter = DateFilterPB {
|
|
|
condition: DateFilterCondition::DateOnOrBefore,
|
|
|
- start: Some(123),
|
|
|
+ timestamp: Some(1668387885),
|
|
|
+ start: None,
|
|
|
end: None,
|
|
|
};
|
|
|
|
|
|
- for (val, visible) in vec![(123, true), (122, true)] {
|
|
|
+ for (val, visible) in vec![(1668387884, true), (1668387885, true)] {
|
|
|
assert_eq!(filter.is_visible(val as i64), visible);
|
|
|
}
|
|
|
}
|
|
@@ -85,24 +119,45 @@ mod tests {
|
|
|
fn date_filter_after_test() {
|
|
|
let filter = DateFilterPB {
|
|
|
condition: DateFilterCondition::DateAfter,
|
|
|
- start: Some(123),
|
|
|
+ timestamp: Some(1668387885),
|
|
|
+ start: None,
|
|
|
end: None,
|
|
|
};
|
|
|
|
|
|
- for (val, visible) in vec![(1234, true), (122, false), (0, false)] {
|
|
|
+ for (val, visible) in vec![(1668387888, false), (1668531885, true), (0, false)] {
|
|
|
assert_eq!(filter.is_visible(val as i64), visible);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
#[test]
|
|
|
fn date_filter_within_test() {
|
|
|
let filter = DateFilterPB {
|
|
|
condition: DateFilterCondition::DateWithIn,
|
|
|
- start: Some(123),
|
|
|
- end: Some(130),
|
|
|
+ start: Some(1668272685), // 11/13
|
|
|
+ end: Some(1668618285), // 11/17
|
|
|
+ timestamp: None,
|
|
|
};
|
|
|
|
|
|
- for (val, visible) in vec![(123, true), (130, true), (132, false)] {
|
|
|
+ for (val, visible, _msg) in vec![
|
|
|
+ (1668272685, true, "11/13"),
|
|
|
+ (1668359085, true, "11/14"),
|
|
|
+ (1668704685, false, "11/18"),
|
|
|
+ ] {
|
|
|
assert_eq!(filter.is_visible(val as i64), visible);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn date_filter_is_empty_test() {
|
|
|
+ let filter = DateFilterPB {
|
|
|
+ condition: DateFilterCondition::DateIsEmpty,
|
|
|
+ start: None,
|
|
|
+ end: None,
|
|
|
+ timestamp: None,
|
|
|
+ };
|
|
|
+
|
|
|
+ for (val, visible) in vec![(None, true), (Some(123), false)] {
|
|
|
+ assert_eq!(filter.is_visible(val), visible);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|