~aleteoryx/muditaos

ref: 624fa2874b0adfb5dc24431cf6b386d4232a7bc3 muditaos/module-apps/application-settings/windows/system/DateAndTimeMainWindow.cpp -rw-r--r-- 5.2 KiB
624fa287 — Kuba Kleczkowski [MOS-373] Fixed missing timezone window 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "DateAndTimeMainWindow.hpp"

#include <application-settings/windows/WindowNames.hpp>

#include <OptionSetting.hpp>
#include <service-time/Constants.hpp>
#include <service-time/api/TimeSettingsApi.hpp>
#include <service-time/service-time/TimeMessage.hpp>

namespace gui
{
    DateAndTimeMainWindow::DateAndTimeMainWindow(app::ApplicationCommon *app, std::string name)
        : BaseSettingsWindow(app, std::move(name))
    {
        setTitle(utils::translate("app_settings_date_and_time"));

        automaticDateAndTimeIsOn = stm::api::isAutomaticDateAndTime();
        timeFormat               = stm::api::timeFormat();
        dateFormat               = stm::api::dateFormat();
        changeDateAndTimeWindow  = window::name::change_date_and_time;
    }

    auto DateAndTimeMainWindow::buildOptionsList() -> std::list<Option>
    {
        std::list<Option> optionList;

        auto addOption = [&](const std::string &text, std::function<bool(Item &)> activatedCallback) {
            optionList.emplace_back(std::make_unique<option::OptionSettings>(
                text, activatedCallback, nullptr, nullptr, option::SettingRightItem::ArrowWhite));
        };
        auto addSwitchOption = [&](const std::string &text,
                                   std::function<bool(Item &)> activatedCallback,
                                   option::SettingRightItem rightItem,
                                   UTF8 textOnRight = UTF8()) {
            optionList.emplace_back(std::make_unique<option::OptionSettings>(
                text,
                activatedCallback,
                [this](Item &item) { return navBarCallback(item); },
                nullptr,
                rightItem,
                false,
                std::move(textOnRight)));
        };

        addSwitchOption(
            utils::translate("app_settings_date_and_time_automatic_date_and_time"),
            [=](Item &item) {
                automaticDateAndTimeIsOn = !automaticDateAndTimeIsOn;
                application->bus.sendUnicast(
                    std::make_shared<stm::message::SetAutomaticDateAndTimeRequest>(automaticDateAndTimeIsOn),
                    service::name::service_time);
                if (!automaticDateAndTimeIsOn) {
                    application->switchWindow(window::name::change_time_zone, nullptr);
                    return true;
                }
                refreshOptionsList();
                return true;
            },
            automaticDateAndTimeIsOn ? option::SettingRightItem::On : option::SettingRightItem::Off);

        if (!automaticDateAndTimeIsOn) {
            addOption(utils::translate("app_settings_date_and_time_change_time_zone"), [=](Item &item) {
                LOG_INFO("switching to %s page", window::name::change_time_zone);
                application->switchWindow(window::name::change_time_zone, nullptr);
                return true;
            });
            addOption(utils::translate("app_settings_date_and_time_change_date_and_time"), [=](Item &item) {
                LOG_INFO("switching to %s page", changeDateAndTimeWindow.c_str());
                application->switchWindow(changeDateAndTimeWindow, nullptr);
                return true;
            });
        }

        addSwitchOption(
            utils::translate("app_settings_date_and_time_time_format"),
            [=](Item &item) {
                timeFormat = (timeFormat == utils::time::Locale::TimeFormat::FormatTime12H)
                                 ? utils::time::Locale::TimeFormat::FormatTime24H
                                 : utils::time::Locale::TimeFormat::FormatTime12H;
                application->bus.sendUnicast(std::make_shared<stm::message::SetTimeFormatRequest>(timeFormat),
                                             service::name::service_time);
                refreshOptionsList();
                return true;
            },
            option::SettingRightItem::Text,
            utils::time::Locale::get_time_format(timeFormat).data());

        addSwitchOption(
            utils::translate("app_settings_date_and_time_date_format"),
            [=](Item &item) {
                dateFormat = (dateFormat == utils::time::Locale::DateFormat::DD_MM_YYYY)
                                 ? utils::time::Locale::DateFormat::MM_DD_YYYY
                                 : utils::time::Locale::DateFormat::DD_MM_YYYY;
                application->bus.sendUnicast(std::make_shared<stm::message::SetDateFormatRequest>(dateFormat),
                                             service::name::service_time);
                refreshOptionsList();
                return true;
            },
            option::SettingRightItem::Text,
            utils::time::Locale::get_date_format(dateFormat).data());

        return optionList;
    }

    bool DateAndTimeMainWindow::navBarCallback(Item &item)
    {
        if (item.focus) {
            setNavBarText(utils::translate(style::strings::common::Switch), nav_bar::Side::Center);
        }
        else {
            setNavBarText(utils::translate(style::strings::common::select), nav_bar::Side::Center);
        }
        return true;
    }
} // namespace gui