SetInternalCSAccount.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // SetInternalAccount.swift
  3. // DigiXLite
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 23/02/23.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. public class SetInternalCSAccount: UITableViewController {
  10. private var searchController: UISearchController!
  11. public var isSetCS = false
  12. var fromNotification = false
  13. private var availableUser: [User] = []
  14. private var fillteredUser: [User] = []
  15. private var isSearchBarEmpty: Bool {
  16. return searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
  17. }
  18. private var isFilltering: Bool {
  19. return searchController.isActive && !isSearchBarEmpty
  20. }
  21. public override func viewDidLoad() {
  22. super.viewDidLoad()
  23. navigationController?.navigationBar.topItem?.backButtonTitle = "Back".localized()
  24. tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellCSInternal")
  25. searchController = UISearchController(searchResultsController: nil)
  26. searchController.delegate = self
  27. searchController.searchResultsUpdater = self
  28. searchController.searchBar.autocapitalizationType = .none
  29. searchController.searchBar.delegate = self
  30. searchController.searchBar.barTintColor = .secondaryColor
  31. searchController.searchBar.searchTextField.backgroundColor = .secondaryColor
  32. searchController.obscuresBackgroundDuringPresentation = false
  33. definesPresentationContext = true
  34. navigationItem.searchController = searchController
  35. navigationItem.hidesSearchBarWhenScrolling = true
  36. if fromNotification {
  37. let imageButton = UIImageView(frame: CGRect(x: -16, y: 0, width: 20, height: 44))
  38. imageButton.image = UIImage(systemName: "chevron.backward", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .regular, scale: .default))?.withTintColor(.white)
  39. imageButton.contentMode = .left
  40. let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapExit))
  41. imageButton.isUserInteractionEnabled = true
  42. imageButton.addGestureRecognizer(tapGestureRecognizer)
  43. let leftItem = UIBarButtonItem(customView: imageButton)
  44. self.navigationItem.leftBarButtonItem = leftItem
  45. }
  46. if #available(iOS 15.0, *) {
  47. tableView.sectionHeaderTopPadding = 0
  48. }
  49. getData { users in
  50. self.availableUser.append(contentsOf: users)
  51. DispatchQueue.main.async {
  52. self.tableView.reloadData()
  53. }
  54. }
  55. }
  56. @objc func didTapExit() {
  57. self.dismiss(animated: true, completion: nil)
  58. }
  59. public override func viewDidAppear(_ animated: Bool) {
  60. self.navigationController?.navigationBar.topItem?.title = isSetCS ? "Set CS Account".localized() :"Set Internal Account".localized()
  61. self.navigationController?.navigationBar.setNeedsLayout()
  62. self.title = isSetCS ? "Set CS Account".localized() :"Set Internal Account".localized()
  63. }
  64. func getData(completion: @escaping ([User]) -> ()) {
  65. DispatchQueue.global().async {
  66. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  67. var r: [User] = []
  68. var query = "SELECT u.f_pin, u.first_name, u.last_name, u.image_id, u.user_type FROM BUDDY u where u.f_pin <> '\(User.getMyPin()!)' and u.official_account <> '1' and u.official_account <> '2' and u.official_account <> '3' order by 2 collate nocase asc"
  69. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  70. while cursorData.next() {
  71. let user = User(pin: cursorData.string(forColumnIndex: 0) ?? "",
  72. firstName: cursorData.string(forColumnIndex: 1) ?? "",
  73. lastName: cursorData.string(forColumnIndex: 2) ?? "",
  74. thumb: cursorData.string(forColumnIndex: 3) ?? "",
  75. userType: cursorData.string(forColumnIndex: 4) ?? "")
  76. if (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces) == "USR\(user.pin)" {
  77. continue
  78. }
  79. if r.first(where: {$0.pin == user.pin}) == nil {
  80. r.append(user)
  81. }
  82. }
  83. cursorData.close()
  84. }
  85. completion(r)
  86. })
  87. }
  88. }
  89. private func setInternalCS(user: User) {
  90. let alert = LibAlertController(title: "", message: "Are you sure want to set".localized() + " \(user.fullName) " + (isSetCS ? "become CS Account?".localized() : "become Internal Account?".localized()), preferredStyle: .alert)
  91. alert.addAction(UIAlertAction(title: "No".localized(), style: .default, handler: nil))
  92. alert.addAction(UIAlertAction(title: "Yes".localized(), style: .default, handler: {(_) in
  93. DigiX.showLoader()
  94. Database.shared.database?.inTransaction({ fmdb, rollback in
  95. if let result = DigiX.writeSync(message: CoreMessage_TMessageBank.getManagementContactCenter(user_type: (self.isSetCS ? "1" : "3"), l_pin: user.pin), timeout: 5000) {
  96. if result.isOk() {
  97. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  98. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  99. imageView.tintColor = .white
  100. let banner = FloatingNotificationBanner(title: "Successfully changed".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
  101. banner.show()
  102. self.getData { users in
  103. self.availableUser.removeAll()
  104. self.availableUser.append(contentsOf: users)
  105. DispatchQueue.main.async {
  106. self.tableView.reloadData()
  107. }
  108. }
  109. DigiX.hideLoader {}
  110. })
  111. } else {
  112. DispatchQueue.main.async {
  113. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  114. imageView.tintColor = .white
  115. let banner = FloatingNotificationBanner(title: "Unable to access servers".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .top)
  116. banner.show()
  117. DigiX.hideLoader {}
  118. }
  119. }
  120. } else {
  121. DispatchQueue.main.async {
  122. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  123. imageView.tintColor = .white
  124. let banner = FloatingNotificationBanner(title: "Unable to access servers".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .top)
  125. banner.show()
  126. }
  127. }
  128. })
  129. }))
  130. self.present(alert, animated: true, completion: nil)
  131. }
  132. private func unsetInternalCS(user: User) {
  133. let alert = LibAlertController(title: "", message: "Are you sure want to unset".localized() + " \(user.fullName) " + (isSetCS ? "from CS Account?".localized() : "from Internal Account?".localized()), preferredStyle: .alert)
  134. alert.addAction(UIAlertAction(title: "No".localized(), style: UIAlertAction.Style.default, handler: nil))
  135. alert.addAction(UIAlertAction(title: "Yes".localized(), style: .destructive, handler: {(_) in
  136. DigiX.showLoader()
  137. Database.shared.database?.inTransaction({ fmdb, rollback in
  138. if let result = DigiX.writeSync(message: CoreMessage_TMessageBank.getManagementContactCenter(user_type: (self.isSetCS ? "0" : "2"), l_pin: user.pin), timeout: 5000) {
  139. if result.isOk() {
  140. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  141. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  142. imageView.tintColor = .white
  143. let banner = FloatingNotificationBanner(title: "Successfully changed".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
  144. banner.show()
  145. self.getData { users in
  146. self.availableUser.removeAll()
  147. self.availableUser.append(contentsOf: users)
  148. DispatchQueue.main.async {
  149. self.tableView.reloadData()
  150. }
  151. }
  152. DigiX.hideLoader {}
  153. })
  154. } else {
  155. DispatchQueue.main.async {
  156. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  157. imageView.tintColor = .white
  158. let banner = FloatingNotificationBanner(title: "Unable to access servers".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .top)
  159. banner.show()
  160. DigiX.hideLoader {}
  161. }
  162. }
  163. } else {
  164. DispatchQueue.main.async {
  165. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  166. imageView.tintColor = .white
  167. let banner = FloatingNotificationBanner(title: "Unable to access servers".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .top)
  168. banner.show()
  169. }
  170. }
  171. })
  172. }))
  173. self.present(alert, animated: true, completion: nil)
  174. }
  175. func filterContentForSearchText(_ searchText: String) {
  176. fillteredUser = availableUser.filter({ d in
  177. let name = "\(d.firstName) \(d.lastName)".trimmingCharacters(in: .whitespaces)
  178. return name.lowercased().contains(searchText.lowercased())
  179. })
  180. tableView.reloadData()
  181. }
  182. // MARK: - Table view data source
  183. public override func numberOfSections(in tableView: UITableView) -> Int {
  184. return 1
  185. }
  186. public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  187. return availableUser.count
  188. }
  189. public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  190. let cell = tableView.dequeueReusableCell(withIdentifier: "cellCSInternal", for: indexPath as IndexPath)
  191. var content = cell.defaultContentConfiguration()
  192. let user: User
  193. if isFilltering {
  194. user = fillteredUser[indexPath.row]
  195. } else {
  196. user = availableUser[indexPath.row]
  197. }
  198. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  199. getImage(name: user.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath) { result, isDownloaded, image in
  200. content.image = image
  201. if !result {
  202. content.imageProperties.tintColor = .mainColor
  203. }
  204. }
  205. if user.userType == "23" {
  206. content.attributedText = self.set(image: UIImage(named: "ic_internal", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " " + (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces), size: 15, y: -4)
  207. } else if user.userType == "24" {
  208. content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " " + (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces), size: 15, y: -4)
  209. } else {
  210. content.text = (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces)
  211. }
  212. cell.contentConfiguration = content
  213. cell.accessoryType = user.isSelected ? .checkmark : .none
  214. return cell
  215. }
  216. public override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  217. tableView.deselectRow(at: indexPath, animated: true)
  218. if !CheckConnection.isConnectedToNetwork() {
  219. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  220. imageView.tintColor = .white
  221. let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  222. banner.show()
  223. return
  224. }
  225. let user: User
  226. if isFilltering {
  227. user = fillteredUser[indexPath.row]
  228. } else {
  229. user = availableUser[indexPath.row]
  230. }
  231. if User.isCallCenter(userType: user.userType!) || (User.isInternal(userType: user.userType!) && !isSetCS) {
  232. unsetInternalCS(user: user)
  233. } else {
  234. if Utils.getDefaultCC() == "No" {
  235. let viewSetOfficer = SetOfficerBNI()
  236. viewSetOfficer.f_pin = user.pin
  237. viewSetOfficer.name = user.fullName
  238. viewSetOfficer.modalTransitionStyle = .crossDissolve
  239. viewSetOfficer.modalPresentationStyle = .custom
  240. self.present(viewSetOfficer, animated: true)
  241. } else {
  242. setInternalCS(user: user)
  243. }
  244. }
  245. }
  246. }
  247. extension SetInternalCSAccount: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  248. public func updateSearchResults(for searchController: UISearchController) {
  249. filterContentForSearchText(searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines))
  250. }
  251. func set(image: UIImage, with text: String, size: CGFloat, y: CGFloat, colorText: UIColor = UIColor.black) -> NSAttributedString {
  252. let attachment = NSTextAttachment()
  253. attachment.image = image
  254. attachment.bounds = CGRect(x: 0, y: y, width: size, height: size)
  255. let attachmentStr = NSAttributedString(attachment: attachment)
  256. let mutableAttributedString = NSMutableAttributedString()
  257. mutableAttributedString.append(attachmentStr)
  258. let attributedStringColor = [NSAttributedString.Key.foregroundColor : colorText]
  259. let textString = NSAttributedString(string: text, attributes: attributedStringColor)
  260. mutableAttributedString.append(textString)
  261. return mutableAttributedString
  262. }
  263. }