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): Support for iOS suites #1966

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions preferences/ios/Sources/PreferencesPlugin/Preferences.swift
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
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
@@ -1,4 +1,13 @@
export interface ConfigureOptions {
/**
* Set the suite name.
*
* If a suite is set, the group is automatically set to NativeStorage.
*
* @default ""
*/
suite?: string;

/**
* Set the preferences group.
*
Expand Down