Skip to content

[FR]: Enhance GleamProperties with a more flexible and functional interface #3

@teogor

Description

@teogor

Is there an existing issue for this?

  • I have searched the existing issues

Describe the problem

Current Implementation:

@ExperimentalGleamApi
class GleamProperties(
  val securePolicy: SecureFlagPolicy,
  val isFocusable: Boolean,
  val shouldDismissOnBackPress: Boolean,
  val animateCorners: Boolean,
  val animateHorizontalEdge: Boolean,
  val maxHorizontalEdge: Dp,
) {
  override fun equals(other: Any?): Boolean {
    if (this === other) return true
    if (other !is GleamProperties) return false

    if (securePolicy != other.securePolicy) return false
    if (isFocusable != other.isFocusable) return false
    return shouldDismissOnBackPress == other.shouldDismissOnBackPress
  }

  override fun hashCode(): Int {
    var result = securePolicy.hashCode()
    result = 31 * result + isFocusable.hashCode()
    result = 31 * result + shouldDismissOnBackPress.hashCode()
    return result
  }
}

Proposed Enhancement:

/**
 * Represents properties for configuring Gleam behavior.
 * @property securePolicy The policy for secure flag behavior.
 * @property isFocusable Whether the component is focusable.
 * @property shouldDismissOnBackPress Whether the component should dismiss on back press.
 * @property animateCorners Whether to animate corners.
 * @property animateHorizontalEdge Whether to animate the horizontal edge.
 * @property maxHorizontalEdge The maximum horizontal edge size.
 */
@ExperimentalGleamApi
interface GleamProperties {
  val securePolicy: SecureFlagPolicy
  val isFocusable: Boolean
  val shouldDismissOnBackPress: Boolean
  val animateCorners: Boolean
  val animateHorizontalEdge: Boolean
  val maxHorizontalEdge: Dp

  /**
   * Copies the current [GleamProperties] instance with optional property values overridden.
   *
   * @param securePolicy The policy for secure flag behavior.
   * @param isFocusable Whether the component is focusable.
   * @param shouldDismissOnBackPress Whether the component should dismiss on back press.
   * @param animateCorners Whether to animate corners.
   * @param animateHorizontalEdge Whether to animate the horizontal edge.
   * @param maxHorizontalEdge The maximum horizontal edge size.
   * @return A new instance of [GleamProperties] with the specified properties.
   */
  fun copy(
    securePolicy: SecureFlagPolicy = this.securePolicy,
    isFocusable: Boolean = this.isFocusable,
    shouldDismissOnBackPress: Boolean = this.shouldDismissOnBackPress,
    animateCorners: Boolean = this.animateCorners,
    animateHorizontalEdge: Boolean = this.animateHorizontalEdge,
    maxHorizontalEdge: Dp = this.maxHorizontalEdge
  ): GleamProperties
}

/**
 * Default implementation of [GleamProperties].
 * @param securePolicy The policy for secure flag behavior.
 * @param isFocusable Whether the component is focusable.
 * @param shouldDismissOnBackPress Whether the component should dismiss on back press.
 * @param animateCorners Whether to animate corners.
 * @param animateHorizontalEdge Whether to animate the horizontal edge.
 * @param maxHorizontalEdge The maximum horizontal edge size.
 */
data class DefaultGleamProperties(
  override val securePolicy: SecureFlagPolicy = SecureFlagPolicy.Inherit,
  override val isFocusable: Boolean = true,
  override val shouldDismissOnBackPress: Boolean = true,
  override val animateCorners: Boolean = false,
  override val animateHorizontalEdge: Boolean = false,
  override val maxHorizontalEdge: Dp = 0.dp,
) : GleamProperties {

  override fun copy(
    securePolicy: SecureFlagPolicy,
    isFocusable: Boolean,
    shouldDismissOnBackPress: Boolean,
    animateCorners: Boolean,
    animateHorizontalEdge: Boolean,
    maxHorizontalEdge: Dp
  ): GleamProperties = copy(
    securePolicy = securePolicy,
    isFocusable = isFocusable,
    shouldDismissOnBackPress = shouldDismissOnBackPress,
    animateCorners = animateCorners,
    animateHorizontalEdge = animateHorizontalEdge,
    maxHorizontalEdge = maxHorizontalEdge
  )
}

Describe the solution

Why This Enhancement is Valuable:

The updated GleamProperties interface provides a more flexible and maintainable approach to defining and manipulating properties for configuring Gleam behavior. By introducing an interface with a copy function and a default implementation, developers can easily create instances with overridden properties while ensuring consistency and type safety. This enhancement promotes cleaner code, reduces boilerplate, and aligns with modern Kotlin conventions, enhancing the usability and maintainability of the Gleam library.

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

@featureNew feature or request

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions