#pragma once // Spec: INV-social 15.5 (NotificationsView.ViewModel): load, refresh, load more with merge and dedupe, open and // route, mark read, mark all read. #include #include #include #include #include #include #include "api/DevPlaceApi.hpp" #include "core/AlertMessage.hpp" #include "model/Notifications.hpp" #include "screens/notifications/NotificationRoute.hpp" namespace dp { class NotificationsModel { public: using AlertSink = std::function; explicit NotificationsModel(DevPlaceApi& api, AlertSink alertSink = nullptr); ~NotificationsModel() { *alive_ = false; } NotificationsModel(const NotificationsModel&) = delete; NotificationsModel& operator=(const NotificationsModel&) = delete; const std::optional& notifications() const { return notifications_; } const std::vector& groups() const; bool isLoadingMore() const { return isLoadingMore_; } bool isRefreshing() const { return refreshing_ > 0; } bool isBusy() const { return pending_ > 0; } // A footer spinner is shown while an older page exists and the entry cap is not reached. bool hasMore() const; std::size_t entryCount() const; void load(std::function done = nullptr); void refresh(std::function done = nullptr); void loadMore(); // GET notifications/open/, then reload and recount; `done` gets the route (nullopt on error or no route). void open(const Notification& notification, std::function)> done); void markRead(const std::string& uid); void markAllRead(); // Pure helpers (unit tested). static Notifications merged(const Notifications& existing, const Notifications& older, std::vector* duplicates = nullptr); static Notifications applyingRead(const Notifications& n, const std::set& uids); private: void refreshUnreadCount(std::function done); DevPlaceApi& api_; AlertSink alertSink_; std::optional notifications_; bool isLoadingMore_ = false; bool loadMoreFailed_ = false; int pending_ = 0; int refreshing_ = 0; std::shared_ptr alive_ = std::make_shared(true); }; } // namespace dp