ProfileViewController.swift 38 KB

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