Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hank121314 committed Aug 12, 2024
1 parent caeb02b commit dd44d0d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Sources/Defaults/Defaults+iCloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ extension Defaults {
/**
Add the keys to be automatically synced.
*/
// TODO: support array of Defaults.Key after swift 6 Pack iteration
public static func add<each Value: Defaults.Serializable>(_ keys: repeat Defaults.Key<each Value>) {
repeat synchronizer.add(each keys)
}
Expand Down Expand Up @@ -267,12 +268,15 @@ final class iCloudSynchronizer {
guard isInserted else {
return
}

localKeysMonitor.add(key: key)

// If the local value is the default value, only sync from remote, since all devices should already have the default value.
if key.isDefaultValue() {
if key._isDefaultValue {
guard case .remote = latestDataSource(forKey: key) else {
return
}

syncWithoutWaiting([key], .remote)
} else {
syncWithoutWaiting([key])
Expand Down
11 changes: 10 additions & 1 deletion Sources/Defaults/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ extension Defaults.Key {

/**
Check whether the stored value is the default value.

- Note: This is only for internal use cause wouldn't work for non-equatable values.
*/
public func isDefaultValue() -> Bool {
var _isDefaultValue: Bool {
let defaultValue = defaultValue
let value = suite[self]
guard
Expand All @@ -205,6 +207,13 @@ extension Defaults.Key {
}
}

extension Defaults.Key where Value: Equatable {
/**
Check whether the stored value is the default value.
*/
public var isDefaultValue: Bool { self._isDefaultValue }
}

extension Defaults {
/**
Remove all entries from the given `UserDefaults` suite.
Expand Down
9 changes: 6 additions & 3 deletions Sources/Defaults/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ extension Collection {

extension Equatable {
func isEqual(_ rhs: any Equatable) -> Bool {
if let rhs = rhs as? Self, rhs == self {
return true
guard
let rhs = rhs as? Self,
rhs == self
else {
return false
}

return false
return true
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/DefaultsTests/DefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ final class DefaultsTests: XCTestCase {

func testIsDefaultValue() {
let key = Defaults.Key<Bool>("isDefaultValue", default: false)
XCTAssert(key.isDefaultValue())
XCTAssert(key.isDefaultValue)
Defaults[key].toggle()
XCTAssert(!key.isDefaultValue())
XCTAssert(!key.isDefaultValue)
}

func testObserveKeyCombine() {
Expand Down

0 comments on commit dd44d0d

Please sign in to comment.