Skip to content

Commit

Permalink
Add state and stateNullable helpers for KotlinBase
Browse files Browse the repository at this point in the history
When Kotlin/Native exports to Objective-C, all custom classes share a common `KotlinBase` interface (which implements `NSObject` and has `isEqual` available).

We take advantage of this in order to add `state` and `stateNullable` helpers without requiring downstream users to define their own `equals` and `mappers`.

This simplification makes the library slightly easier to use and perhaps a little more intuitive. Instead of forcing callers through `state(_:equals:mapper:)` for custom types, it's possible to just use the simpler `state(_)` helper.
  • Loading branch information
darronschall committed Mar 16, 2024
1 parent 0d9d02a commit 53abaa4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,12 @@ public extension ObservableObject where Self: ViewModel {
mapper: { $0 as! Array<T> }
)
}

func state<T: KotlinBase>(_ flowKey: KeyPath<Self, CStateFlow<T>>) -> T {
return state(
flowKey,
equals: { $0.isEqual($1) },
mapper: { $0 }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,12 @@ extension ObservableObject where Self: ViewModel {
mapper: { $0?.localized() }
)
}

func stateNullable<T: KotlinBase>(_ flowKey: KeyPath<Self, CStateFlow<T>>) -> T? {
return stateNullable(
flowKey,
equals: { $0?.isEqual($1) == true },
mapper: { $0 }
)
}
}

0 comments on commit 53abaa4

Please sign in to comment.