summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/UserInterfaceInteractionRestriction.swift
blob: a8f1455b5fe4b7bdb4b14060cf2a1f8498702a40 (plain)
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
//
//  UserInterfaceInteractionRestriction.swift
//  MullvadVPN
//
//  Created by pronebird on 20/03/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Combine
import Foundation

/// A protocol describing a common interface for the implementations of user interaction restriction
protocol UserInterfaceInteractionRestrictionProtocol {
    /// Raise the user interface interaction restrictions
    func lift(animated: Bool)

    /// Lift the user interface interaction restrictions
    func raise(animated: Bool)
}

/// A counter based user interface interaction restriction implementation
class UserInterfaceInteractionRestriction<S: Scheduler>
    : UserInterfaceInteractionRestrictionProtocol
{
    typealias Action = (_ disableUserInteraction: Bool, _ animated: Bool) -> Void

    private let action: Action
    private let scheduler: S
    private var counter: UInt = 0

    init(scheduler: S, action: @escaping Action) {
        self.action = action
        self.scheduler = scheduler
    }

    func raise(animated: Bool) {
        scheduler.schedule {
            if self.counter == 0 {
                self.action(false, animated)
            }
            self.counter += 1
        }
    }

    func lift(animated: Bool) {
        scheduler.schedule {
            guard self.counter > 0 else { return }

            self.counter -= 1
            if self.counter == 0 {
                self.action(true, animated)
            }
        }
    }
}

/// A user interface restriction implementation that simply combines multiple child restrictions
/// into one and automatically forwards all calls to them in the order in which they are given to
/// the initializer.
class CompoundUserInterfaceInteractionRestriction: UserInterfaceInteractionRestrictionProtocol {
    private let restrictions: [UserInterfaceInteractionRestrictionProtocol]

    init(restrictions: [UserInterfaceInteractionRestrictionProtocol]) {
        self.restrictions = restrictions
    }

    func lift(animated: Bool) {
        restrictions.forEach { $0.lift(animated: animated) }
    }

    func raise(animated: Bool) {
        restrictions.forEach { $0.raise(animated: animated) }
    }
}

extension Publisher {
    func restrictUserInterfaceInteraction(
        with restriction: UserInterfaceInteractionRestrictionProtocol,
        animated: Bool
    ) -> Publishers.HandleEvents<Self>
    {
        return handleEvents(receiveSubscription: { _ in
            restriction.raise(animated: animated)
        }, receiveCompletion: { _ in
            restriction.lift(animated: animated)
        }, receiveCancel: { () in
            restriction.lift(animated: animated)
        })
    }
}