// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import Foundation /// Possible errors with model downloading. public enum DownloadError: Error, Equatable { /// No model with this name found on server. case notFound /// Caller does not have necessary permissions for this operation. case permissionDenied /// Conditions not met to perform download. case failedPrecondition /// Not enough space for model on device. case notEnoughSpace /// Malformed model name. case invalidArgument /// Other errors with description. case internalError(description: String) } /// Possible errors with locating model on device. public enum DownloadedModelError: Error { /// File system error. case fileIOError /// Model not found on device. case notFound } /// Possible ways to get a custom model. public enum ModelDownloadType { /// Get local model stored on device. case localModel /// Get local model on device and update to latest model from server in the background. case localModelUpdateInBackground /// Get latest model from server. case latestModel } /// Downloader to manage custom model downloads. public struct ModelDownloader { /// Downloads a custom model to device or gets a custom model already on device, w/ optional handler for progress. public func getModel(name modelName: String, downloadType: ModelDownloadType, conditions: ModelDownloadConditions, progressHandler: ((Float) -> Void)? = nil, completion: @escaping (Result) -> Void) { // TODO: Model download let modelSize = Int() let modelPath = String() let modelHash = String() let customModel = CustomModel( name: modelName, size: modelSize, path: modelPath, hash: modelHash ) completion(.success(customModel)) completion(.failure(.notFound)) } /// Gets all downloaded models. public func listDownloadedModels(completion: @escaping (Result, DownloadedModelError>) -> Void) { let customModels = Set() // TODO: List downloaded models completion(.success(customModels)) completion(.failure(.notFound)) } /// Deletes a custom model from device. public func deleteDownloadedModel(name modelName: String, completion: @escaping (Result) -> Void) { // TODO: Delete previously downloaded model completion(.success(())) completion(.failure(.notFound)) } }