Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(preferences): Added support for iOS suites #2153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Removes old data with `_cap_` prefix from the Capacitor 2 Storage plugin.
| Prop | Type | Description | Default | Since |
| ----------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ----- |
| **`group`** | <code>string</code> | Set the preferences group. Preferences groups are used to organize key/value pairs. Using the value 'NativeStorage' provides backwards-compatibility with [`cordova-plugin-nativestorage`](https://www.npmjs.com/package/cordova-plugin-nativestorage). WARNING: The `clear()` method can delete unintended values when using the 'NativeStorage' group. | <code>CapacitorStorage</code> | 1.0.0 |
| **`suite`** | <code>string</code> | iOS: Set the suite name. If a suite is set, the group is automatically set to NativeStorage. | <code></code> | 6.0.0 |


#### GetResult
Expand Down
20 changes: 18 additions & 2 deletions preferences/ios/Sources/PreferencesPlugin/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,37 @@ public struct PreferencesConfiguration {
case named(String), cordovaNativeStorage
}

let suite: String
let group: Group

public init(for group: Group = .named("CapacitorStorage")) {
self.group = group
public init(for suite: String = "", for group: Group = .named("CapacitorStorage")) {
self.suite = suite
if !suite.isEmpty {
self.group = .cordovaNativeStorage;
} else {
self.group = group
}
}
}

public class Preferences {
private let configuration: PreferencesConfiguration

private var defaults: UserDefaults {
if !configuration.suite.isEmpty {
if let i = UserDefaults.init(suiteName: configuration.suite) {
return i
}
}

return UserDefaults.standard
}

private var prefix: String {
if !configuration.suite.isEmpty {
return ""
}

switch configuration.group {
case .cordovaNativeStorage:
return ""
Expand Down
17 changes: 11 additions & 6 deletions preferences/ios/Sources/PreferencesPlugin/PreferencesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ public class PreferencesPlugin: CAPPlugin, CAPBridgedPlugin {

@objc func configure(_ call: CAPPluginCall) {
let group = call.getString("group")
let suite = call.getString("suite")
let configuration: PreferencesConfiguration

if let group = group {
if group == "NativeStorage" {
configuration = PreferencesConfiguration(for: .cordovaNativeStorage)
if group == nil, let suite = suite {
configuration = PreferencesConfiguration(for: suite)
} else {
if let group = group, let suite = suite {
if group == "NativeStorage" {
configuration = PreferencesConfiguration(for: suite, for: .cordovaNativeStorage)
} else {
configuration = PreferencesConfiguration(for: suite, for: .named(group))
}
} else {
configuration = PreferencesConfiguration(for: .named(group))
configuration = PreferencesConfiguration()
}
} else {
configuration = PreferencesConfiguration()
}

preferences = Preferences(with: configuration)
Expand Down
9 changes: 9 additions & 0 deletions preferences/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ export interface ConfigureOptions {
* @since 1.0.0
*/
group?: string;

/**
* Set the suite name.
*
* If a suite is set, the group is automatically set to NativeStorage.
*
* @default ""
*/
suite?: string;
}

export interface GetOptions {
Expand Down