-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugViewController.swift
More file actions
128 lines (103 loc) · 4.77 KB
/
DebugViewController.swift
File metadata and controls
128 lines (103 loc) · 4.77 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// DebugViewController.swift
// PippinDebugging
//
// Created by Andrew McKnight on 7/31/17.
// Copyright © 2019 Two Ring Software. All rights reserved.
//
import Anchorage
import Pippin
import UIKit
protocol DebugViewControllerDelegate {
func debugViewControllerDisplayedMenu(debugViewController: DebugViewController)
func debugViewControllerHidMenu(debugViewController: DebugViewController)
}
public class DebugViewController: UIViewController {
private var delegate: DebugViewControllerDelegate!
private var environment: Environment
private var debugMenu: UIView!
private var displayButton: UIButton!
private var assetBundle: Bundle?
init(delegate: DebugViewControllerDelegate, environment: Environment, assetBundle: Bundle? = nil, buttonTintColor: UIColor, buttonStartLocation: CGPoint, appControlPanel: UIView? = nil) {
self.delegate = delegate
self.environment = environment
self.assetBundle = assetBundle
super.init(nibName: nil, bundle: nil)
setUpUI(buttonTintColor: buttonTintColor, buttonStartLocation: buttonStartLocation, appControlPanel: appControlPanel)
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@objc extension DebugViewController {
func closePressed() {
debugMenu.isHidden = true
displayButton.isHidden = false
delegate.debugViewControllerHidMenu(debugViewController: self)
}
func displayPressed() {
debugMenu.isHidden = false
displayButton.isHidden = true
delegate.debugViewControllerDisplayedMenu(debugViewController: self)
}
func draggedDisplayButton(recognizer: UIPanGestureRecognizer) {
guard let button = recognizer.view else { return }
let translation = recognizer.translation(in: view)
button.center = CGPoint(x: button.center.x + translation.x, y: button.center.y + translation.y)
recognizer.setTranslation(.zero, in: view)
}
}
private extension DebugViewController {
func setUpUI(buttonTintColor: UIColor, buttonStartLocation: CGPoint, appControlPanel: UIView? = nil) {
debugMenu = UIView(frame: .zero)
debugMenu.isHidden = true
debugMenu.backgroundColor = .white
view.addSubview(debugMenu)
debugMenu.fillSuperview()
let scrollView = UIScrollView(frame: .zero)
debugMenu.addSubview(scrollView)
scrollView.fillSuperview()
let stackWidthSizingView = UIView(frame: .zero)
debugMenu.addSubview(stackWidthSizingView)
stackWidthSizingView.horizontalAnchors == debugMenu.horizontalAnchors
stackWidthSizingView.heightAnchor == 0
stackWidthSizingView.topAnchor == debugMenu.topAnchor
var controlPanels = [UIView]()
let debuggingControls: [UIView?] = [
environment.model?.debuggingControlPanel(),
environment.activityIndicator?.debuggingControlPanel(),
environment.alerter?.debuggingControlPanel(),
environment.crashReporter?.debuggingControlPanel(),
environment.locator?.debuggingControlPanel(),
environment.logger?.debuggingControlPanel(),
environment.touchVisualizer?.debuggingControlPanel(),
environment.bugReporter?.debuggingControlPanel(),
]
debuggingControls.forEach {
if let view = $0 {
controlPanels.append(view)
}
}
let closeButton = UIButton(frame: .zero)
closeButton.configure(title: "Close", target: self, selector: #selector(closePressed))
controlPanels.append(closeButton)
if let appControlPanel = appControlPanel {
controlPanels.insert(appControlPanel, at: 0)
}
let stack = UIStackView(arrangedSubviews: controlPanels)
stack.axis = .vertical
stack.distribution = .equalSpacing
scrollView.addSubview(stack)
stack.edgeAnchors == scrollView.edgeAnchors
stack.widthAnchor == stackWidthSizingView.widthAnchor
displayButton = UIButton.button(withImageSetName: "debug", emphasisSuffix: "-pressed", tintColor: buttonTintColor, target: self, selector: #selector(displayPressed), imageBundle: assetBundle)
let size: CGFloat = 50
displayButton.frame = CGRect(x: buttonStartLocation.x, y: buttonStartLocation.y, width: size, height: size)
displayButton.layer.cornerRadius = size / 2
displayButton.layer.borderWidth = 2
displayButton.layer.borderColor = buttonTintColor.cgColor
view.addSubview(displayButton)
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(draggedDisplayButton))
displayButton.addGestureRecognizer(panGestureRecognizer)
}
}