ProfileViewController.swift 42 KB

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