KeyboardViewController.swift 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. //
  2. // KeyboardViewController.swift
  3. // AiKeyboard
  4. //
  5. // Created by Destiny on 2025/4/23.
  6. //
  7. import UIKit
  8. import Lottie
  9. import SnapKit
  10. import MarqueeLabel
  11. import GravityEngineSDK
  12. enum KeyboardType: Int {
  13. case help = 0 // 帮聊
  14. case teach = 1 // 教你说
  15. case prologue = 2 // 开场白
  16. }
  17. class KeyboardViewController: UIInputViewController {
  18. struct UX {
  19. static let keyboardDefaultHeight = 292.0
  20. static let keyboardWithTipsHeight = 318.0
  21. }
  22. // 选择的键盘类型
  23. var chooseType: KeyboardType = .help {
  24. didSet {
  25. UserDefaults.standard.setValue(chooseType.rawValue, forKey: "KeyboardChooseType")
  26. self.updateMainViewUI()
  27. }
  28. }
  29. // 键盘列表
  30. var keyboardList: [KeyboardModel]?
  31. // 选择的键盘
  32. var chooseKeyboard: KeyboardModel?
  33. // 人设列表
  34. var characterList: [CharacterModel]?
  35. // 开场白列表
  36. var prologueList: [PrologueModel]?
  37. // 开始持续删除
  38. private var deleteTimer: Timer?
  39. private var deleteAcceleration: TimeInterval = 0.1
  40. var heightConstriaint: NSLayoutConstraint?
  41. lazy var bgImageView: UIImageView = {
  42. let imageView = UIImageView()
  43. imageView.image = UIImage(named: "keyboard_bg")
  44. return imageView
  45. }()
  46. lazy var menuBtn: UIButton = {
  47. let button = UIButton()
  48. button.setImage(UIImage(named: "keyboard_menu_btn"), for: .normal)
  49. button.addTarget(self, action: #selector(menuBtnClickAction), for: .touchUpInside)
  50. return button
  51. }()
  52. lazy var heartAnimation: LottieAnimationView = {
  53. let animate = LottieAnimationView(name: "heart")
  54. animate.loopMode = .loop
  55. animate.backgroundColor = .clear
  56. animate.isUserInteractionEnabled = true
  57. let tap = UITapGestureRecognizer(target: self, action: #selector(heartBtnClickAction))
  58. animate.addGestureRecognizer(tap)
  59. return animate
  60. }()
  61. lazy var heartLabel: UILabel = {
  62. let label = UILabel()
  63. label.text = "30%"
  64. label.font = .boldSystemFont(ofSize: 12)
  65. label.textColor = .white
  66. label.isUserInteractionEnabled = true
  67. let tap = UITapGestureRecognizer(target: self, action: #selector(heartBtnClickAction))
  68. label.addGestureRecognizer(tap)
  69. return label
  70. }()
  71. lazy var exchangeBtn: UIButton = {
  72. let button = UIButton()
  73. button.setImage(UIImage(named: "keyboard_exchange_btn"), for: .normal)
  74. button.addTarget(self, action: #selector(exchangeBtnClickAction), for: .touchUpInside)
  75. return button
  76. }()
  77. lazy var chooseView: UIView = {
  78. let view = UIView()
  79. view.backgroundColor = .white
  80. view.layer.cornerRadius = 17
  81. return view
  82. }()
  83. lazy var functionView: UIView = {
  84. let view = UIView()
  85. view.backgroundColor = .hexStringColor(hexString: "#DDCFFD")
  86. view.layer.cornerRadius = 16
  87. view.isUserInteractionEnabled = true
  88. let tap = UITapGestureRecognizer(target: self, action: #selector(functionBtnClickAction))
  89. view.addGestureRecognizer(tap)
  90. return view
  91. }()
  92. lazy var functionLabel: UILabel = {
  93. let label = UILabel()
  94. label.text = "帮聊"
  95. label.font = .boldSystemFont(ofSize: 14)
  96. label.textColor = .hexStringColor(hexString: "#000000", alpha: 0.8)
  97. return label
  98. }()
  99. lazy var arrowIcon: UIImageView = {
  100. let imageView = UIImageView()
  101. imageView.image = UIImage(named: "icon_arrow_down")
  102. return imageView
  103. }()
  104. lazy var userChangeBtn: UIButton = {
  105. let btn = UIButton()
  106. btn.setTitle("小蕾", for: .normal)
  107. btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .medium)
  108. btn.setTitleColor(.hexStringColor(hexString: "#000000").withAlphaComponent(0.8), for: .normal)
  109. btn.setImage(UIImage(named: "keyboard_user_change_icon"), for: .normal)
  110. btn.setImageTitleLayout(.imgRight, spacing: 8)
  111. btn.addTarget(self, action: #selector(changeBtnClickAction), for: .touchUpInside)
  112. return btn
  113. }()
  114. lazy var userChangeView: UIView = {
  115. let view = UIView()
  116. let tap = UITapGestureRecognizer(target: self, action: #selector(changeBtnClickAction))
  117. view.addGestureRecognizer(tap)
  118. return view
  119. }()
  120. lazy var userChangeLabel: MarqueeLabel = {
  121. let label = MarqueeLabel(frame: .zero, duration: 3.0, fadeLength: 0)
  122. label.type = .leftRight
  123. label.text = "通用键盘"
  124. label.font = .systemFont(ofSize: 14)
  125. label.textColor = .hexStringColor(hexString: "#000000")
  126. label.textAlignment = .center
  127. return label
  128. }()
  129. lazy var userChangeIcon: UIImageView = {
  130. let icon = UIImageView()
  131. icon.image = UIImage(named: "keyboard_user_change_icon")
  132. return icon
  133. }()
  134. lazy var helpView: KeyboardHelpView = {
  135. let view = KeyboardHelpView()
  136. view.delegate = self
  137. view.helpDelegate = self
  138. return view
  139. }()
  140. lazy var teachView: KeyboardTeachView = {
  141. let view = KeyboardTeachView()
  142. view.delegate = self
  143. view.teachDelegate = self
  144. view.isHidden = true
  145. return view
  146. }()
  147. lazy var prologueView: KeyboardPrologueView = {
  148. let view = KeyboardPrologueView()
  149. view.delegate = self
  150. view.prologueDelegate = self
  151. view.isHidden = true
  152. return view
  153. }()
  154. lazy var exchangeView: KeyboardExchangeView = {
  155. let view = KeyboardExchangeView()
  156. view.isHidden = true
  157. view.delegate = self
  158. return view
  159. }()
  160. lazy var menuView: KeyboardMenuView = {
  161. let view = KeyboardMenuView()
  162. view.isHidden = true
  163. view.delegate = self
  164. return view
  165. }()
  166. lazy var loginView: KeyboardLoginTipView = {
  167. let view = KeyboardLoginTipView()
  168. view.isHidden = true
  169. view.loginBtnClosure = {
  170. self.navigateToLogin()
  171. }
  172. view.backBtnClosure = {
  173. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.login_close)
  174. self.clearPopView()
  175. self.loginView.isHidden = true
  176. self.nowShowView?.isHidden = false
  177. }
  178. return view
  179. }()
  180. lazy var vipView: KeyboardVipTipView = {
  181. let view = KeyboardVipTipView()
  182. view.isHidden = true
  183. view.unlockBtnClosure = {
  184. self.navigateToMembership()
  185. }
  186. view.backBtnClosure = {
  187. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.vip_close)
  188. self.clearPopView()
  189. self.vipView.isHidden = true
  190. self.nowShowView?.isHidden = false
  191. }
  192. return view
  193. }()
  194. lazy var tipPopView: KeyboardTipsPopView = {
  195. let view = KeyboardTipsPopView()
  196. view.isHidden = true
  197. view.settingBtnClosure = {
  198. if let url = URL(string: "com.qihuan.zhuiaijianpan:///open/setting") {
  199. self.openURL(url)
  200. }
  201. }
  202. view.backBtnClosure = {
  203. self.tipPopView.isHidden = true
  204. }
  205. return view
  206. }()
  207. var nowShowView: UIView?
  208. // 命令检查定时器
  209. private var guideCheckTimer: Timer?
  210. override func viewWillAppear(_ animated: Bool) {
  211. super.viewWillAppear(animated)
  212. self.prepareHeightConstraint()
  213. }
  214. override func viewDidLoad() {
  215. super.viewDidLoad()
  216. setUI()
  217. checkFullAccess()
  218. KeyboardApi.initParams()
  219. let isFullAccess = self.hasFullAccess
  220. if isFullAccess {
  221. requestKeyboardList()
  222. requestPrologueList()
  223. } else {
  224. getLocalSystemKeyboard()
  225. }
  226. if let defaultType = UserDefaults.standard.value(forKey: "KeyboardChooseType") as? Int, let type = KeyboardType(rawValue: defaultType) {
  227. self.chooseType = type
  228. }
  229. startMonitoringPasteboard()
  230. startListenGuide()
  231. // self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
  232. // self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
  233. }
  234. override func viewWillDisappear(_ animated: Bool) {
  235. super.viewWillDisappear(animated)
  236. }
  237. override func viewWillLayoutSubviews() {
  238. super.viewWillLayoutSubviews()
  239. }
  240. override func updateViewConstraints() {
  241. super.updateViewConstraints()
  242. if self.view.frame.size.width == 0 && self.view.frame.size.height == 0 {
  243. return
  244. }
  245. self.prepareHeightConstraint()
  246. }
  247. override func textWillChange(_ textInput: UITextInput?) {
  248. // The app is about to change the document's contents. Perform any preparation here.
  249. }
  250. override func textDidChange(_ textInput: UITextInput?) {
  251. // The app has just changed the document's contents, the document context has been updated.
  252. var textColor: UIColor
  253. let proxy = self.textDocumentProxy
  254. if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
  255. textColor = UIColor.white
  256. } else {
  257. textColor = UIColor.black
  258. }
  259. }
  260. func prepareHeightConstraint() {
  261. if self.heightConstriaint == nil {
  262. if let view = self.view {
  263. self.heightConstriaint = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0.0, constant: UX.keyboardDefaultHeight)
  264. // self.heightConstriaint?.priority = UILayoutPriority(750)
  265. self.view.addConstraint(self.heightConstriaint!)
  266. }
  267. } else {
  268. self.heightConstriaint?.constant = UX.keyboardDefaultHeight
  269. }
  270. }
  271. // 开始监听是否需要打开引导页
  272. func startListenGuide() {
  273. let isNotFirstOpen = UserDefaults.standard.bool(forKey: "isNotFirstOpenKey")
  274. if !isNotFirstOpen {
  275. // 创建定时器定期检查命令
  276. guideCheckTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in
  277. self?.popGuideView()
  278. }
  279. }
  280. }
  281. func popGuideView() {
  282. let hasFullAccess = self.hasFullAccess
  283. let showGuide = KeyboardSharedDataManager.shared.getIsShowGuide()
  284. if hasFullAccess && (showGuide == true) {
  285. let guideView = KeyboardGuideView()
  286. self.view.addSubview(guideView)
  287. guideView.snp.makeConstraints { make in
  288. make.top.left.right.bottom.equalTo(0)
  289. }
  290. UserDefaults.standard.set(true, forKey: "isNotFirstOpenKey")
  291. guideCheckTimer?.invalidate()
  292. guideCheckTimer = nil
  293. }
  294. }
  295. }
  296. // MARK: - 网络请求
  297. extension KeyboardViewController {
  298. func getLocalSystemKeyboard() {
  299. if let systemKeyboardJson = KeyboardSharedDataManager.shared.getSystemkeyboard() {
  300. if let jsonData = systemKeyboardJson.data(using: .utf8) {
  301. let decoder = JSONDecoder()
  302. do {
  303. let characterList = try decoder.decode([CharacterModel].self, from: jsonData)
  304. self.characterList = characterList
  305. self.updateCharacterUI()
  306. // 成功解码,可以使用 keyboardModel
  307. print("CharacterList: \(characterList)")
  308. } catch {
  309. print("解码 KeyboardModel 失败: \(error)")
  310. }
  311. }
  312. }
  313. }
  314. // 获取键盘列表
  315. func requestKeyboardList() {
  316. KeyboardNetworkManager.request(.keyboard_list(params: [:])) { response in
  317. if let keyboardInfos = try? response.mapArray(KeyboardModel.self, atKeyPath: "data.keyboardInfos") {
  318. self.keyboardList = keyboardInfos
  319. self.updateUserChangeBtnUI()
  320. self.requestCharacterList()
  321. print("成功解析数组,数量:\(keyboardInfos.count)")
  322. }
  323. } fail: { code, error in
  324. self.view.toast(text: error)
  325. print(error)
  326. }
  327. }
  328. // 获取键盘人设列表
  329. func requestCharacterList() {
  330. var params: [String: Any] = [String: Any]()
  331. if let chooseKeyboard = self.chooseKeyboard {
  332. params = ["keyboardId": chooseKeyboard.id ?? ""]
  333. }
  334. KeyboardNetworkManager.request(.character_list(params: params)) { response in
  335. if let characterInfos = try? response.mapArray(CharacterModel.self, atKeyPath: "data.characterInfos") {
  336. self.characterList = characterInfos
  337. self.updateCharacterUI()
  338. print("成功解析数组,数量:\(characterInfos.count)")
  339. }
  340. } fail: { code, error in
  341. self.view.toast(text: error)
  342. print(error)
  343. }
  344. }
  345. // 获取键盘开场白列表
  346. func requestPrologueList() {
  347. KeyboardNetworkManager.request(.prologue_list(params: [:])) { response in
  348. if let prologueList = try? response.mapArray(PrologueModel.self, atKeyPath: "data.prologues") {
  349. self.prologueList = prologueList
  350. self.updatePrologueUI()
  351. }
  352. } fail: { code, error in
  353. self.view.toast(text: error)
  354. print(error)
  355. }
  356. }
  357. // 选择键盘
  358. func requestChooseKeyboard(keyboardId: String, success: @escaping (() -> Void)) {
  359. var params: [String: Any] = [String: Any]()
  360. params = ["keyboardId": keyboardId]
  361. KeyboardNetworkManager.request(.keyboard_choose(params: params)) { response in
  362. self.view.toast(text: "保存成功")
  363. success()
  364. } fail: { code, error in
  365. self.view.toast(text: error)
  366. print(error)
  367. }
  368. }
  369. // 请求获取超会回
  370. func requestSuperReply(characterId: String, content: String, complete: @escaping ((String) -> ())) {
  371. var params: [String: Any] = [String: Any]()
  372. params = ["keyboardId": self.chooseKeyboard?.id ?? "",
  373. "characterId": characterId,
  374. "content": content]
  375. KeyboardNetworkManager.request(.chat_super_reply(params: params)) { response in
  376. if let contentModel = try? response.mapObject(ReplyModel.self, atKeyPath: "data") {
  377. complete(contentModel.content ?? "")
  378. } else {
  379. complete("")
  380. }
  381. } fail: { code, error in
  382. self.view.toast(text: error)
  383. print(error)
  384. complete("")
  385. if code == 1006 {
  386. // 弹出登录页
  387. self.popLoginView()
  388. } else if code == 1008 {
  389. // 弹出会员页
  390. self.popVipView()
  391. }
  392. }
  393. }
  394. // 请求获取超会说
  395. func requestSuperSpeak(characterId: String, content: String, complete: @escaping (([String]) -> ())) {
  396. var params: [String: Any] = [String: Any]()
  397. params = ["keyboardId": self.chooseKeyboard?.id ?? "",
  398. "characterId": characterId,
  399. "content": content]
  400. KeyboardNetworkManager.request(.chat_super_speak(params: params)) { response in
  401. if let contentModel = try? response.mapObject(SpeakModel.self, atKeyPath: "data"), let list = contentModel.list {
  402. complete(list)
  403. } else {
  404. complete([String]())
  405. }
  406. } fail: { code, error in
  407. self.view.toast(text: error)
  408. print(error)
  409. complete([String]())
  410. if code == 1006 {
  411. // 弹出登录页
  412. self.popLoginView()
  413. } else if code == 1008 {
  414. // 弹出会员页
  415. self.popVipView()
  416. }
  417. }
  418. }
  419. // 请求获取超会说
  420. func requestSuperPrologue(name: String, complete: @escaping (([String]) -> ())) {
  421. var params: [String: Any] = [String: Any]()
  422. params = ["name": name]
  423. KeyboardNetworkManager.request(.chat_super_prologue(params: params)) { response in
  424. if let contentModel = try? response.mapObject(SpeakModel.self, atKeyPath: "data"), let list = contentModel.list {
  425. complete(list)
  426. } else {
  427. complete([String]())
  428. }
  429. } fail: { code, error in
  430. self.view.toast(text: error)
  431. print(error)
  432. complete([String]())
  433. if code == 1006 {
  434. // 弹出登录页
  435. self.popLoginView()
  436. } else if code == 1008 {
  437. // 弹出会员页
  438. self.popVipView()
  439. }
  440. }
  441. }
  442. }
  443. // MARK: - 点击事件
  444. extension KeyboardViewController: KeyboardMenuViewDelegate, KeyboardExchangeViewDelegate {
  445. // 亲密度按钮点击
  446. @objc func heartBtnClickAction() {
  447. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.initmacy_click)
  448. let url = URL(string: "com.qihuan.zhuiaijianpan:///intimacy")!
  449. openURL(url)
  450. }
  451. // 切换系统键盘
  452. @objc func exchangeBtnClickAction() {
  453. self.advanceToNextInputMode()
  454. }
  455. // 功能按钮选择点击
  456. @objc func functionBtnClickAction() {
  457. let isFullAccess = self.hasFullAccess
  458. if !isFullAccess {
  459. self.popTipsView()
  460. return
  461. }
  462. KeyboardFunctionPopView.show(view: self.view, selectType: self.chooseType) { type in
  463. self.chooseType = type
  464. switch type {
  465. case .help:
  466. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.mode_help)
  467. case .teach:
  468. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.mode_teach)
  469. case .prologue:
  470. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.mode_prologue)
  471. }
  472. }
  473. }
  474. // 菜单按钮点击
  475. @objc func menuBtnClickAction() {
  476. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.menu_click)
  477. clearPopView()
  478. self.menuView.isHidden = false
  479. }
  480. // 菜单按钮返回
  481. func menuBackBtnClickAction() {
  482. clearPopView()
  483. self.nowShowView?.isHidden = false
  484. }
  485. // 会员按钮点击
  486. func vipBtnClickAction() {
  487. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.menu_vip)
  488. navigateToMembership()
  489. }
  490. // 定制人设
  491. func diyBtnClickAction() {
  492. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.menu_diy)
  493. let url = URL(string: "com.qihuan.zhuiaijianpan:///character/custom")!
  494. openURL(url)
  495. }
  496. // 人设市场
  497. func marketBtnClickAction() {
  498. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.menu_market)
  499. let url = URL(string: "com.qihuan.zhuiaijianpan:///character/market")!
  500. openURL(url)
  501. }
  502. // 跳转到主应用的登录页面
  503. @objc func navigateToLogin() {
  504. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.login_click)
  505. let url = URL(string: "com.qihuan.zhuiaijianpan:///login")!
  506. openURL(url)
  507. }
  508. // 跳转到主应用的会员页面
  509. @objc func navigateToMembership() {
  510. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.vip_click)
  511. let url = URL(string: "com.qihuan.zhuiaijianpan:///member")!
  512. openURL(url)
  513. }
  514. @objc @discardableResult private func openURL(_ url: URL) -> Bool {
  515. var responder: UIResponder? = self
  516. while responder != nil {
  517. if let application = responder as? UIApplication {
  518. if #available(iOS 18.0, *) {
  519. application.open(url, options: [:], completionHandler: nil)
  520. return true
  521. } else {
  522. return application.perform(#selector(openURL(_:)), with: url) != nil
  523. }
  524. }
  525. responder = responder?.next
  526. }
  527. return false
  528. }
  529. // // 通用的打开URL方法
  530. // private func openURL(_ url: URL) {
  531. //// var responder = self as UIResponder?
  532. //// while (responder != nil && !(responder is UIApplication)) {
  533. //// responder = responder?.next
  534. //// }
  535. //// if responder != nil {
  536. //// let selectorOpenURL = sel_registerName("openURL:options:completionHandler:")
  537. //// if responder!.responds(to: selectorOpenURL) {
  538. //// self.dismissKeyboard()
  539. //// responder?.perform(selectorOpenURL, with: [url, nil, nil])
  540. //// }
  541. //// }
  542. //
  543. // var responder: UIResponder? = self
  544. // while responder != nil {
  545. // if let application = responder as? UIApplication {
  546. // if #available(iOS 18.0, *) {
  547. // application.open(url, options: [:], completionHandler: nil)
  548. // } else {
  549. // application.perform(#selector(openURL(_:)), with: url)
  550. // }
  551. // }
  552. // responder = responder?.next
  553. // }
  554. // // 不知道为什么无法使用
  555. //// self.extensionContext?.open(url, completionHandler: nil)
  556. // }
  557. // 选择键盘按钮点击
  558. @objc func changeBtnClickAction() {
  559. let isFullAccess = self.hasFullAccess
  560. if !isFullAccess {
  561. self.popTipsView()
  562. return
  563. }
  564. clearPopView()
  565. self.exchangeView.keyboardList = self.keyboardList
  566. self.exchangeView.isHidden = false
  567. }
  568. // 选择键盘后返回
  569. func exchangeViewBackClickAction() {
  570. clearPopView()
  571. self.nowShowView?.isHidden = false
  572. }
  573. // 点击保存按钮
  574. func exchangeViewSaveClickAction(keyboardList: [KeyboardModel], success: @escaping (() -> ())) {
  575. var keyboardId = ""
  576. var selectKeyboard: KeyboardModel?
  577. for keyboard in keyboardList {
  578. if keyboard.isChoose == true {
  579. keyboardId = keyboard.id ?? ""
  580. selectKeyboard = keyboard
  581. break
  582. }
  583. }
  584. if let selectKeyboard = selectKeyboard {
  585. // 当为系统键盘时不需要调接口
  586. if selectKeyboard.type == "system" {
  587. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.keyboard_system)
  588. self.keyboardList = keyboardList
  589. self.chooseKeyboard = selectKeyboard
  590. self.requestCharacterList()
  591. success()
  592. self.userChangeLabel.text = self.chooseKeyboard?.name
  593. self.clearPopView()
  594. self.nowShowView?.isHidden = false
  595. } else {
  596. requestChooseKeyboard(keyboardId: keyboardId) {
  597. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.keyborad_custom)
  598. self.keyboardList = keyboardList
  599. self.chooseKeyboard = selectKeyboard
  600. self.requestCharacterList()
  601. success()
  602. self.userChangeLabel.text = self.chooseKeyboard?.name
  603. self.clearPopView()
  604. self.nowShowView?.isHidden = false
  605. }
  606. }
  607. }
  608. }
  609. }
  610. extension KeyboardViewController: KeyboardHelpViewDelegate, KeyboardTeachViewDelegate, KeyboardPrologueViewDelegate {
  611. // 帮聊点击cell
  612. func helpCollectionViewDidSelectItem(characterId: String, content: String, complete: @escaping ((Bool) -> ())) {
  613. let isFullAccess = self.hasFullAccess
  614. if !isFullAccess {
  615. self.popTipsView()
  616. complete(false)
  617. return
  618. }
  619. // 判断是否登录
  620. let isLogin = KeyboardSharedDataManager.shared.isLoggedIn()
  621. if !isLogin {
  622. popLoginView()
  623. complete(false)
  624. return
  625. }
  626. self.requestSuperReply(characterId: characterId, content: content) { text in
  627. self.insertText(text)
  628. complete(true)
  629. }
  630. }
  631. // 教你说点击cell
  632. func teachCollectionViewDidSelectItem(characterId: String, content: String, complete: @escaping (([String], Bool, Bool) -> ())) {
  633. let isFullAccess = self.hasFullAccess
  634. if !isFullAccess {
  635. self.popTipsView()
  636. complete([String](), false, false)
  637. return
  638. }
  639. // 判断是否登录
  640. let isLogin = KeyboardSharedDataManager.shared.isLoggedIn()
  641. if !isLogin {
  642. popLoginView()
  643. complete([String](), false, false)
  644. return
  645. } else {
  646. complete([String](), true, false)
  647. }
  648. self.requestSuperSpeak(characterId: characterId, content: content) { list in
  649. complete(list, true, true)
  650. }
  651. }
  652. // 教你说点击 TableViewCell
  653. func teachTableViewDidSelectItem(content: String) {
  654. self.insertText(content)
  655. }
  656. // 开场白点击cell
  657. func prologueCollectionViewDidSelectItem(name: String, complete: @escaping (([String], Bool, Bool) -> ())) {
  658. // 判断是否登录
  659. let isLogin = KeyboardSharedDataManager.shared.isLoggedIn()
  660. if !isLogin {
  661. popLoginView()
  662. complete([String](), false, false)
  663. return
  664. } else {
  665. complete([String](), true, false)
  666. }
  667. self.requestSuperPrologue(name: name) { list in
  668. complete(list, true, true)
  669. }
  670. }
  671. // 开场白点击 TableViewCell
  672. func prologueTableViewDidSelectItem(content: String) {
  673. self.insertText(content)
  674. }
  675. // 点击添加人设
  676. func addCharacterJump() {
  677. self.marketBtnClickAction()
  678. }
  679. }
  680. // MARK: - 处理键盘操作
  681. extension KeyboardViewController: KeyboardBaseViewDelegate {
  682. // 插入文字
  683. func insertText(_ text: String) {
  684. self.textDocumentProxy.insertText(text)
  685. }
  686. // 点击粘贴按钮
  687. func pasteBtnClickAction() {
  688. detectPasteboardPermissionAlert { isShowing in
  689. if isShowing {
  690. print("系统正在显示剪贴板权限弹窗")
  691. self.view.toast(text: "请允许访问剪贴板")
  692. self.openPasteTipView()
  693. }
  694. if let content = self.getPasteboardContent() {
  695. print("获取到剪贴板内容: \(content)")
  696. self.view.toast(text: "获取到剪贴板内容")
  697. self.helpView.setPasteStr(content: content)
  698. self.teachView.setPasteStr(content: content)
  699. } else {
  700. print("无法获取剪贴板内容")
  701. self.view.toast(text: "无法获取剪贴板内容")
  702. }
  703. }
  704. }
  705. // 删除按钮
  706. func deleteBtnClickAction() {
  707. self.textDocumentProxy.deleteBackward()
  708. }
  709. // 清除按钮
  710. func clearBtnClickAction() {
  711. // 获取当前文本
  712. let proxy = self.textDocumentProxy
  713. // 清空所有文本
  714. for _ in 0..<100 {
  715. proxy.deleteBackward()
  716. }
  717. }
  718. // 发送按钮
  719. func sendBtnClickAction() {
  720. self.textDocumentProxy.insertText("\n")
  721. }
  722. func deleteBtnLongPressBegin() {
  723. // 停止已有的定时器
  724. stopContinuousDelete()
  725. // 创建新的定时器,初始间隔为0.5秒
  726. deleteTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(continuousDeleteAction), userInfo: nil, repeats: false)
  727. }
  728. func deleteBtnLongPressEnd() {
  729. stopContinuousDelete()
  730. }
  731. // 停止删除
  732. func stopContinuousDelete() {
  733. deleteTimer?.invalidate()
  734. deleteTimer = nil
  735. deleteAcceleration = 0.1 // 重置加速度
  736. }
  737. @objc func continuousDeleteAction() {
  738. // 执行删除操作
  739. self.textDocumentProxy.deleteBackward()
  740. // 停止当前定时器
  741. deleteTimer?.invalidate()
  742. // 加速删除(最快0.05秒一次)
  743. let newInterval = max(0.05, 0.5 - deleteAcceleration)
  744. deleteAcceleration += 0.05 // 每次加速0.05秒
  745. // 创建新的更快的定时器
  746. deleteTimer = Timer.scheduledTimer(timeInterval: newInterval, target: self, selector: #selector(continuousDeleteAction), userInfo: nil, repeats: false)
  747. }
  748. // 监听剪贴板变化
  749. func startMonitoringPasteboard() {
  750. // 记录当前剪贴板变化计数
  751. var initialChangeCount = UIPasteboard.general.changeCount
  752. // 记录上一次的剪贴板内容
  753. var lastPasteboardContent: String? = UIPasteboard.general.string
  754. // 创建定时器定期检查剪贴板变化
  755. Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { [weak self] timer in
  756. guard let self = self else {
  757. timer.invalidate()
  758. return
  759. }
  760. let currentChangeCount = UIPasteboard.general.changeCount
  761. if currentChangeCount != initialChangeCount {
  762. // 记录访问前的时间
  763. let startTime = Date()
  764. // 剪贴板计数已变化,检查内容是否真的变化
  765. if let copiedString = UIPasteboard.general.string {
  766. // 只有当内容真的不同时才处理
  767. if !copiedString.isEmpty {
  768. print("检测到新复制的内容: \(copiedString)")
  769. // 更新记录的内容和计数
  770. lastPasteboardContent = copiedString
  771. initialChangeCount = currentChangeCount
  772. // 处理复制的内容
  773. self.handleCopiedContent(copiedString)
  774. // 检查访问后的时间延迟
  775. let timeDelay = Date().timeIntervalSince(startTime)
  776. // 如果访问剪贴板的时间超过一定阈值,可能是因为系统弹出了权限弹窗
  777. // 通常正常访问剪贴板的时间应该很短,如果超过100毫秒,可能是弹出了弹窗
  778. if timeDelay > 0.1 {
  779. self.openPasteTipView()
  780. } else {
  781. }
  782. } else {
  783. // 内容相同或为空,只更新计数
  784. initialChangeCount = currentChangeCount
  785. print("剪贴板计数变化但内容未变或为空")
  786. self.openPasteTipView()
  787. }
  788. } else {
  789. // 剪贴板内容为nil,更新计数
  790. initialChangeCount = currentChangeCount
  791. lastPasteboardContent = nil
  792. print("剪贴板内容被清空")
  793. }
  794. }
  795. }
  796. }
  797. // 处理复制的内容
  798. func handleCopiedContent(_ content: String) {
  799. self.helpView.setPasteStr(content: content)
  800. self.teachView.setPasteStr(content: content)
  801. }
  802. // 检查键盘权限
  803. func checkPasteboardPermission() -> Bool {
  804. var hasPasteboardPermission = false
  805. // iOS 14及以上版本需要检查权限
  806. if #available(iOS 14.0, *) {
  807. // 尝试读取剪贴板内容来检查权限
  808. let pasteboard = UIPasteboard.general
  809. if let _ = pasteboard.string {
  810. hasPasteboardPermission = true
  811. print("键盘扩展有剪贴板访问权限")
  812. } else {
  813. hasPasteboardPermission = false
  814. print("键盘扩展没有剪贴板访问权限")
  815. }
  816. } else {
  817. // iOS 14以下版本默认有权限
  818. hasPasteboardPermission = true
  819. }
  820. return hasPasteboardPermission
  821. }
  822. // 检测系统是否弹出剪贴板权限弹窗
  823. func detectPasteboardPermissionAlert(completion: @escaping (Bool) -> Void) {
  824. // 记录访问前的时间
  825. let startTime = Date()
  826. // 尝试访问剪贴板
  827. let _ = UIPasteboard.general.string
  828. // 检查访问后的时间延迟
  829. let timeDelay = Date().timeIntervalSince(startTime)
  830. // 如果访问剪贴板的时间超过一定阈值,可能是因为系统弹出了权限弹窗
  831. // 通常正常访问剪贴板的时间应该很短,如果超过100毫秒,可能是弹出了弹窗
  832. if timeDelay > 0.1 {
  833. completion(true)
  834. } else {
  835. completion(false)
  836. }
  837. }
  838. // 获取剪贴板内容
  839. func getPasteboardContent() -> String? {
  840. let pasteboard = UIPasteboard.general
  841. return pasteboard.string
  842. }
  843. // 允许粘贴跳转至设置页
  844. func jumpToSettings() {
  845. if let url = URL(string: UIApplication.openSettingsURLString) {
  846. self.openURL(url)
  847. }
  848. }
  849. func tipsCloseBtnAction() {
  850. self.helpView.topView.isHidden = true
  851. self.helpView.topView.snp.updateConstraints { make in
  852. make.height.equalTo(0)
  853. }
  854. self.teachView.topView.isHidden = true
  855. self.teachView.topView.snp.updateConstraints { make in
  856. make.height.equalTo(0)
  857. }
  858. self.prologueView.topView.isHidden = true
  859. self.prologueView.topView.snp.updateConstraints { make in
  860. make.height.equalTo(0)
  861. }
  862. self.view.removeConstraint(self.heightConstriaint!)
  863. self.heightConstriaint = NSLayoutConstraint(item: self.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0.0, constant: UX.keyboardDefaultHeight)
  864. // newHeightConstriaint.priority = UILayoutPriority(750)
  865. self.view.addConstraint(self.heightConstriaint!)
  866. }
  867. }
  868. extension KeyboardViewController {
  869. // 打开弹窗
  870. func openPasteTipView() {
  871. self.helpView.topView.isHidden = false
  872. self.helpView.topView.snp.updateConstraints { make in
  873. make.height.equalTo(32)
  874. }
  875. self.teachView.topView.isHidden = false
  876. self.teachView.topView.snp.updateConstraints { make in
  877. make.height.equalTo(32)
  878. }
  879. self.prologueView.topView.isHidden = false
  880. self.prologueView.topView.snp.updateConstraints { make in
  881. make.height.equalTo(32)
  882. }
  883. self.view.removeConstraint(self.heightConstriaint!)
  884. self.heightConstriaint = NSLayoutConstraint(item: self.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0.0, constant: UX.keyboardWithTipsHeight)
  885. // newHeightConstriaint.priority = UILayoutPriority(750)
  886. self.view.addConstraint(self.heightConstriaint!)
  887. }
  888. // 检查是否有完全访问权限
  889. func checkFullAccess() {
  890. let isFullAccess = self.hasFullAccess
  891. if !isFullAccess {
  892. popTipsView()
  893. }
  894. }
  895. // 弹出完全访问设置页
  896. func popTipsView() {
  897. self.tipPopView.isHidden = false
  898. }
  899. // 跳转登录页
  900. func popLoginView() {
  901. self.clearPopView()
  902. self.loginView.isHidden = false
  903. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.login_show)
  904. }
  905. // 弹出vip页面
  906. func popVipView() {
  907. self.clearPopView()
  908. self.vipView.isHidden = false
  909. GravityEngineSDK.sharedInstance()?.track(KeyboardPoint.vip_show)
  910. }
  911. // 关闭其他弹出页
  912. func clearPopView() {
  913. self.vipView.isHidden = true
  914. self.loginView.isHidden = true
  915. self.menuView.isHidden = true
  916. self.exchangeView.isHidden = true
  917. self.helpView.isHidden = true
  918. self.teachView.isHidden = true
  919. self.prologueView.isHidden = true
  920. }
  921. // 更新主要界面
  922. func updateMainViewUI() {
  923. clearPopView()
  924. switch chooseType {
  925. case .help:
  926. // self.helpView.characterList = self.characterList
  927. self.helpView.isHidden = false
  928. self.nowShowView = self.helpView
  929. self.functionLabel.text = "帮聊"
  930. break
  931. case .teach:
  932. // self.teachView.characterList = self.characterList
  933. self.teachView.isHidden = false
  934. self.nowShowView = self.teachView
  935. self.functionLabel.text = "教你说"
  936. break
  937. case .prologue:
  938. self.prologueView.isHidden = false
  939. self.nowShowView = self.prologueView
  940. self.functionLabel.text = "开场白"
  941. break
  942. }
  943. }
  944. // 更新人设列表
  945. func updateCharacterUI() {
  946. self.helpView.characterList = self.characterList
  947. self.teachView.characterList = self.characterList
  948. }
  949. // 更新开场白
  950. func updatePrologueUI() {
  951. self.prologueView.prologueList = self.prologueList
  952. }
  953. // 更新键盘选择按钮
  954. func updateUserChangeBtnUI() {
  955. if self.keyboardList?.count == 1 {
  956. self.keyboardList?[0].isChoose = true
  957. self.chooseKeyboard = self.keyboardList?.first
  958. } else {
  959. if let keyboardList = self.keyboardList {
  960. for keyboard in keyboardList {
  961. if keyboard.isChoose == true {
  962. self.chooseKeyboard = keyboard
  963. }
  964. }
  965. }
  966. }
  967. if let chooseKeyboard = self.chooseKeyboard {
  968. self.userChangeLabel.text = chooseKeyboard.name
  969. self.heartLabel.text = "\(chooseKeyboard.intimacy ?? 0)%"
  970. }
  971. }
  972. func setUI() {
  973. self.view.addSubview(bgImageView)
  974. bgImageView.snp.makeConstraints { make in
  975. make.edges.equalToSuperview()
  976. }
  977. self.view.addSubview(menuBtn)
  978. menuBtn.snp.makeConstraints { make in
  979. make.size.equalTo(CGSize(width: 32, height: 32))
  980. make.left.equalTo(13)
  981. make.top.equalTo(17)
  982. }
  983. self.view.addSubview(heartAnimation)
  984. heartAnimation.snp.makeConstraints { make in
  985. make.size.equalTo(CGSize(width: 35, height: 30))
  986. make.right.equalTo(-10)
  987. make.centerY.equalTo(menuBtn.snp.centerY)
  988. }
  989. heartAnimation.play()
  990. self.view.addSubview(heartLabel)
  991. heartLabel.snp.makeConstraints { make in
  992. make.centerY.equalTo(heartAnimation.snp.centerY).offset(-2)
  993. make.centerX.equalTo(heartAnimation.snp.centerX)
  994. }
  995. self.view.addSubview(exchangeBtn)
  996. exchangeBtn.snp.makeConstraints { make in
  997. make.size.equalTo(CGSize(width: 34, height: 34))
  998. make.right.equalTo(heartAnimation.snp.left).offset(-10)
  999. make.centerY.equalTo(menuBtn.snp.centerY)
  1000. }
  1001. self.view.addSubview(teachView)
  1002. teachView.snp.makeConstraints { make in
  1003. make.left.right.bottom.equalTo(0)
  1004. make.top.equalTo(60)
  1005. }
  1006. self.view.addSubview(helpView)
  1007. helpView.snp.makeConstraints { make in
  1008. make.left.right.bottom.equalTo(0)
  1009. make.top.equalTo(60)
  1010. }
  1011. self.view.addSubview(prologueView)
  1012. prologueView.snp.makeConstraints { make in
  1013. make.left.right.bottom.equalTo(0)
  1014. make.top.equalTo(60)
  1015. }
  1016. self.view.addSubview(chooseView)
  1017. chooseView.snp.makeConstraints { make in
  1018. make.width.equalTo(185)
  1019. make.height.equalTo(34)
  1020. make.left.equalTo(menuBtn.snp.right).offset(9)
  1021. make.centerY.equalTo(menuBtn.snp.centerY)
  1022. }
  1023. self.view.addSubview(exchangeView)
  1024. exchangeView.snp.makeConstraints { make in
  1025. make.top.equalTo(menuBtn.snp.bottom)
  1026. make.left.right.bottom.equalTo(0)
  1027. }
  1028. self.view.addSubview(menuView)
  1029. menuView.snp.makeConstraints { make in
  1030. make.left.right.bottom.equalTo(0)
  1031. make.top.equalTo(menuBtn.snp.bottom)
  1032. }
  1033. self.view.addSubview(loginView)
  1034. loginView.snp.makeConstraints { make in
  1035. make.top.left.right.bottom.equalTo(0)
  1036. }
  1037. self.view.addSubview(vipView)
  1038. vipView.snp.makeConstraints { make in
  1039. make.top.left.right.bottom.equalTo(0)
  1040. }
  1041. self.view.addSubview(tipPopView)
  1042. tipPopView.snp.makeConstraints { make in
  1043. make.left.right.bottom.equalTo(0)
  1044. make.top.equalTo(56)
  1045. }
  1046. chooseView.addSubview(functionView)
  1047. functionView.snp.makeConstraints { make in
  1048. make.top.left.equalTo(1)
  1049. make.bottom.equalTo(-1)
  1050. make.width.equalTo(85)
  1051. }
  1052. functionView.addSubview(functionLabel)
  1053. functionLabel.snp.makeConstraints { make in
  1054. make.left.equalTo(12)
  1055. make.centerY.equalToSuperview()
  1056. }
  1057. functionView.addSubview(arrowIcon)
  1058. arrowIcon.snp.makeConstraints { make in
  1059. make.size.equalTo(CGSize(width: 18, height: 18))
  1060. make.centerY.equalToSuperview()
  1061. make.right.equalTo(-5)
  1062. }
  1063. chooseView.addSubview(userChangeView)
  1064. userChangeView.snp.makeConstraints { make in
  1065. make.left.equalTo(functionView.snp.right)
  1066. make.right.equalToSuperview()
  1067. make.top.bottom.equalTo(0)
  1068. }
  1069. userChangeView.addSubview(userChangeIcon)
  1070. userChangeIcon.snp.makeConstraints { make in
  1071. make.size.equalTo(CGSize(width: 12, height: 12))
  1072. make.centerY.equalToSuperview()
  1073. make.right.equalTo(-8)
  1074. }
  1075. userChangeView.addSubview(userChangeLabel)
  1076. userChangeLabel.snp.makeConstraints { make in
  1077. make.right.equalTo(userChangeIcon.snp.left).offset(-4)
  1078. make.left.equalTo(4)
  1079. make.centerY.equalToSuperview()
  1080. }
  1081. // chooseView.addSubview(userChangeBtn)
  1082. // userChangeBtn.snp.makeConstraints { make in
  1083. // make.left.equalTo(functionView.snp.right)
  1084. // make.right.equalToSuperview()
  1085. // make.top.bottom.equalTo(0)
  1086. // }
  1087. self.nowShowView = self.helpView
  1088. }
  1089. }