ComposableBitConverter

A utility class for converting between ComposableType and an integer representation using bitwise operations. This is used to efficiently represent different combinations of ComposableFrames as view types in a RecyclerView.

Each ComposableFrame (Left, Icon, Title, Widget) is assigned a range of bits within the integer. The number of bits allocated for each frame type depends on the number of possible ComposableFrames defined in the ComposableStrategy.

For example, if there are:

  • 3 possible Left frames

  • 5 possible Icon frames

  • 2 possible Title frames

  • 4 possible Widget frames

The bit allocation would be:

  • Left: ceil(log2(3 + 1)) = 2 bits (can represent 0-3, where 0 means no Left frame)

  • Icon: ceil(log2(5 + 1)) = 3 bits (can represent 0-7, where 0 means no Icon frame)

  • Title: ceil(log2(2 + 1)) = 2 bits (can represent 0-3, where 0 means no Title frame)

  • Widget: ceil(log2(4 + 1)) = 3 bits (can represent 0-7, where 0 means no Widget frame)

The final integer (view type) would be constructed by shifting and ORing these bits together.

This class provides methods to:

  • encodeAsBits(composableType: ComposableType): Convert a ComposableType to its integer representation.

  • decodeAsType(viewType: Int): Convert an integer (view type) back to a ComposableType.

  • getMaxBit(): Get the maximum possible integer value that can be encoded.

Caching is used to optimize repeated conversions.

Parameters

frameStrategy

The ComposableStrategy defining the available ComposableFrames for each slot.

Constructors

Link copied to clipboard
constructor(frameStrategy: ComposableStrategy)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
Link copied to clipboard
fun encodeAsBits(composableType: ComposableType): Int
Link copied to clipboard
fun getMaxBit(): Int
Link copied to clipboard
fun readBit(position: Int, data: Int): Int