Skip to content

Commit f01e682

Browse files
Merge pull request #6 from carloshpdoc/feat/app-group-support
Add App Group support to NoteletStorage and noteletSheet
2 parents 71bf0dd + 5ed2a7b commit f01e682

3 files changed

Lines changed: 53 additions & 12 deletions

File tree

Sources/Notelet/Constants.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,15 @@
55
// Created by Mykola Harmash on 05.05.26.
66
//
77

8-
public let NOTELET_APP_STORAGE_LATEST_SEEN_APP_VERSION_KEY = "Notelet.LatestSeenAppVersion"
8+
/// Namespaced keys used by `NoteletStorage` when reading from or writing to a
9+
/// `UserDefaults` instance. Exposed publicly so host apps that share state with
10+
/// an extension target (widget, intent, share extension) can read or clear the
11+
/// same keys from the App Group `UserDefaults`.
12+
public enum NoteletStorageKey {
13+
/// The persisted app version string that was last seen by the user when a
14+
/// `noteletSheet` was dismissed in `.current` mode.
15+
public static let latestSeenAppVersion = "Notelet.LatestSeenAppVersion"
16+
}
17+
18+
@available(*, deprecated, renamed: "NoteletStorageKey.latestSeenAppVersion", message: "Use NoteletStorageKey.latestSeenAppVersion instead.")
19+
public let NOTELET_APP_STORAGE_LATEST_SEEN_APP_VERSION_KEY = NoteletStorageKey.latestSeenAppVersion

Sources/Notelet/NoteletSheet.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct NoteletSheet: ViewModifier {
1414
let version: NoteletPresentedVersion?
1515
let onDismiss: () -> Void
1616
let configuration: NoteletConfiguration
17+
let userDefaults: UserDefaults
1718

1819
private var versionToShow: String? {
1920
switch version {
@@ -43,8 +44,8 @@ struct NoteletSheet: ViewModifier {
4344
}
4445

4546
private var isCurrentVersionAlreadySeen: Bool {
46-
UserDefaults.standard.string(
47-
forKey: NOTELET_APP_STORAGE_LATEST_SEEN_APP_VERSION_KEY
47+
userDefaults.string(
48+
forKey: NoteletStorageKey.latestSeenAppVersion
4849
) == Helpers.getCurrentAppVersion()
4950
}
5051

@@ -83,26 +84,35 @@ struct NoteletSheet: ViewModifier {
8384

8485
private func handleDismiss() {
8586
if isCurrentVersionMode {
86-
NoteletStorage.markCurrentVersionAsSeen()
87+
NoteletStorage.markCurrentVersionAsSeen(userDefaults: userDefaults)
8788
}
88-
89+
8990
onDismiss()
9091
}
9192
}
9293

9394
extension View {
95+
/// Attach a release-notes sheet to the modified view.
96+
///
97+
/// - Parameter userDefaults: Storage backing the "seen version" check used
98+
/// in `.current` presentation mode. Defaults to `.standard`. Pass an App
99+
/// Group `UserDefaults` (e.g. `UserDefaults(suiteName: "group.com.example.myapp")`)
100+
/// when the host app needs to share "seen" state with an extension
101+
/// target like a widget, intent, or share extension.
94102
public func noteletSheet(
95103
notes: [NoteletVersionNotes],
96104
version: NoteletPresentedVersion? = nil,
97105
onDismiss: @escaping () -> Void = { },
98-
configuration: NoteletConfiguration = .init()
106+
configuration: NoteletConfiguration = .init(),
107+
userDefaults: UserDefaults = .standard
99108
) -> some View {
100109
modifier(
101110
NoteletSheet(
102111
notes: notes,
103112
version: version,
104113
onDismiss: onDismiss,
105-
configuration: configuration
114+
configuration: configuration,
115+
userDefaults: userDefaults
106116
)
107117
)
108118
}

Sources/Notelet/Storage.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import Foundation
22

33
public final class NoteletStorage {
4-
public static func markCurrentVersionAsSeen() {
5-
UserDefaults.standard.set(
4+
/// Mark the current bundle version as seen.
5+
///
6+
/// - Parameter userDefaults: Storage to write to. Defaults to `.standard`.
7+
/// Pass `UserDefaults(suiteName: "group.com.example.myapp")` to share the
8+
/// "seen" state with an App Group target (widget, intent, share extension).
9+
public static func markCurrentVersionAsSeen(userDefaults: UserDefaults = .standard) {
10+
userDefaults.set(
611
Helpers.getCurrentAppVersion(),
7-
forKey: NOTELET_APP_STORAGE_LATEST_SEEN_APP_VERSION_KEY
12+
forKey: NoteletStorageKey.latestSeenAppVersion
813
)
914
}
1015

11-
public static func resetSeenVersion() {
12-
UserDefaults.standard.removeObject(forKey: NOTELET_APP_STORAGE_LATEST_SEEN_APP_VERSION_KEY)
16+
/// Clear the persisted "latest seen version" so the next `.current`
17+
/// presentation triggers regardless of bundle version.
18+
///
19+
/// - Parameter userDefaults: Storage to clear. Defaults to `.standard`.
20+
/// Pass an App Group `UserDefaults` here if you used one in
21+
/// `markCurrentVersionAsSeen(userDefaults:)`.
22+
public static func resetSeenVersion(userDefaults: UserDefaults = .standard) {
23+
userDefaults.removeObject(forKey: NoteletStorageKey.latestSeenAppVersion)
24+
}
25+
26+
/// Read the persisted "latest seen version" string, or `nil` if it has
27+
/// never been set.
28+
///
29+
/// - Parameter userDefaults: Storage to read from. Defaults to `.standard`.
30+
/// - Returns: The bundle version string the user last saw, or `nil`.
31+
public static func getLatestSeenAppVersion(userDefaults: UserDefaults = .standard) -> String? {
32+
userDefaults.string(forKey: NoteletStorageKey.latestSeenAppVersion)
1333
}
1434
}

0 commit comments

Comments
 (0)