NumberPopupViewController.swift
7.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// NumberPopupViewController.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 15/7/22.
//
import UIKit
protocol NumbersPopupDelegate {
func numbersPopupTapped(_ sender: Any)
func presentedPopup()
func dismissedPopup()
func optionSelected(_ option: String)
}
@objc public class NumberPopupViewController: UIViewController, UIPopoverPresentationControllerDelegate {
@IBOutlet weak var popupView: UIView!
@IBOutlet weak var headerLabel: UILabel!
@IBOutlet weak var closeButton: UIButton!
@IBOutlet weak var tableView: UITableView!
let uiscreen: CGRect = UIScreen.main.bounds
var delegateBallon: NumbersPopupDelegate?
var numbersList: Array<String> = []
var headerText: String = "Αποστολέας"
var widthPopup: Int = 0
var heightPopup: Int = 0
func InitWithController(controller: UIViewController, numbersList: Array<String>, sender: Any, delegate: NumbersPopupDelegate, headerText: String){
//print("Ok InitWithController sender ")
delegateBallon = delegate
// set the presentation style
self.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
// set up the popover presentation controller
// self.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
self.popoverPresentationController?.delegate = self
self.popoverPresentationController?.sourceView = (sender as! UIView ) // button
//--- Better center of the arrow
// let rect: CGRect = (sender as AnyObject).bounds
// let rectFixed: CGRect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width-10, height: rect.size.height)
// self.popoverPresentationController?.sourceRect = rectFixed
self.popoverPresentationController?.backgroundColor = UIColor.clear
// self.popoverPresentationController?.backgroundColor = UIColor(red: 0.22, green: 0.32, blue: 0.40, alpha: 0.58)
DispatchQueue.main.async {
// present the popover
controller.present(self, animated: true, completion: nil)
}
self.numbersList = numbersList
self.headerText = headerText
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
public override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// setupScreens()
tableView.delegate = self
tableView.dataSource = self
popupView.clipsToBounds = true
popupView.layer.cornerRadius = 12
popupView.layer.maskedCorners = [ .layerMinXMinYCorner, .layerMaxXMinYCorner] // Top left, right corner radius
self.preferredContentSize = CGSize(width: widthPopup, height: heightPopup)
closeButton.setImage(UIImage(named: "ic_close_3.png", in: MyEmptyClass.resourceBundle(), compatibleWith: nil), for: .normal)
closeButton.imageView?.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
// self.tableView.reloadData()
self.tableView.invalidateIntrinsicContentSize()
self.headerLabel.text = self.headerText
}
func setupScreens() {
// if UIDevice.current.userInterfaceIdiom == .pad {
// // Do what you want
// widthPopup = 450
// heightPopup = 550
// textView.font = UIFont(name: Fonts.ProximaNova.semibold, size: 20)
// } else if UIDevice.current.userInterfaceIdiom == .phone {
// // Do what you want
// widthPopup = 250
// heightPopup = 360
// textView.font = UIFont(name: Fonts.ProximaNova.semibold, size: 14)
// }
}
public override func viewDidAppear(_ animated: Bool) {
delegateBallon?.presentedPopup()
}
public override func viewDidDisappear(_ animated: Bool) {
delegateBallon?.dismissedPopup()
}
public override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//print("In prepare")
}
public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.overFullScreen
}
public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return UIModalPresentationStyle.overFullScreen
// return UIModalPresentationStyle.none
}
// MARK: - UIButton Action
@IBAction func closeButtonAction(_ sender: Any) {
self.dismiss(animated: true, completion: {
})
}
}
// MARK: - TableView
extension NumberPopupViewController: UITableViewDelegate, UITableViewDataSource{
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.numbersList.count
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50 //+ 30.0
// return UITableViewAutomaticDimension
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NumbersTableViewCellId", for: indexPath) as! NumbersTableViewCell
let isLast = indexPath.row == (self.numbersList.count - 1)
cell.configureCell(number: numbersList[indexPath.row], isLast: isLast)
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegateBallon?.optionSelected(self.numbersList[indexPath.row])
self.dismiss(animated: true, completion: {})
// Logs
// let couponSetData: swiftApi.CouponSetItemModel? = coupons[indexPath.row].couponset_data
// print("Coupon clicked: " + (coupons[indexPath.row].coupon ?? ""))
// print("Coupon Name clicked: " + (couponSetData?.name ?? ""))
// print("Coupon Description clicked: " + (couponSetData?.short_description ?? ""))
// print("Coupon Expiration clicked: " + (coupons[indexPath.row].expiration ?? ""))
//
// let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
// let vc = storyboard.instantiateViewController(withIdentifier: "CouponBarcodeViewController") as! CouponBarcodeViewController
// vc.coupon = coupons[indexPath.row]
// self.navigationController?.pushViewController(vc, animated: true)
}
}
// Class for dynamic height table view
@objc public class DynamicSizeTableView: UITableView
{
override public func layoutSubviews() {
super.layoutSubviews()
if bounds.size != intrinsicContentSize {
invalidateIntrinsicContentSize()
}
}
override public var intrinsicContentSize: CGSize {
return contentSize
}
}