Skip to content

Commit

Permalink
Feature: Add Poko to generate equals, hashcode, and toString (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordond authored Dec 18, 2023
1 parent 8d2758f commit 6815674
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 40 deletions.
6 changes: 2 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import org.jetbrains.dokka.gradle.AbstractDokkaTask
import org.jetbrains.dokka.gradle.DokkaMultiModuleTask

plugins {
alias(libs.plugins.multiplatform) apply false
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.compose) apply false
alias(libs.plugins.poko) apply false
alias(libs.plugins.dokka)
alias(libs.plugins.dependencies)
alias(libs.plugins.binaryCompatibility)

val kotlinVersion = libs.versions.kotlin.get()
kotlin("multiplatform") version kotlinVersion apply false
kotlin("jvm") version kotlinVersion apply false
}

apiValidation {
Expand Down
2 changes: 2 additions & 0 deletions core/api/android/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class com/dragselectcompose/core/DragSelectState {
public fun hashCode ()I
public final fun isSelected (Ljava/lang/Object;)Z
public final fun removeSelected (Ljava/lang/Object;)V
public fun toString ()Ljava/lang/String;
public final fun toggleSelectionMode ()V
public final fun updateSelected (Ljava/util/List;)V
}
Expand All @@ -27,6 +28,7 @@ public final class com/dragselectcompose/core/DragState {
public fun <init> (II)V
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/dragselectcompose/core/GridDragSelectDefaults {
Expand Down
2 changes: 2 additions & 0 deletions core/api/desktop/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class com/dragselectcompose/core/DragSelectState {
public fun hashCode ()I
public final fun isSelected (Ljava/lang/Object;)Z
public final fun removeSelected (Ljava/lang/Object;)V
public fun toString ()Ljava/lang/String;
public final fun toggleSelectionMode ()V
public final fun updateSelected (Ljava/util/List;)V
}
Expand All @@ -27,6 +28,7 @@ public final class com/dragselectcompose/core/DragState {
public fun <init> (II)V
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class com/dragselectcompose/core/GridDragSelectDefaults {
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
alias(libs.plugins.compose)
alias(libs.plugins.dokka)
alias(libs.plugins.publish)
alias(libs.plugins.poko)
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import dev.drewhamilton.poko.Poko

/**
* Creates a [DragSelectState] that is remembered across compositions.
Expand Down Expand Up @@ -69,6 +70,7 @@ public fun <Item> rememberDragSelectState(
* @param[compareSelector] A factory for selecting a property of [Item] to compare.
*/
@Suppress("MemberVisibilityCanBePrivate")
@Poko
@Stable
public class DragSelectState<Item>(
initialSelection: List<Item>,
Expand Down Expand Up @@ -225,24 +227,4 @@ public class DragSelectState<Item>(
dragState = dragState.copy(initial = DragState.None)
autoScrollSpeed.value = 0f
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false

other as DragSelectState<*>

if (gridState != other.gridState) return false
if (compareSelector != other.compareSelector) return false
if (dragState != other.dragState) return false
return autoScrollSpeed == other.autoScrollSpeed
}

override fun hashCode(): Int {
var result = gridState.hashCode()
result = 31 * result + compareSelector.hashCode()
result = 31 * result + dragState.hashCode()
result = 31 * result + autoScrollSpeed.hashCode()
return result
}
}
18 changes: 2 additions & 16 deletions core/src/commonMain/kotlin/com/dragselectcompose/core/DragState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.dragselectcompose.core

import androidx.compose.runtime.Stable
import androidx.compose.runtime.saveable.Saver
import dev.drewhamilton.poko.Poko

/**
* Represents the current state of a drag gesture.
*
* @param[initial] The index of the item where the drag gesture started.
* @param[current] The index of the item where the drag gesture is currently at.
*/
@Poko
@Stable
public class DragState(
internal val initial: Int,
Expand All @@ -22,22 +24,6 @@ public class DragState(
return DragState(initial = initial, current = current)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false

other as DragState

if (initial != other.initial) return false
return current == other.current
}

override fun hashCode(): Int {
var result = initial
result = 31 * result + current
return result
}

internal companion object {

internal const val None = -1
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kamel = "0.9.0"
ktor = "2.3.7"
binaryCompatibility = "0.13.2"
publish = "0.25.3"
poko = "0.15.1"

[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
Expand Down Expand Up @@ -56,5 +57,6 @@ dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
binaryCompatibility = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibility" }
publish = { id = "com.vanniktech.maven.publish", version.ref = "publish" }
poko = { id = "dev.drewhamilton.poko", version.ref = "poko" }

[bundles]

0 comments on commit 6815674

Please sign in to comment.