// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md #include "NotesListModel.hpp" #include "module-apps/application-notes/widgets/NotesItem.hpp" #include "module-apps/application-notes/style/NotesListStyle.hpp" #include "module-apps/application-notes/windows/NotesOptions.hpp" #include "module-apps/application-notes/data/NoteSwitchData.hpp" #include #include #include #include namespace app::notes { NotesListItemProvider::NotesListItemProvider(ApplicationCommon *app) : DatabaseModel(app) {} NotesListModel::NotesListModel(app::ApplicationCommon *app, std::shared_ptr notesRepository) : NotesListItemProvider(app), notesRepository{std::move(notesRepository)} {} unsigned int NotesListModel::requestRecordsCount() { return recordsCount; } bool NotesListModel::updateRecords(std::vector records) { DatabaseModel::updateRecords(std::move(records)); list->onProviderDataUpdate(); return true; } unsigned int NotesListModel::getMinimalItemSpaceRequired() const { return style::list::item::Height; } gui::ListItem *NotesListModel::getItem(gui::Order order) { std::shared_ptr note = getRecord(order); if (!note) { return nullptr; } auto item = new gui::NotesItem(note); item->activatedCallback = [this, note](gui::Item &) { application->switchWindow(gui::name::window::note_preview, std::make_unique(note)); return true; }; item->inputCallback = [this, note = note.get()](gui::Item &, const gui::InputEvent &event) { if (event.isShortRelease(gui::KeyCode::KEY_LF)) { application->switchWindow( window::name::option_window, std::make_unique(noteListOptions(application, *note, *notesRepository))); } return false; }; return item; } void NotesListModel::requestRecords(uint32_t offset, uint32_t limit) { notesRepository->get( offset, limit, [this](const std::vector &records, unsigned int notesRepoCount) { return onNotesRetrieved(records, notesRepoCount); }); } bool NotesListModel::onNotesRetrieved(const std::vector &records, unsigned int notesRepoCount) { if (recordsCount != notesRepoCount) { recordsCount = notesRepoCount; list->reSendLastRebuildRequest(); return false; } return updateRecords(records); } } // namespace app::notes