{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

AbstractApplier

public abstract class AbstractApplier<T extends Object> implements Applier

Known direct subclasses

An abstract Applier implementation.

import androidx.compose.runtime.AbstractApplier
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composition
import androidx.compose.runtime.CompositionContext
import androidx.compose.runtime.ComposeNode
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

// Provided we have a tree with a node base type like the following
abstract class Node {
    val children = mutableListOf<Node>()
}

// We would implement an Applier class like the following, which would teach compose how to
// manage a tree of Nodes.
class NodeApplier(root: Node) : AbstractApplier<Node>(root) {
    override fun insertTopDown(index: Int, instance: Node) {
        current.children.add(index, instance)
    }

    override fun insertBottomUp(index: Int, instance: Node) {
        // Ignored as the tree is built top-down.
    }

    override fun remove(index: Int, count: Int) {
        current.children.remove(index, count)
    }

    override fun move(from: Int, to: Int, count: Int) {
        current.children.move(from, to, count)
    }

    override fun onClear() {
        root.children.clear()
    }
}

// A function like the following could be created to create a composition provided a root Node.
fun Node.setContent(
    parent: CompositionContext,
    content: @Composable () -> Unit
): Composition {
    return Composition(NodeApplier(this), parent).apply {
        setContent(content)
    }
}

// assuming we have Node sub-classes like "TextNode" and "GroupNode"
class TextNode : Node() {
    var text: String = ""
    var onClick: () -> Unit = {}
}
class GroupNode : Node()

// Composable equivalents could be created
@Composable fun Text(text: String, onClick: () -> Unit = {}) {
    ComposeNode<TextNode, NodeApplier>(::TextNode) {
        set(text) { this.text = it }
        set(onClick) { this.onClick = it }
    }
}

@Composable fun Group(content: @Composable () -> Unit) {
    ComposeNode<GroupNode, NodeApplier>(::GroupNode, {}, content)
}

// and then a sample tree could be composed:
fun runApp(root: GroupNode, parent: CompositionContext) {
    root.setContent(parent) {
        var count by remember { mutableStateOf(0) }
        Group {
            Text("Count: $count")
            Text("Increment") { count++ }
        }
    }
}
See also
Applier
Composition
Composer
ComposeNode

Summary

Public constructors

<T extends Object> AbstractApplier(@NonNull T root)

Public methods

final void

Move to the root and remove all nodes from the root, preparing both this Applier and its root to be used as the target of a new composition in the future.

void
down(@NonNull T node)

Indicates that the applier is getting traversed "down" the tree.

@NonNull T

The node that operations will be applied on at any given time.

final @NonNull T
void
up()

Indicates that the applier is getting traversed "up" the tree.

Protected methods

final void
move(@NonNull List<@NonNull T> receiver, int from, int to, int count)
abstract void

Called to perform clearing of the root when clear is called.

final void
remove(@NonNull List<@NonNull T> receiver, int index, int count)
void
setCurrent(@NonNull T current)

The node that operations will be applied on at any given time.

Inherited methods

From androidx.compose.runtime.Applier
abstract void
insertBottomUp(int index, @NonNull T instance)

Indicates that instance should be inserted as a child of current at index.

abstract void
insertTopDown(int index, @NonNull T instance)

Indicates that instance should be inserted as a child to current at index.

abstract void
move(int from, int to, int count)

Indicates that count children of current should be moved from index from to index to.

void

Called when the Composer is about to begin applying changes using this applier.

void

Called when the Composer is finished applying changes using this applier.

abstract void
remove(int index, int count)

Indicates that the children of current from index to index + count should be removed.

Public constructors

AbstractApplier

public <T extends Object> AbstractApplier(@NonNull T root)

Public methods

clear

public final void clear()

Move to the root and remove all nodes from the root, preparing both this Applier and its root to be used as the target of a new composition in the future.

down

public void down(@NonNull T node)

Indicates that the applier is getting traversed "down" the tree. When this gets called, node is expected to be a child of current, and after this operation, node is expected to be the new current.

getCurrent

public @NonNullgetCurrent()

The node that operations will be applied on at any given time. It is expected that the value of this property will change as down and up are called.

getRoot

public final @NonNullgetRoot()

up

public void up()

Indicates that the applier is getting traversed "up" the tree. After this operation completes, the current should return the "parent" of the current node at the beginning of this operation.

Protected methods

move

protected final void move(@NonNull List<@NonNull T> receiver, int from, int to, int count)

onClear

protected abstract void onClear()

Called to perform clearing of the root when clear is called.

remove

protected final void remove(@NonNull List<@NonNull T> receiver, int index, int count)

setCurrent

protected void setCurrent(@NonNull T current)

The node that operations will be applied on at any given time. It is expected that the value of this property will change as down and up are called.