LocalModelInfo.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import FirebaseCore
  16. /// Model info object with details about downloaded and locally available model.
  17. // TODO: Can this be backed by user defaults property wrappers?
  18. class LocalModelInfo {
  19. /// Model name.
  20. let name: String
  21. /// Hash of the model, as returned by server.
  22. let modelHash: String
  23. /// Size of the model, as returned by server.
  24. let size: Int
  25. /// Local path of the model.
  26. let path: String
  27. init(name: String, modelHash: String, size: Int, path: String) {
  28. self.name = name
  29. self.modelHash = modelHash
  30. self.size = size
  31. self.path = path
  32. }
  33. /// Convenience init to create local model info from remotely downloaded model info and a local model path.
  34. convenience init(from remoteModelInfo: RemoteModelInfo, path: String) {
  35. self.init(
  36. name: remoteModelInfo.name,
  37. modelHash: remoteModelInfo.modelHash,
  38. size: remoteModelInfo.size,
  39. path: path
  40. )
  41. }
  42. /// Convenience init to create local model info from stored info in user defaults.
  43. convenience init?(fromDefaults defaults: UserDefaults, name: String, appName: String) {
  44. let defaultsPrefix = LocalModelInfo.getUserDefaultsKeyPrefix(appName: appName, modelName: name)
  45. guard let modelHash = defaults.value(forKey: "\(defaultsPrefix).model-hash") as? String,
  46. let size = defaults.value(forKey: "\(defaultsPrefix).model-size") as? Int,
  47. let path = defaults.value(forKey: "\(defaultsPrefix).model-path") as? String else {
  48. return nil
  49. }
  50. self.init(name: name, modelHash: modelHash, size: size, path: path)
  51. }
  52. }
  53. /// Extension to write local model info to user defaults.
  54. extension LocalModelInfo: DownloaderUserDefaultsWriteable {
  55. /// Get user defaults key prefix.
  56. private static func getUserDefaultsKeyPrefix(appName: String, modelName: String) -> String {
  57. let bundleID = Bundle.main.bundleIdentifier ?? ""
  58. return "\(bundleID).\(appName).\(modelName)"
  59. }
  60. /// Write local model info to user defaults.
  61. func writeToDefaults(_ defaults: UserDefaults, appName: String) {
  62. let defaultsPrefix = LocalModelInfo.getUserDefaultsKeyPrefix(appName: appName, modelName: name)
  63. defaults.setValue(modelHash, forKey: "\(defaultsPrefix).model-hash")
  64. defaults.setValue(size, forKey: "\(defaultsPrefix).model-size")
  65. defaults.setValue(path, forKey: "\(defaultsPrefix).model-path")
  66. }
  67. }
  68. /// Named user defaults for FirebaseML.
  69. extension UserDefaults {
  70. static var firebaseMLDefaults: UserDefaults {
  71. let suiteName = "com.google.firebase.ml"
  72. // TODO: reconsider force unwrapping
  73. let defaults = UserDefaults(suiteName: suiteName)!
  74. return defaults
  75. }
  76. }