KMCancelSubscribeSuccessWindowController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. //
  2. // KMCancelSubscribeSuccessWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/12/18.
  6. //
  7. import Cocoa
  8. @objc enum KMCancelSubscribeSuccessType: Int {
  9. case none = 0
  10. // 功能不满足
  11. case unsatisfiedFunctions
  12. // 价格过高
  13. case priceInappropriate
  14. // Bug过多
  15. case tooManyBugs
  16. // 意外订阅
  17. case accidentallySubscribed
  18. // 其他
  19. case other
  20. }
  21. // 高级年订阅订阅(试用)
  22. let kKMAdvancedYearSubscribedOfTrial = "KMAdvancedYearSubscribedOfTrialKey"
  23. @objcMembers class KMCancelSubscribeSuccessWindowController: KMBaseWindowController {
  24. @IBOutlet weak var contentBox: NSBox!
  25. @IBOutlet weak var backgroundIv: NSImageView!
  26. @IBOutlet weak var titleLabel: NSTextField!
  27. @IBOutlet weak var subTitleLabel: NSTextField!
  28. @IBOutlet weak var listTitleLabel: NSTextField!
  29. @IBOutlet weak var scrollView: NSScrollView!
  30. @IBOutlet weak var tableView: NSTableView!
  31. @IBOutlet weak var buttonBox: NSBox!
  32. @IBOutlet weak var iconIv: NSImageView!
  33. static let shared = KMCancelSubscribeSuccessWindowController(windowNibName: "KMCancelSubscribeSuccessWindowController")
  34. private var datas_: [KMCancelSubscribeSuccessModel] = []
  35. private var selectedModel_: KMCancelSubscribeSuccessModel? = nil
  36. private var couponsWinC: KMCancelSubscribeCouponsWindowController?
  37. private let hasShowKey_ = "CancelSubscribeSuccessHasShow"
  38. private let couponsShowCountKey_ = "CancelSubscribeSuccessCouponsShowCount"
  39. private let appLaunchCountOfCouponsKey_ = "CancelSubscribeSuccessAppLaunchCountOfCoupons"
  40. private lazy var submitButton_: NSButton = {
  41. let view = NSButton()
  42. view.isBordered = false
  43. return view
  44. }()
  45. private lazy var buttonIv_: NSImageView = {
  46. let view = NSImageView()
  47. return view
  48. }()
  49. override func windowDidLoad() {
  50. super.windowDidLoad()
  51. window?.title = ""
  52. window?.delegate = self
  53. window?.standardWindowButton(.zoomButton)?.isHidden = true
  54. window?.standardWindowButton(.miniaturizeButton)?.isHidden = true
  55. let closeButton = window?.standardWindowButton(.closeButton)
  56. closeButton?.target = self
  57. closeButton?.action = #selector(closeAction)
  58. backgroundIv.image = NSImage(named: "KMImageNameCancelSubscribeSuccessBg")
  59. backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(470, 540)
  60. iconIv.image = NSImage(named: "KMImageNameCancelSubscribeSuccessIcon")
  61. backgroundIv.imageScaling = .scaleAxesIndependently
  62. titleLabel.stringValue = NSLocalizedString("Cancel Subscription Successfully", comment: "")
  63. titleLabel.font = .UbuntuMediumFontWithSize(24)
  64. subTitleLabel.stringValue = NSLocalizedString("Your feedback is essential for improving our product.", comment: "")
  65. subTitleLabel.font = .UbuntuMediumFontWithSize(14)
  66. listTitleLabel.stringValue = NSLocalizedString("Why didn't you continue with PDF Reader Pro?", comment: "")
  67. listTitleLabel.font = .UbuntuMediumFontWithSize(16)
  68. tableView.delegate = self
  69. tableView.dataSource = self
  70. tableView.usesAutomaticRowHeights = true
  71. tableView.backgroundColor = .clear
  72. tableView.enclosingScrollView?.drawsBackground = false
  73. tableView.enclosingScrollView?.borderType = .noBorder
  74. tableView.selectionHighlightStyle = .none
  75. buttonBox.borderWidth = 0
  76. contentBox.contentView?.addSubview(buttonIv_, positioned: .below, relativeTo: buttonBox)
  77. buttonIv_.image = NSImage(named: "KMImageNameCancelSubscribeSuccessButton")
  78. buttonIv_.mas_makeConstraints { make in
  79. make?.centerX.mas_equalTo()
  80. make?.centerY.equalTo()(buttonBox)?.offset()(6)
  81. }
  82. buttonBox.contentView?.addSubview(submitButton_)
  83. submitButton_.frame = buttonBox.contentView?.frame ?? .zero
  84. submitButton_.autoresizingMask = [.width, .height]
  85. submitButton_.title = NSLocalizedString("Submit", comment: "")
  86. submitButton_.font = .UbuntuMediumFontWithSize(16)
  87. submitButton_.target = self
  88. submitButton_.action = #selector(submitAction)
  89. _initData()
  90. interfaceThemeDidChanged(self.window?.appearance?.name ?? .aqua)
  91. }
  92. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  93. super.interfaceThemeDidChanged(appearance)
  94. KMMainThreadExecute {
  95. if KMAppearance.isDarkMode() {
  96. self.listTitleLabel.textColor = .white
  97. self.titleLabel.textColor = .white
  98. self.subTitleLabel.textColor = .white
  99. self.submitButton_.setTitleColor(NSColor(hex: "#050022"))
  100. } else {
  101. self.listTitleLabel.textColor = NSColor(hex: "#000150")
  102. self.titleLabel.textColor = NSColor(hex: "#000150")
  103. self.subTitleLabel.textColor = NSColor(hex: "#000150")
  104. self.submitButton_.setTitleColor(.white)
  105. }
  106. self.tableView.reloadData()
  107. }
  108. }
  109. // MARK: - Public Methods
  110. public func openWindow() {
  111. self.showWindow(nil)
  112. selectType(.unsatisfiedFunctions)
  113. _showCenter(animate: false)
  114. let member = KMMemberInfo.shared
  115. if member.isLogin && member.userEmail.isEmpty == false {
  116. KMDataManager.udExtension_set(true, forKey: member.userEmail+hasShowKey_)
  117. } else {
  118. KMDataManager.ud_set(true, forKey: hasShowKey_)
  119. }
  120. trackEvent(eventName: "PUW_2", params: ["PUW_Exposure" : "PUW_CancelFreeTrial"], platform: .AppCenter)
  121. }
  122. public func selectType(_ type: KMCancelSubscribeSuccessType) {
  123. if selectedModel_?.type == type { // 不能重复选中且选中再点击取消选中
  124. return
  125. }
  126. let preType = selectedModel_?.type
  127. for model in datas_ {
  128. model.isSelected = (model.type == type)
  129. if model.isSelected {
  130. selectedModel_ = model
  131. }
  132. }
  133. // 80+12
  134. let height: CGFloat = 92
  135. KMMainThreadExecute {
  136. // self.tableView.reloadData()
  137. if type == .other {
  138. var winFrame = self.window?.frame ?? .zero
  139. winFrame.size.height = NSHeight(winFrame) + height
  140. self.window?.setFrame(winFrame, display: true, animate: false)
  141. } else {
  142. if preType == .other {
  143. var winFrame = self.window?.frame ?? .zero
  144. winFrame.size.height = NSHeight(winFrame) - height
  145. self.window?.setFrame(winFrame, display: true, animate: false)
  146. } else {
  147. // no things
  148. }
  149. }
  150. self.tableView.animator().reloadData()
  151. }
  152. }
  153. public func closeWindow() {
  154. window?.close()
  155. }
  156. public func openCouponsWindow() {
  157. if couponsShowCount() > 2 {
  158. return
  159. }
  160. let member = KMMemberInfo.shared
  161. if (IAPProductsManager.default().isAvailableAllFunction() && member.userScenarioType != .lite_type8) { // 本地有权益
  162. return
  163. }
  164. trackEvent(eventName: "PUW_2", params: ["PUW_Exposure" : "CancelFreeTrial_Price_CouponCode"], platform: .AppCenter)
  165. recordCouponsShow()
  166. recordAppLaunchCountOfCoupons()
  167. if let winC = couponsWinC {
  168. winC.openWindow()
  169. return
  170. }
  171. let winC = KMCancelSubscribeCouponsWindowController()
  172. winC.openWindow()
  173. self.couponsWinC = winC
  174. }
  175. public func saveRecord() {
  176. let member = KMMemberInfo.shared
  177. if member.isLogin == false {
  178. return
  179. }
  180. if member.is_advanced_year_subscribe() == false {
  181. return
  182. }
  183. if member.isCancelSubscribe() { // 已经退订
  184. return
  185. }
  186. // 高级版年订阅
  187. var isTrail = false
  188. for vip in member.activeVips {
  189. if vip.levels == "3" && vip.paymentModel == "1" && vip.cycle == 4 {
  190. isTrail = vip.isTrail == "1"
  191. break
  192. }
  193. }
  194. if isTrail == false {
  195. return
  196. }
  197. KMDataManager.ud_set(true, forKey: kKMAdvancedYearSubscribedOfTrial)
  198. }
  199. public func needShow() -> Bool {
  200. let member = KMMemberInfo.shared
  201. if member.isLogin && member.userEmail.isEmpty == false {
  202. let data = KMDataManager.udExtension_object(forKey: member.userEmail+hasShowKey_) as? Bool ?? false
  203. return data == false
  204. }
  205. return KMDataManager.ud_bool(forKey: hasShowKey_) == false
  206. }
  207. public func recordCouponsShow() {
  208. let member = KMMemberInfo.shared
  209. if member.isLogin && member.userEmail.isEmpty == false {
  210. let key = member.userEmail+couponsShowCountKey_
  211. let cnt = KMDataManager.ud_integer(forKey: key)
  212. KMDataManager.ud_set(cnt+1, forKey: key)
  213. } else {
  214. let cnt = KMDataManager.ud_integer(forKey: couponsShowCountKey_)
  215. KMDataManager.ud_set(cnt+1, forKey: couponsShowCountKey_)
  216. }
  217. }
  218. public func couponsShowCount() -> Int {
  219. let member = KMMemberInfo.shared
  220. if member.isLogin && member.userEmail.isEmpty == false {
  221. let key = member.userEmail+couponsShowCountKey_
  222. return KMDataManager.ud_integer(forKey: key)
  223. } else {
  224. return KMDataManager.ud_integer(forKey: couponsShowCountKey_)
  225. }
  226. }
  227. public func recordAppLaunchCountOfCoupons() {
  228. let member = KMMemberInfo.shared
  229. if member.isLogin && member.userEmail.isEmpty == false {
  230. let key = member.userEmail+appLaunchCountOfCouponsKey_
  231. let cnt = KMDataManager.ud_integer(forKey: key)
  232. KMDataManager.ud_set(cnt+1, forKey: key)
  233. } else {
  234. let cnt = KMDataManager.ud_integer(forKey: appLaunchCountOfCouponsKey_)
  235. KMDataManager.ud_set(cnt+1, forKey: appLaunchCountOfCouponsKey_)
  236. }
  237. }
  238. public func appLaunchCountOfCoupons() -> Int {
  239. let member = KMMemberInfo.shared
  240. if member.isLogin && member.userEmail.isEmpty == false {
  241. let key = member.userEmail+appLaunchCountOfCouponsKey_
  242. return KMDataManager.ud_integer(forKey: key)
  243. } else {
  244. return KMDataManager.ud_integer(forKey: appLaunchCountOfCouponsKey_)
  245. }
  246. }
  247. // MARK: - Actions
  248. @objc func submitAction() {
  249. guard let model = selectedModel_ else {
  250. return
  251. }
  252. if isConnectionAvailable() == false {
  253. showHud(msg: NSLocalizedString("Please make sure your internet connection is available.", comment: ""))
  254. return
  255. }
  256. _trackEvent(type: model.type)
  257. let modelV = KMIsDMGVersion() ? "Dmg" : "Mac"
  258. let params: [String : Any] = [
  259. "model" : modelV,
  260. "title" : _titleString(from: model.type),
  261. "content" : model.type == .other ? model.content : ""
  262. ]
  263. KMRequestServer.Member_POST(url: kURLAPI_memberSystemSSO_user_cancelReason, params: params) { success, resultModel, error in
  264. self.closeWindow()
  265. if model.type == .priceInappropriate {
  266. self.openCouponsWindow()
  267. }
  268. }
  269. }
  270. @objc func closeAction() {
  271. if let model = selectedModel_, model.type == .priceInappropriate {
  272. self.openCouponsWindow()
  273. }
  274. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Cancel"], platform: .AppCenter)
  275. self.closeWindow()
  276. }
  277. // MARK: - Private Methods
  278. private func _initData() {
  279. datas_.removeAll()
  280. let types: [KMCancelSubscribeSuccessType] = [.unsatisfiedFunctions, .priceInappropriate, .tooManyBugs, .accidentallySubscribed, .other]
  281. for type in types {
  282. datas_.append(_initModel(type: type))
  283. }
  284. KMMainThreadExecute {
  285. self.tableView.reloadData()
  286. }
  287. }
  288. private func _initModel(type: KMCancelSubscribeSuccessType) -> KMCancelSubscribeSuccessModel {
  289. let model = KMCancelSubscribeSuccessModel()
  290. model.type = type
  291. if type == .unsatisfiedFunctions {
  292. model.titleText = NSLocalizedString("1. Unsatisfied functions", comment: "")
  293. } else if type == .priceInappropriate {
  294. model.titleText = NSLocalizedString("2. The price is inappropriate", comment: "")
  295. } else if type == .tooManyBugs {
  296. model.titleText = NSLocalizedString("3. Too many bugs and unsatisfied experience", comment: "")
  297. } else if type == .accidentallySubscribed {
  298. model.titleText = NSLocalizedString("4. Accidentally subscribed", comment: "")
  299. } else if type == .other {
  300. model.titleText = NSLocalizedString("5. Others", comment: "")
  301. }
  302. return model
  303. }
  304. private func _updateCellView(_ cellView: KMCancelSubscribeSuccessCellView?, model: KMCancelSubscribeSuccessModel) {
  305. KMMainThreadExecute {
  306. cellView?.contentBox.cornerRadius = 8
  307. // cell?.radio.font = .UbuntuMediumFontWithSize(16)
  308. cellView?.radio.title = model.titleText
  309. cellView?.radio.state = model.isSelected ? .on : .off
  310. if KMAppearance.isDarkMode() {
  311. cellView?.contentBox.borderColor = NSColor(hex: "#5023B8").withAlphaComponent(0.15)
  312. cellView?.contentBox.fillColor = NSColor(hex: "#1A0D32")
  313. cellView?.radio.setTitleColor(.white)
  314. if let data = cellView as? KMCancelSubscribeSuccessOtherCellView {
  315. data.inputBox.cornerRadius = 8
  316. data.inputBox.borderColor = NSColor(hex: "#5023B8").withAlphaComponent(0.15)
  317. data.inputBox.fillColor = NSColor(hex: "#1A0D32")
  318. }
  319. } else {
  320. cellView?.contentBox.borderColor = NSColor(hex: "#8A58FF").withAlphaComponent(0.15)
  321. cellView?.contentBox.fillColor = .white
  322. cellView?.radio.setTitleColor(NSColor(hex: "#42464D"))
  323. if let data = cellView as? KMCancelSubscribeSuccessOtherCellView {
  324. data.inputBox.cornerRadius = 8
  325. data.inputBox.borderColor = NSColor(hex: "#8A58FF").withAlphaComponent(0.15)
  326. data.inputBox.fillColor = .white
  327. }
  328. }
  329. }
  330. }
  331. private func _titleString(from type: KMCancelSubscribeSuccessType) -> String {
  332. switch type {
  333. case .unsatisfiedFunctions:
  334. return "1"
  335. case .priceInappropriate:
  336. return "5"
  337. case .tooManyBugs:
  338. return "2"
  339. case .accidentallySubscribed:
  340. return "3"
  341. case .other:
  342. return "4"
  343. case .none:
  344. return "1"
  345. }
  346. }
  347. private func _trackEvent(type: KMCancelSubscribeSuccessType) {
  348. if type == .unsatisfiedFunctions {
  349. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_LackFunction"], platform: .AppCenter)
  350. } else if type == .priceInappropriate {
  351. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Price"], platform: .AppCenter)
  352. } else if type == .tooManyBugs {
  353. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Bugs"], platform: .AppCenter)
  354. } else if type == .accidentallySubscribed {
  355. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_AccidentlyPurchase"], platform: .AppCenter)
  356. } else if type == .other {
  357. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Others"], platform: .AppCenter)
  358. }
  359. }
  360. private func _showCenter(animate: Bool){
  361. guard let screenFrame = NSScreen.main?.frame else {
  362. return
  363. }
  364. guard let win = self.window else {
  365. return
  366. }
  367. var frame = win.frame
  368. frame.origin.y = (screenFrame.size.height-frame.size.height)*0.5
  369. frame.origin.x = (screenFrame.size.width-frame.size.width)*0.5
  370. win.setFrame(frame, display: true, animate: animate)
  371. }
  372. }
  373. // MARK: - NSWindowDelegate
  374. extension KMCancelSubscribeSuccessWindowController: NSWindowDelegate {
  375. func windowDidResize(_ notification: Notification) {
  376. guard let data = window?.isEqual(to: notification.object), data == true else {
  377. return
  378. }
  379. backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(470, 540)
  380. }
  381. }
  382. extension KMCancelSubscribeSuccessWindowController: NSTableViewDelegate, NSTableViewDataSource {
  383. func numberOfRows(in tableView: NSTableView) -> Int {
  384. return self.datas_.count
  385. }
  386. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  387. let model = datas_[row]
  388. if model.type == .other && model.isSelected {
  389. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cellID"), owner: self) as? KMCancelSubscribeSuccessOtherCellView
  390. if cell == nil {
  391. cell = KMCancelSubscribeSuccessOtherCellView()
  392. }
  393. _updateCellView(cell, model: model)
  394. window?.makeFirstResponder(cell?.textView)
  395. cell?.itemClick = { [unowned self] in
  396. selectType(model.type)
  397. }
  398. cell?.valueDidChange = { content, _ in
  399. model.content = content as? String ?? ""
  400. }
  401. return cell
  402. }
  403. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cellID"), owner: self) as? KMCancelSubscribeSuccessCellView
  404. if cell == nil {
  405. cell = KMCancelSubscribeSuccessCellView()
  406. }
  407. _updateCellView(cell, model: model)
  408. cell?.itemClick = { [unowned self] in
  409. selectType(model.type)
  410. }
  411. return cell
  412. }
  413. func tableViewSelectionDidChange(_ notification: Notification) {
  414. let row = self.tableView.selectedRow
  415. let model = datas_[row]
  416. selectType(model.type)
  417. }
  418. }