2023-03-20 10:00:01 +00:00
|
|
|
//
|
|
|
|
// LockableView.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/20/23.
|
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct LockableView<Content: View, LockedContent: View>: View {
|
|
|
|
@Binding var locksInBackground: Bool
|
|
|
|
|
|
|
|
let content: () -> Content
|
|
|
|
|
|
|
|
let lockedContent: () -> LockedContent
|
|
|
|
|
2023-04-05 14:31:17 +00:00
|
|
|
let unlockBlock: () async -> Bool
|
2023-03-25 15:47:08 +00:00
|
|
|
|
2023-03-20 10:00:01 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
|
|
|
|
2023-03-25 15:47:08 +00:00
|
|
|
@ObservedObject private var lock: Lock = .shared
|
2023-03-20 10:00:01 +00:00
|
|
|
|
2023-04-05 14:31:17 +00:00
|
|
|
@Binding private var state: Lock.State
|
|
|
|
|
|
|
|
init(
|
|
|
|
locksInBackground: Binding<Bool>,
|
|
|
|
content: @escaping () -> Content,
|
|
|
|
lockedContent: @escaping () -> LockedContent,
|
|
|
|
unlockBlock: @escaping () async -> Bool
|
|
|
|
) {
|
|
|
|
_locksInBackground = locksInBackground
|
|
|
|
self.content = content
|
|
|
|
self.lockedContent = lockedContent
|
|
|
|
self.unlockBlock = unlockBlock
|
|
|
|
|
|
|
|
_state = .init {
|
|
|
|
Lock.shared.state
|
2023-03-25 15:47:08 +00:00
|
|
|
} set: {
|
2023-04-05 14:31:17 +00:00
|
|
|
Lock.shared.state = $0
|
2023-03-25 15:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-20 10:00:01 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2023-03-25 15:47:08 +00:00
|
|
|
ZStack {
|
|
|
|
content()
|
2023-04-05 14:31:17 +00:00
|
|
|
if locksInBackground && state != .none {
|
2023-03-20 10:00:01 +00:00
|
|
|
lockedContent()
|
|
|
|
}
|
|
|
|
}.onChange(of: scenePhase, perform: onScenePhase)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func onScenePhase(_ scenePhase: ScenePhase) {
|
|
|
|
switch scenePhase {
|
|
|
|
case .active:
|
|
|
|
unlockIfNeeded()
|
|
|
|
|
2023-04-05 14:31:17 +00:00
|
|
|
case .inactive:
|
|
|
|
if state == .none {
|
|
|
|
state = .covered
|
|
|
|
}
|
|
|
|
|
2023-03-31 21:47:29 +00:00
|
|
|
case .background:
|
2023-03-20 10:00:01 +00:00
|
|
|
lockIfNeeded()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lockIfNeeded() {
|
|
|
|
guard locksInBackground else {
|
|
|
|
return
|
|
|
|
}
|
2023-04-05 14:31:17 +00:00
|
|
|
state = .locked
|
2023-03-20 10:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unlockIfNeeded() {
|
2023-03-20 13:12:42 +00:00
|
|
|
guard locksInBackground else {
|
2023-04-05 14:31:17 +00:00
|
|
|
state = .none
|
2023-03-20 10:00:01 +00:00
|
|
|
return
|
|
|
|
}
|
2023-04-05 14:31:17 +00:00
|
|
|
switch state {
|
|
|
|
case .none:
|
|
|
|
break
|
|
|
|
|
|
|
|
case .covered:
|
|
|
|
state = .none
|
|
|
|
|
|
|
|
case .locked:
|
|
|
|
Task { @MainActor in
|
|
|
|
guard await unlockBlock() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
state = .none
|
|
|
|
}
|
2023-03-20 10:00:01 +00:00
|
|
|
}
|
2023-03-25 15:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:19:47 +00:00
|
|
|
private final class Lock: ObservableObject {
|
2023-04-05 14:31:17 +00:00
|
|
|
enum State {
|
|
|
|
case none
|
|
|
|
|
|
|
|
case covered
|
|
|
|
|
|
|
|
case locked
|
|
|
|
}
|
|
|
|
|
2023-03-25 15:47:08 +00:00
|
|
|
static let shared = Lock()
|
|
|
|
|
2023-04-05 14:31:17 +00:00
|
|
|
@Published var state: State = .locked
|
2023-03-25 15:47:08 +00:00
|
|
|
|
|
|
|
private init() {
|
2023-03-20 10:00:01 +00:00
|
|
|
}
|
|
|
|
}
|