You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.4 KiB

2 years ago
  1. //
  2. // ReferralView.swift
  3. // GME Remit
  4. //
  5. // Created by Armaan Shrestha on 21/08/2022.
  6. // Copyright © 2022 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. private let reuseIdentifier = "cell"
  10. class ReferralView: UIView {
  11. lazy var containerView: UIView = {
  12. let view = UIView()
  13. view.backgroundColor = .themeSubBackground
  14. view.isUserInteractionEnabled = true
  15. view.translatesAutoresizingMaskIntoConstraints = false
  16. return view
  17. }()
  18. private var collectionView: UICollectionView!
  19. var screenWidth = UIScreen.main.bounds.width
  20. var screenHeight = UIScreen.main.bounds.height
  21. var serviceIconSize: CGFloat = 65
  22. var serviceIconSpacing: CGFloat = 14
  23. public var data: [ReferralDetails]? {
  24. didSet {
  25. collectionView.reloadData()
  26. }
  27. }
  28. override init(frame: CGRect) {
  29. super.init(frame: frame)
  30. setup()
  31. }
  32. required init?(coder: NSCoder) {
  33. super.init(coder: coder)
  34. setup()
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. func setup() {
  38. setupView()
  39. }
  40. func setupView() {
  41. self.backgroundColor = .black
  42. self.addSubview(containerView)
  43. let layout = UICollectionViewFlowLayout()
  44. collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  45. collectionView.delegate = self
  46. collectionView.dataSource = self
  47. collectionView.register(ReferralCell.self, forCellWithReuseIdentifier: reuseIdentifier)
  48. collectionView.backgroundColor = .clear
  49. collectionView.isScrollEnabled = true
  50. // Hide Scroll bars
  51. collectionView.showsHorizontalScrollIndicator = false
  52. collectionView.showsVerticalScrollIndicator = false
  53. collectionView.translatesAutoresizingMaskIntoConstraints = false
  54. // Avoids shadow of collection view cell from being clipped
  55. collectionView.clipsToBounds = false
  56. containerView.addSubview(collectionView)
  57. setupLayoutConstraints()
  58. // setupClickListiner()
  59. }
  60. func setupLayoutConstraints() {
  61. NSLayoutConstraint.activate([
  62. containerView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
  63. containerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  64. containerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  65. containerView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
  66. ])
  67. NSLayoutConstraint.activate([
  68. collectionView.topAnchor.constraint(equalTo: containerView.topAnchor),
  69. collectionView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
  70. collectionView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
  71. collectionView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
  72. ])
  73. }
  74. }
  75. extension ReferralView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  76. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  77. return data?.count ?? 0
  78. // return 4
  79. }
  80. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  81. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ReferralCell
  82. let item = data![indexPath.row]
  83. let dates = (item.createdDate ?? "").split(separator: " ")
  84. let date = dates[0].replacingOccurrences(of: "/", with: "-", options: .literal, range: nil)
  85. cell.titleText = date
  86. cell.amountText = item.rewardAmount ?? ""
  87. cell.typeText = item.rewardType ?? ""
  88. return cell
  89. }
  90. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  91. let iconHeight = serviceIconSize
  92. return CGSize(width: collectionView.frame.width, height: iconHeight)
  93. }
  94. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  95. return serviceIconSpacing
  96. }
  97. }
  98. class ReferralCell: UICollectionViewCell {
  99. lazy var wrapperView: UIView = {
  100. let view = UIView()
  101. view.backgroundColor = .clear
  102. view.set(cornerRadius: 5)
  103. view.translatesAutoresizingMaskIntoConstraints = false
  104. return view
  105. }()
  106. lazy var mainStackView: UIStackView = {
  107. let stackView = UIStackView()
  108. stackView.axis = .vertical
  109. stackView.spacing = 10
  110. stackView.translatesAutoresizingMaskIntoConstraints = false
  111. return stackView
  112. }()
  113. lazy var titleStackView : UIStackView = {
  114. let stackView = UIStackView()
  115. stackView.axis = .horizontal
  116. stackView.spacing = 5
  117. stackView.translatesAutoresizingMaskIntoConstraints = false
  118. return stackView
  119. }()
  120. lazy var titleLabel: UILabel = {
  121. let label = UILabel()
  122. label.text = "text here"
  123. label.font = .sanfrancisco(.semibold, size: 16)
  124. label.numberOfLines = 1
  125. label.textColor = .themeBlack
  126. label.translatesAutoresizingMaskIntoConstraints = false
  127. return label
  128. }()
  129. lazy var amountLabel: UILabel = {
  130. let label = UILabel()
  131. label.text = "text here"
  132. label.font = .sanfrancisco(.bold, size: 14)
  133. label.numberOfLines = 1
  134. label.textColor = .themeBlue
  135. label.translatesAutoresizingMaskIntoConstraints = false
  136. return label
  137. }()
  138. lazy var typeLabel: UILabel = {
  139. let label = UILabel()
  140. label.text = "text here"
  141. label.font = .sanfrancisco(.regular, size: 14)
  142. label.numberOfLines = 1
  143. label.textColor = .themeBlack
  144. label.translatesAutoresizingMaskIntoConstraints = false
  145. return label
  146. }()
  147. var titleText = String() {
  148. didSet {
  149. titleLabel.text = titleText
  150. }
  151. }
  152. var amountText = String() {
  153. didSet {
  154. amountLabel.text = amountText
  155. }
  156. }
  157. var verticalLine: UIView = {
  158. var view = UIView()
  159. view.backgroundColor = .themeGray2
  160. view.translatesAutoresizingMaskIntoConstraints = false
  161. return view
  162. }()
  163. var typeText = String() {
  164. didSet {
  165. typeLabel.text = typeText
  166. }
  167. }
  168. override init(frame: CGRect) {
  169. super.init(frame: frame)
  170. setupView()
  171. }
  172. required init?(coder: NSCoder) {
  173. super.init(coder: coder)
  174. }
  175. override func awakeFromNib() {
  176. super.awakeFromNib()
  177. }
  178. func setupView() {
  179. contentView.addSubview(wrapperView)
  180. wrapperView.addSubview(mainStackView)
  181. wrapperView.addSubview(verticalLine)
  182. mainStackView.addArrangedSubview(titleStackView)
  183. mainStackView.addArrangedSubview(typeLabel)
  184. titleStackView.addArrangedSubview(titleLabel)
  185. titleStackView.addArrangedSubview(amountLabel)
  186. setupConstraint()
  187. }
  188. private func setupConstraint() {
  189. NSLayoutConstraint.activate([
  190. wrapperView.topAnchor.constraint(equalTo: contentView.topAnchor),
  191. wrapperView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
  192. wrapperView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
  193. ])
  194. NSLayoutConstraint.activate([
  195. mainStackView.topAnchor.constraint(equalTo: wrapperView.topAnchor, constant: 20),
  196. mainStackView.leadingAnchor.constraint(equalTo: wrapperView.leadingAnchor, constant: 20),
  197. mainStackView.trailingAnchor.constraint(equalTo: wrapperView.trailingAnchor, constant: -20),
  198. mainStackView.bottomAnchor.constraint(equalTo: wrapperView.bottomAnchor, constant: -20)
  199. ])
  200. NSLayoutConstraint.activate([
  201. verticalLine.bottomAnchor.constraint(equalTo: wrapperView.bottomAnchor),
  202. verticalLine.leadingAnchor.constraint(equalTo: wrapperView.leadingAnchor, constant: 10),
  203. verticalLine.trailingAnchor.constraint(equalTo: wrapperView.trailingAnchor, constant: -10),
  204. verticalLine.heightAnchor.constraint(equalToConstant: 1)
  205. ])
  206. }
  207. }