Check the specification to know more about the definition of Nimbus Operations.
Nimbus already comes with some pre-defined operations, but for most applications, they're not enough and we need to extend them.
We'll use a function to format a value as a currency as an example.
Write the structure with the parameters of the operation and make it conform to the protocol OperationDecodable
.
import NimbusSwiftUI
struct FormatCurrency: OperationDecodable {
static var properties = ["value", "currency"]
var value: Double
var currency: String
func execute() -> String? {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.currencyCode = code
return formatter.string(from: NSNumber(value: value))
}
}
The OperationDecodable
protocol is defined as follows:
public protocol OperationDecodable: Decodable {
associatedtype Return
func execute() -> Return
static var properties: [String] { get }
}
func execute() -> Return
: A function that holds the implementation of the operation and returns a generic typeReturn
.static var properties: [String]
: An array with each element being the name of the struct's properties in the order they appear in the operation.
The same structure used for registering components and actions is used for registering operations: NimbusComposeUILibrary
. See the example below:
let myAppUI = NimbusSwiftUILibrary("myApp")
.addOperation("formatCurrency", FormatCurrency.self)
Whenever the operation "formatCurrency" is used by a server driven view, the function execute
of FormatCurrency
will be executed.
import SwiftUI
import NimbusSwiftUI
struct ContentView: View {
var body: some View {
Nimbus(baseUrl: "my-base-url") {
// Your navigator here
}
.ui([layout, myAppUI])
}
}
👉 If you want to learn more about the Decodable
usage in NimbusSwiftUI: Decodable.
:point_right: Otherwise: Actions