~aleteoryx/muditaos

ref: a405cad694b867fcd2498984830bd97d4b9bde2f muditaos/module-apps/apps-common/locks/widgets/Lock.hpp -rw-r--r-- 3.2 KiB
a405cad6Aleteoryx trim readme 8 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <vector>
#include <string>
#include <functional>
#include <limits>

namespace locks
{
    class PhoneLockHandler;
    class SimLockHandler;

    class Lock
    {
      public:
        enum class LockState
        {
            Unlocked,
            InputRequired,
            InputInvalid,
            Blocked,
            NewInputRequired,
            NewInputConfirmRequired,
            NewInputInvalid,
            ErrorOccurred
        };

        [[nodiscard]] LockState getState() const noexcept
        {
            return lockState;
        }
        [[nodiscard]] unsigned getMaxInputSize() const noexcept
        {
            return maxInputSize;
        }
        /// returns current position of a Input character to be inserted
        [[nodiscard]] unsigned getCharCount() const noexcept
        {
            return inputValue.size();
        }
        [[nodiscard]] bool canPut() const noexcept
        {
            return getCharCount() < getMaxInputSize();
        }
        [[nodiscard]] bool canVerify() const noexcept
        {
            return getCharCount() >= minInputSize;
        }
        [[nodiscard]] std::vector<unsigned> getInput() const
        {
            return inputValue;
        }
        [[nodiscard]] unsigned getAttemptsLeft() const noexcept
        {
            return attemptsLeft;
        }
        [[nodiscard]] bool isState(LockState state) const noexcept
        {
            return lockState == state;
        }
        [[nodiscard]] const std::string &getLockName() const noexcept
        {
            return lockName;
        }
        [[nodiscard]] const std::string &getNextUnlockAttemptFormattedTime() const noexcept
        {
            return nextUnlockAttemptFormattedTime;
        }
        void setNextUnlockAttemptFormattedTime(const std::string &time);

        void putNextChar(unsigned c);
        /// removes a last character passed to Lock via putNextChar. The last character can not be popped
        void popChar();
        /// clear all characters passed to the Lock
        void clearAttempt() noexcept;
        /// consumes LockState::InputInvalid state and LockState::NewInputInvalid
        void consumeState() noexcept;

        explicit Lock(LockState state, unsigned attemptsLeft = unlimitedNumOfAttempts)
            : lockState{state}, attemptsLeft{attemptsLeft}
        {}

      private:
        std::string lockName;
        LockState lockState       = LockState::Unlocked;
        unsigned attemptsLeft     = 0;
        std::string nextUnlockAttemptFormattedTime;

        std::vector<unsigned> inputValue;
        unsigned maxInputSize = defaultInputSize;
        unsigned minInputSize = defaultInputSize;

        static constexpr unsigned defaultInputSize       = 4;
        static constexpr unsigned unlimitedNumOfAttempts = std::numeric_limits<unsigned>::max();

        void setInputSizeBounds(unsigned _minInputSize, unsigned _maxInputSize)
        {
            minInputSize = _minInputSize;
            maxInputSize = _maxInputSize;
        }

        friend class PhoneLockHandler;
        friend class SimLockHandler;
    };
} // namespace locks