~aleteoryx/muditaos

muditaos/module-apps/application-messages/widgets/ThreadItem.cpp -rw-r--r-- 3.9 KiB
a405cad6Aleteoryx trim readme 6 days 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
// 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 "MessagesStyle.hpp"
#include "ThreadItem.hpp"

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

namespace gui
{
    ThreadItem *ThreadItem::makeThreadItem(const std::shared_ptr<ThreadListStruct> &threadStruct)
    {
        ThreadItem *threadItem = threadStruct->thread->isUnread() ? new ThreadItemNotRead() : new ThreadItem();
        threadItem->setThreadItem(threadStruct);
        return threadItem;
    }

    auto ThreadItem::getThreadName() const -> UTF8
    {
        std::ostringstream name;
        name << contact->getText();
        if (const auto &importance = numberImportance->getText(); !importance.empty()) {
            name << ' ' << importance;
        }
        return name.str();
    }

    void ThreadItem::setPreview()
    {
        switch (threadStruct->thread->type) {
        case SMSType::DRAFT:
            snippetPrefix->setText(utils::translate("app_messages_thread_draft"));
            break;
        case SMSType::FAILED:
            snippetPrefix->setText(utils::translate("app_messages_thread_not_sent"));
            break;
        case SMSType::OUTBOX:
        case SMSType::QUEUED:
            snippetPrefix->setText(utils::translate("app_messages_thread_you"));
            break;
        default:
            break;
        }
        snippet->setText(threadStruct->thread->snippet);
    }

    void ThreadItem::setThreadItem(std::shared_ptr<ThreadListStruct> _threadStruct)
    {
        threadStruct = std::move(_threadStruct);

        setContactName(getNumberImportance());
        using namespace utils::time;
        timestamp->setText(
            TimestampFactory().createTimestamp(TimestampType::DateOrTime, threadStruct->thread->date)->str());
        setPreview();
    }

    auto ThreadItem::getNumberImportance() -> std::optional<long>
    {
        if (threadStruct->contact->numbers.size() < 2) {
            // At least two phone numbers are needed to indicate a number importance.
            return std::nullopt;
        }

        const auto numberRec     = threadStruct->number;
        const auto &phoneNumbers = threadStruct->contact->numbers;

        const auto it = std::find_if(phoneNumbers.begin(), phoneNumbers.end(), [&numberRec](const auto &phoneNumber) {
            return phoneNumber.number == *numberRec;
        });

        if (it == phoneNumbers.end()) {
            return std::nullopt;
        }
        return std::distance(phoneNumbers.begin(), it) + 1;
    }

    void ThreadItem::setContactName(std::optional<long int> numberImportance)
    {
        auto contactRecord = threadStruct->contact;
        if (!contactRecord || contactRecord->isTemporary()) {
            contact->setText(threadStruct->number->getFormatted());
        }
        else {
            contact->setText(threadStruct->contact->getFormattedName());
            if (numberImportance.has_value()) {
                displayNumberImportance(numberImportance.value());
            }
        }
    }

    ThreadItemWithIndicator::ThreadItemWithIndicator(const UTF8 &indicatorName) : ThreadItem()
    {
        indicator = new gui::Image(this, 0, 0, indicatorName);
    }

    void ThreadItemWithIndicator::onDimensionChangedBottom(const BoundingBox & /*oldDim*/, const BoundingBox &newDim)
    {
        namespace msgStyle = style::messages::threadItem;

        const auto indicatorWidth = indicator->getSize(gui::Axis::X) + 6;
        resizeSnippet(newDim, indicatorWidth);
        indicator->setPosition(msgStyle::leftMargin,
                               newDim.h / 2 + snippet->getSize(gui::Axis::Y) / 2 -
                                   indicator->getSize(gui::Axis::Y) / 2); // align with text
    }

    ThreadItemNotRead::ThreadItemNotRead() : ThreadItemWithIndicator(ThreadItemNotRead::indicatorName)
    {}
} /*namespace gui*/