|
@@ -1,205 +0,0 @@
|
|
|
-//
|
|
|
-// KMStampManagerNew.swift
|
|
|
-// PDF Reader Pro
|
|
|
-//
|
|
|
-// Created by liujiajie on 2023/11/21.
|
|
|
-//
|
|
|
-
|
|
|
-import Foundation
|
|
|
-
|
|
|
-class KMStamp: NSObject {
|
|
|
- @objc var tag: String = ""
|
|
|
- @objc var string: String = ""
|
|
|
- @objc var subString: String = ""
|
|
|
- @objc var imagePath: String = ""
|
|
|
- var color: Int = 0
|
|
|
- var date: Date?
|
|
|
- var isImageStamp: Bool = false
|
|
|
-}
|
|
|
-
|
|
|
-class KMStampManagerNew: NSObject {
|
|
|
- var stamps: [KMStamp] = []
|
|
|
-
|
|
|
- static let shareManager = KMStampManagerNew()
|
|
|
- func tagString() -> String {
|
|
|
- let dateFormatter = DateFormatter()
|
|
|
- dateFormatter.dateFormat = "yyMMddHHmmss"
|
|
|
- return "\(dateFormatter.string(from: Date()))\(Int.random(in: 0..<10000))"
|
|
|
- }
|
|
|
-
|
|
|
- override init() {
|
|
|
- super.init()
|
|
|
- stamps = []
|
|
|
- if FileManager.default.fileExists(atPath: kStampPlistPath.path),
|
|
|
- let dictionary = NSDictionary(contentsOfFile: kStampPlistPath.path) as? [String: Any] {
|
|
|
-
|
|
|
- var deleteKeys: [String] = []
|
|
|
-
|
|
|
- for (key, value) in dictionary {
|
|
|
- var isAdd = true
|
|
|
- var stamp: KMStamp = KMStamp()
|
|
|
- stamp.isImageStamp = false
|
|
|
- stamp.tag = key
|
|
|
- if let dic = value as? [String: Any] {
|
|
|
- if let stampOfString = dic[kStampOfStringKey] as? String {
|
|
|
- stamp.string = stampOfString
|
|
|
- }
|
|
|
- if let stampOfSubString = dic[kStampOfSubStringKey] as? String {
|
|
|
- stamp.subString = stampOfSubString
|
|
|
- }
|
|
|
- if let stampOfColor = dic[kStampOfColorKey] as? Int {
|
|
|
- stamp.color = stampOfColor
|
|
|
- }
|
|
|
- if let stampOfDate = dic[kStampOfDateKey] as? Date {
|
|
|
- stamp.date = stampOfDate
|
|
|
- }
|
|
|
-
|
|
|
- if let stampOfImagePathKey = dic[kStampOfImagePathKey] as? String {
|
|
|
- stamp.isImageStamp = true
|
|
|
- let path = kStampFolderPath.appendingPathComponent(stampOfImagePathKey).path
|
|
|
- if FileManager.default.fileExists(atPath: path) {
|
|
|
- stamp.imagePath = path
|
|
|
- } else {
|
|
|
- isAdd = false
|
|
|
- deleteKeys.append(key)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if isAdd {
|
|
|
- stamps.append(stamp)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if !deleteKeys.isEmpty {
|
|
|
- var newDictionary = dictionary
|
|
|
- for key in deleteKeys {
|
|
|
- newDictionary.removeValue(forKey: key)
|
|
|
- }
|
|
|
- (newDictionary as NSDictionary).write(toFile: kStampPlistPath.path, atomically: true)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- func addStamp(withString string: String?, dateString: String?, color: KMAnnotationStampColorType) -> Bool {
|
|
|
- if !FileManager.default.fileExists(atPath: kStampFolderPath.path) {
|
|
|
- do {
|
|
|
- try FileManager.default.createDirectory(atPath: kStampFolderPath.path, withIntermediateDirectories: true, attributes: nil)
|
|
|
- } catch {
|
|
|
- return false
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if !FileManager.default.fileExists(atPath: kStampPlistPath.path) {
|
|
|
- if !FileManager.default.createFile(atPath: kStampPlistPath.path, contents: nil, attributes: nil) {
|
|
|
- return false
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if var dictionary = NSDictionary(contentsOfFile: kStampPlistPath.path) as? [String: Any] {
|
|
|
- var newDictionary = dictionary
|
|
|
-
|
|
|
- let tag = tagString()
|
|
|
- var stamp: KMStamp = KMStamp()
|
|
|
-
|
|
|
- var stampDictionary: [String: Any] = [:]
|
|
|
- if let string = string {
|
|
|
- stampDictionary[kStampOfStringKey] = string
|
|
|
- stamp.string = string
|
|
|
- }
|
|
|
- if let dateString = dateString {
|
|
|
- stampDictionary[kStampOfSubStringKey] = dateString
|
|
|
- stamp.subString = dateString
|
|
|
- }
|
|
|
-
|
|
|
- stampDictionary[kStampOfColorKey] = color.rawValue
|
|
|
- stampDictionary[kStampOfDateKey] = Date()
|
|
|
-
|
|
|
- newDictionary[tag] = stampDictionary
|
|
|
-
|
|
|
- if (newDictionary as NSDictionary).write(toFile: kStampPlistPath.path, atomically: true) {
|
|
|
- self.stamps.append(stamp)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return false
|
|
|
- }
|
|
|
-
|
|
|
- func addStamp(withImagePath path: String) -> Bool {
|
|
|
- if !FileManager.default.fileExists(atPath: kStampFolderPath.path) {
|
|
|
- do {
|
|
|
- try FileManager.default.createDirectory(atPath: kStampFolderPath.path, withIntermediateDirectories: false, attributes: nil)
|
|
|
- } catch {
|
|
|
- return false
|
|
|
- }
|
|
|
- }
|
|
|
- if !FileManager.default.fileExists(atPath: kStampPlistPath.path) {
|
|
|
- if !FileManager.default.createFile(atPath: kStampPlistPath.path, contents: nil, attributes: nil) {
|
|
|
- return false
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- let tag = tagString()
|
|
|
- let rPath = (kStampFolderPath.path as NSString).appendingPathComponent(tag).stringByAppendingPathExtension("jpg")
|
|
|
-
|
|
|
- guard let image = NSImage(contentsOfFile: path) else {
|
|
|
- return false
|
|
|
- }
|
|
|
- image.lockFocus()
|
|
|
- let bits = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height))
|
|
|
- image.unlockFocus()
|
|
|
- let imageProps = [NSBitmapImageRep.PropertyKey.compressionFactor: NSNumber(value: 0.5)]
|
|
|
- guard let imageData = bits?.representation(using: NSBitmapImageRep.FileType.jpeg, properties: imageProps) else {
|
|
|
- return false
|
|
|
- }
|
|
|
- if !FileManager.default.createFile(atPath: rPath, contents: imageData, attributes: nil) {
|
|
|
- return false
|
|
|
- }
|
|
|
-
|
|
|
-// guard let dictionary = NSDictionary(contentsOfFile: kStampPlistPath.path) as? [String: Any] else {
|
|
|
-// return false
|
|
|
-// }
|
|
|
- let dictionary = NSDictionary(contentsOfFile: kStampPlistPath.path) as? [String: Any]
|
|
|
- var newDictionary = NSMutableDictionary(dictionary: dictionary ?? [:])
|
|
|
-
|
|
|
- let stamp = KMStamp()
|
|
|
- stamp.isImageStamp = true
|
|
|
- stamp.date = Date()
|
|
|
- stamp.imagePath = rPath
|
|
|
- stamp.tag = tag
|
|
|
-
|
|
|
- let stampDictionary = NSMutableDictionary()
|
|
|
- stampDictionary.setObject((rPath as NSString).lastPathComponent, forKey: kStampOfImagePathKey as NSCopying)
|
|
|
- stampDictionary.setObject(Date(), forKey: kStampOfDateKey as NSCopying)
|
|
|
-
|
|
|
- newDictionary.setObject(stampDictionary, forKey: tag as NSCopying)
|
|
|
-
|
|
|
- if newDictionary.write(toFile: kStampPlistPath.path, atomically: true) {
|
|
|
- stamps.append(stamp)
|
|
|
- }
|
|
|
- return false
|
|
|
- }
|
|
|
- func removeStamp(at index: Int) -> Bool {
|
|
|
- if index < self.stamps.count {
|
|
|
- let stamp = self.stamps[index]
|
|
|
- let key = stamp.tag
|
|
|
- if !FileManager.default.fileExists(atPath: kStampPlistPath.path) {
|
|
|
- return false
|
|
|
- }
|
|
|
- var dictionary = NSDictionary(contentsOfFile: kStampPlistPath.path) as? [String: Any]
|
|
|
- var newDictionary = dictionary
|
|
|
- newDictionary!.removeValue(forKey: key)
|
|
|
- if (newDictionary! as NSDictionary).write(toFile: kStampPlistPath.path, atomically: true) {
|
|
|
- if stamp.isImageStamp {
|
|
|
- do {
|
|
|
- try FileManager.default.removeItem(atPath: stamp.imagePath)
|
|
|
- } catch {
|
|
|
- return false
|
|
|
- }
|
|
|
- }
|
|
|
- self.stamps.removeObject(stamp)
|
|
|
- return true
|
|
|
- }
|
|
|
- }
|
|
|
- return false
|
|
|
- }
|
|
|
-}
|