KMCancelSubscribeSuccessWindowController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 VERSION_DMG
  162. if (IAPProductsManager.default().isAvailableAllFunction() && member.userScenarioType != .lite_type8) { // 本地有权益
  163. return
  164. }
  165. #else
  166. let result = IAPReceiptTool.isFreeTrailing(with: IAPProductsManager.default()?.receiptModel, withProductId: IAPProductsManager.default()?.fourDevicesAllAccessPackNew12months_lite?.product?.productIdentifier)
  167. if ((IAPProductsManager.default().isAvailableAllFunction() && result == false) && member.userScenarioType != .lite_type8) { // 本地有权益
  168. return
  169. }
  170. #endif
  171. trackEvent(eventName: "PUW_2", params: ["PUW_Exposure" : "CancelFreeTrial_Price_CouponCode"], platform: .AppCenter)
  172. recordCouponsShow()
  173. recordAppLaunchCountOfCoupons()
  174. if let winC = couponsWinC {
  175. winC.openWindow()
  176. return
  177. }
  178. let winC = KMCancelSubscribeCouponsWindowController()
  179. winC.openWindow()
  180. self.couponsWinC = winC
  181. }
  182. public func saveRecord() {
  183. let member = KMMemberInfo.shared
  184. if member.isLogin == false {
  185. return
  186. }
  187. if member.is_advanced_year_subscribe() == false {
  188. return
  189. }
  190. if member.isCancelSubscribe() { // 已经退订
  191. return
  192. }
  193. // 高级版年订阅
  194. var isTrail = false
  195. for vip in member.activeVips {
  196. if vip.levels == "3" && vip.paymentModel == "1" && vip.cycle == 4 {
  197. isTrail = vip.isTrail == "1"
  198. break
  199. }
  200. }
  201. if isTrail == false {
  202. return
  203. }
  204. KMDataManager.ud_set(true, forKey: kKMAdvancedYearSubscribedOfTrial)
  205. }
  206. public func needShow() -> Bool {
  207. let member = KMMemberInfo.shared
  208. if member.isLogin && member.userEmail.isEmpty == false {
  209. let data = KMDataManager.udExtension_object(forKey: member.userEmail+hasShowKey_) as? Bool ?? false
  210. return data == false
  211. }
  212. return KMDataManager.ud_bool(forKey: hasShowKey_) == false
  213. }
  214. public func recordCouponsShow() {
  215. let member = KMMemberInfo.shared
  216. if member.isLogin && member.userEmail.isEmpty == false {
  217. let key = member.userEmail+couponsShowCountKey_
  218. let cnt = KMDataManager.ud_integer(forKey: key)
  219. KMDataManager.ud_set(cnt+1, forKey: key)
  220. } else {
  221. let cnt = KMDataManager.ud_integer(forKey: couponsShowCountKey_)
  222. KMDataManager.ud_set(cnt+1, forKey: couponsShowCountKey_)
  223. }
  224. }
  225. public func couponsShowCount() -> Int {
  226. let member = KMMemberInfo.shared
  227. if member.isLogin && member.userEmail.isEmpty == false {
  228. let key = member.userEmail+couponsShowCountKey_
  229. return KMDataManager.ud_integer(forKey: key)
  230. } else {
  231. return KMDataManager.ud_integer(forKey: couponsShowCountKey_)
  232. }
  233. }
  234. public func recordAppLaunchCountOfCoupons() {
  235. let member = KMMemberInfo.shared
  236. if member.isLogin && member.userEmail.isEmpty == false {
  237. let key = member.userEmail+appLaunchCountOfCouponsKey_
  238. let cnt = KMDataManager.ud_integer(forKey: key)
  239. KMDataManager.ud_set(cnt+1, forKey: key)
  240. } else {
  241. let cnt = KMDataManager.ud_integer(forKey: appLaunchCountOfCouponsKey_)
  242. KMDataManager.ud_set(cnt+1, forKey: appLaunchCountOfCouponsKey_)
  243. }
  244. }
  245. public func appLaunchCountOfCoupons() -> Int {
  246. let member = KMMemberInfo.shared
  247. if member.isLogin && member.userEmail.isEmpty == false {
  248. let key = member.userEmail+appLaunchCountOfCouponsKey_
  249. return KMDataManager.ud_integer(forKey: key)
  250. } else {
  251. return KMDataManager.ud_integer(forKey: appLaunchCountOfCouponsKey_)
  252. }
  253. }
  254. // MARK: - Actions
  255. @objc func submitAction() {
  256. guard let model = selectedModel_ else {
  257. return
  258. }
  259. if isConnectionAvailable() == false {
  260. showHud(msg: NSLocalizedString("Please make sure your internet connection is available.", comment: ""))
  261. return
  262. }
  263. _trackEvent(type: model.type)
  264. let modelV = KMIsDMGVersion() ? "Dmg" : "Mac"
  265. let params: [String : Any] = [
  266. "model" : modelV,
  267. "title" : _titleString(from: model.type),
  268. "content" : model.type == .other ? model.content : ""
  269. ]
  270. KMRequestServer.Member_POST(url: kURLAPI_memberSystemSSO_user_cancelReason, params: params) { success, resultModel, error in
  271. self.closeWindow()
  272. if model.type == .priceInappropriate {
  273. self.openCouponsWindow()
  274. }
  275. }
  276. }
  277. @objc func closeAction() {
  278. if let model = selectedModel_, model.type == .priceInappropriate {
  279. self.openCouponsWindow()
  280. }
  281. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Cancel"], platform: .AppCenter)
  282. self.closeWindow()
  283. }
  284. // MARK: - Private Methods
  285. private func _initData() {
  286. datas_.removeAll()
  287. let types: [KMCancelSubscribeSuccessType] = [.unsatisfiedFunctions, .priceInappropriate, .tooManyBugs, .accidentallySubscribed, .other]
  288. for type in types {
  289. datas_.append(_initModel(type: type))
  290. }
  291. KMMainThreadExecute {
  292. self.tableView.reloadData()
  293. }
  294. }
  295. private func _initModel(type: KMCancelSubscribeSuccessType) -> KMCancelSubscribeSuccessModel {
  296. let model = KMCancelSubscribeSuccessModel()
  297. model.type = type
  298. if type == .unsatisfiedFunctions {
  299. model.titleText = NSLocalizedString("1. Unsatisfied functions", comment: "")
  300. } else if type == .priceInappropriate {
  301. model.titleText = NSLocalizedString("2. The price is inappropriate", comment: "")
  302. } else if type == .tooManyBugs {
  303. model.titleText = NSLocalizedString("3. Too many bugs and unsatisfied experience", comment: "")
  304. } else if type == .accidentallySubscribed {
  305. model.titleText = NSLocalizedString("4. Accidentally subscribed", comment: "")
  306. } else if type == .other {
  307. model.titleText = NSLocalizedString("5. Others", comment: "")
  308. }
  309. return model
  310. }
  311. private func _updateCellView(_ cellView: KMCancelSubscribeSuccessCellView?, model: KMCancelSubscribeSuccessModel) {
  312. KMMainThreadExecute {
  313. cellView?.contentBox.cornerRadius = 8
  314. // cell?.radio.font = .UbuntuMediumFontWithSize(16)
  315. cellView?.radio.title = model.titleText
  316. cellView?.radio.state = model.isSelected ? .on : .off
  317. if KMAppearance.isDarkMode() {
  318. cellView?.contentBox.borderColor = NSColor(hex: "#5023B8").withAlphaComponent(0.15)
  319. cellView?.contentBox.fillColor = NSColor(hex: "#1A0D32")
  320. cellView?.radio.setTitleColor(.white)
  321. if let data = cellView as? KMCancelSubscribeSuccessOtherCellView {
  322. data.inputBox.cornerRadius = 8
  323. data.inputBox.borderColor = NSColor(hex: "#5023B8").withAlphaComponent(0.15)
  324. data.inputBox.fillColor = NSColor(hex: "#1A0D32")
  325. }
  326. } else {
  327. cellView?.contentBox.borderColor = NSColor(hex: "#8A58FF").withAlphaComponent(0.15)
  328. cellView?.contentBox.fillColor = .white
  329. cellView?.radio.setTitleColor(NSColor(hex: "#42464D"))
  330. if let data = cellView as? KMCancelSubscribeSuccessOtherCellView {
  331. data.inputBox.cornerRadius = 8
  332. data.inputBox.borderColor = NSColor(hex: "#8A58FF").withAlphaComponent(0.15)
  333. data.inputBox.fillColor = .white
  334. }
  335. }
  336. }
  337. }
  338. private func _titleString(from type: KMCancelSubscribeSuccessType) -> String {
  339. switch type {
  340. case .unsatisfiedFunctions:
  341. return "1"
  342. case .priceInappropriate:
  343. return "5"
  344. case .tooManyBugs:
  345. return "2"
  346. case .accidentallySubscribed:
  347. return "3"
  348. case .other:
  349. return "4"
  350. case .none:
  351. return "1"
  352. }
  353. }
  354. private func _trackEvent(type: KMCancelSubscribeSuccessType) {
  355. if type == .unsatisfiedFunctions {
  356. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_LackFunction"], platform: .AppCenter)
  357. } else if type == .priceInappropriate {
  358. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Price"], platform: .AppCenter)
  359. } else if type == .tooManyBugs {
  360. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Bugs"], platform: .AppCenter)
  361. } else if type == .accidentallySubscribed {
  362. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_AccidentlyPurchase"], platform: .AppCenter)
  363. } else if type == .other {
  364. trackEvent(eventName: "PUW_2", params: ["PUW_Btn" : "CancelFreeTrial_Others"], platform: .AppCenter)
  365. }
  366. }
  367. private func _showCenter(animate: Bool){
  368. guard let screenFrame = NSScreen.main?.frame else {
  369. return
  370. }
  371. guard let win = self.window else {
  372. return
  373. }
  374. var frame = win.frame
  375. frame.origin.y = (screenFrame.size.height-frame.size.height)*0.5
  376. frame.origin.x = (screenFrame.size.width-frame.size.width)*0.5
  377. win.setFrame(frame, display: true, animate: animate)
  378. }
  379. }
  380. // MARK: - NSWindowDelegate
  381. extension KMCancelSubscribeSuccessWindowController: NSWindowDelegate {
  382. func windowDidResize(_ notification: Notification) {
  383. guard let data = window?.isEqual(to: notification.object), data == true else {
  384. return
  385. }
  386. backgroundIv.image?.size = window?.frame.size ?? NSMakeSize(470, 540)
  387. }
  388. }
  389. extension KMCancelSubscribeSuccessWindowController: NSTableViewDelegate, NSTableViewDataSource {
  390. func numberOfRows(in tableView: NSTableView) -> Int {
  391. return self.datas_.count
  392. }
  393. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  394. let model = datas_[row]
  395. if model.type == .other && model.isSelected {
  396. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cellID"), owner: self) as? KMCancelSubscribeSuccessOtherCellView
  397. if cell == nil {
  398. cell = KMCancelSubscribeSuccessOtherCellView()
  399. }
  400. _updateCellView(cell, model: model)
  401. window?.makeFirstResponder(cell?.textView)
  402. cell?.itemClick = { [unowned self] in
  403. selectType(model.type)
  404. }
  405. cell?.valueDidChange = { content, _ in
  406. model.content = content as? String ?? ""
  407. }
  408. return cell
  409. }
  410. var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "cellID"), owner: self) as? KMCancelSubscribeSuccessCellView
  411. if cell == nil {
  412. cell = KMCancelSubscribeSuccessCellView()
  413. }
  414. _updateCellView(cell, model: model)
  415. cell?.itemClick = { [unowned self] in
  416. selectType(model.type)
  417. }
  418. return cell
  419. }
  420. func tableViewSelectionDidChange(_ notification: Notification) {
  421. let row = self.tableView.selectedRow
  422. let model = datas_[row]
  423. selectType(model.type)
  424. }
  425. }