1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:57:35 +00:00

Calendar/EventManager: Store events as structs

Previously, the EventManager stored the calendar events as a raw
JsonArray of objects. Now, we parse the JSON into a Vector<Event>
structure and store that in the EventManager. This makes it easier to
access the events from the outside, as you now don't have to know the
JSON structure anymore.
This commit is contained in:
david072 2023-10-28 10:30:42 +02:00 committed by Andrew Kaster
parent 7df936b660
commit 887f040d0e
4 changed files with 94 additions and 50 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, the SerenityOS developers.
* Copyright (c) 2023, David Ganz <david.g.ganz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,11 +11,18 @@
#include <AK/JsonValue.h>
#include <AK/Noncopyable.h>
#include <AK/OwnPtr.h>
#include <LibCore/DateTime.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Window.h>
namespace Calendar {
struct Event {
String summary;
Core::DateTime start;
Core::DateTime end;
};
class EventManager {
AK_MAKE_NONCOPYABLE(EventManager);
AK_MAKE_NONMOVABLE(EventManager);
@ -29,18 +37,21 @@ public:
ErrorOr<void> save(FileSystemAccessClient::File& file);
ErrorOr<void> load_file(FileSystemAccessClient::File& file);
ErrorOr<void> add_event(JsonObject);
void set_events(JsonArray events);
void add_event(Event);
void set_events(Vector<Event>);
void clear() { m_events.clear(); }
JsonArray const& events() const { return m_events; }
Span<Event const> events() const { return m_events.span(); }
Function<void()> on_events_change;
private:
explicit EventManager();
JsonArray m_events;
ErrorOr<JsonArray> serialize_events();
ErrorOr<Vector<Event>> deserialize_events(JsonArray const& json);
Vector<Event> m_events;
String m_current_filename;
bool m_dirty { false };