1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 import SwiftUI
10 
11 final class ResourceManager: ObservableObject {
12   @AppStorage("modelPath") var modelPath = ""
13   @AppStorage("tokenizerPath") var tokenizerPath = ""
14   private let fileManager = FileManager.default
15 
16   var isModelValid: Bool {
17     fileManager.fileExists(atPath: modelPath)
18   }
19 
20   var isTokenizerValid: Bool {
21     fileManager.fileExists(atPath: tokenizerPath)
22   }
23 
24   var modelName: String {
25     URL(fileURLWithPath: modelPath).deletingPathExtension().lastPathComponent
26   }
27 
28   var tokenizerName: String {
29     URL(fileURLWithPath: tokenizerPath).deletingPathExtension().lastPathComponent
30   }
31 
createDirectoriesIfNeedednull32   func createDirectoriesIfNeeded() throws {
33     guard let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
34     try fileManager.createDirectory(at: documentsDirectory.appendingPathComponent("models"), withIntermediateDirectories: true, attributes: nil)
35     try fileManager.createDirectory(at: documentsDirectory.appendingPathComponent("tokenizers"), withIntermediateDirectories: true, attributes: nil)
36   }
37 }
38