2017-02-11 23:45:00 +08:00
|
|
|
//
|
|
|
|
|
// PasswordEntity.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Mingshen Sun on 11/2/2017.
|
|
|
|
|
// Copyright © 2017 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import SwiftyUserDefaults
|
|
|
|
|
|
|
|
|
|
extension PasswordEntity {
|
2017-06-13 11:42:49 +08:00
|
|
|
public var nameWithCategory: String {
|
2020-09-20 15:07:18 +02:00
|
|
|
if let path = path, path.hasSuffix(".gpg") {
|
|
|
|
|
return String(path.prefix(upTo: path.index(path.endIndex, offsetBy: -4)))
|
2017-03-16 00:38:38 +08:00
|
|
|
}
|
2018-12-15 21:49:18 +01:00
|
|
|
return ""
|
2017-03-16 00:38:38 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-06-13 11:42:49 +08:00
|
|
|
public func getCategoryText() -> String {
|
2020-06-28 21:25:40 +02:00
|
|
|
getCategoryArray().joined(separator: " > ")
|
2017-10-28 15:32:30 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2017-10-28 15:32:30 +08:00
|
|
|
public func getCategoryArray() -> [String] {
|
2017-03-10 23:01:56 -08:00
|
|
|
var parentEntity = parent
|
|
|
|
|
var passwordCategoryArray: [String] = []
|
|
|
|
|
while parentEntity != nil {
|
|
|
|
|
passwordCategoryArray.append(parentEntity!.name!)
|
|
|
|
|
parentEntity = parentEntity!.parent
|
|
|
|
|
}
|
|
|
|
|
passwordCategoryArray.reverse()
|
2017-10-28 15:32:30 +08:00
|
|
|
return passwordCategoryArray
|
2017-03-10 23:01:56 -08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2019-06-09 22:18:54 -07:00
|
|
|
public func getURL() throws -> URL {
|
2020-09-20 15:07:18 +02:00
|
|
|
if let path = getPath().stringByAddingPercentEncodingForRFC3986(), let url = URL(string: path) {
|
|
|
|
|
return url
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2020-09-20 15:07:18 +02:00
|
|
|
throw AppError.unknown
|
2017-04-25 13:01:17 -07:00
|
|
|
}
|
2018-11-10 22:38:12 -08:00
|
|
|
|
|
|
|
|
// XXX: define some getters to get core data, we need to consider
|
|
|
|
|
// manually write models instead auto generation.
|
|
|
|
|
|
|
|
|
|
public func getImage() -> Data? {
|
2020-06-28 21:25:40 +02:00
|
|
|
image
|
2018-11-10 22:38:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func getName() -> String {
|
|
|
|
|
// unwrap non-optional core data
|
2020-06-28 21:25:40 +02:00
|
|
|
name ?? ""
|
2018-11-10 22:38:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func getPath() -> String {
|
|
|
|
|
// unwrap non-optional core data
|
2020-06-28 21:25:40 +02:00
|
|
|
path ?? ""
|
2018-11-10 22:38:12 -08:00
|
|
|
}
|
2017-02-11 23:45:00 +08:00
|
|
|
}
|