~aleteoryx/muditaos

ref: 3cc3f50f7b9364ffac38349238cd6d9aa243dc23 muditaos/module-apps/apps-common/popups/TetheringConfirmationPopup.cpp -rw-r--r-- 3.2 KiB
3cc3f50f — Maciej Gibowicz [BH-1809][BH-1835] Add date format setting 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TetheringConfirmationPopup.hpp"
#include "DialogMetadataMessage.hpp"
#include "ApplicationCommon.hpp"

#include <system/messages/TetheringQuestionRequest.hpp>
#include <service-appmgr/Controller.hpp>

namespace gui
{
    TetheringConfirmationPopup::TetheringConfirmationPopup(app::ApplicationCommon *app, const std::string &name)
        : DialogYesNo{app, name}
    {}

    void TetheringConfirmationPopup::onBeforeShow(ShowMode mode, [[maybe_unused]] SwitchData *data)
    {
        DialogMetadata metadata;
        metadata.title  = utils::translate("tethering");
        metadata.text   = utils::translate("tethering_enable_question");
        metadata.icon   = "tethering_128px_W_G";
        metadata.action = [this]() {
            // check if there is any unsaved data in any window in stack
            bool IsDataUnsaved   = false;
            auto windowStackSize = application->getSizeOfWindowsStack();
            for (auto windowOnStackNumber = windowStackSize; windowOnStackNumber > 0; windowOnStackNumber--) {
                if (const auto &previousWindowName = application->getPreviousWindow(windowOnStackNumber);
                    previousWindowName.has_value()) {
                    const auto previousWindow = application->getWindow(previousWindowName.value());
                    IsDataUnsaved             = previousWindow->isAnyUnsavedUserDataInWindow() ? true : IsDataUnsaved;
                }
            }

            // what is the expected action when YES is selected
            auto onYesAction = [this]() {
                application->bus.sendUnicast(std::make_shared<sys::TetheringEnabledResponse>(),
                                             service::name::system_manager);
                app::manager::Controller::sendAction(application, app::manager::actions::Home);
                return true;
            };

            // if there are any unsaved changes, open the appropriate popup, otherwise proceed as usual
            if (IsDataUnsaved) {
                showDialogUnsavedChanges(onYesAction);
                return true;
            }
            onYesAction();
            return true;
        };
        auto msg = std::make_unique<DialogMetadataMessage>(std::move(metadata));
        DialogYesNo::onBeforeShow(mode, msg.get());
    }

    void TetheringConfirmationPopup::showDialogUnsavedChanges(std::function<bool()> whatToDoOnYes)
    {
        // Show a popup warning about possible data loss
        auto metaData = std::make_unique<gui::DialogMetadataMessage>(
            gui::DialogMetadata{utils::translate("unsaved_changes"),
                                "delete_128px_W_G",
                                utils::translate("exit_without_saving"),
                                "",
                                [=]() -> bool {
                                    application->returnToPreviousWindow(); // To exit this popup
                                    return whatToDoOnYes();
                                }});

        application->switchWindow(gui::window::name::dialog_yes_no, std::move(metaData));
    }
} // namespace gui