Flows
Atlas supports flows to handle state management.
To get started using flows please import the api library and plugin (Swift Generator) into your project (shared.gradle file):
_11plugins {_11 id("io.github.thearchitect123.atlasFlowsGenPlugin") // generates the swift classes used to bridge to Flows on Kotlin side_11}_11_11 kotlin {_11 sourceSets {_11 commonMain.dependencies {_11 implementation("io.github.thearchitect123:atlas-flow:+") // contains the DSL Api used to access the flows api_11 }_11 }_11 }
Get Started
To get started using Flows, please use the below class:
_1val myVar = MutableAtlasFlow("") // the data type can be anything
You can add an observer to run on the current context, or the main thread
_9// observe on current context_9myVar.asStateFlow().asCFlow().observe {_9_9}_9_9// observe data on main thread_9myVar.asStateFlow().asCFlow().observeMain {_9_9}
To broadcast data and notify all listeners, you can use:
_2myVar.postValueOnMainThread("MyValue") // notifies on main thread_2myVar.setValueOnContext("MyValue") // notifies on main thread
To get the current value:
_1val currentVal = myVar.getCurrentValue()
What about iOS?
This depends on how your project is written.
SwiftUI
On Kotlin side:
_7import com.architect.atlas.architecture.mvvm.ViewModel_7class MyViewModel : ViewModel(){_7 val name = MutableAtlasFlow("")_7 val samples = MutableAtlasFlow("")_7}_7_7data class SampleProcess(val result: String)
On your Swift (Xcode Side)
_43import SwiftUI_43import shared_43_43struct ContentView: View {_43 let vm: MyViewModel_43 @StateObject var name: FlowBinding<String>_43 @StateObject var samples: FlowBinding<[SampleProcess]>_43_43 init(vm: MyViewModel) {_43 self.vm = vm_43 _name = StateObject(wrappedValue: FlowBinding(flow: vm.testText.asSwiftFlow(), adapter: stringAdapter))_43 _samples = StateObject(wrappedValue: FlowBinding(flow: vm.resultsSamples.asSwiftFlow(), adapter: makeListAdapter(SampleProcess.self)))_43 }_43_43 var body: some View {_43 List(samples.value, id: \.id) { person in_43 Text(person.name)_43 }_43_43 VStack{_43 Text(name) // to bind the state directly_43_43_43 Text("").bindTextTwoWay(vm.testText.asSwiftFlow()) // if using the extensions_43 }.onAppear{_43_43 // to fetch the current data_43 let currentValue = stringAdapter.fromKotlin(vm.testText.asSwiftFlow().getValue())_43_43 // to observe for changes_43 observe(vm.testText.asSwiftFlow(), adapter: stringAdapter) { res in_43 print ("RES \(res)")_43 }_43 }_43_43 // if you need to bind a list of data_43 List {_43 ForEachBinding(samples.listBinding, id: \.self) { sample in_43 Text(sample[\.result].value())_43 }_43 }_43 }_43}