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 {
|
2021-01-17 20:47:52 -08:00
|
|
|
if let path = path {
|
|
|
|
|
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 {
|
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
|
|
|
|
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()
|
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
|
|
|
|
2020-11-07 12:06:28 +01:00
|
|
|
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.
|
|
|
|
|
|
2020-11-07 12:06:28 +01:00
|
|
|
func getImage() -> Data? {
|
2020-06-28 21:25:40 +02:00
|
|
|
image
|
2018-11-10 22:38:12 -08:00
|
|
|
}
|
|
|
|
|
|
2020-11-07 12:06:28 +01:00
|
|
|
func getName() -> String {
|
2018-11-10 22:38:12 -08:00
|
|
|
// unwrap non-optional core data
|
2020-06-28 21:25:40 +02:00
|
|
|
name ?? ""
|
2018-11-10 22:38:12 -08:00
|
|
|
}
|
|
|
|
|
|
2020-11-07 12:06:28 +01:00
|
|
|
func getPath() -> String {
|
2018-11-10 22:38:12 -08:00
|
|
|
// 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
|
|
|
}
|