Skip to main content

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):


_11
plugins {
_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:


_1
val 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
_9
myVar.asStateFlow().asCFlow().observe {
_9
_9
}
_9
_9
// observe data on main thread
_9
myVar.asStateFlow().asCFlow().observeMain {
_9
_9
}

To broadcast data and notify all listeners, you can use:


_2
myVar.postValueOnMainThread("MyValue") // notifies on main thread
_2
myVar.setValueOnContext("MyValue") // notifies on main thread

To get the current value:


_1
val currentVal = myVar.getCurrentValue()

What about iOS?

This depends on how your project is written.

SwiftUI

On Kotlin side:


_7
import com.architect.atlas.architecture.mvvm.ViewModel
_7
class MyViewModel : ViewModel(){
_7
val name = MutableAtlasFlow("")
_7
val samples = MutableAtlasFlow("")
_7
}
_7
_7
data class SampleProcess(val result: String)

On your Swift (Xcode Side)


_43
import SwiftUI
_43
import shared
_43
_43
struct 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
}

UIKit