Skip to content

0.4.0

Compare
Choose a tag to compare
@github-actions github-actions released this 07 Apr 06:28
· 445 commits to master since this release

Changed

Update dependencies

  • Kotlin 1.8.10.
  • Target Java 11.
  • Touchlab Stately 1.2.5.
  • AndroidX Lifecycle 2.6.0.
  • Android Gradle Plugin 7.4.2.

Flow wrappers

  • Add NonNullStateFlowWrapper and NullableFlowWrapper to common source set.

  • Move all Flow wrappers to common source set.
    Previously, they were only available for Darwin targets (iOS, macOS, tvOS, watchOS).

  • Add Flow.wrap() extension methods to wrap Flows sources:

    • Flow<T: Any>.wrap(): NonNullFlowWrapper<T>.
    • Flow<T>.wrap(): NullableFlowWrapper<T>.
    • StateFlow<T: Any>.wrap(): NonNullStateFlowWrapper<T>.
    • StateFlow<T>.wrap(): NullableStateFlowWrapper<T>.

    In common code, you can use these methods to wrap Flow sources and use them in Swift code easily.

    // Kotlin code
    data class State(...)
    
    class SharedViewModel : ViewModel() {
      private val _state = MutableStateFlow(State(...))
      val stateFlow: NonNullStateFlowWrapper<State> = _state.wrap()
    }
    // Swift code
    @MainActor class IosViewModel: ObservableObject {
      private let vm: SharedViewModel
    
      @Published private(set) var state: State
    
      init(viewModel: SharedViewModel) {
        vm = viewModel
    
        state = vm.stateFlow.value       //  <--- Use `value` property with type safety (do not need to cast).
        vm.stateFlow.subscribe(          //  <--- Use `subscribe(scope:onValue:)` method directly.
          scope: vm.viewModelScope,
          onValue: { [weak self] in self?.state = $0 }
        )
      }
    
      deinit { vm.clear() }
    }

Example, docs and tests

  • Refactor example code.
  • Add more docs: 0.x docs.
  • Add more tests.