~aleteoryx/muditaos

ref: b9661350f8dd5180b5f8fe7c758e5ae16452f7cf muditaos/module-sys/PhoneModes/Observer.hpp -rw-r--r-- 1.3 KiB
b9661350 — Piotr Tański [EGD-5697] Framework for phone modes introduced 5 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <functional>
#include <stdexcept>

#include "Common.hpp"

namespace sys
{
    class Service; // Forward declaration
} // namespace sys

namespace sys::phone_modes
{
    class Observer
    {
      public:
        using OnChangeCallback   = std::function<void(PhoneMode, Tethering)>;
        using OnCompleteCallback = std::function<void()>;
        using OnErrorCallback    = std::function<void(const std::exception &)>;

        void connect(Service *owner);
        void subscribe(OnChangeCallback &&onChange,
                       OnCompleteCallback &&onComplete = {},
                       OnErrorCallback &&onError       = {}) noexcept;

        bool isInMode(PhoneMode mode) const noexcept;
        bool isTetheringOn() const noexcept;

      private:
        sys::MessagePointer handlePhoneModeChange(PhoneModeChanged *message);
        void onPhoneModeChanged();

        OnChangeCallback onChangeCallback;
        OnCompleteCallback onCompleteCallback;
        OnErrorCallback onErrorCallback;
        PhoneMode phoneMode     = PhoneMode::Connected;
        Tethering tetheringMode = Tethering::Off;
    };
} // namespace sys::phone_modes