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

Mirroring feature #46

Open
sindresorhus opened this issue May 6, 2020 · 1 comment
Open

Mirroring feature #46

sindresorhus opened this issue May 6, 2020 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@sindresorhus
Copy link
Owner

I just implemented a today widget in one of my apps. I needed to read a preference item in the today widget that was currently stored in my main app. Usually, this would mean having to migrate the key over to the App Groups user defaults suite. However, I'm lazy and instead just went with observing the key and then setting it in the App Groups user defaults suite when changed:

// Constants.swift

extension Defaults.Keys {
	static let timeZonesInMenu = Key<[ChosenTimeZone]>("timeZonesInMenu", default: [])
}


// SharedConstants.swift

struct Constants {
	static let appGroupId = "XXXX.com.sindresorhus.Dato.shared"
	static let sharedDefaults = UserDefaults(suiteName: appGroupId)!
}
extension Defaults.Keys {
	static let sharedTimeZones = Key<[ChosenTimeZone]>("sharedTimeZones", default: [], suite: Constants.sharedDefaults)
}


// OtherFile.swift

// Sync time zone changes to the shared item.
Defaults.observe(.timeZonesInMenu) { change in
	Defaults[.sharedTimeZones] = change.newValue
}
	.tieToLifetime(of: self)

And I was thinking this might be useful for others too.

I'm thinking:

// Key with same name
Defaults.mirror(.key, to: suite)

// Key with different name (where the key is strongly-typed and defined in a shared Constants.swift file or something)
Defaults.mirror(.key, to: suite, as: .sharedKey)

// Key with different name
Defaults.mirror(.key, to: suite, as: "sharedKey")

Would probably need a similar unmirror method too.

Note that this is not syncing. It's only one way.

Feedback welcome.

@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels May 6, 2020
@ShlomoCode
Copy link

I wonder why it would be right to encourage such use. Setting up an app group is really easy, just a few clicks in Xcode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants