1
0

SettingsViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // SettingsViewController.swift
  3. // AIPaint
  4. //
  5. // Created by Fengyu He on 2022/11/29.
  6. //
  7. import UIKit
  8. import SnapKit
  9. import SwiftyJSON
  10. import AuthenticationServices
  11. class SettingsViewController: UIViewController, UITextFieldDelegate {
  12. var settingsTableView: UITableView!
  13. var headers: [String]!
  14. var settingsSection: Int!
  15. let welcomeView: UIView = {
  16. var view = UIView()
  17. view.layer.borderWidth = 1
  18. view.layer.borderColor = UIColor.systemGray.cgColor
  19. view.layer.cornerRadius = 14
  20. return view
  21. }()
  22. lazy var welcomeText: UILabel = {
  23. var label = UILabel()
  24. label.numberOfLines = 0
  25. label.lineBreakMode = .byWordWrapping
  26. label.text = "感谢你参与画酱的封闭测试,见证画酱的诞生!\n你可以加入QQ群来对本 App 提出宝贵意见:390674039 \n此 App 目前还在初期稳定行测试阶段,如果你有什么功能上的需求,或是对于 App 的设计有任何的建议,欢迎进群与我讨论!\n \n暂时还没适配暗黑模式,请在正常模式使用此 App。"
  27. return label
  28. }()
  29. lazy var userInfoCell = UserInfoCell(style: .default, reuseIdentifier: "UserInfo")
  30. lazy var userIdLabel: UILabel = {
  31. var label = UILabel()
  32. return label
  33. }()
  34. var signInWithAppleButton = ASAuthorizationAppleIDButton(authorizationButtonType: .default, authorizationButtonStyle: .black)
  35. let userDefaults = UserDefaults.standard
  36. let netTools = NetTools()
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. if UITraitCollection.current.userInterfaceStyle == .dark {
  40. view.backgroundColor = .black
  41. welcomeText.textColor = .white
  42. userIdLabel.textColor = .white
  43. } else {
  44. view.backgroundColor = .white
  45. welcomeText.textColor = .black
  46. userIdLabel.textColor = .black
  47. }
  48. self.title = "设置"
  49. self.settingsTableView = UITableView(frame: self.view.frame, style: .insetGrouped)
  50. self.settingsTableView.delegate = self
  51. self.settingsTableView.dataSource = self
  52. self.settingsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "SettingsCell")
  53. view.addSubview(settingsTableView)
  54. headers = [
  55. "用户协议",
  56. "关于"
  57. ]
  58. settingsSection = userDefaults.bool(forKey: UserDefaultKeys.UserInfo.isLogin) ? 3 : 2
  59. signInWithAppleButton.cornerRadius = 8.0
  60. signInWithAppleButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
  61. view.addSubview(signInWithAppleButton)
  62. if #available(iOS 15.0, *) {
  63. signInWithAppleButton.isHidden = false
  64. } else {
  65. signInWithAppleButton.isHidden = true
  66. }
  67. signInWithAppleButton.snp.makeConstraints { (make) in
  68. make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(5)
  69. make.centerX.equalTo(view.safeAreaLayoutGuide.snp.centerX)
  70. make.width.equalTo(view.safeAreaLayoutGuide.snp.width).multipliedBy(0.35)
  71. make.height.equalTo(view.safeAreaLayoutGuide.snp.height).multipliedBy(0.05)
  72. }
  73. }
  74. override func viewDidAppear(_ animated: Bool) {
  75. if userDefaults.bool(forKey: UserDefaultKeys.UserInfo.isLogin) {
  76. DispatchQueue.main.async { [self] in
  77. signInWithAppleButton.isHidden = true
  78. userInfoCell.setNickNameForCell(nickname: userDefaults.string(forKey: UserDefaultKeys.UserInfo.nickname))
  79. }
  80. }
  81. }
  82. func performExistingAccountSetupFlows() {
  83. if #available(iOS 13.0, *) {
  84. let request = [ASAuthorizationAppleIDProvider().createRequest(), ASAuthorizationPasswordProvider().createRequest()]
  85. let authorizationController = ASAuthorizationController(authorizationRequests: request)
  86. authorizationController.delegate = self
  87. authorizationController.presentationContextProvider = self
  88. authorizationController.performRequests()
  89. } else {
  90. // Fallback on earlier versions
  91. }
  92. }
  93. // @objc func renameAlert() {
  94. // let alertController = UIAlertController(title: "修改昵称", message: nil, preferredStyle: .alert)
  95. // let cancelAction = UIAlertAction(title: "取消", style: .cancel)
  96. // let confirmAction = UIAlertAction(title: "确定", style: .default, handler: { [self] action in
  97. // let textField = alertController.textFields![0]
  98. // changeNickName(userId: self.userDefault.string(forKey: UserDefaultKeys.UserInfo.userId)!, nickName: textField.text!) { [self] updatedNickName in
  99. // DispatchQueue.main.async { [self] in
  100. //// nickNameLabel.text = updatedNickName
  101. // }
  102. // userDefault.set(updatedNickName, forKey: UserDefaultKeys.UserInfo.nickname)
  103. // }
  104. //// self.nickNameLabel.text = textField.text
  105. // })
  106. // alertController.addTextField { (textField) in
  107. // textField.delegate = self
  108. // }
  109. // alertController.addAction(cancelAction)
  110. // alertController.addAction(confirmAction)
  111. // self.present(alertController, animated: true)
  112. // }
  113. @objc func handleAuthorizationAppleIDButtonPress() {
  114. let provider = ASAuthorizationAppleIDProvider()
  115. let request = provider.createRequest()
  116. request.requestedScopes = [.email, .fullName]
  117. let authorizationController = ASAuthorizationController(authorizationRequests: [request])
  118. authorizationController.delegate = self
  119. authorizationController.presentationContextProvider = self
  120. authorizationController.performRequests()
  121. }
  122. // func changeNickName(userId: String, nickName: String, complition: @escaping(_ updatedNickname: String) -> Void) {
  123. // let task = URLSession.shared.dataTask(with: netTools.get(url: "http://hefengyu.org:8080/user/changeNickname?", parameters: ["userId": userId, "nickname": nickName])) { (data, response, error) in
  124. // guard let unwrapedData = data else {
  125. // return
  126. // }
  127. // guard let updatedName = String(data: unwrapedData, encoding: .utf8) else {
  128. // return
  129. // }
  130. // print(updatedName)
  131. // if updatedName == nickName {
  132. // complition(updatedName)
  133. // } else {
  134. // complition(self.nickNameLabel.text!)
  135. // }
  136. // }
  137. // task.resume()
  138. // }
  139. }
  140. extension SettingsViewController: UITableViewDataSource, UITableViewDelegate {
  141. func numberOfSections(in tableView: UITableView) -> Int {
  142. return settingsSection
  143. }
  144. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  145. switch section {
  146. case 0: return 1
  147. case 1: return 2
  148. case 2: return 1
  149. default: return 0
  150. }
  151. }
  152. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  153. return 20
  154. }
  155. // func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  156. // <#code#>
  157. // }
  158. //
  159. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  160. switch indexPath.section {
  161. case 0: return 100
  162. case 1: return 44
  163. case 2: return 44
  164. default: return 44
  165. }
  166. }
  167. //每行对应的 Cell
  168. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  169. let identifier = "Cell"
  170. var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
  171. if cell == nil {
  172. cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
  173. }
  174. if indexPath.section == 0 {
  175. userInfoCell.setValueForCell(image: UIImage(named: "DefaultAvatar"), nickname: userDefaults.string(forKey: UserDefaultKeys.UserInfo.nickname))
  176. return userInfoCell
  177. } else if indexPath.section == 1 {
  178. cell?.textLabel!.text = headers[indexPath.row]
  179. cell?.textLabel?.textAlignment = .left
  180. } else if indexPath.section == 2 {
  181. cell?.textLabel?.text = "退出登录"
  182. cell?.textLabel?.textAlignment = .center
  183. }
  184. return cell!
  185. }
  186. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  187. if indexPath.section == 2 {
  188. let optionMenu = UIAlertController(title: nil, message: "退出登录?", preferredStyle: .actionSheet)
  189. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { action in
  190. tableView.deselectRow(at: indexPath, animated: true)
  191. })
  192. let confirmAction = UIAlertAction(title: "确认", style: .default, handler: { [self] action in
  193. userDefaults.setValue(false, forKey: UserDefaultKeys.UserInfo.isLogin)
  194. userDefaults.setValue("", forKey: UserDefaultKeys.UserInfo.userId)
  195. userDefaults.setValue("", forKey: UserDefaultKeys.UserInfo.nickname)
  196. signInWithAppleButton.isHidden = false
  197. settingsSection = 2
  198. tableView.deselectRow(at: indexPath, animated: true)
  199. tableView.reloadData()
  200. })
  201. optionMenu.addAction(confirmAction)
  202. optionMenu.addAction(cancelAction)
  203. present(optionMenu, animated: true)
  204. }
  205. }
  206. }
  207. extension SettingsViewController: ASAuthorizationControllerDelegate {
  208. @available(iOS 13.0, *)
  209. func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
  210. //苹果用户唯一标识符,该值在同一个开发者账号下的所有 App 下是一样的,开发者可以用该唯一标识符与自己后台系统的账号体系绑定起来。
  211. if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
  212. let fullName = appleIDCredential.fullName
  213. var name = ""
  214. if let givenName = fullName?.givenName, let familyName = fullName?.familyName {
  215. name = "\(familyName) \(givenName)"
  216. }
  217. if name == "" {
  218. let identityToken = String(data: appleIDCredential.identityToken!, encoding: String.Encoding.utf8)!
  219. userLogin(appleToken: identityToken) { [self] isLogin, userId in
  220. if isLogin {
  221. findUserByAppleId(appleId: userId) { [self] nickname in
  222. userDefaults.set(nickname, forKey: UserDefaultKeys.UserInfo.nickname)
  223. DispatchQueue.main.async { [self] in
  224. settingsTableView.reloadSections(IndexSet(integer: 0), with: .none)
  225. }
  226. }
  227. userDefaults.set(userId, forKey: UserDefaultKeys.UserInfo.userId)
  228. userDefaults.set(isLogin, forKey: UserDefaultKeys.UserInfo.isLogin)
  229. DispatchQueue.main.async { [self] in
  230. signInWithAppleButton.isHidden = true
  231. userInfoCell.setNickNameForCell(nickname: userDefaults.string(forKey: UserDefaultKeys.UserInfo.nickname))
  232. settingsSection = 3
  233. settingsTableView.reloadData()
  234. }
  235. }
  236. }
  237. } else {
  238. let identityToken = String(data: appleIDCredential.identityToken!, encoding: String.Encoding.utf8)!
  239. userRegister(appleToken: identityToken, fullName: name) { [self] isLogin, userId in
  240. if isLogin {
  241. findUserByAppleId(appleId: userId) { [self] nickname in
  242. userDefaults.set(nickname, forKey: UserDefaultKeys.UserInfo.nickname)
  243. DispatchQueue.main.async { [self] in
  244. settingsTableView.reloadSections(IndexSet(integer: 0), with: .none)
  245. }
  246. }
  247. userDefaults.set(userId, forKey: UserDefaultKeys.UserInfo.userId)
  248. userDefaults.set(isLogin, forKey: UserDefaultKeys.UserInfo.isLogin)
  249. DispatchQueue.main.async { [self] in
  250. self.signInWithAppleButton.isHidden = true
  251. userInfoCell.setNickNameForCell(nickname: userDefaults.string(forKey: UserDefaultKeys.UserInfo.nickname))
  252. settingsSection = 3
  253. settingsTableView.reloadData()
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. func userRegister(appleToken: String, fullName: String, complition: @escaping(_ isLogin: Bool, _ userId: String) -> Void) {
  261. let task = URLSession.shared.dataTask(with: netTools.get(url: APIs.Apple.Register, parameters: ["appleToken": appleToken, "fullName": fullName])) { (data, response, error) in
  262. guard let unwrapedData = data else {
  263. complition(false, "")
  264. return
  265. }
  266. guard let userId = String(data: unwrapedData, encoding: .utf8) else {
  267. complition(false, "")
  268. return
  269. }
  270. if userId == "-1" {
  271. complition(false, "")
  272. } else {
  273. complition(true, userId)
  274. }
  275. }
  276. task.resume()
  277. }
  278. func userLogin(appleToken: String, complition: @escaping(_ isLogin: Bool, _ userId: String) -> Void) {
  279. let task = URLSession.shared.dataTask(with: netTools.get(url: APIs.Apple.Login, parameters: ["appleToken": appleToken])) { (data, response, error) in
  280. guard let unwrapedData = data else {
  281. complition(false, "")
  282. return
  283. }
  284. guard let userId = String(data: unwrapedData, encoding: .utf8) else {
  285. complition(false, "")
  286. return
  287. }
  288. if userId == "-1" {
  289. complition(false, "")
  290. } else {
  291. complition(true, userId)
  292. }
  293. }
  294. task.resume()
  295. }
  296. func findUserByAppleId(appleId: String, complition: @escaping(_ nickname: String) -> Void) {
  297. let task = URLSession.shared.dataTask(with: netTools.get(url: APIs.User.FindUserByID, parameters: ["appleId": appleId])) { (data, response, error) in
  298. guard let unwrapedData = data else {
  299. return
  300. }
  301. guard let nickName = String(data: unwrapedData, encoding: .utf8) else {
  302. return
  303. }
  304. complition(nickName)
  305. }
  306. task.resume()
  307. }
  308. func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
  309. print("Login Error")
  310. }
  311. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  312. if #available(iOS 13, *) {
  313. if self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
  314. if self.traitCollection.userInterfaceStyle == .dark {
  315. // 黑夜模式
  316. view.backgroundColor = .black
  317. welcomeText.textColor = .white
  318. userIdLabel.textColor = .white
  319. } else {
  320. // 白天模式
  321. view.backgroundColor = .white
  322. welcomeText.textColor = .black
  323. userIdLabel.textColor = .black
  324. }
  325. }
  326. }
  327. }
  328. }
  329. extension SettingsViewController: ASAuthorizationControllerPresentationContextProviding {
  330. func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
  331. return self.view.window!
  332. }
  333. }