Anyone can point me in the right direction when us...
# multiplatform
g
Anyone can point me in the right direction when using a kotlin multiplatform repository with flows in a swiftui project? I currently have a repository that gets an object as a flow and has a bunch of methods to set values on the object. I can easily read that flow in swift, my issue is tweaking and setting new value for them. For example, a swiftui
Toggle
uses a boolean binding to manage it's state, and I can't use my object from the repository 'cause it's only a getter. So I need to create a separate state variable for the Toggle and somehow sync it with the repository. Anyone already found the more optimal way to do this, I just wanna avoid having invalid state due to state duplication. Here is some of my code as a sample:
Copy code
// Sample view model
@MainActor
class ZbViewModel: ObservableObject {
    private let repository: ISettingsRepository
    
    @Published var settings: ZbSettings = ZbSettings.Companion.shared.default_
    
    init(repository: ISettingsRepository) {
        self.repository = repository
        
        Task {
            do {
                let sequence = repository.getSettingsFlow()
                for try await settingsFromFlow in sequence {
                    self.settings = settingsFromFlow
                }
            } catch {
                print("Failed getting settings from repository flow: \(error)")
            }
        }
    }
    
    func setEnabled(enabled: Bool) {
        repository.setEnabled(enabled: enabled)
    }
}

// This is a sample view
struct MenuBarWindow: View {
    @StateObject private var viewModel = ZbViewModel(
        repository: DefaultSettingsRepository()
    )
   
    @State private var enabled = false
    
    init() {
        enabled = viewModel.settings.enabled
    }

    
    var body: some View {
        VStack(spacing: 8) {
            Toggle(isOn: $enabled) {
                Text("ZenBreak - \(enabled ? "enabled" : "disabled")")
                    .font(.title3)
                    .frame(maxWidth: .infinity, alignment: .leading)
            }
            .toggleStyle(.switch)
            .keyboardShortcut(.defaultAction)
            .onChange(of: enabled) { newValue in
                viewModel.setEnabled(enabled: newValue)
            }
// ...
As you can see the state for Toggle is duplicated and I don't know if this is the best way to do what I need, any help or tip is appreciated!
o
Hi @Giuliopime. Did you find a solution for this?
g
o
I guess you didn't use any binding right? I couldn't see any
$
operator
g
I did but created the getter and setter myself
Copy code
private var skipping: Binding<Bool> { Binding(
        get: {
            viewModel.settings.breakSkip
        },
        set: { skip in
            viewModel.setBreakSkip(skip: skip)
        }
    )}
heck I can't format well on mobile
o
Yeah that's one way to do it. But of course so much boilerplate. thanks.
g
you could probably do some code generation but I went with the easier route
o
i don't think it's worth it though.