KMCancelSubscribeSuccessWindowController.swift 19 KB

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