passforios/passKit/Models/PasswordEntity.swift

59 lines
1.4 KiB
Swift
Raw Normal View History

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
2020-11-07 12:06:28 +01:00
public extension PasswordEntity {
var nameWithCategory: String {
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
2020-11-07 12:06:28 +01:00
func getCategoryText() -> String {
getCategoryArray().joined(separator: " > ")
}
2018-12-09 16:59:07 -08:00
2020-11-07 12:06:28 +01:00
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()
return passwordCategoryArray
2017-03-10 23:01:56 -08:00
}
2018-12-09 16:59:07 -08:00
2020-11-07 12:06:28 +01:00
func getURL() throws -> URL {
if let path = getPath().stringByAddingPercentEncodingForRFC3986(), let url = URL(string: path) {
return url
}
throw AppError.unknown
}
// XXX: define some getters to get core data, we need to consider
// manually write models instead auto generation.
2020-11-07 12:06:28 +01:00
func getImage() -> Data? {
image
}
2020-11-07 12:06:28 +01:00
func getName() -> String {
// unwrap non-optional core data
name ?? ""
}
2020-11-07 12:06:28 +01:00
func getPath() -> String {
// unwrap non-optional core data
path ?? ""
}
2017-02-11 23:45:00 +08:00
}