~aleteoryx/muditaos

ref: 8dccfeef72f47319bff50fb9b3f3c6a97fc57c66 muditaos/module-apps/application-notes/windows/NotePreviewWindow.cpp -rw-r--r-- 5.2 KiB
8dccfeef — rrandomsky [MOS-1042] Fixed inability to save a contact that did not previously have a phone number 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotePreviewWindow.hpp"

#include <application-notes/ApplicationNotes.hpp>
#include <module-apps/application-notes/data/NoteSwitchData.hpp>
#include <module-apps/application-notes/style/NotePreviewStyle.hpp>
#include <module-apps/application-notes/windows/NotesOptions.hpp>
#include <apps-common/messages/OptionsWindow.hpp>
#include <apps-common/options/OptionWindowName.hpp>

#include <i18n/i18n.hpp>
#include <time/time_conversion_factory.hpp>

#include <Style.hpp>

namespace app::notes
{
    NotePreviewWindow::NotePreviewWindow(app::ApplicationCommon *app,
                                         std::unique_ptr<NotePreviewWindowContract::Presenter> &&windowPresenter)
        : AppWindow(app, gui::name::window::note_preview), presenter(std::move(windowPresenter))
    {
        presenter->attach(this);
        buildInterface();
    }

    NotePreviewWindow::~NotePreviewWindow() noexcept
    {
        destroyInterface();
    }

    void NotePreviewWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void NotePreviewWindow::destroyInterface()
    {
        erase();
        date = nullptr;
        note = nullptr;
    }

    void NotePreviewWindow::buildInterface()
    {
        AppWindow::buildInterface();

        setTitle(utils::translate("app_notes_title_main"));

        namespace previewStyle = app::notes::style::preview;
        date                   = new gui::Label(
            this, previewStyle::LeftMargin, previewStyle::TopMargin, previewStyle::Width, previewStyle::date::Height);
        date->setEdges(gui::RectangleEdge::None);
        date->setFont(::style::window::font::small);

        note = new gui::Text(this,
                             previewStyle::LeftMargin,
                             previewStyle::TopMargin + previewStyle::date::Height + previewStyle::text::TopMargin,
                             previewStyle::Width,
                             previewStyle::text::Height);
        note->setEdges(gui::RectangleEdge::None);
        note->setFont(::style::window::font::medium);
        note->setAlignment(gui::Alignment{gui::Alignment::Vertical::Top});
        note->setPenFocusWidth(::style::window::default_border_focus_w);
        note->setPenWidth(::style::window::default_border_rect_no_focus);
        note->setEditMode(gui::EditMode::Scroll);
        note->setCursorStartPosition(gui::CursorStartPosition::DocumentBegin);

        navBar->setActive(gui::nav_bar::Side::Left, true);
        navBar->setText(gui::nav_bar::Side::Left, utils::translate(::style::strings::common::options));

        navBar->setActive(gui::nav_bar::Side::Center, true);
        navBar->setText(gui::nav_bar::Side::Center, utils::translate("app_notes_edit"));

        navBar->setActive(gui::nav_bar::Side::Right, true);
        navBar->setText(gui::nav_bar::Side::Right, utils::translate(::style::strings::common::back));

        setFocusItem(note);
    }

    void NotePreviewWindow::onBeforeShow(gui::ShowMode mode, gui::SwitchData *data)
    {
        auto previewData = dynamic_cast<NoteSwitchData *>(data);
        if (previewData == nullptr) {
            if (mode == gui::ShowMode::GUI_SHOW_RETURN) {
                updatePreview();
            }
            return;
        }

        notesRecord = previewData->getRecord();
        updatePreview();
    }

    void NotePreviewWindow::updatePreview()
    {
        if (notesRecord) {
            setEditDateText(notesRecord->date);
            note->setText(notesRecord->snippet);
        }
        else {
            LOG_ERROR("Unable to update the preview: the note does not exist.");
        }
    }

    void NotePreviewWindow::setEditDateText(std::uint32_t timestamp)
    {
        using namespace utils::time;
        auto dateTime = TimestampFactory().createTimestamp(TimestampType::DateOrTime, timestamp);
        auto dt       = dynamic_cast<DateTime *>(dateTime.get());
        if (dt == nullptr) {
            return;
        }
        std::ostringstream dateText;
        dateText << utils::translate("app_notes_edited") << ": ";
        if (dt->isToday()) {
            dateText << utils::translate("common_today") << ", ";
        }
        else if (dt->isYesterday()) {
            dateText << utils::translate("common_yesterday") << ", ";
        }
        dateText << *dt;
        date->setText(dateText.str());
    }

    bool NotePreviewWindow::onInput(const gui::InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            if (inputEvent.is(gui::KeyCode::KEY_ENTER)) {
                application->switchWindow(gui::name::window::note_edit, std::make_unique<NoteSwitchData>(notesRecord));
            }
            else if (inputEvent.is(gui::KeyCode::KEY_LF)) {
                application->switchWindow(window::name::option_window,
                                          std::make_unique<gui::OptionsWindowOptions>(notePreviewOptions(
                                              application, *notesRecord, presenter->getRepository(), note)));
            }
        }
        return AppWindow::onInput(inputEvent);
    }
} // namespace app::notes