代码拉取完成,页面将自动刷新
diff --git a/calendarmanager/napi/src/event_filter_napi.cpp b/calendarmanager/napi/src/event_filter_napi.cpp
index d7180a5..7790f02 100644
--- a/calendarmanager/napi/src/event_filter_napi.cpp
+++ b/calendarmanager/napi/src/event_filter_napi.cpp
@@ -87,40 +87,12 @@ napi_value EventFilterNapi::FilterById(napi_env env, napi_callback_info info)
napi_value argv[1] = { 0 };
napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
NAPI_ASSERT(env, argc == 1, "requires 1 parameter");
- napi_valuetype valueType = napi_null;
- napi_typeof(env, argv[0], &valueType);
- if (valueType != napi_object) {
- LOG_ERROR("type mismatch for parameter 1");
- return result;
- }
std::vector<int> ids;
- bool isArray = false;
- napi_status status = napi_is_array(env, argv[0], &isArray);
- if (status != napi_ok || !isArray) {
- LOG_ERROR("ParseBytesVector, not array");
- return result;
- }
- uint32_t arrayLength = 0;
- napi_get_array_length(env, argv[0], &arrayLength);
- for (uint32_t i = 0; i < arrayLength; i++) {
- napi_value element = nullptr;
- napi_get_element(env, argv[0], i, &element);
-
- napi_valuetype valueType = napi_undefined;
- napi_typeof(env, element, &valueType);
- if (valueType != napi_number) {
- LOG_ERROR("ParseBytesVector, not number!");
- return result;
- }
-
- uint32_t byteValue = 0x0;
- napi_get_value_uint32(env, element, &byteValue);
- ids.push_back(static_cast<int>(byteValue));
- }
-
+ auto status = NapiUtil::GetValueArray(env, argv[0], ids);
+ CHECK_RETURN(status == napi_ok, "GetValueArray failed", nullptr);
EventFilterNapi *filter;
status = napi_new_instance(env, EventFilterNapi::Constructor(env), argc, argv, &result);
- CHECK_RETURN(status == napi_ok, "napi_new_instance failed", result);
+ CHECK_RETURN(status == napi_ok, "napi_new_instance failed", result);
CHECK_RETURN(result != nullptr, "napi_new_instance failed", result);
status = napi_unwrap(env, result, reinterpret_cast<void**>(&filter));
CHECK_RETURN(status == napi_ok, "napi_unwrap failed", nullptr);
diff --git a/calendarmanager/napi/src/napi_util.cpp b/calendarmanager/napi/src/napi_util.cpp
index b5cde20..34eba88 100644
--- a/calendarmanager/napi/src/napi_util.cpp
+++ b/calendarmanager/napi/src/napi_util.cpp
@@ -140,24 +140,7 @@ napi_status SetValue(napi_env env, const std::string& in, napi_value& out)
napi_status GetValue(napi_env env, napi_value in, std::vector<std::string>& out)
{
LOG_DEBUG("napi_value -> std::vector<std::string>");
- out.clear();
- bool isArray = false;
- napi_is_array(env, in, &isArray);
- CHECK_RETURN(isArray, "not an array", napi_invalid_arg);
-
- uint32_t length = 0;
- napi_status status = napi_get_array_length(env, in, &length);
- CHECK_RETURN((status == napi_ok) && (length > 0), "get_array failed!", napi_invalid_arg);
- for (uint32_t i = 0; i < length; ++i) {
- napi_value item = nullptr;
- status = napi_get_element(env, in, i, &item);
- CHECK_RETURN((item != nullptr) && (status == napi_ok), "no element", napi_invalid_arg);
- std::string value;
- status = GetValue(env, item, value);
- CHECK_RETURN(status == napi_ok, "not a string", napi_invalid_arg);
- out.push_back(value);
- }
- return status;
+ return GetValueArray(env, in, out);;
}
napi_status SetValue(napi_env env, const std::vector<std::string>& in, napi_value& out)
@@ -212,72 +195,11 @@ napi_status SetValue(napi_env env, const std::vector<uint8_t>& in, napi_value& o
return status;
}
-template <typename T>
-void TypedArray2Vector(uint8_t* data, size_t length, napi_typedarray_type type, std::vector<T>& out)
-{
- auto convert = [&out](auto* data, size_t elements) {
- for (size_t index = 0; index < elements; index++) {
- out.push_back(static_cast<T>(data[index]));
- }
- };
-
- switch (type) {
- case napi_int8_array:
- convert(reinterpret_cast<int8_t*>(data), length);
- break;
- case napi_uint8_array:
- convert(data, length);
- break;
- case napi_uint8_clamped_array:
- convert(data, length);
- break;
- case napi_int16_array:
- convert(reinterpret_cast<int16_t*>(data), length / sizeof(int16_t));
- break;
- case napi_uint16_array:
- convert(reinterpret_cast<uint16_t*>(data), length / sizeof(uint16_t));
- break;
- case napi_int32_array:
- convert(reinterpret_cast<int32_t*>(data), length / sizeof(int32_t));
- break;
- case napi_uint32_array:
- convert(reinterpret_cast<uint32_t*>(data), length / sizeof(uint32_t));
- break;
- case napi_float32_array:
- convert(reinterpret_cast<float*>(data), length / sizeof(float));
- break;
- case napi_float64_array:
- convert(reinterpret_cast<double*>(data), length / sizeof(double));
- break;
- case napi_bigint64_array:
- convert(reinterpret_cast<int64_t*>(data), length / sizeof(int64_t));
- break;
- case napi_biguint64_array:
- convert(reinterpret_cast<uint64_t*>(data), length / sizeof(uint64_t));
- break;
- default:
- CHECK_RETURN_VOID(false, "[FATAL] invalid napi_typedarray_type!");
- }
-}
-
/* napi_value <-> std::vector<int32_t> */
napi_status GetValue(napi_env env, napi_value in, std::vector<int32_t>& out)
{
- out.clear();
LOG_DEBUG("napi_value -> std::vector<int32_t> ");
- napi_typedarray_type type = napi_biguint64_array;
- size_t length = 0;
- napi_value buffer = nullptr;
- size_t offset = 0;
- uint8_t* data = nullptr;
- napi_status status = napi_get_typedarray_info(env, in, &type, &length,
- reinterpret_cast<void**>(&data), &buffer, &offset);
- LOG_DEBUG("array type=%{public}d length=%{public}d offset=%{public}d", (int)type, (int)length, (int)offset);
- CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
- CHECK_RETURN(type <= napi_int32_array, "is not int32 supported typed array!", napi_invalid_arg);
- CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
- TypedArray2Vector<int32_t>(data, length, type, out);
- return status;
+ return GetValueArray(env, in, out);
}
napi_status SetValue(napi_env env, const std::vector<int32_t>& in, napi_value& out)
@@ -302,21 +224,8 @@ napi_status SetValue(napi_env env, const std::vector<int32_t>& in, napi_value& o
/* napi_value <-> std::vector<uint32_t> */
napi_status GetValue(napi_env env, napi_value in, std::vector<uint32_t>& out)
{
- out.clear();
LOG_DEBUG("napi_value -> std::vector<uint32_t> ");
- napi_typedarray_type type = napi_biguint64_array;
- size_t length = 0;
- napi_value buffer = nullptr;
- size_t offset = 0;
- uint8_t* data = nullptr;
- napi_status status = napi_get_typedarray_info(env, in, &type, &length,
- reinterpret_cast<void**>(&data), &buffer, &offset);
- LOG_DEBUG("napi_get_typedarray_info type=%{public}d", (int)type);
- CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
- CHECK_RETURN((type <= napi_uint16_array) || (type == napi_uint32_array), "invalid type!", napi_invalid_arg);
- CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
- TypedArray2Vector<uint32_t>(data, length, type, out);
- return status;
+ return GetValueArray(env, in, out);
}
napi_status SetValue(napi_env env, const std::vector<uint32_t>& in, napi_value& out)
@@ -341,21 +250,8 @@ napi_status SetValue(napi_env env, const std::vector<uint32_t>& in, napi_value&
/* napi_value <-> std::vector<int64_t> */
napi_status GetValue(napi_env env, napi_value in, std::vector<int64_t>& out)
{
- out.clear();
LOG_DEBUG("napi_value -> std::vector<int64_t> ");
- napi_typedarray_type type = napi_biguint64_array;
- size_t length = 0;
- napi_value buffer = nullptr;
- size_t offset = 0;
- uint8_t* data = nullptr;
- napi_status status = napi_get_typedarray_info(env, in, &type, &length,
- reinterpret_cast<void**>(&data), &buffer, &offset);
- LOG_DEBUG("array type=%{public}d length=%{public}d offset=%{public}d", (int)type, (int)length, (int)offset);
- CHECK_RETURN(status == napi_ok, "napi_get_typedarray_info failed!", napi_invalid_arg);
- CHECK_RETURN((type <= napi_uint32_array) || (type == napi_bigint64_array), "invalid type!", napi_invalid_arg);
- CHECK_RETURN((length > 0) && (data != nullptr), "invalid data!", napi_invalid_arg);
- TypedArray2Vector<int64_t>(data, length, type, out);
- return status;
+ return GetValueArray(env, in, out);
}
napi_status SetValue(napi_env env, const std::vector<int64_t>& in, napi_value& out)
@@ -627,7 +523,7 @@ napi_status SetValue(napi_env env, const Event& in, napi_value& out)
napi_set_named_property(env, out, "id", id);
napi_value type = nullptr;
- status = SetValue(env, in.type, type);
+ status = SetValue(env, static_cast<int>(in.type), type);
CHECK_RETURN((status == napi_ok), "invalid entry type", status);
napi_set_named_property(env, out, "type", type);
@@ -666,6 +562,13 @@ napi_status SetValue(napi_env env, const Event& in, napi_value& out)
CHECK_RETURN((status == napi_ok), "invalid service", status);
napi_set_named_property(env, out, "service", value);
}
+
+ if (in.reminderTime) {
+ napi_value value = nullptr;
+ status = SetValue(env, in.reminderTime.value(), value);
+ CHECK_RETURN((status == napi_ok), "invalid entry reminderTime", status);
+ napi_set_named_property(env, out, "reminderTime", value);
+ }
return status;
}
diff --git a/calendarmanager/native/include/native_calendar.h b/calendarmanager/native/include/native_calendar.h
index 73f4f86..59935a4 100644
--- a/calendarmanager/native/include/native_calendar.h
+++ b/calendarmanager/native/include/native_calendar.h
@@ -45,6 +45,7 @@ public:
bool SetConfig(const CalendarConfig& config);
std::vector<Event> GetEvents(std::shared_ptr<EventFilter> filter, const std::vector<string>& eventKey);
std::vector<Attendee> GetAttendeesByEventId(int id);
+ std::optional<std::vector<int>> GetRemindersByEventId(int id);
CalendarAccount GetAccount() const
{
return m_account;
@@ -60,6 +61,7 @@ private:
std::unique_ptr<Uri> m_eventUri;
std::unique_ptr<Uri> m_attendeeUri;
std::unique_ptr<Uri> m_calendarUri;
+ std::unique_ptr<Uri> m_reminderUrl;
};
} // namespace OHOS::Calendar
#endif // NATIVE_CALENDAR_H
\ No newline at end of file
diff --git a/calendarmanager/native/include/native_util.h b/calendarmanager/native/include/native_util.h
index 58e08d8..7c1de01 100644
--- a/calendarmanager/native/include/native_util.h
+++ b/calendarmanager/native/include/native_util.h
@@ -40,6 +40,7 @@ namespace OHOS::CalendarApi::Native {
int ResultSetToEvents(std::vector<Event> &events,
DataShareResultSetPtr &resultSet, const std::vector<std::string>& columns);
int ResultSetToAttendees(std::vector<Attendee> &attendees, DataShareResultSetPtr &resultSet);
+ int ResultSetToReminders(std::vector<int> &reminders, DataShareResultSetPtr &resultSet);
bool ColorParse(const std::string& colorStr, uint32_t& colorValue);
diff --git a/calendarmanager/native/src/native_calendar.cpp b/calendarmanager/native/src/native_calendar.cpp
index c0725d6..205c9e8 100644
--- a/calendarmanager/native/src/native_calendar.cpp
+++ b/calendarmanager/native/src/native_calendar.cpp
@@ -24,6 +24,7 @@ namespace {
const string eventUrl = "datashare:///calendardata/Events";
const string attendeeUrl = "datashare:///calendardata/Attendees";
const string calendarUrl = "datashare:///calendardata/Calendars";
+ const string reminderUrl = "datashare:///calendardata/Reminders";
}
namespace OHOS::CalendarApi::Native {
Calendar::Calendar(int id)
@@ -48,6 +49,7 @@ Calendar::Calendar(CalendarAccount account, int id)
m_eventUri = std::make_unique<Uri>(eventUrl + bundleName_tokeId);
m_attendeeUri = std::make_unique<Uri>(attendeeUrl + bundleName_tokeId);
m_calendarUri = std::make_unique<Uri>(calendarUrl + bundleName_tokeId);
+ m_reminderUrl = std::make_unique<Uri>(reminderUrl + bundleName_tokeId);
}
int Calendar::AddEvent(const Event& event)
@@ -64,6 +66,17 @@ int Calendar::AddEvent(const Event& event)
auto index = DataShareHelperManager::GetInstance().Insert(*(m_attendeeUri.get()), valueAttendee);
LOG_INFO("Insert attendee index %{public}d", index);
}
+ // insert reminder
+ if (event.reminderTime) {
+ for (const auto &reminder : event.reminderTime.value()) {
+ DataShare::DataShareValuesBucket valuesBucket;
+ valuesBucket.Put("event_id", eventId);
+ valuesBucket.Put("minutes", reminder);
+ auto index = DataShareHelperManager::GetInstance().Insert(*(m_reminderUrl.get()), valuesBucket);
+ LOG_INFO("Insert reminder index %{public}d", index);
+ }
+ }
+
return eventId;
}
#define SUPPORT_BATCH_INSERT 0
@@ -95,6 +108,7 @@ int Calendar::AddEvents(const std::vector<Event>& events)
}
#endif
+
bool Calendar::DeleteEvent(int id)
{
DataShare::DataSharePredicates predicates;
@@ -109,6 +123,13 @@ bool Calendar::DeleteEvent(int id)
auto ret = DataShareHelperManager::GetInstance().Delete(*(m_attendeeUri.get()), predicates);
LOG_INFO("Delete attendee num %{public}d", ret);
}
+ {
+ // delete reminder
+ DataShare::DataSharePredicates predicates;
+ predicates.EqualTo("event_id", id);
+ auto ret = DataShareHelperManager::GetInstance().Delete(*(m_reminderUrl.get()), predicates);
+ LOG_INFO("Delete reminder num %{public}d", ret);
+ }
return ret == 1;
}
@@ -139,11 +160,42 @@ bool Calendar::UpdateEvent(const Event& event)
LOG_ERROR("event id not exist");
return false;
}
+ const auto eventId = event.id.value();
DataShare::DataSharePredicates m_predicates;
- m_predicates.EqualTo("_id", event.id.value());
+ m_predicates.EqualTo("_id", eventId);
auto valueEvent = BuildValueEvent(event, m_id);
auto ret = DataShareHelperManager::GetInstance().Update(*(m_eventUri.get()), m_predicates, valueEvent);
LOG_INFO(" Update code %{public}d", ret);
+ {
+ // delete attendee
+ DataShare::DataSharePredicates predicates;
+ predicates.EqualTo("event_id", eventId);
+ auto ret = DataShareHelperManager::GetInstance().Delete(*(m_attendeeUri.get()), predicates);
+ LOG_INFO("Delete attendee num %{public}d", ret);
+ }
+ for (const auto &attendee : event.attendees) {
+ auto valueAttendee = BuildAttendeeValue(attendee, eventId);
+ auto index = DataShareHelperManager::GetInstance().Insert(*(m_attendeeUri.get()), valueAttendee);
+ LOG_INFO("Update attendee index %{public}d", index);
+ }
+
+ {
+ // delete reminder
+ DataShare::DataSharePredicates predicates;
+ predicates.EqualTo("event_id", eventId);
+ auto ret = DataShareHelperManager::GetInstance().Delete(*(m_reminderUrl.get()), predicates);
+ LOG_INFO("Delete reminder num %{public}d", ret);
+ }
+ if (event.reminderTime) {
+ for (const auto &reminder : event.reminderTime.value()) {
+ DataShare::DataShareValuesBucket valuesBucket;
+ valuesBucket.Put("event_id", eventId);
+ valuesBucket.Put("minutes", reminder);
+ auto index = DataShareHelperManager::GetInstance().Insert(*(m_reminderUrl.get()), valuesBucket);
+ LOG_INFO("Insert reminder index %{public}d", index);
+ }
+ }
+
return ret == 1;
}
@@ -172,6 +224,22 @@ std::vector<Attendee> Calendar::GetAttendeesByEventId(int id)
return attendees;
}
+std::optional<std::vector<int>> Calendar::GetRemindersByEventId(int id)
+{
+ DataShare::DataSharePredicates predicates;
+ predicates.EqualTo("event_id", id);
+ std::vector<std::string> columns = {"event_id", "minutes"};
+ DataShare::DatashareBusinessError error;
+ auto result = DataShareHelperManager::GetInstance().Query(*(m_reminderUrl.get()), predicates, columns, &error);
+ std::vector<int> reminders;
+ auto ret = ResultSetToReminders(reminders, result);
+ if (ret != DataShare::E_OK) {
+ return std::nullopt;
+ }
+ LOG_INFO("query attendee finished");
+ return reminders;
+}
+
std::vector<Event> Calendar::GetEvents(std::shared_ptr<EventFilter> filter, const std::vector<string>& eventKey)
{
std::vector<Event> events;
@@ -199,8 +267,10 @@ std::vector<Event> Calendar::GetEvents(std::shared_ptr<EventFilter> filter, cons
}
ResultSetToEvents(events, result, columns);
for (auto &event : events) {
- auto attendees = GetAttendeesByEventId(event.id.value()); // exist bug id must exist
+ const auto eventId = event.id.value();
+ auto attendees = GetAttendeesByEventId(eventId); // exist bug id must exist
event.attendees = std::move(attendees);
+ event.reminderTime = GetRemindersByEventId(eventId);
}
LOG_INFO("query finished");
return events;
diff --git a/calendarmanager/native/src/native_util.cpp b/calendarmanager/native/src/native_util.cpp
index da3aec9..cb7b45e 100644
--- a/calendarmanager/native/src/native_util.cpp
+++ b/calendarmanager/native/src/native_util.cpp
@@ -279,7 +279,9 @@ int ResultSetToEvents(std::vector<Event> &events, DataShareResultSetPtr &resultS
do {
Event event;
GetValueOptional(resultSet, "_id", event.id);
- GetValue(resultSet, "event_calendar_type", event.type);
+ int type = 0;
+ GetValue(resultSet, "event_calendar_type", type);
+ event.type = static_cast<EventType>(type);
GetValueOptional(resultSet, "title", event.title);
GetValue(resultSet, "dtstart", event.startTime);
GetValue(resultSet, "dtend", event.endTime);
@@ -315,6 +317,27 @@ int ResultSetToAttendees(std::vector<Attendee> &attendees, DataShareResultSetPtr
return 0;
}
+int ResultSetToReminders(std::vector<int> &reminders, DataShareResultSetPtr &resultSet)
+{
+ int rowCount = 0;
+ resultSet->GetRowCount(rowCount);
+ LOG_INFO("GetRowCount is %{public}d", rowCount);
+ if (rowCount <= 0) {
+ return -1;
+ }
+ auto err = resultSet->GoToFirstRow();
+ if (err != DataShare::E_OK) {
+ LOG_ERROR("Failed GoToFirstRow %{public}d", err);
+ return -1;
+ }
+ do {
+ int minutes;
+ GetValue(resultSet, "minutes", minutes);
+ reminders.emplace_back(minutes);
+ } while (resultSet->GoToNextRow() == DataShare::E_OK);
+ return 0;
+}
+
bool IsValidHexString(const std::string& colorStr)
{
if (colorStr.empty()) {
diff --git a/calendarmanager/test/BUILD.gn b/calendarmanager/test/BUILD.gn
index d36dd42..f939cb9 100644
--- a/calendarmanager/test/BUILD.gn
+++ b/calendarmanager/test/BUILD.gn
@@ -29,6 +29,7 @@ ohos_unittest("calendarmanager_test") {
"unittest/src/event_attendee_test.cpp",
"unittest/src/event_filter_test.cpp",
"unittest/src/event_location_test.cpp",
+ "unittest/src/event_reminder_test.cpp",
"unittest/src/event_service_test.cpp",
"unittest/src/get_self_permissions.cpp",
"unittest/src/test_main.cpp",
diff --git a/calendarmanager/test/unittest/src/event_attendee_test.cpp b/calendarmanager/test/unittest/src/event_attendee_test.cpp
index cd7a7a7..da90b1f 100644
--- a/calendarmanager/test/unittest/src/event_attendee_test.cpp
+++ b/calendarmanager/test/unittest/src/event_attendee_test.cpp
@@ -139,7 +139,7 @@ HWTEST_F(EventAttendeeTest, UpdateEventWithAttendee, testing::ext::TestSize.Leve
events = calendar->GetEvents(FilterById({eventId}), {});
ASSERT_EQ(events.size(), 1);
resultEvent = events.at(0);
- CompareAttendeeVec(resultEvent.attendees, testAttendees);
+ CompareAttendeeVec(resultEvent.attendees, newTestAttendees);
}
}
\ No newline at end of file
diff --git a/calendarmanager/test/unittest/src/event_filter_test.cpp b/calendarmanager/test/unittest/src/event_filter_test.cpp
index 780da9c..59734c7 100644
--- a/calendarmanager/test/unittest/src/event_filter_test.cpp
+++ b/calendarmanager/test/unittest/src/event_filter_test.cpp
@@ -95,7 +95,7 @@ HWTEST_F(EventFilterTest, FilterByTitle_test_partialMatch, testing::ext::TestSiz
event.title = title;
auto eventId = calendar->AddEvent(event);
ASSERT_NE(eventId, 0);
- auto events = calendar->GetEvents(FilterByTitle("test"), {});
+ auto events = calendar->GetEvents(FilterByTitle("partialMatch"), {});
ASSERT_FALSE(events.empty());
EXPECT_EQ(events.at(0).title.value(), title);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。