forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionSheetDaxDialogViewController.swift
114 lines (95 loc) · 4.31 KB
/
ActionSheetDaxDialogViewController.swift
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
//
// ActionSheetDaxDialogViewController.swift
// DuckDuckGo
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
import Core
protocol ActionSheetDaxDialogDelegate: NSObjectProtocol {
func actionSheetDaxDialogDidConfirmAction(controller: ActionSheetDaxDialogViewController)
}
class ActionSheetDaxDialogViewController: UIViewController {
@IBOutlet weak var highlightCutOutView: HighlightCutOutView!
@IBOutlet weak var containerHeight: NSLayoutConstraint!
weak var daxDialogViewController: DaxDialogViewController?
weak var alertController: UIAlertController?
@IBOutlet weak var actionSheetContainerView: UIView!
weak var delegate: ActionSheetDaxDialogDelegate?
var spec: DaxDialogs.ActionSheetSpec?
override func viewDidLoad() {
super.viewDidLoad()
daxDialogViewController?.message = spec?.message
if let spec = spec {
let alertController = UIAlertController()
alertController.addAction(title: spec.confirmAction, style: spec.isConfirmActionDestructive ? .destructive : .default) {
Pixel.fire(pixel: spec.confirmActionPixelName)
self.delegate?.actionSheetDaxDialogDidConfirmAction(controller: self)
self.dismiss(animated: false)
}
alertController.addAction(title: spec.cancelAction, style: .cancel) {
Pixel.fire(pixel: spec.cancelActionPixelName)
self.dismiss(animated: true)
}
addChild(alertController)
alertController.view.translatesAutoresizingMaskIntoConstraints = false
actionSheetContainerView.addSubview(alertController.view)
NSLayoutConstraint.activate([
alertController.view.centerXAnchor.constraint(equalTo: actionSheetContainerView.centerXAnchor, constant: 0),
alertController.view.topAnchor.constraint(equalTo: actionSheetContainerView.topAnchor, constant: 0),
alertController.view.bottomAnchor.constraint(equalTo: actionSheetContainerView.bottomAnchor, constant: 0)
])
}
highlightCutOutView.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
containerHeight.constant = daxDialogViewController?.calculateHeight() ?? 0
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let spec = spec {
Pixel.fire(pixel: spec.displayedPixelName)
}
containerHeight.constant = daxDialogViewController?.calculateHeight() ?? 0
daxDialogViewController?.start()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.destination is DaxDialogViewController {
daxDialogViewController = segue.destination as? DaxDialogViewController
}
}
@IBAction func onTapOutside(_ sender: Any) {
guard daxDialogViewController?.isFinished ?? true else {
daxDialogViewController?.finish()
return
}
self.dismiss(animated: true)
if let spec = spec {
Pixel.fire(pixel: spec.cancelActionPixelName)
}
}
}
extension MainViewController: ActionSheetDaxDialogDelegate {
func actionSheetDaxDialogDidConfirmAction(controller: ActionSheetDaxDialogViewController) {
forgetAllWithAnimation(showNextDaxDialog: true)
}
}
extension TabSwitcherViewController: ActionSheetDaxDialogDelegate {
func actionSheetDaxDialogDidConfirmAction(controller: ActionSheetDaxDialogViewController) {
delegate?.tabSwitcherDidRequestForgetAll(tabSwitcher: self)
}
}