CompanyDirectoryContact.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. public class CompanyDirectoryContact: NSObject
  22. {
  23. var id: String
  24. var pk: Data
  25. @objc var first: String?
  26. @objc var last: String?
  27. var csi: String?
  28. var cat: [String]?
  29. public init(dictionary: [AnyHashable: Any?]) {
  30. id = dictionary["id"] as! String
  31. pk = NSData(base64Encoded: dictionary["pk"] as! String, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)! as Data
  32. if let tmp = dictionary["first"] as? String {
  33. first = tmp
  34. }
  35. if let tmp = dictionary["last"] as? String {
  36. last = tmp
  37. }
  38. if let tmp = dictionary["csi"] as? String {
  39. csi = tmp
  40. }
  41. if let tmp = dictionary["cat"] as? [String] {
  42. cat = tmp
  43. }
  44. }
  45. func fullName() -> String {
  46. var fullName = ""
  47. if UserSettings.shared().displayOrderFirstName == true {
  48. if first != nil {
  49. fullName.append(first!)
  50. }
  51. if last != nil {
  52. if fullName.count > 0 {
  53. fullName.append(" ")
  54. }
  55. fullName.append(last!)
  56. }
  57. } else {
  58. if last != nil {
  59. fullName.append(last!)
  60. }
  61. if first != nil {
  62. if fullName.count > 0 {
  63. fullName.append(" ")
  64. }
  65. fullName.append(first!)
  66. }
  67. }
  68. return fullName
  69. }
  70. func fullNameWithCSI() -> String {
  71. var fullName = self.fullName()
  72. if csi != nil {
  73. if fullName.count > 0 {
  74. fullName.append(" ")
  75. }
  76. fullName.append("("+csi!+")")
  77. }
  78. return fullName
  79. }
  80. func categoryString() -> String {
  81. if cat != nil {
  82. let categoryDict: [String:String] = MyIdentityStore.shared().directoryCategories as! [String:String]
  83. var categoryString = ""
  84. for category in cat! {
  85. if categoryString.count > 0 {
  86. categoryString.append(", ")
  87. }
  88. let catName = categoryDict[category]
  89. categoryString.append(catName!)
  90. }
  91. return categoryString
  92. }
  93. return ""
  94. }
  95. }