passforios/passKit/Models/PasswordEntity.swift

62 lines
1.5 KiB
Swift
Raw Normal View History

2017-02-11 23:45:00 +08:00
//
// PasswordEntity.swift
2021-08-28 07:32:31 +02:00
// passKit
2017-02-11 23:45:00 +08:00
//
// 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 {
2021-01-17 20:47:52 -08:00
if path.hasSuffix(".gpg") {
return String(path.prefix(upTo: path.index(path.endIndex, offsetBy: -4)))
}
2021-01-31 13:34:37 +01:00
return path
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.other(message: "cannot decode URL")
}
// 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
}