ProfileViewController.swift 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. //
  2. // ProfileViewController.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 17/09/21.
  6. //
  7. import UIKit
  8. import NotificationBannerSwift
  9. import nuSDKService
  10. public class ProfileViewController: UITableViewController {
  11. @IBOutlet weak var profile: UIImageView!
  12. @IBOutlet weak var call: UIButton!
  13. @IBOutlet weak var video: UIButton!
  14. @IBOutlet weak var message: UIButton!
  15. @IBOutlet weak var viewUserType: UIView!
  16. @IBOutlet weak var imageUserType: UIImageView!
  17. @IBOutlet weak var labelUserType: UILabel!
  18. @IBOutlet weak var buttonGroup: UIStackView!
  19. @IBOutlet weak var myViewGroup: UIView!
  20. @IBOutlet weak var switchPrivateAccount: UISwitch!
  21. @IBOutlet weak var buttonEditPass: UIButton!
  22. @IBOutlet weak var switchAcceptCall: UISwitch!
  23. @IBOutlet weak var buttonHistoryCC: UIButton!
  24. @IBOutlet weak var viewFriend: UIView!
  25. @IBOutlet weak var countFriend: UILabel!
  26. @IBOutlet weak var labelPrivateAccount: UILabel!
  27. @IBOutlet weak var labelChangePassword: UILabel!
  28. @IBOutlet weak var buttonEditpass: UIButton!
  29. @IBOutlet weak var labelAcceptCall: UILabel!
  30. private var imageVideoPicker : ImageVideoPicker!
  31. public enum Flag {
  32. case me
  33. case friend
  34. case invite
  35. }
  36. var user: User?
  37. public var data: String = ""
  38. public var flag: Flag = Flag.friend
  39. var name: String = ""
  40. var picture: String = ""
  41. var checkReadMessage: (() -> ())?
  42. public var isDismiss: (() -> ())?
  43. public var dismissImage: ((UIImage, String) -> ())?
  44. var fromRootViewController = false
  45. var isBNI = false
  46. var fromListFriend = false
  47. var isLoadingAddFriend = false
  48. var publicBanner = FloatingNotificationBanner()
  49. var fromAPI = false
  50. var timerSwitchPA = Timer()
  51. var timerSwitchAC = Timer()
  52. private func reload() {
  53. if let user = self.user {
  54. self.navigationController?.navigationBar.topItem?.title = "\(user.firstName) \(user.lastName)"
  55. self.navigationController?.navigationBar.setNeedsLayout()
  56. self.title = "\(user.firstName) \(user.lastName)"
  57. if !user.thumb.isEmpty {
  58. self.profile.setImage(name: user.thumb)
  59. }
  60. } else {
  61. getData { user in
  62. self.user = user
  63. DispatchQueue.main.async {
  64. guard let user = user else {
  65. return
  66. }
  67. if let me = User.getMyPin(), me == self.data || self.flag == Flag.me {
  68. Database.shared.database?.inTransaction({ fmdb, rollback in
  69. do {
  70. let idMe = User.getMyPin()!
  71. if let cursorCount = Database.shared.getRecords(fmdb: fmdb, query: "select COUNT(*) from BUDDY where f_pin <> '\(idMe)' and first_name NOT LIKE 'USR%' "), cursorCount.next() {
  72. let count = cursorCount.string(forColumnIndex: 0)!
  73. self.countFriend.text = count + " " + "Friends".localized()
  74. self.countFriend.font = .systemFont(ofSize: 12)
  75. self.viewFriend.layer.cornerRadius = 5.0
  76. self.viewFriend.clipsToBounds = true
  77. self.viewFriend.isHidden = false
  78. self.viewFriend.isUserInteractionEnabled = true
  79. self.viewFriend.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.friendsTapped)))
  80. cursorCount.close()
  81. }
  82. } catch {
  83. rollback.pointee = true
  84. print("Access database error: \(error.localizedDescription)")
  85. }
  86. })
  87. }
  88. if User.isOfficialRegular(official_account: user.official ?? "") || User.isOfficial(official_account: user.official ?? "") || User.isVerified(official_account: user.official ?? "") || User.isCallCenter(userType: user.userType ?? "") || User.isInternal(userType: user.userType ?? "") {
  89. self.viewUserType.layer.cornerRadius = 5.0
  90. self.viewUserType.clipsToBounds = true
  91. self.viewUserType.isHidden = false
  92. if User.isOfficialRegular(official_account: user.official ?? "") || User.isOfficial(official_account: user.official ?? "") {
  93. self.imageUserType.image = UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  94. self.labelUserType.text = "Official".localized()
  95. } else if User.isVerified(official_account: user.official ?? "") {
  96. self.imageUserType.image = UIImage(named: "ic_verified", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  97. self.labelUserType.text = "Verified".localized()
  98. } else if User.isCallCenter(userType: user.userType ?? "") {
  99. let dataCategory = CategoryCC.getDataFromServiceId(service_id: user.ex_offmp!)
  100. self.imageUserType.image = UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  101. // if dataCategory != nil {
  102. // self.labelUserType.text = "Call Center (\(dataCategory!.service_name))".localized()
  103. // } else {
  104. self.labelUserType.text = "Call Center".localized()
  105. // }
  106. // self.buttonHistoryCC.isHidden = true
  107. }
  108. }
  109. self.navigationController?.navigationBar.topItem?.title = "\(user.firstName) \(user.lastName)"
  110. self.navigationController?.navigationBar.setNeedsLayout()
  111. self.title = "\(user.firstName) \(user.lastName)"
  112. if !user.thumb.isEmpty {
  113. self.profile.setImage(name: user.thumb)
  114. }
  115. }
  116. }
  117. }
  118. }
  119. private func getData(completion: @escaping (User?) -> ()) {
  120. do {
  121. DispatchQueue.global().async {
  122. var r: User?
  123. r = User.getData(pin: self.data)
  124. Database.shared.database?.inTransaction({ fmdb, rollback in
  125. do {
  126. let idMe = User.getMyPin()!
  127. if let cursorCount = Database.shared.getRecords(fmdb: fmdb, query: "select COUNT(*) from BUDDY where f_pin <> '\(idMe)' and first_name NOT LIKE 'USR%' "), cursorCount.next() {
  128. DispatchQueue.main.async {
  129. self.countFriend.text = cursorCount.string(forColumnIndex: 0) ?? "" + " " + "Friends".localized()
  130. }
  131. cursorCount.close()
  132. }
  133. } catch {
  134. rollback.pointee = true
  135. print("Access database error: \(error.localizedDescription)")
  136. }
  137. })
  138. completion(r)
  139. }
  140. } catch {
  141. }
  142. }
  143. public override func viewWillDisappear(_ animated: Bool) {
  144. if self.isMovingFromParent {
  145. self.checkReadMessage?()
  146. }
  147. }
  148. public override func viewWillAppear(_ animated: Bool) {
  149. // if navigationController?.navigationBar.backgroundColor != .clear || self.traitCollection.userInterfaceStyle == .dark {
  150. navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  151. navigationController?.navigationBar.tintColor = .white
  152. navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
  153. // } else {
  154. // navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
  155. // navigationController?.navigationBar.tintColor = .black
  156. // navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
  157. // }
  158. if fromListFriend {
  159. if let me = User.getMyPin(), me == self.data || self.flag == Flag.me {
  160. Database.shared.database?.inTransaction({ fmdb, rollback in
  161. let idMe = User.getMyPin()!
  162. if let cursorCount = Database.shared.getRecords(fmdb: fmdb, query: "select COUNT(*) from BUDDY where f_pin <> '\(idMe)' and first_name NOT LIKE 'USR%'"), cursorCount.next() {
  163. let count = cursorCount.string(forColumnIndex: 0)!
  164. self.countFriend.text = count + " " + "Friends".localized()
  165. cursorCount.close()
  166. }
  167. })
  168. }
  169. }
  170. }
  171. public override func viewDidDisappear(_ animated: Bool) {
  172. navigationController?.navigationBar.titleTextAttributes = nil
  173. }
  174. public override func viewDidAppear(_ animated: Bool) {
  175. if let me = User.getMyPin(), me == data || flag == Flag.me || flag == Flag.friend {
  176. reload()
  177. }
  178. }
  179. public override func viewDidLoad() {
  180. super.viewDidLoad()
  181. pullBuddy()
  182. profile.circle()
  183. profile.contentMode = .scaleAspectFill
  184. profile.isUserInteractionEnabled = true
  185. profile.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(profileTapped)))
  186. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  187. let navBarAppearance = UINavigationBarAppearance()
  188. navBarAppearance.configureWithOpaqueBackground()
  189. navBarAppearance.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : UIColor.mainColor
  190. navBarAppearance.titleTextAttributes = attributes
  191. navigationController?.navigationBar.standardAppearance = navBarAppearance
  192. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  193. if fromAPI {
  194. let backButton = UIBarButtonItem(image: UIImage(systemName: "chevron.backward"), style: .plain, target: self, action: #selector(self.didTapExit))
  195. self.navigationItem.leftBarButtonItem = backButton
  196. }
  197. // self.view.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .black : .white
  198. if fromRootViewController {
  199. navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didTapExit(sender:)))
  200. }
  201. let myData = User.getData(pin: self.data)
  202. labelPrivateAccount.text = "Private Account Mode".localized()
  203. labelChangePassword.text = "Change Password".localized()
  204. labelAcceptCall.text = "Accept Call".localized()
  205. buttonHistoryCC.setAttributedTitle(NSAttributedString(string: "Call Center History".localized(), attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor : self.traitCollection.userInterfaceStyle == .dark ? UIColor.blackDarkMode : UIColor.white]), for: .normal)
  206. navigationController?.navigationBar.topItem?.backButtonTitle = "Back".localized()
  207. switchPrivateAccount.onTintColor = .mainColor
  208. switchAcceptCall.onTintColor = .mainColor
  209. buttonEditpass.tintColor = self.traitCollection.userInterfaceStyle == .dark ? .white : .black
  210. buttonHistoryCC.tintColor = self.traitCollection.userInterfaceStyle == .dark ? .secondaryColor : .black
  211. if let me = User.getMyPin(), me == data || flag == Flag.me {
  212. buttonGroup.removeFromSuperview()
  213. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit".localized(), style: .plain, target: self, action: #selector(didTapEdit(sender:)))
  214. imageVideoPicker = ImageVideoPicker(presentationController: self, delegate: self)
  215. buttonEditPass.addTarget(self, action: #selector(editPassword(sender:)), for: .touchUpInside)
  216. buttonHistoryCC.addTarget(self, action: #selector(historyCC(sender:)), for: .touchUpInside)
  217. if myData?.privacy_flag == "1" {
  218. switchPrivateAccount.setOn(true, animated: false)
  219. }
  220. if myData?.offline_mode == "1" {
  221. switchAcceptCall.setOn(false, animated: false)
  222. }
  223. switchPrivateAccount.addTarget(self, action: #selector(privateAccountSwitch), for: .valueChanged)
  224. switchAcceptCall.addTarget(self, action: #selector(acceptCallSwitch), for: .valueChanged)
  225. } else if flag == Flag.invite {
  226. navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(didTapAdd(sender:)))
  227. call.isEnabled = false
  228. video.isEnabled = false
  229. message.isEnabled = false
  230. myViewGroup.removeFromSuperview()
  231. buttonGroup.removeFromSuperview()
  232. title = name
  233. profile.setImage(name: picture)
  234. } else if flag == Flag.friend {
  235. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "person.crop.circle.badge.xmark"), style: .plain, target: self, action: #selector(didTapUnfriend(sender:)))
  236. if !isBNI {
  237. call.addTarget(self, action: #selector(call(sender:)), for: .touchUpInside)
  238. video.addTarget(self, action: #selector(video(sender:)), for: .touchUpInside)
  239. message.addTarget(self, action: #selector(chat(sender:)), for: .touchUpInside)
  240. } else {
  241. call.isEnabled = false
  242. video.isEnabled = false
  243. message.isEnabled = false
  244. buttonGroup.removeFromSuperview()
  245. }
  246. myViewGroup.removeFromSuperview()
  247. }
  248. }
  249. @objc func acceptCallSwitch(mySwitch: UISwitch) {
  250. timerSwitchAC.invalidate()
  251. timerSwitchAC = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { [self] _ in
  252. let value = mySwitch.isOn
  253. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  254. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  255. imageView.tintColor = .white
  256. publicBanner.dismiss()
  257. publicBanner = 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)
  258. publicBanner.show()
  259. self.switchPrivateAccount.setOn(!value, animated: true)
  260. return
  261. }
  262. DispatchQueue.global().async {
  263. let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: self.data)
  264. tMessage.mBodies[CoreMessage_TMessageKey.OFFLINE_MODE] = value ? "0" : "1"
  265. if let resp = Nexilis.writeAndWait(message: tMessage) {
  266. if resp.isOk() {
  267. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  268. do {
  269. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: [
  270. "offline_mode" : value ? "0" : "1"
  271. ], _where: "f_pin = '\(self.data)'")
  272. } catch {
  273. rollback.pointee = true
  274. print("Access database error: \(error.localizedDescription)")
  275. }
  276. })
  277. DispatchQueue.main.async { [self] in
  278. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  279. imageView.tintColor = .white
  280. publicBanner.dismiss()
  281. publicBanner = 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)
  282. publicBanner.show()
  283. }
  284. } else {
  285. DispatchQueue.main.async { [self] in
  286. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  287. imageView.tintColor = .white
  288. publicBanner.dismiss()
  289. publicBanner = 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: .center)
  290. publicBanner.show()
  291. self.switchPrivateAccount.setOn(!value, animated: true)
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. @objc func privateAccountSwitch(mySwitch: UISwitch) {
  299. timerSwitchPA.invalidate()
  300. timerSwitchPA = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { [self] _ in
  301. let value = mySwitch.isOn
  302. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  303. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  304. imageView.tintColor = .white
  305. publicBanner.dismiss()
  306. publicBanner = 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)
  307. publicBanner.show()
  308. self.switchPrivateAccount.setOn(!value, animated: true)
  309. return
  310. }
  311. DispatchQueue.global().async {
  312. let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: self.data)
  313. tMessage.mBodies[CoreMessage_TMessageKey.PRIVACY_FLAG] = value ? "1" : "0"
  314. if let resp = Nexilis.writeAndWait(message: tMessage) {
  315. if resp.isOk() {
  316. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  317. do {
  318. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: [
  319. "privacy_flag" : value ? "1" : "0"
  320. ], _where: "f_pin = '\(self.data)'")
  321. } catch {
  322. rollback.pointee = true
  323. print("Access database error: \(error.localizedDescription)")
  324. }
  325. })
  326. DispatchQueue.main.async { [self] in
  327. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  328. imageView.tintColor = .white
  329. publicBanner.dismiss()
  330. publicBanner = 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)
  331. publicBanner.show()
  332. }
  333. } else {
  334. DispatchQueue.main.async { [self] in
  335. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  336. imageView.tintColor = .white
  337. publicBanner.dismiss()
  338. publicBanner = 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: .center)
  339. publicBanner.show()
  340. self.switchPrivateAccount.setOn(!value, animated: true)
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. @objc func editPassword(sender: Any) {
  348. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changePWD") as! ChangePasswordViewController
  349. navigationController?.show(controller, sender: nil)
  350. }
  351. @objc func historyCC(sender: Any) {
  352. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "myHistoryCC") as! HistoryCCViewController
  353. if user?.userType == "24" {
  354. controller.isOfficer = true
  355. } else {
  356. controller.isOfficer = false
  357. }
  358. navigationController?.show(controller, sender: nil)
  359. }
  360. @objc func call(sender: Any) {
  361. if !Nexilis.checkingAccess(key: "audio_call") {
  362. showToast(message: "Feature disabled..".localized(), font: UIFont.systemFont(ofSize: 12), controller: self)
  363. return
  364. }
  365. let myData = User.getData(pin: self.data)
  366. if myData?.ex_block == "1" || myData?.ex_block == "-1" {
  367. var title = "You blocked this user".localized()
  368. if myData?.ex_block == "-1" {
  369. title = "You have been blocked by this user".localized()
  370. }
  371. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  372. imageView.tintColor = .white
  373. publicBanner.dismiss()
  374. publicBanner = FloatingNotificationBanner(title: title, 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)
  375. publicBanner.show()
  376. return
  377. }
  378. if !CheckConnection.isConnectedToNetwork() {
  379. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  380. imageView.tintColor = .white
  381. publicBanner.dismiss()
  382. publicBanner = 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)
  383. publicBanner.show()
  384. return
  385. }
  386. let controller = QmeraAudioViewController()
  387. controller.user = user
  388. controller.isOutgoing = true
  389. controller.modalPresentationStyle = .overCurrentContext
  390. present(controller, animated: true, completion: nil)
  391. }
  392. @objc func video(sender: Any) {
  393. if !Nexilis.checkingAccess(key: "video_call") {
  394. showToast(message: "Feature disabled..".localized(), font: UIFont.systemFont(ofSize: 12), controller: self)
  395. return
  396. }
  397. let myData = User.getData(pin: self.data)
  398. if myData?.ex_block == "1" || myData?.ex_block == "-1" {
  399. var title = "You blocked this user".localized()
  400. if myData?.ex_block == "-1" {
  401. title = "You have been blocked by this user".localized()
  402. }
  403. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  404. imageView.tintColor = .white
  405. publicBanner.dismiss()
  406. publicBanner = FloatingNotificationBanner(title: title, 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)
  407. publicBanner.show()
  408. return
  409. }
  410. if !CheckConnection.isConnectedToNetwork() {
  411. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  412. imageView.tintColor = .white
  413. publicBanner.dismiss()
  414. publicBanner = 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)
  415. publicBanner.show()
  416. return
  417. }
  418. if let user = user {
  419. let videoVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "videoVCQmera") as! QmeraVideoViewController
  420. videoVC.fPin = user.pin
  421. self.show(videoVC, sender: nil)
  422. }
  423. }
  424. @objc func chat(sender: Any) {
  425. if let _ = previousViewController as? EditorPersonal {
  426. navigationController?.popViewController(animated: true)
  427. return
  428. }
  429. if let user = self.user {
  430. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  431. editorPersonalVC.hidesBottomBarWhenPushed = true
  432. editorPersonalVC.unique_l_pin = user.pin
  433. navigationController?.show(editorPersonalVC, sender: nil)
  434. }
  435. }
  436. private func addFriend(completion: @escaping (Bool) -> ()) {
  437. DispatchQueue.global().async {
  438. guard !self.data.isEmpty else {
  439. completion(false)
  440. return
  441. }
  442. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddFriendQRCode(fpin: self.data)), response.isOk() {
  443. completion(true)
  444. } else {
  445. completion(false)
  446. }
  447. }
  448. }
  449. private func unFriend(completion: @escaping (Bool) -> ()) {
  450. DispatchQueue.global().async {
  451. guard !self.data.isEmpty else {
  452. completion(false)
  453. return
  454. }
  455. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.removeFriend(lpin: self.user!.pin)), response.isOk() {
  456. completion(true)
  457. } else {
  458. completion(false)
  459. }
  460. }
  461. }
  462. func didTapProfile() {
  463. if let userImage = user?.thumb {
  464. if !userImage.isEmpty {
  465. let firstAlert = LibAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  466. firstAlert.addAction(UIAlertAction(title: "Change Profile Picture".localized(), style: .default, handler: { action in
  467. let alert = LibAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  468. alert.addAction(UIAlertAction(title: "Take Photo".localized(), style: .default, handler: { action in
  469. self.imageVideoPicker.present(source: .imageCamera)
  470. }))
  471. alert.addAction(UIAlertAction(title: "Choose Photo".localized(), style: .default, handler: { action in
  472. self.imageVideoPicker.present(source: .imageAlbum)
  473. }))
  474. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: { action in
  475. }))
  476. self.navigationController?.present(alert, animated: true)
  477. }))
  478. firstAlert.addAction(UIAlertAction(title: "Remove Profile Picture".localized(), style: .default, handler: { action in
  479. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getChangePersonImage(thumb_id: "")), response.isOk() {
  480. guard let me = User.getMyPin() else {
  481. return
  482. }
  483. Database.shared.database?.inTransaction({ fmdb, rollback in
  484. do {
  485. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: ["image_id": ""], _where: "f_pin = '\(me)'")
  486. } catch {
  487. rollback.pointee = true
  488. print("Access database error: \(error.localizedDescription)")
  489. }
  490. })
  491. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateFifthTab"), object: nil, userInfo: nil)
  492. DispatchQueue.main.async { [self] in
  493. self.profile.image = UIImage(systemName: "person.circle.fill")!
  494. self.profile.backgroundColor = .white
  495. self.user?.thumb = ""
  496. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  497. imageView.tintColor = .white
  498. publicBanner.dismiss()
  499. publicBanner = FloatingNotificationBanner(title: "Successfully removed profile picture".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)
  500. publicBanner.show()
  501. self.dismissImage?(UIImage(systemName: "person.circle.fill")!, "")
  502. }
  503. }
  504. }))
  505. firstAlert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  506. self.navigationController?.present(firstAlert, animated: true)
  507. } else {
  508. let alert = LibAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  509. alert.addAction(UIAlertAction(title: "Take Photo".localized(), style: .default, handler: { action in
  510. self.imageVideoPicker.present(source: .imageCamera)
  511. }))
  512. alert.addAction(UIAlertAction(title: "Choose Photo".localized(), style: .default, handler: { action in
  513. self.imageVideoPicker.present(source: .imageAlbum)
  514. }))
  515. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: { action in
  516. }))
  517. self.navigationController?.present(alert, animated: true)
  518. }
  519. }
  520. }
  521. @objc func didTapAdd(sender: Any) {
  522. if isLoadingAddFriend {
  523. return
  524. }
  525. Nexilis.showLoader()
  526. isLoadingAddFriend = true
  527. addFriend { result in
  528. DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
  529. Nexilis.hideLoader { [self] in
  530. if result {
  531. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  532. imageView.tintColor = .white
  533. publicBanner.dismiss()
  534. publicBanner = FloatingNotificationBanner(title: "Successfully add friend".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)
  535. publicBanner.show()
  536. self.isDismiss?()
  537. self.navigationController?.popViewController(animated: true)
  538. } else {
  539. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  540. imageView.tintColor = .white
  541. publicBanner.dismiss()
  542. publicBanner = FloatingNotificationBanner(title: "Server busy, please 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)
  543. publicBanner.show()
  544. self.isLoadingAddFriend = false
  545. }
  546. }
  547. })
  548. }
  549. }
  550. @objc func didTapUnfriend(sender: Any) {
  551. if call != nil {
  552. call.isEnabled = false
  553. video.isEnabled = false
  554. message.isEnabled = false
  555. }
  556. Nexilis.shared.stateUnfriend = self.data
  557. let alert = LibAlertController(title: "", message: "Are you sure to unfriend".localized() + " \(self.user!.fullName)", preferredStyle: .alert)
  558. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  559. if self.call != nil {
  560. self.call.isEnabled = true
  561. self.video.isEnabled = true
  562. self.message.isEnabled = true
  563. }
  564. Nexilis.shared.stateUnfriend = ""
  565. } ))
  566. alert.addAction(UIAlertAction(title: "Delete".localized(), style: .destructive, handler: {(_) in
  567. Nexilis.showLoader()
  568. self.unFriend { result in
  569. DispatchQueue.main.async { [self] in
  570. if result {
  571. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  572. do {
  573. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select * from BUDDY where f_pin = '\(self.data)'"), cursor.next() {
  574. _ = Database.shared.deleteRecord(fmdb: fmdb, table: "BUDDY", _where: "f_pin = '\(self.data)'")
  575. _ = Database.shared.deleteRecord(fmdb: fmdb, table: "MESSAGE_SUMMARY", _where: "l_pin='\(self.data)'")
  576. _ = Database.shared.deleteRecord(fmdb: fmdb, table: "MESSAGE", _where: "(f_pin='\(self.data)' or l_pin='\(self.data)') and message_scope_id='3'")
  577. cursor.close()
  578. }
  579. Nexilis.hideLoader(completion: {
  580. if self.previousViewController is GroupDetailViewController || self.isBNI {
  581. self.isDismiss?()
  582. self.navigationController?.popViewController(animated: true)
  583. } else {
  584. if let editor = self.previousViewController as? EditorPersonal {
  585. editor.afterUnfriend()
  586. } else if let editor = self.previousViewController as? EditorGroup {
  587. editor.afterUnfriend()
  588. }
  589. self.navigationController?.popToRootViewController(animated: true)
  590. }
  591. Nexilis.shared.stateUnfriend = ""
  592. })
  593. } catch {
  594. rollback.pointee = true
  595. print("Access database error: \(error.localizedDescription)")
  596. }
  597. })
  598. } else {
  599. if self.call != nil {
  600. self.call.isEnabled = true
  601. self.video.isEnabled = true
  602. self.message.isEnabled = true
  603. }
  604. Nexilis.shared.stateUnfriend = ""
  605. Nexilis.hideLoader(completion: {})
  606. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  607. imageView.tintColor = .white
  608. publicBanner.dismiss()
  609. publicBanner = FloatingNotificationBanner(title: "Server busy, please 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)
  610. publicBanner.show()
  611. }
  612. }
  613. }
  614. }))
  615. self.present(alert, animated: true, completion: nil)
  616. }
  617. @objc func didTapExit(sender: Any) {
  618. self.dismiss(animated: true, completion: nil)
  619. }
  620. @objc func didTapEdit(sender: Any) {
  621. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeNameView") as! ChangeNameTableViewController
  622. controller.data = data
  623. controller.isDismiss = {
  624. self.getData { user in
  625. self.user = user
  626. DispatchQueue.main.async {
  627. guard let user = user else {
  628. return
  629. }
  630. self.title = "\(user.firstName) \(user.lastName)"
  631. if !user.thumb.isEmpty {
  632. self.profile.setImage(name: user.thumb)
  633. }
  634. }
  635. }
  636. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  637. imageView.tintColor = .white
  638. self.publicBanner.dismiss()
  639. self.publicBanner = FloatingNotificationBanner(title: "Successfully changed name".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)
  640. self.publicBanner.show()
  641. }
  642. navigationItem.backButtonTitle = ""
  643. navigationController?.show(controller, sender: nil)
  644. }
  645. @objc func profileTapped() {
  646. if let me = User.getMyPin(), me == data || flag == Flag.me {
  647. didTapProfile()
  648. }
  649. }
  650. @objc func friendsTapped() {
  651. if let me = User.getMyPin(), me == data || flag == Flag.me {
  652. let controller = QmeraCallContactViewController()
  653. controller.isInviteCC = true
  654. controller.listFriends = true
  655. show(controller, sender: nil)
  656. fromListFriend = true
  657. }
  658. }
  659. public override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  660. if indexPath.section == 1 {
  661. if let me = User.getMyPin(), me == data || flag == Flag.me {
  662. return 170
  663. }
  664. return 56
  665. }
  666. return 200
  667. }
  668. private func pullBuddy() {
  669. if let me = User.getMyPin() {
  670. DispatchQueue.global().async {
  671. let _ = Nexilis.write(message: CoreMessage_TMessageBank.getBatchBuddiesInfos(p_f_pin: me, last_update: 0))
  672. }
  673. }
  674. }
  675. }
  676. extension ProfileViewController: ImageVideoPickerDelegate {
  677. public func didSelect(imagevideo: Any?) {
  678. if let info = imagevideo as? [UIImagePickerController.InfoKey: Any], let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
  679. guard let me = User.getMyPin() else {
  680. return
  681. }
  682. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
  683. Nexilis.showLoader()
  684. DispatchQueue.global().async {
  685. let resize = image.resize(target: CGSize(width: 800, height: 600))
  686. let documentDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  687. let fileDir = documentDir.appendingPathComponent("THUMB_\(me)\(Date().currentTimeMillis().toHex())")
  688. if !FileManager.default.fileExists(atPath: fileDir.path), let data = resize.jpegData(compressionQuality: 0.8) {
  689. try! data.write(to: fileDir)
  690. Network().uploadHTTP(name: fileDir.lastPathComponent) { result, progress , response in
  691. guard result else {
  692. DispatchQueue.main.async {
  693. Nexilis.hideLoader(completion: { [self] in
  694. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  695. imageView.tintColor = .white
  696. publicBanner.dismiss()
  697. publicBanner = FloatingNotificationBanner(title: "Can't change profile picture, 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)
  698. publicBanner.show()
  699. self.dismissImage?(image, fileDir.lastPathComponent)
  700. })
  701. }
  702. return
  703. }
  704. guard progress == 100 else {
  705. return
  706. }
  707. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getChangePersonImage(thumb_id: fileDir.lastPathComponent)), response.isOk() {
  708. Database.shared.database?.inTransaction({ fmdb, rollback in
  709. do {
  710. _ = Database.shared.updateRecord(fmdb: fmdb, table: "BUDDY", cvalues: ["image_id": fileDir.lastPathComponent], _where: "f_pin = '\(me)'")
  711. } catch {
  712. rollback.pointee = true
  713. print("Access database error: \(error.localizedDescription)")
  714. }
  715. })
  716. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateFifthTab"), object: nil, userInfo: nil)
  717. DispatchQueue.main.async {
  718. Nexilis.hideLoader(completion: { [self] in
  719. self.profile.image = image
  720. let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
  721. self.user?.thumb = fileDir.lastPathComponent
  722. imageView.tintColor = .white
  723. publicBanner.dismiss()
  724. publicBanner = FloatingNotificationBanner(title: "Successfully changed profile picture".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)
  725. publicBanner.show()
  726. self.dismissImage?(image, fileDir.lastPathComponent)
  727. })
  728. }
  729. } else {
  730. Nexilis.hideLoader(completion: {})
  731. }
  732. }
  733. } else {
  734. Nexilis.hideLoader(completion: {})
  735. }
  736. }
  737. })
  738. }
  739. }
  740. }
  741. //if auto {
  742. // DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
  743. // let objectTapAuto = ObjectGesture()
  744. // objectTapAuto.image_id = imageChat
  745. // self.contentMessageTapped(objectTap)
  746. // })
  747. //}