KMSubscribeWaterMarkWindowController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // KMSubscribeWaterMarkWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/6/12.
  6. //
  7. import Cocoa
  8. var subscribeWaterMarkController: KMSubscribeWaterMarkWindowController?
  9. var subscribeWaterMarkMainWindow: NSWindow?
  10. @objc enum KMSubscribeWaterMarkType: Int {
  11. case none = 0
  12. case stamp = 1
  13. case link
  14. case sign
  15. case editText
  16. case editImage
  17. case insert
  18. case extract
  19. case replace
  20. case split
  21. case delete
  22. case rotate
  23. case copy
  24. case toWord
  25. case toExcel
  26. case toPPT
  27. case toRTF
  28. case toCSV
  29. case toHTML
  30. case toText
  31. case toImage
  32. case compress
  33. case merge
  34. case setPassword
  35. case removePassword
  36. case crop
  37. case aiTranslate
  38. case aiRewrite
  39. case aiCorrect
  40. case save
  41. func isConvertType() -> Bool {
  42. if (self == .toWord || self == .toExcel || self == .toPPT || self == .toRTF || self == .toCSV || self == .toHTML || self == .toText || self == .toImage) {
  43. return true
  44. }
  45. return false
  46. }
  47. }
  48. typealias KMSubscribeWaterMarkWindowControllerCloseAction = (_ controller: KMSubscribeWaterMarkWindowController) -> Void
  49. typealias KMSubscribeWaterMarkWindowControllerWatermarkExportAction = (_ controller: KMSubscribeWaterMarkWindowController) -> Void
  50. typealias KMSubscribeWaterMarkWindowControllerSubscribeAction = (_ controller: KMSubscribeWaterMarkWindowController) -> Void
  51. var tempType: KMSubscribeWaterMarkType = .none
  52. var tempLimit: Bool = false
  53. var tempContinue: Bool = false
  54. class KMSubscribeWaterMarkWindowController: NSWindowController {
  55. @IBOutlet weak var waterMarkView: KMSubscribeWaterMarkView!
  56. @IBOutlet weak var waterViewHeightConstrain: NSLayoutConstraint!
  57. var closeAction: KMSubscribeWaterMarkWindowControllerCloseAction?
  58. var subscribeAction: KMSubscribeWaterMarkWindowControllerSubscribeAction?
  59. var watermarkExportAction: KMSubscribeWaterMarkWindowControllerWatermarkExportAction?
  60. var isContinue: Bool?
  61. var isAI: Bool?
  62. var isLimit: Bool?
  63. var type: KMSubscribeWaterMarkType? {
  64. didSet {
  65. self.waterMarkView?.type = type
  66. }
  67. }
  68. deinit {
  69. KMPrint("KMSubscribeWaterMarkWindowController 释放")
  70. }
  71. override func windowDidLoad() {
  72. super.windowDidLoad()
  73. self.waterMarkView.subscribeLabel.stringValue = ""
  74. self.setup()
  75. self.reloadData()
  76. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  77. }
  78. override func awakeFromNib() {
  79. super.awakeFromNib()
  80. // self.waterMarkView.subscribeLabel.stringValue = ""
  81. }
  82. //MARK: 打开文件
  83. @objc static func show(window: NSWindow, isContinue: Bool = false, limit: Bool = false, type: KMSubscribeWaterMarkType = .none, subscribeDidClick: (()->Void)? = nil, completion: @escaping (_ isSubscribeSuccess: Bool, _ isWaterMarkExport: Bool, _ isClose: Bool) -> Void) -> KMSubscribeWaterMarkWindowController {
  84. #if VERSION_DMG
  85. if type != .save && !KMLightMemberManager.manager.isLogin() {
  86. KMLoginWindowController.show(window: window)
  87. return KMSubscribeWaterMarkWindowController()
  88. }
  89. #endif
  90. tempLimit = limit
  91. tempType = type
  92. tempContinue = isContinue
  93. var isAI = false
  94. if type == .aiCorrect ||
  95. type == .aiRewrite ||
  96. type == .aiTranslate {
  97. isAI = true
  98. } else {
  99. isAI = false
  100. }
  101. if isAI && KMLightMemberManager.manager.purchaseState != .subscription {
  102. if KMLightMemberManager.manager.isLogin() {
  103. KMComparativeTableViewController.show(window: NSApp.mainWindow!, .equity)
  104. } else {
  105. let controller = KMLoginWindowController.show(window: NSApp.mainWindow!,.equity)
  106. if isAI {
  107. let alert = NSAlert()
  108. alert.messageText = NSLocalizedString("Please Login or Sign up to proceed to the next step.", comment: "")
  109. alert.informativeText = NSLocalizedString("On upgrading your account, you can enjoy all AI features in PDF Master.", comment: "")
  110. alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
  111. alert.beginSheetModal(for: (controller?.window)!) { result in
  112. }
  113. }
  114. }
  115. return KMSubscribeWaterMarkWindowController.init()
  116. } else {
  117. let subscribeWaterMarkWindowController: KMSubscribeWaterMarkWindowController = KMSubscribeWaterMarkWindowController.init(windowNibName: "KMSubscribeWaterMarkWindowController")
  118. subscribeWaterMarkWindowController.isContinue = isContinue
  119. subscribeWaterMarkWindowController.isAI = isAI
  120. subscribeWaterMarkWindowController.isLimit = limit
  121. window.beginSheet(subscribeWaterMarkWindowController.window!)
  122. subscribeWaterMarkWindowController.window?.center()
  123. subscribeWaterMarkWindowController.type = type
  124. subscribeWaterMarkWindowController.reloadData()
  125. subscribeWaterMarkWindowController.closeAction = { controller in
  126. completion(false, false, true)
  127. subscribeWaterMarkMainWindow?.endSheet(controller.window!)
  128. controller.window?.close()
  129. subscribeWaterMarkController = nil
  130. subscribeWaterMarkMainWindow = nil
  131. }
  132. subscribeWaterMarkWindowController.watermarkExportAction = { controller in
  133. completion(false, true, false)
  134. subscribeWaterMarkMainWindow?.endSheet(controller.window!)
  135. controller.window?.close()
  136. subscribeWaterMarkController = nil
  137. subscribeWaterMarkMainWindow = nil
  138. }
  139. subscribeWaterMarkWindowController.subscribeAction = { controller in
  140. if let callback = subscribeDidClick {
  141. callback()
  142. }
  143. subscribeWaterMarkMainWindow?.endSheet(controller.window!)
  144. controller.window?.close()
  145. subscribeWaterMarkController = nil
  146. if KMLightMemberManager.manager.isLogin() ||
  147. type == .save ||
  148. ((type.isConvertType() || type == .merge) && !limit) {
  149. _ = KMComparativeTableViewController.show(window: subscribeWaterMarkMainWindow ?? NSWindow(), .equity, inputType: type)
  150. } else {
  151. _ = KMLoginWindowController.show(window: subscribeWaterMarkMainWindow ?? NSWindow())
  152. }
  153. subscribeWaterMarkMainWindow = nil
  154. }
  155. subscribeWaterMarkController = subscribeWaterMarkWindowController
  156. subscribeWaterMarkMainWindow = window
  157. return subscribeWaterMarkWindowController
  158. }
  159. }
  160. func setup() {
  161. self.window?.contentView?.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  162. self.waterMarkView.waterExportButton.title = ""
  163. self.waterMarkView.subscribeLabel.stringValue = ""
  164. self.waterMarkView.closeAction = { [weak self] view in
  165. guard let callBack = self?.closeAction else { return }
  166. callBack((self!))
  167. }
  168. self.waterMarkView.waterMarkExportAction = { [weak self] view in
  169. self?.trackEvent(eventName: "PUW_Sub_SaveWatermark")
  170. guard let callBack = self?.watermarkExportAction else { return }
  171. callBack((self!))
  172. }
  173. self.waterMarkView.subscribeAction = { [weak self] view in
  174. guard let callBack = self?.subscribeAction else { return }
  175. callBack((self!))
  176. }
  177. }
  178. func closeWindow() {
  179. if subscribeWaterMarkMainWindow != nil {
  180. subscribeWaterMarkMainWindow?.endSheet((subscribeWaterMarkController?.window)!)
  181. }
  182. if subscribeWaterMarkController != nil {
  183. subscribeWaterMarkController?.window?.close()
  184. }
  185. subscribeWaterMarkController = nil
  186. subscribeWaterMarkMainWindow = nil
  187. }
  188. func reloadData() {
  189. if KMSubscribeWaterMarkWindowController.canShowSubscripBenefits() {
  190. self.waterViewHeightConstrain.constant = 462
  191. } else {
  192. self.waterViewHeightConstrain.constant = 563
  193. }
  194. self.waterMarkView.type = tempType
  195. self.waterMarkView.updateConstraints()
  196. self.updateLanguage()
  197. }
  198. func updateLanguage() {
  199. if self.waterMarkView != nil {
  200. if tempContinue {
  201. self.waterMarkView.waterExportButton.title = NSLocalizedString("Save with Watermark", comment: "")
  202. } else {
  203. if tempType == .merge {
  204. if (tempLimit) {
  205. self.waterMarkView.waterExportButton.title = NSLocalizedString("Exceed Times Limit", comment: "")
  206. self.waterMarkView.isSecondEnable = false
  207. self.waterMarkView.subscribeLabel.stringValue = NSLocalizedString("Upgrade Now", comment: "")
  208. } else {
  209. self.waterMarkView.isSecondEnable = true
  210. self.waterMarkView.waterExportButton.title = NSLocalizedString("Delete Files from List", comment: "")
  211. }
  212. } else if tempType.isConvertType() {
  213. if (tempLimit) {
  214. self.waterMarkView.waterExportButton.title = NSLocalizedString("Exceed Times Limit", comment: "")
  215. self.waterMarkView.isSecondEnable = false
  216. self.waterMarkView.subscribeLabel.stringValue = NSLocalizedString("Upgrade Now", comment: "")
  217. } else {
  218. self.waterMarkView.isSecondEnable = true
  219. // if KMLightMemberManager.manager.isLogin() {
  220. self.waterMarkView.waterExportButton.title = NSLocalizedString("Convert First 10 Pages", comment: "")
  221. // } else {
  222. // subscribeWaterMarkWindowController.waterMarkView.waterExportButton.title = NSLocalizedString("Later", comment: "")
  223. // }
  224. }
  225. } else if (type == .save) {
  226. self.waterMarkView.subscribeLabel.stringValue = NSLocalizedString("Upgrade Now", comment: "")
  227. self.waterMarkView.waterExportButton.title = NSLocalizedString("Save with Watermark", comment: "")
  228. } else {
  229. self.waterMarkView.waterExportButton.title = NSLocalizedString("Save with Watermark", comment: "")
  230. }
  231. }
  232. }
  233. }
  234. static func isSampleController() -> KMSubscribeWaterMarkWindowController {
  235. for window in NSApp.windows {
  236. let controller = window.windowController
  237. if controller is KMSubscribeWaterMarkWindowController {
  238. return controller as! KMSubscribeWaterMarkWindowController
  239. }
  240. }
  241. return KMSubscribeWaterMarkWindowController()
  242. }
  243. static func canShowSubscripBenefits() -> Bool {
  244. #if VERSION_DMG
  245. return KMLightMemberManager.manager.isLogin() || tempType == .save || ((tempType.isConvertType() || tempType == .merge) && tempLimit)
  246. #else
  247. return KMLightMemberManager.manager.isLogin() || tempType == .save || ((tempType.isConvertType() || tempType == .merge) && !tempLimit)
  248. #endif
  249. }
  250. }
  251. // MARK: - Analytics (埋点)
  252. extension KMSubscribeWaterMarkWindowController {
  253. func trackEvent(eventName: String) -> Void {
  254. KMAnalytics.trackEvent(eventName: eventName, parameters: [
  255. KMAnalytics.Parameter.categoryKey : KMAnalytics.Category.puw,
  256. KMAnalytics.Parameter.labelKey : KMAnalytics.Label.sub_PUW], platform: .AppCenter, appTarget: .all)
  257. }
  258. func trackEvent(type: KMSubscribeWaterMarkType) -> Void {
  259. KMTools.trackEvent(type: type)
  260. }
  261. }