|
| 1 | +// |
| 2 | +// Copyright Amazon.com Inc. or its affiliates. |
| 3 | +// All Rights Reserved. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: Apache-2.0 |
| 6 | +// |
| 7 | + |
| 8 | +/// The Geo category enables you to interact with geospacial services. |
| 9 | +final public class GeoCategory: Category { |
| 10 | + /// Geo category type |
| 11 | + public let categoryType = CategoryType.geo |
| 12 | + |
| 13 | + /// Geo category plugins |
| 14 | + var plugins = [PluginKey: GeoCategoryPlugin]() |
| 15 | + |
| 16 | + /// Returns the plugin added to the category, if only one plugin is added. Accessing this property if no plugins |
| 17 | + /// are added, or if more than one plugin is added, will cause a preconditionFailure. |
| 18 | + var plugin: GeoCategoryPlugin { |
| 19 | + guard isConfigured else { |
| 20 | + preconditionFailure( |
| 21 | + """ |
| 22 | + \(categoryType.displayName) category is not configured. Call Amplify.configure() before using \ |
| 23 | + any methods on the category. |
| 24 | + """ |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + guard !plugins.isEmpty else { |
| 29 | + preconditionFailure("No plugins added to \(categoryType.displayName) category.") |
| 30 | + } |
| 31 | + |
| 32 | + guard plugins.count == 1, let plugin = plugins.first?.value else { |
| 33 | + preconditionFailure( |
| 34 | + """ |
| 35 | + More than 1 plugin added to \(categoryType.displayName) category. \ |
| 36 | + You must invoke operations on this category by getting the plugin you want, as in: |
| 37 | + #"Amplify.\(categoryType.displayName).getPlugin(for: "ThePluginKey").foo() |
| 38 | + """ |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + return plugin |
| 43 | + } |
| 44 | + |
| 45 | + var isConfigured = false |
| 46 | + |
| 47 | + // MARK: - Plugin handling |
| 48 | + |
| 49 | + /// Adds `plugin` to the list of Plugins that implement functionality for this category. |
| 50 | + /// |
| 51 | + /// - Parameter plugin: The Plugin to add |
| 52 | + public func add(plugin: GeoCategoryPlugin) throws { |
| 53 | + log.debug("Adding plugin: \(String(describing: plugin))") |
| 54 | + let key = plugin.key |
| 55 | + guard !key.isEmpty else { |
| 56 | + let pluginDescription = String(describing: plugin) |
| 57 | + let error = GeoError.configuration( |
| 58 | + "Plugin \(pluginDescription) has an empty `key`.", |
| 59 | + "Set the `key` property for \(String(describing: plugin))") |
| 60 | + throw error |
| 61 | + } |
| 62 | + |
| 63 | + guard !isConfigured else { |
| 64 | + let pluginDescription = String(describing: plugin) |
| 65 | + let error = ConfigurationError.amplifyAlreadyConfigured( |
| 66 | + "\(pluginDescription) cannot be added after `Amplify.configure()`.", |
| 67 | + "Do not add plugins after calling `Amplify.configure()`." |
| 68 | + ) |
| 69 | + throw error |
| 70 | + } |
| 71 | + |
| 72 | + plugins[plugin.key] = plugin |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns the added plugin with the specified `key` property. |
| 76 | + /// |
| 77 | + /// - Parameter key: The PluginKey (String) of the plugin to retrieve |
| 78 | + /// - Returns: The wrapped plugin |
| 79 | + public func getPlugin(for key: PluginKey) throws -> GeoCategoryPlugin { |
| 80 | + guard let plugin = plugins[key] else { |
| 81 | + let keys = plugins.keys.joined(separator: ", ") |
| 82 | + let error = GeoError.configuration( |
| 83 | + "No plugin has been added for '\(key)'.", |
| 84 | + "Either add a plugin for '\(key)', or use one of the known keys: \(keys)") |
| 85 | + throw error |
| 86 | + } |
| 87 | + return plugin |
| 88 | + } |
| 89 | + |
| 90 | + /// Removes the plugin registered for `key` from the list of Plugins that implement functionality for this category. |
| 91 | + /// If no plugin has been added for `key`, no action is taken, making this method safe to call multiple times. |
| 92 | + /// |
| 93 | + /// - Parameter key: The key used to `add` the plugin |
| 94 | + public func removePlugin(for key: PluginKey) { |
| 95 | + plugins.removeValue(forKey: key) |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments