Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

feat: sort by last modified time or created time (#4601)

Richard Shiueef4bce25d816
diff --git a/frontend/appflowy_flutter/lib/plugins/database/application/field/field_info.dart b/frontend/appflowy_flutter/lib/plugins/database/application/field/field_info.dart
index 81592e09c6..717776f075 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/application/field/field_info.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/application/field/field_info.dart
@@ -71,6 +71,8 @@ class FieldInfo with _$FieldInfo {
case FieldType.DateTime:
case FieldType.SingleSelect:
case FieldType.MultiSelect:
+ case FieldType.LastEditedTime:
+ case FieldType.CreatedTime:
return true;
default:
return false;
diff --git a/frontend/appflowy_flutter/packages/appflowy_backend/ios/Classes/binding.h b/frontend/appflowy_flutter/packages/appflowy_backend/ios/Classes/binding.h
index 710af508fd..3fe1f39faa 100644
--- a/frontend/appflowy_flutter/packages/appflowy_backend/ios/Classes/binding.h
+++ b/frontend/appflowy_flutter/packages/appflowy_backend/ios/Classes/binding.h
@@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdlib.h>
-int64_t init_sdk(char *data);
+int64_t init_sdk(int64_t port, char *data);
void async_event(int64_t port, const uint8_t *input, uintptr_t len);
diff --git a/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs b/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs
index 04e68c3b23..42dcc256a3 100644
--- a/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs
+++ b/frontend/rust-lib/flowy-database2/src/services/database_view/view_editor.rs
@@ -160,6 +160,9 @@ impl DatabaseViewEditor {
send_notification(&self.view_id, DatabaseNotification::DidUpdateViewRows)
.payload(changes)
.send();
+ self
+ .gen_view_tasks(row_detail.row.id.clone(), "".to_string())
+ .await;
}
pub async fn v_did_duplicate_row(&self, row_detail: &RowDetail) {
@@ -253,30 +256,9 @@ impl DatabaseViewEditor {
// Each row update will trigger a calculations, filter and sort operation. We don't want
// to block the main thread, so we spawn a new task to do the work.
- let row_id = row_detail.row.id.clone();
- let weak_filter_controller = Arc::downgrade(&self.filter_controller);
- let weak_sort_controller = Arc::downgrade(&self.sort_controller);
- let weak_calculations_controller = Arc::downgrade(&self.calculations_controller);
- af_spawn(async move {
- if let Some(filter_controller) = weak_filter_controller.upgrade() {
- filter_controller
- .did_receive_row_changed(row_id.clone())
- .await;
- }
- if let Some(sort_controller) = weak_sort_controller.upgrade() {
- sort_controller
- .read()
- .await
- .did_receive_row_changed(row_id.clone())
- .await;
- }
-
- if let Some(calculations_controller) = weak_calculations_controller.upgrade() {
- calculations_controller
- .did_receive_cell_changed(field_id)
- .await;
- }
- });
+ self
+ .gen_view_tasks(row_detail.row.id.clone(), field_id)
+ .await;
}
pub async fn v_filter_rows(&self, row_details: &mut Vec<Arc<RowDetail>>) {
@@ -1073,4 +1055,29 @@ impl DatabaseViewEditor {
None
}
}
+
+ async fn gen_view_tasks(&self, row_id: RowId, field_id: String) {
+ let weak_filter_controller = Arc::downgrade(&self.filter_controller);
+ let weak_sort_controller = Arc::downgrade(&self.sort_controller);
+ let weak_calculations_controller = Arc::downgrade(&self.calculations_controller);
+ af_spawn(async move {
+ if let Some(filter_controller) = weak_filter_controller.upgrade() {
+ filter_controller
+ .did_receive_row_changed(row_id.clone())
+ .await;
+ }
+ if let Some(sort_controller) = weak_sort_controller.upgrade() {
+ sort_controller
+ .read()
+ .await
+ .did_receive_row_changed(row_id)
+ .await;
+ }
+ if let Some(calculations_controller) = weak_calculations_controller.upgrade() {
+ calculations_controller
+ .did_receive_cell_changed(field_id)
+ .await;
+ }
+ });
+ }
}
diff --git a/frontend/rust-lib/flowy-database2/src/services/sort/controller.rs b/frontend/rust-lib/flowy-database2/src/services/sort/controller.rs
index 599a67303c..b1d709728f 100644
--- a/frontend/rust-lib/flowy-database2/src/services/sort/controller.rs
+++ b/frontend/rust-lib/flowy-database2/src/services/sort/controller.rs
@@ -17,7 +17,9 @@ use crate::entities::SortChangesetNotificationPB;
use crate::entities::{FieldType, SortWithIndexPB};
use crate::services::cell::CellCache;
use crate::services::database_view::{DatabaseViewChanged, DatabaseViewChangedNotifier};
-use crate::services::field::{default_order, TypeOptionCellExt};
+use crate::services::field::{
+ default_order, TimestampCellData, TimestampCellDataWrapper, TypeOptionCellExt,
+};
use crate::services::sort::{
ReorderAllRowsResult, ReorderSingleRowResult, Sort, SortChangeset, SortCondition,
};
@@ -81,9 +83,13 @@ impl SortController {
}
pub async fn did_receive_row_changed(&self, row_id: RowId) {
- let task_type = SortEvent::RowDidChanged(row_id);
if !self.sorts.is_empty() {
- self.gen_task(task_type, QualityOfService::Background).await;
+ self
+ .gen_task(
+ SortEvent::RowDidChanged(row_id),
+ QualityOfService::Background,
+ )
+ .await;
}
}
@@ -252,14 +258,36 @@ fn cmp_row(
.find(|field_rev| field_rev.id == sort.field_id)
{
None => default_order(),
- Some(field_rev) => cmp_cell(
- left.cells.get(&sort.field_id),
- right.cells.get(&sort.field_id),
- field_rev,
- field_type,
- cell_data_cache,
- sort.condition,
- ),
+ Some(field_rev) => {
+ let timestamp_cells = match field_type {
+ FieldType::LastEditedTime | FieldType::CreatedTime => {
+ let (left_cell, right_cell) = if field_type.is_created_time() {
+ (left.created_at, right.created_at)
+ } else {
+ (left.modified_at, right.modified_at)
+ };
+ let (left_cell, right_cell) = (
+ TimestampCellDataWrapper::from((field_type, TimestampCellData::new(left_cell))),
+ TimestampCellDataWrapper::from((field_type, TimestampCellData::new(right_cell))),
+ );
+ Some((Some(left_cell.into()), Some(right_cell.into())))
+ },
+ _ => None,
+ };
+
+ cmp_cell(
+ timestamp_cells
+ .as_ref()
+ .map_or_else(|| left.cells.get(&sort.field_id), |cell| cell.0.as_ref()),
+ timestamp_cells
+ .as_ref()
+ .map_or_else(|| right.cells.get(&sort.field_id), |cell| cell.1.as_ref()),
+ field_rev,
+ field_type,
+ cell_data_cache,
+ sort.condition,
+ )
+ },
}
}
@@ -276,13 +304,7 @@ fn cmp_cell(
{
None => default_order(),
Some(handler) => {
- let cal_order = || {
- let order =
- handler.handle_cell_compare(left_cell, right_cell, field.as_ref(), sort_condition);
- Option::<Ordering>::Some(order)
- };
-
- cal_order().unwrap_or_else(default_order)
+ handler.handle_cell_compare(left_cell, right_cell, field.as_ref(), sort_condition)
},
}
}