FourthTabViewController.swift 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. //
  2. // SettingTableViewController.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 16/09/21.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. import nuSDKService
  10. import NexilisLite
  11. import Photos
  12. public class FourthTabViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, UIGestureRecognizerDelegate {
  13. var language: [[String: String]] = [["Indonesia": "id"],["English": "en"]]
  14. var alert: UIAlertController?
  15. var textFields = [UITextField]()
  16. var switchVibrateMode = UISwitch()
  17. var switchSaveToGallery = UISwitch()
  18. var switchAutoDownload = UISwitch()
  19. @IBOutlet weak var tableView: UITableView!
  20. @IBOutlet weak var backgroundImage: UIImageView!
  21. var notInTab = false
  22. public override func viewDidLoad() {
  23. super.viewDidLoad()
  24. tableView.delegate = self
  25. tableView.dataSource = self
  26. tableView.layoutMargins = .init(top: 0, left: 5, bottom: 0, right: 5)
  27. // tableView.separatorColor = .gray
  28. tableView.separatorStyle = .none
  29. switchVibrateMode.tintColor = .gray
  30. switchSaveToGallery.tintColor = .gray
  31. switchAutoDownload.tintColor = .gray
  32. switchVibrateMode.onTintColor = .mainColor
  33. switchSaveToGallery.onTintColor = .mainColor
  34. switchAutoDownload.onTintColor = .mainColor
  35. let vibrateMode = UserDefaults.standard.bool(forKey: "vibrateMode")
  36. let saveGallery = UserDefaults.standard.bool(forKey: "saveToGallery")
  37. let autoDownload = UserDefaults.standard.bool(forKey: "autoDownload")
  38. if vibrateMode {
  39. switchVibrateMode.setOn(true, animated: false)
  40. }
  41. if saveGallery {
  42. switchSaveToGallery.setOn(true, animated: false)
  43. }
  44. if autoDownload {
  45. switchAutoDownload.setOn(true, animated: false)
  46. }
  47. switchVibrateMode.addTarget(self, action: #selector(vibrateModeSwitch), for: .valueChanged)
  48. switchSaveToGallery.addTarget(self, action: #selector(saveToGallerySwitch), for: .valueChanged)
  49. switchAutoDownload.addTarget(self, action: #selector(autoDownloadSwitch), for: .valueChanged)
  50. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(collapseDocked))
  51. tapGesture.cancelsTouchesInView = false
  52. tapGesture.delegate = self
  53. self.view.addGestureRecognizer(tapGesture)
  54. // navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didTapCancel))
  55. }
  56. @objc func collapseDocked() {
  57. if ViewController.isExpandButton {
  58. ViewController.expandButton()
  59. }
  60. }
  61. public override func viewWillDisappear(_ animated: Bool) {
  62. if ViewController.isExpandButton {
  63. ViewController.expandButton()
  64. }
  65. }
  66. public override func viewDidAppear(_ animated: Bool) {
  67. DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
  68. var viewController = UIApplication.shared.windows.first!.rootViewController
  69. if !(viewController is ViewController) {
  70. viewController = self.parent
  71. }
  72. if ViewController.middleButton.isHidden {
  73. ViewController.isExpandButton = false
  74. if let viewController = viewController as? ViewController {
  75. if viewController.tabBar.isHidden {
  76. viewController.tabBar.isHidden = false
  77. ViewController.middleButton.isHidden = false
  78. }
  79. }
  80. } else if PrefsUtil.getCpaasMode() != PrefsUtil.CPAAS_MODE_DOCKED {
  81. DispatchQueue.main.async {
  82. if let viewController = viewController as? ViewController {
  83. if viewController.tabBar.isHidden {
  84. viewController.tabBar.isHidden = false
  85. }
  86. }
  87. }
  88. }
  89. })
  90. }
  91. @objc func vibrateModeSwitch() {
  92. UserDefaults.standard.set(switchVibrateMode.isOn, forKey: "vibrateMode")
  93. }
  94. @objc func saveToGallerySwitch() {
  95. if switchSaveToGallery.isOn {
  96. PHPhotoLibrary.requestAuthorization({status in
  97. DispatchQueue.main.async {
  98. if status == .authorized {
  99. UserDefaults.standard.set(self.switchSaveToGallery.isOn, forKey: "saveToGallery")
  100. } else {
  101. self.switchSaveToGallery.setOn(false, animated: true)
  102. }
  103. }
  104. })
  105. } else {
  106. UserDefaults.standard.set(self.switchSaveToGallery.isOn, forKey: "saveToGallery")
  107. }
  108. }
  109. @objc func autoDownloadSwitch() {
  110. UserDefaults.standard.set(switchAutoDownload.isOn, forKey: "autoDownload")
  111. }
  112. func makeMenu(imageSignIn: String = ""){
  113. let isChangeProfile = Utils.getSetProfile()
  114. Database.shared.database?.inTransaction({ fmdb, rollback in
  115. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  116. if let cursorUser = Database.shared.getRecords(fmdb: fmdb, query: "SELECT user_type, image_id FROM BUDDY where f_pin='\(idMe!)'"), cursorUser.next() {
  117. var groupId = ""
  118. if let cursorGroup = Database.shared.getRecords(fmdb: fmdb, query: "SELECT group_id FROM GROUPZ where group_type = 1 AND official = 1"), cursorGroup.next() {
  119. groupId = cursorGroup.string(forColumnIndex: 0) ?? ""
  120. cursorGroup.close()
  121. }
  122. var position = ""
  123. if let cursorIsAdmin = Database.shared.getRecords(fmdb: fmdb, query: "SELECT position FROM GROUPZ_MEMBER where group_id = '\(groupId)' AND f_pin = '\(idMe!)'"), cursorIsAdmin.next() {
  124. position = cursorIsAdmin.string(forColumnIndex: 0) ?? ""
  125. cursorIsAdmin.close()
  126. }
  127. if cursorUser.string(forColumnIndex: 0) == "23" && position == "1" {
  128. Item.menus["Personal"] = [
  129. Item(icon: UIImage(systemName: "person.fill"), title: "Personal Information".localized()),
  130. Item(icon: UIImage(systemName: "textformat.abc"), title: "Change Language".localized()),
  131. Item(icon: UIImage(systemName: "person.crop.rectangle"), title: "Change Admin / Internal Password".localized()),
  132. Item(icon: UIImage(systemName: "laptopcomputer.and.iphone"), title: "Login to Web".localized()),
  133. ]
  134. } else if cursorUser.string(forColumnIndex: 0) == "23" || cursorUser.string(forColumnIndex: 0) == "24" {
  135. Item.menus["Personal"] = [
  136. Item(icon: UIImage(systemName: "person.fill"), title: "Personal Information".localized()),
  137. Item(icon: UIImage(systemName: "textformat.abc"), title: "Change Language".localized()),
  138. Item(icon: UIImage(systemName: "laptopcomputer.and.iphone"), title: "Login to Web".localized()),
  139. ]
  140. } else {
  141. Item.menus["Personal"] = [
  142. Item(icon: UIImage(systemName: "person.fill"), title: "Personal Information".localized()),
  143. Item(icon: UIImage(systemName: "textformat.abc"), title: "Change Language".localized()),
  144. Item(icon: UIImage(systemName: "person.crop.rectangle"), title: "Access Admin / Internal Features".localized()),
  145. ]
  146. }
  147. if !isChangeProfile {
  148. Item.menus["Personal"]?.append(Item(icon: UIImage(systemName: "arrow.up.and.person.rectangle.portrait"), title: "Login".localized()))
  149. } else if isChangeProfile {
  150. Item.menus["Personal"]?.append(Item(icon: UIImage(systemName: "rectangle.portrait.and.arrow.right"), title: "Logout".localized()))
  151. }
  152. let image = cursorUser.string(forColumnIndex: 1)
  153. if image != nil {
  154. if !image!.isEmpty {
  155. do {
  156. let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  157. let file = documentDir.appendingPathComponent(image!)
  158. if FileManager().fileExists(atPath: file.path) {
  159. let image = UIImage(contentsOfFile: file.path)
  160. Item.menus["Personal"]?[0].icon = image?.circleMasked
  161. if !imageSignIn.isEmpty {
  162. var dataImage: [AnyHashable : Any] = [:]
  163. dataImage["name"] = imageSignIn
  164. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
  165. }
  166. } else {
  167. Download().start(forKey: image!) { (name, progress) in
  168. guard progress == 100 else {
  169. return
  170. }
  171. DispatchQueue.main.async {
  172. let image = UIImage(contentsOfFile: file.path)
  173. Item.menus["Personal"]?[0].icon = image?.circleMasked
  174. self.tableView.reloadData()
  175. if !imageSignIn.isEmpty {
  176. var dataImage: [AnyHashable : Any] = [:]
  177. dataImage["name"] = imageSignIn
  178. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
  179. }
  180. }
  181. }
  182. }
  183. } catch {}
  184. }
  185. }
  186. cursorUser.close()
  187. } else {
  188. Item.menus["Personal"] = [
  189. Item(icon: UIImage(systemName: "person.fill"), title: "Personal Information".localized()),
  190. Item(icon: UIImage(systemName: "textformat.abc"), title: "Change Language".localized()),
  191. Item(icon: UIImage(systemName: "person.crop.rectangle"), title: "Access Admin / Internal Features".localized()),
  192. Item(icon: UIImage(systemName: "arrow.up.and.person.rectangle.portrait"), title: "Login".localized())
  193. ]
  194. if !imageSignIn.isEmpty {
  195. do {
  196. let documentDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  197. let file = documentDir.appendingPathComponent(imageSignIn)
  198. if FileManager().fileExists(atPath: file.path) {
  199. let image = UIImage(contentsOfFile: file.path)
  200. Item.menus["Personal"]?[0].icon = image?.circleMasked
  201. var dataImage: [AnyHashable : Any] = [:]
  202. dataImage["name"] = imageSignIn
  203. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
  204. } else {
  205. Download().start(forKey: imageSignIn) { (name, progress) in
  206. guard progress == 100 else {
  207. return
  208. }
  209. DispatchQueue.main.async {
  210. let image = UIImage(contentsOfFile: file.path)
  211. Item.menus["Personal"]?[0].icon = image?.circleMasked
  212. self.tableView.reloadData()
  213. var dataImage: [AnyHashable : Any] = [:]
  214. dataImage["name"] = imageSignIn
  215. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
  216. }
  217. }
  218. }
  219. } catch {}
  220. }
  221. }
  222. })
  223. Item.menus["Call"] = [
  224. // Item(icon: UIImage(systemName: "message"), title: "Incoming Message(s)".localized()),
  225. // Item(icon: UIImage(systemName: "phone"), title: "Incoming Call(s)".localized()),
  226. Item(icon: UIImage(systemName: "iphone.homebutton.radiowaves.left.and.right"), title: "Vibrate Mode".localized()),
  227. Item(icon: UIImage(systemName: "photo.on.rectangle.angled"), title: "Save to Gallery".localized()),
  228. Item(icon: UIImage(systemName: "arrow.down.square"), title: "Auto Download".localized()),
  229. ]
  230. Item.menus["Version"] = [
  231. Item(icon: UIImage(systemName: "gear"), title: "Version".localized()),
  232. Item(icon: UIImage(named: "pb_powered_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), title: "Powered by Nexilis".localized()),
  233. ]
  234. }
  235. override public func viewWillAppear(_ animated: Bool) {
  236. backgroundImage.backgroundColor = .white
  237. backgroundImage.image = nil
  238. DispatchQueue.global().async {
  239. if let listBg = PrefsUtil.getBackground() {
  240. if listBg.isEmpty {
  241. return
  242. }
  243. var bgChoosen = ""
  244. let arrayBg = listBg.split(separator: ",")
  245. bgChoosen = String(arrayBg[Int.random(in: 0..<arrayBg.count)])
  246. ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getURLBase() + "/dashboardv2/uploads/background/" + bgChoosen)!) { data, response, error in
  247. guard let data = data, error == nil else { return }
  248. // always update the UI from the main thread
  249. DispatchQueue.main.async() { [self] in
  250. backgroundImage.image = UIImage(data: data)!
  251. }
  252. }
  253. }
  254. }
  255. if notInTab {
  256. self.title = "Settings".localized()
  257. self.navigationController?.navigationBar.topItem?.title = ""
  258. } else {
  259. self.navigationController?.navigationBar.topItem?.title = "Settings".localized()
  260. }
  261. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
  262. navigationController?.setNavigationBarHidden(false, animated: false)
  263. navigationController?.navigationBar.backgroundColor = .clear
  264. navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  265. navigationController?.navigationBar.shadowImage = UIImage()
  266. navigationController?.navigationBar.isTranslucent = true
  267. makeMenu()
  268. tableView.reloadData()
  269. }
  270. // MARK: - Table view data source
  271. public func numberOfSections(in tableView: UITableView) -> Int {
  272. return Item.sections.count
  273. }
  274. public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  275. return 1
  276. }
  277. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  278. return Item.menuFor(section: section).count
  279. }
  280. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  281. let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
  282. cell.accessoryType = .none
  283. cell.indentationLevel = 0
  284. var content = cell.defaultContentConfiguration()
  285. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  286. content.secondaryTextProperties.font = UIFont.systemFont(ofSize: 14)
  287. content.secondaryTextProperties.color = .gray
  288. content.prefersSideBySideTextAndSecondaryText = true
  289. let section = Item.sections[indexPath.section]
  290. if let arr = Item.menus[section] {
  291. let menu = arr[indexPath.row]
  292. content.image = menu.icon
  293. content.imageProperties.tintColor = .mainColor
  294. content.imageProperties.maximumSize = CGSize(width: 24, height: 24)
  295. content.text = menu.title
  296. cell.accessoryView = nil
  297. cell.separatorInset = UIEdgeInsets(top: .greatestFiniteMagnitude, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
  298. switch menu.title {
  299. case "Personal Information".localized():
  300. cell.accessoryType = .disclosureIndicator
  301. case "Access Admin / Internal Features".localized():
  302. cell.accessoryType = .disclosureIndicator
  303. case "Login to Web".localized():
  304. cell.accessoryType = .disclosureIndicator
  305. case "Login".localized():
  306. cell.accessoryType = .disclosureIndicator
  307. // case "Logout".localized():
  308. case "Change Admin / Internal Password".localized():
  309. cell.accessoryType = .disclosureIndicator
  310. case "Change Language".localized():
  311. cell.accessoryType = .disclosureIndicator
  312. case "Version".localized():
  313. let accessoryButton = UIButton(type: .custom)
  314. accessoryButton.setTitle(UIApplication.appVersion, for: .normal)
  315. accessoryButton.setTitleColor(.black, for: .normal)
  316. accessoryButton.contentHorizontalAlignment = .right;
  317. accessoryButton.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
  318. accessoryButton.contentMode = .scaleAspectFit
  319. cell.accessoryView = accessoryButton as UIView
  320. case "Vibrate Mode".localized():
  321. cell.accessoryView = switchVibrateMode
  322. case "Save to Gallery".localized():
  323. cell.accessoryView = switchSaveToGallery
  324. case "Auto Download".localized():
  325. cell.accessoryView = switchAutoDownload
  326. default:
  327. content.secondaryText = nil
  328. }
  329. }
  330. cell.contentConfiguration = content
  331. return cell
  332. }
  333. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  334. let item = Item.menuFor(section: indexPath.section)[indexPath.row]
  335. if item.title == "Personal Information".localized() {
  336. if(ViewController.checkIsChangePerson()){
  337. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "profileView") as! ProfileViewController
  338. controller.data = UserDefaults.standard.string(forKey: "me")!
  339. controller.flag = .me
  340. controller.dismissImage = { image, imageName in
  341. var dataImage: [AnyHashable : Any] = [:]
  342. dataImage["name"] = imageName
  343. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
  344. self.makeMenu()
  345. self.tableView.reloadData()
  346. }
  347. navigationController?.show(controller, sender: nil)
  348. }
  349. } else if item.title == "Access Admin / Internal Features".localized() || item.title == "Change Admin / Internal Password".localized() {
  350. if(ViewController.checkIsChangePerson()){
  351. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  352. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  353. imageView.tintColor = .white
  354. 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)
  355. banner.show()
  356. return
  357. }
  358. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  359. if(item.title.contains("Change")){
  360. if let action = self.actionChangePassword(for: "admin", title: "Change Admin Password".localized()) {
  361. alertController.addAction(action)
  362. }
  363. if let action = self.actionChangePassword(for: "internal", title: "Change Internal Password".localized()) {
  364. alertController.addAction(action)
  365. }
  366. }
  367. else {
  368. if let action = self.actionLogin(for: "admin", title: "Access Admin Features".localized()) {
  369. alertController.addAction(action)
  370. }
  371. if let action = self.actionLogin(for: "internal", title: "Access Internal Features".localized()) {
  372. alertController.addAction(action)
  373. }
  374. }
  375. alertController.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  376. self.present(alertController, animated: true)
  377. }
  378. } else if item.title == "Change Language".localized() {
  379. let vc = UIViewController()
  380. vc.preferredContentSize = CGSize(width: UIScreen.main.bounds.width - 10, height: 150)
  381. let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 10, height: 150))
  382. pickerView.dataSource = self
  383. pickerView.delegate = self
  384. let lang = UserDefaults.standard.string(forKey: "i18n_language")
  385. var index = 1
  386. if lang == "id" {
  387. index = 0
  388. }
  389. pickerView.selectRow(index, inComponent: 0, animated: false)
  390. vc.view.addSubview(pickerView)
  391. pickerView.translatesAutoresizingMaskIntoConstraints = false
  392. pickerView.centerXAnchor.constraint(equalTo: vc.view.centerXAnchor).isActive = true
  393. pickerView.centerYAnchor.constraint(equalTo: vc.view.centerYAnchor).isActive = true
  394. let alert = UIAlertController(title: "Select Language".localized(), message: "", preferredStyle: .actionSheet)
  395. alert.setValue(vc, forKey: "contentViewController")
  396. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: { (UIAlertAction) in
  397. }))
  398. alert.addAction(UIAlertAction(title: "Select".localized(), style: .default, handler: { (UIAlertAction) in
  399. let selectedIndex = pickerView.selectedRow(inComponent: 0)
  400. let lang = self.language[selectedIndex].values.first
  401. UserDefaults.standard.set(lang, forKey: "i18n_language")
  402. self.navigationController?.navigationBar.topItem?.title = "Settings".localized();
  403. self.makeMenu()
  404. self.tableView.reloadData()
  405. FirstTabViewController.forceRefresh = true
  406. ThirdTabViewController.forceRefresh = true
  407. }))
  408. self.present(alert, animated: true, completion: nil)
  409. } else if item.title == "Login".localized() {
  410. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeDevice") as! ChangeDeviceViewController
  411. controller.isDismiss = { newThumb in
  412. self.makeMenu(imageSignIn: newThumb)
  413. self.tableView.reloadData()
  414. }
  415. navigationController?.show(controller, sender: nil)
  416. } else if item.title == "Logout".localized() {
  417. let alert = UIAlertController(title: "Logout".localized(), message: "Are you sure want to logout?".localized(), preferredStyle: .alert)
  418. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: UIAlertAction.Style.default, handler: nil))
  419. alert.addAction(UIAlertAction(title: "Yes".localized(), style: .destructive, handler: {(_) in
  420. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  421. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  422. imageView.tintColor = .white
  423. 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)
  424. banner.show()
  425. return
  426. }
  427. Nexilis.showLoader()
  428. DispatchQueue.global().async {
  429. self.deleteAllRecordDatabase()
  430. Nexilis.destroyAll()
  431. let apiKey = Nexilis.sAPIKey
  432. var id = UIDevice.current.identifierForVendor?.uuidString ?? "UNK-DEVICE"
  433. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSignUpApi(api: apiKey, p_pin: id), timeout: 30 * 1000) {
  434. id = response.getBody(key: CoreMessage_TMessageKey.F_PIN, default_value: "")
  435. if(!id.isEmpty){
  436. Nexilis.changeUser(f_pin: id)
  437. UserDefaults.standard.setValue(id, forKey: "me")
  438. Utils.setProfile(value: false)
  439. UserDefaults.standard.synchronize()
  440. if Utils.getForceAnonymous() {
  441. _ = Nexilis.write(message: CoreMessage_TMessageBank.getPostRegistration(p_pin: id))
  442. }
  443. DispatchQueue.main.async {
  444. Nexilis.hideLoader()
  445. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  446. imageView.tintColor = .white
  447. let banner = FloatingNotificationBanner(title: "Successfully Logout".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)
  448. banner.show()
  449. var dataImage: [AnyHashable : Any] = [:]
  450. dataImage["name"] = ""
  451. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil, userInfo: dataImage)
  452. self.makeMenu()
  453. self.tableView.reloadData()
  454. }
  455. if !Utils.getForceAnonymous() {
  456. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
  457. var viewController = UIApplication.shared.windows.first!.rootViewController
  458. if !(viewController is ViewController) {
  459. viewController = self.parent
  460. }
  461. if let viewController = viewController as? ViewController {
  462. viewController.viewWillAppear(false)
  463. }
  464. })
  465. }
  466. } else {
  467. Nexilis.hideLoader()
  468. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  469. imageView.tintColor = .white
  470. let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".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)
  471. banner.show()
  472. }
  473. } else {
  474. DispatchQueue.main.async {
  475. Nexilis.hideLoader()
  476. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  477. imageView.tintColor = .white
  478. let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".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)
  479. banner.show()
  480. }
  481. }
  482. }
  483. }))
  484. self.present(alert, animated: true, completion: nil)
  485. } else if item.title == "Login to Web".localized() {
  486. var permissionCheck = -1
  487. if AVCaptureDevice.authorizationStatus(for: .video) == .authorized {
  488. permissionCheck = 1
  489. } else if AVCaptureDevice.authorizationStatus(for: .video) == .denied {
  490. permissionCheck = 0
  491. } else {
  492. AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) -> Void in
  493. if granted == true {
  494. permissionCheck = 1
  495. } else {
  496. permissionCheck = 0
  497. }
  498. })
  499. }
  500. while permissionCheck == -1 {
  501. sleep(1)
  502. }
  503. if permissionCheck == 0 {
  504. let alert = UIAlertController(title: "Attention!".localized(), message: "Please allow camera permission in your settings".localized(), preferredStyle: .alert)
  505. alert.addAction(UIAlertAction(title: "OK".localized(), style: UIAlertAction.Style.default, handler: { _ in
  506. if let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) {
  507. UIApplication.shared.open(url, options: [:], completionHandler: nil)
  508. }
  509. }))
  510. if UIApplication.shared.visibleViewController?.navigationController != nil {
  511. UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
  512. } else {
  513. UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
  514. }
  515. return
  516. }
  517. let controller = ScannerViewController()
  518. let navigationController = UINavigationController(rootViewController: controller)
  519. navigationController.navigationBar.tintColor = .white
  520. navigationController.navigationBar.barTintColor = .mainColor
  521. navigationController.navigationBar.isTranslucent = false
  522. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  523. navigationController.navigationBar.titleTextAttributes = textAttributes
  524. navigationController.view.backgroundColor = .mainColor
  525. navigationController.modalPresentationStyle = .custom
  526. self.present(navigationController, animated: true)
  527. }
  528. }
  529. private func actionLogin(for type: String, title: String) -> UIAlertAction? {
  530. return UIAlertAction(title: title, style: .default) { _ in
  531. self.alert = UIAlertController(title:"Access Admin Features".localized(), message: nil, preferredStyle: .alert)
  532. if type == "internal" {
  533. self.alert = UIAlertController(title: "Access Internal Features".localized(), message: nil, preferredStyle: .alert)
  534. }
  535. self.textFields.removeAll()
  536. self.alert?.addTextField{ (texfield) in
  537. texfield.placeholder = "Password".localized()
  538. texfield.isSecureTextEntry = true
  539. texfield.addPadding(.right(40))
  540. texfield.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControl.Event.editingChanged)
  541. let buttonHideUnhide = UIButton()
  542. buttonHideUnhide.tag = 0
  543. texfield.addSubview(buttonHideUnhide)
  544. buttonHideUnhide.anchor(top: texfield.topAnchor, right: texfield.rightAnchor, paddingTop: -7, width: 30, height: 30)
  545. buttonHideUnhide.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  546. buttonHideUnhide.tintColor = .black
  547. buttonHideUnhide.addTarget(self, action: #selector(self.showPassword), for: .touchUpInside)
  548. }
  549. let submitAction = UIAlertAction(title: "Login".localized(), style: .default, handler: { (action) -> Void in
  550. let textField = self.alert?.textFields![0]
  551. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  552. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  553. imageView.tintColor = .white
  554. 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)
  555. banner.show()
  556. return
  557. }
  558. if type == "admin" {
  559. self.signInAdmin(password: textField!.text!, completion: { result in
  560. if result {
  561. DispatchQueue.main.async {
  562. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  563. imageView.tintColor = .white
  564. let banner = FloatingNotificationBanner(title: "Successfully login Admin".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)
  565. banner.show()
  566. let itemCP = Item(icon: UIImage(systemName: "person.crop.rectangle"), title: "Change Admin / Internal Password".localized())
  567. Item.menus["Personal"]?[1] = itemCP
  568. self.tableView.reloadData()
  569. }
  570. }
  571. })
  572. } else {
  573. self.signInInternal(password: textField!.text!, completion: { result in
  574. if result {
  575. DispatchQueue.main.async {
  576. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  577. imageView.tintColor = .white
  578. let banner = FloatingNotificationBanner(title: "Successfully login Internal Team".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)
  579. banner.show()
  580. Item.menus["Personal"]?.remove(at: 1)
  581. self.tableView.reloadData()
  582. }
  583. }
  584. })
  585. }
  586. })
  587. submitAction.isEnabled = false
  588. self.alert?.addAction(submitAction)
  589. self.alert?.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  590. self.present(self.alert!, animated: true, completion: nil)
  591. }
  592. }
  593. private func actionChangePassword(for type: String, title: String) -> UIAlertAction? {
  594. return UIAlertAction(title: title, style: .default) { _ in
  595. self.alert = UIAlertController(title: "Change Admin Password".localized(), message: nil, preferredStyle: .alert)
  596. if type == "internal" {
  597. self.alert = UIAlertController(title: "Change Internal Password".localized(), message: nil, preferredStyle: .alert)
  598. }
  599. self.textFields.removeAll()
  600. self.alert?.addTextField{ (texfield) in
  601. texfield.placeholder = "Old Password"
  602. texfield.isSecureTextEntry = true
  603. texfield.addPadding(.right(40))
  604. texfield.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControl.Event.editingChanged)
  605. self.textFields.append(texfield)
  606. let buttonHideUnhide = UIButton()
  607. buttonHideUnhide.tag = 0
  608. texfield.addSubview(buttonHideUnhide)
  609. buttonHideUnhide.anchor(top: texfield.topAnchor, right: texfield.rightAnchor, paddingTop: -7, width: 30, height: 30)
  610. buttonHideUnhide.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  611. buttonHideUnhide.tintColor = .black
  612. buttonHideUnhide.addTarget(self, action: #selector(self.showPassword), for: .touchUpInside)
  613. }
  614. self.alert?.addTextField{ (texfield) in
  615. texfield.placeholder = "New Password"
  616. texfield.isSecureTextEntry = true
  617. texfield.addPadding(.right(40))
  618. texfield.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControl.Event.editingChanged)
  619. self.textFields.append(texfield)
  620. let buttonHideUnhide = UIButton()
  621. buttonHideUnhide.tag = 1
  622. texfield.addSubview(buttonHideUnhide)
  623. buttonHideUnhide.anchor(top: texfield.topAnchor, right: texfield.rightAnchor, paddingTop: -7, width: 30, height: 30)
  624. buttonHideUnhide.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  625. buttonHideUnhide.tintColor = .black
  626. buttonHideUnhide.addTarget(self, action: #selector(self.showPassword), for: .touchUpInside)
  627. }
  628. let submitAction = UIAlertAction(title: "Change Password".localized(), style: .default, handler: { (action) -> Void in
  629. let textFieldOld = self.alert?.textFields![0]
  630. let textFieldNew = self.alert?.textFields![1]
  631. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  632. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  633. imageView.tintColor = .white
  634. 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)
  635. banner.show()
  636. return
  637. }
  638. if type == "admin" {
  639. self.changePasswordAdmin(oldPassword: textFieldOld!.text!, newPassword: textFieldNew!.text!, completion: { result in
  640. if result {
  641. DispatchQueue.main.async {
  642. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  643. imageView.tintColor = .white
  644. let banner = FloatingNotificationBanner(title: "Admin password changed successfully".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)
  645. banner.show()
  646. }
  647. }
  648. })
  649. } else {
  650. self.changePasswordInternal(oldPassword: textFieldOld!.text!, newPassword: textFieldNew!.text!, completion: { result in
  651. if result {
  652. DispatchQueue.main.async {
  653. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  654. imageView.tintColor = .white
  655. let banner = FloatingNotificationBanner(title: "Internal password changed successfully".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)
  656. banner.show()
  657. }
  658. }
  659. })
  660. }
  661. })
  662. submitAction.isEnabled = false
  663. self.alert?.addAction(submitAction)
  664. self.alert?.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  665. self.present(self.alert!, animated: true, completion: nil)
  666. }
  667. }
  668. @objc func showPassword(_ sender:UIButton) {
  669. if alert!.textFields![sender.tag].isSecureTextEntry {
  670. alert!.textFields![sender.tag].isSecureTextEntry = false
  671. let buttonImage = alert!.textFields![sender.tag].subviews[0] as! UIButton
  672. buttonImage.setImage(UIImage(systemName: "eye.fill"), for: .normal)
  673. } else {
  674. alert!.textFields![sender.tag].isSecureTextEntry = true
  675. let buttonImage = alert!.textFields![sender.tag].subviews[0] as! UIButton
  676. buttonImage.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
  677. }
  678. }
  679. @objc func alertTextFieldDidChange(_ sender: UITextField) {
  680. if(!textFields.isEmpty){
  681. print("text count 0: \(textFields[0].text!.count)")
  682. print("text count 1: \(textFields[1].text!.count)")
  683. alert?.actions[0].isEnabled = textFields[0].text!.count > 0 && textFields[1].text!.count > 0
  684. }
  685. else {
  686. alert?.actions[0].isEnabled = sender.text!.count > 0
  687. }
  688. }
  689. private func signInAdmin(password: String, completion: @escaping (Bool) -> ()) {
  690. DispatchQueue.global().async {
  691. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  692. let p_password = password
  693. let md5Hex = Utils.getMD5(string: p_password).map { String(format: "%02hhx", $0) }.joined()
  694. var result: Bool = false
  695. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSignInApiAdmin(p_name: idMe!, p_password: md5Hex)) {
  696. if response.isOk() {
  697. result = true
  698. }
  699. DispatchQueue.main.async {
  700. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
  701. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  702. imageView.tintColor = .white
  703. let banner = FloatingNotificationBanner(title: "Username or password does not match".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)
  704. banner.show()
  705. } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "20" {
  706. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  707. imageView.tintColor = .white
  708. let banner = FloatingNotificationBanner(title: "Invalid password".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)
  709. banner.show()
  710. }
  711. }
  712. } else {
  713. DispatchQueue.main.async {
  714. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  715. imageView.tintColor = .white
  716. 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)
  717. banner.show()
  718. }
  719. }
  720. completion(result)
  721. }
  722. }
  723. private func signInInternal(password: String, completion: @escaping (Bool) -> ()) {
  724. DispatchQueue.global().async {
  725. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  726. let p_password = password
  727. let md5Hex = Utils.getMD5(string: p_password).map { String(format: "%02hhx", $0) }.joined()
  728. var result: Bool = false
  729. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getSignInApiInternal(p_name: idMe!, p_password: md5Hex)) {
  730. if response.isOk() {
  731. result = true
  732. }
  733. DispatchQueue.main.async {
  734. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
  735. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  736. imageView.tintColor = .white
  737. let banner = FloatingNotificationBanner(title: "Username or password does not match".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)
  738. banner.show()
  739. } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "20" {
  740. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  741. imageView.tintColor = .white
  742. let banner = FloatingNotificationBanner(title: "Invalid password".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)
  743. banner.show()
  744. }
  745. }
  746. } else {
  747. DispatchQueue.main.async {
  748. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  749. imageView.tintColor = .white
  750. 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)
  751. banner.show()
  752. }
  753. }
  754. completion(result)
  755. }
  756. }
  757. private func changePasswordAdmin(oldPassword: String, newPassword: String, completion: @escaping (Bool) -> ()) {
  758. DispatchQueue.global().async {
  759. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  760. let p_password = oldPassword
  761. let n_password = newPassword
  762. let md5Hex = Utils.getMD5(string: p_password).map { String(format: "%02hhx", $0) }.joined()
  763. let md5HexNew = Utils.getMD5(string: n_password).map { String(format: "%02hhx", $0) }.joined()
  764. var result: Bool = false
  765. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getChangePasswordAdmin(p_f_pin: idMe!, pwd_en: md5HexNew, pwd_old: md5Hex)) {
  766. if response.isOk() {
  767. result = true
  768. }
  769. DispatchQueue.main.async {
  770. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
  771. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  772. imageView.tintColor = .white
  773. let banner = FloatingNotificationBanner(title: "Username or password does not match".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)
  774. banner.show()
  775. } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "20" {
  776. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  777. imageView.tintColor = .white
  778. let banner = FloatingNotificationBanner(title: "Invalid password".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)
  779. banner.show()
  780. }
  781. }
  782. } else {
  783. DispatchQueue.main.async {
  784. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  785. imageView.tintColor = .white
  786. 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)
  787. banner.show()
  788. }
  789. }
  790. completion(result)
  791. }
  792. }
  793. private func changePasswordInternal(oldPassword: String, newPassword: String, completion: @escaping (Bool) -> ()) {
  794. DispatchQueue.global().async {
  795. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  796. let p_password = oldPassword
  797. let n_password = newPassword
  798. let md5Hex = Utils.getMD5(string: p_password).map { String(format: "%02hhx", $0) }.joined()
  799. let md5HexNew = Utils.getMD5(string: n_password).map { String(format: "%02hhx", $0) }.joined()
  800. var result: Bool = false
  801. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getChangePasswordInternal(p_f_pin: idMe!, pwd_en: md5HexNew, pwd_old: md5Hex)) {
  802. if response.isOk() {
  803. result = true
  804. }
  805. DispatchQueue.main.async {
  806. if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "11" {
  807. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  808. imageView.tintColor = .white
  809. let banner = FloatingNotificationBanner(title: "Username or password does not match".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)
  810. banner.show()
  811. } else if response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "20" {
  812. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  813. imageView.tintColor = .white
  814. let banner = FloatingNotificationBanner(title: "Invalid password".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)
  815. banner.show()
  816. }
  817. }
  818. } else {
  819. DispatchQueue.main.async {
  820. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  821. imageView.tintColor = .white
  822. 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)
  823. banner.show()
  824. }
  825. }
  826. completion(result)
  827. }
  828. }
  829. }
  830. // MARK: - Item
  831. struct Item: Hashable {
  832. static func == (lhs: Item, rhs: Item) -> Bool {
  833. return lhs.title == rhs.title
  834. }
  835. var icon: UIImage?
  836. var title = ""
  837. static var sections: [String] {
  838. return ["Personal", "Call", "Version"]
  839. }
  840. static var menus: [String: [Item]] = [:]
  841. static func menuFor(section: Int) -> [Item] {
  842. let sec = sections[section]
  843. if let arr = menus[sec] {
  844. return arr
  845. }
  846. return []
  847. }
  848. }
  849. extension FourthTabViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  850. public func numberOfComponents(in pickerView: UIPickerView) -> Int {
  851. return 1
  852. }
  853. public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  854. return language.count
  855. }
  856. public func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  857. return 60
  858. }
  859. public func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  860. let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 10, height: 30))
  861. label.text = (language[row]).keys.first
  862. label.sizeToFit()
  863. return label
  864. }
  865. }