From 53abaa446776569ba03ed7d28a6465ff87ba6f92 Mon Sep 17 00:00:00 2001 From: darronschall Date: Sat, 16 Mar 2024 09:47:32 -0400 Subject: [PATCH] Add `state` and `stateNullable` helpers for `KotlinBase` 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. --- .../apple/xcode/mokoMvvmFlowSwiftUI/ViewModelState.swift | 8 ++++++++ .../mokoMvvmFlowSwiftUI/ViewModelStateNullable.swift | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelState.swift b/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelState.swift index 358adf7..00f9d4b 100644 --- a/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelState.swift +++ b/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelState.swift @@ -93,4 +93,12 @@ public extension ObservableObject where Self: ViewModel { mapper: { $0 as! Array } ) } + + func state(_ flowKey: KeyPath>) -> T { + return state( + flowKey, + equals: { $0.isEqual($1) }, + mapper: { $0 } + ) + } } diff --git a/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelStateNullable.swift b/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelStateNullable.swift index ed49ce3..b0fbdf4 100644 --- a/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelStateNullable.swift +++ b/mvvm-flow/apple/xcode/mokoMvvmFlowSwiftUI/ViewModelStateNullable.swift @@ -103,4 +103,12 @@ extension ObservableObject where Self: ViewModel { mapper: { $0?.localized() } ) } + + func stateNullable(_ flowKey: KeyPath>) -> T? { + return stateNullable( + flowKey, + equals: { $0?.isEqual($1) == true }, + mapper: { $0 } + ) + } }