passforios/passKit/Models/PasswordEntity.swift
Bob Sun 2abbceb2e9
Set name and url in Password non-optional
Name and url in Password class shouldn't be optional because we store
them in core data as non-optional. This change also help us to avoid
man unneccessary unwrap.
2018-11-10 22:38:12 -08:00

63 lines
1.5 KiB
Swift

//
// PasswordEntity.swift
// pass
//
// Created by Mingshen Sun on 11/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
import SwiftyUserDefaults
extension PasswordEntity {
public var nameWithCategory: String {
get {
if let p = path, p.hasSuffix(".gpg") {
return String(p.prefix(upTo: p.index(p.endIndex, offsetBy: -4)))
} else {
return ""
}
}
}
public func getCategoryText() -> String {
return getCategoryArray().joined(separator: " > ")
}
public func getCategoryArray() -> [String] {
var parentEntity = parent
var passwordCategoryArray: [String] = []
while parentEntity != nil {
passwordCategoryArray.append(parentEntity!.name!)
parentEntity = parentEntity!.parent
}
passwordCategoryArray.reverse()
return passwordCategoryArray
}
public func getURL() -> URL? {
if let p = getPath().stringByAddingPercentEncodingForRFC3986() {
return URL(string: p)
}
return nil
}
// XXX: define some getters to get core data, we need to consider
// manually write models instead auto generation.
public func getImage() -> Data? {
return image
}
public func getName() -> String {
// unwrap non-optional core data
return name ?? ""
}
public func getPath() -> String {
// unwrap non-optional core data
return path ?? ""
}
}