ProfileViewController.swift 39 KB

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