Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS nullable value from CStateFlow #181

Closed
kramlex opened this issue Jun 3, 2022 · 4 comments · Fixed by #205
Closed

iOS nullable value from CStateFlow #181

kramlex opened this issue Jun 3, 2022 · 4 comments · Fixed by #205
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@kramlex
Copy link
Contributor

kramlex commented Jun 3, 2022

mokoMvvmFramework does not allow to get a iOS nullable value from CStateFlow, , it is necessary to add
at the moment, force unwrap is being used there

func stateNullable<T, R>(
        _ flowKey: KeyPath<Self, CStateFlow<T>>,
        equals: @escaping (T?, T?) -> Bool,
        mapper: @escaping (T?) -> R?
    ) -> R? {
        let stateFlow: CStateFlow<T> = self[keyPath: flowKey]
        var lastValue: T? = stateFlow.value

        var disposable: DisposableHandle? = nil

        disposable = stateFlow.subscribe(onCollect: { value in
            if !equals(lastValue, value) {
                lastValue = value
                self.objectWillChange.send()
                disposable?.dispose()
            }
        })

        return mapper(stateFlow.value)
    }

Снимок экрана 2022-06-03 в 9 03 36 PM

@kramlex kramlex changed the title iOS nullable value from CStateFlow, iOS nullable value from CStateFlow Jun 3, 2022
@Alex009 Alex009 added the enhancement New feature or request label Jun 4, 2022
@Alex009 Alex009 added this to the 0.14.0 milestone Jun 4, 2022
@ln-12
Copy link

ln-12 commented Aug 19, 2022

Hey @kramlex, I tried to use your solution, but could not get it working. I added a debug output, but it only prints nil. I double checked on the KMM side, added a output there and got the correct state after the update.

func stateNullable<T, R>(
    _ flowKey: KeyPath<Self, CStateFlow<T>>,
    equals: @escaping (T?, T?) -> Bool,
    mapper: @escaping (T?) -> R?
) -> R? {
    let stateFlow: CStateFlow<T> = self[keyPath: flowKey]
    var lastValue: T? = stateFlow.value

    var disposable: DisposableHandle?

    disposable = stateFlow.subscribe(onCollect: { value in
        print(value)
        if !equals(lastValue, value) {
            lastValue = value
            self.objectWillChange.send()
            disposable?.dispose()
        }
    })

    return mapper(stateFlow.value)
}

My code in the KMM module looks like this:

private var _availableItems = MutableStateFlow<List<Item>?>(null)
val availableItems = _availableItems.cStateFlow()

Inside my SwiftUI view, I am calling it like this:

vehicles: viewModel.stateNullable(\.availableItems, equals: { $0 === $1 }, mapper: { $0 as! [Item]? }),

The normal/predefined states work just fine. Do you have any hint what I am missing?

Edit: after switching to a non-nullable list, I got the similar effect that the list value is not updated and stays empty. So from m experience, there seems to be a problem with lists in general.

Edit 2: I could work around this limitation by using the state pattern in your example. As the state is always non-nullable, the code from above is not even need. Nevertheless, it would still be nice if nullable data types would be supported.

@Alex009 Alex009 modified the milestones: 0.14.0, 0.15.0 Sep 13, 2022
@kramlex
Copy link
Contributor Author

kramlex commented Sep 24, 2022

Hi, @ln-12. I analyzed your problem and couldn't reproduce it. The proposed solution works correctly.
If the problem hasn't been solved yet, then I would like to take a look at your view model or case.

kramlex added a commit to kramlex/moko-mvvm that referenced this issue Sep 24, 2022
kramlex added a commit to kramlex/moko-mvvm that referenced this issue Sep 26, 2022
@ln-12
Copy link

ln-12 commented Sep 26, 2022

Thanks @kramlex, I found another approach which is working fine for me.

kramlex added a commit to kramlex/moko-mvvm that referenced this issue Oct 2, 2022
@Alex009 Alex009 linked a pull request Oct 3, 2022 that will close this issue
Alex009 added a commit that referenced this issue Oct 3, 2022
@Alex009
Copy link
Member

Alex009 commented Oct 3, 2022

will be released in 0.15.0

@Alex009 Alex009 closed this as completed Oct 3, 2022
@Alex009 Alex009 mentioned this issue Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants