ProfileViewController.swift 36 KB

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