Merge pull request #1731 from vector-im/feature/bma/variousCleanup

Konsist: check if sealed class could be sealed interface
This commit is contained in:
Benoit Marty
2023-11-03 18:09:49 +01:00
committed by GitHub
30 changed files with 148 additions and 59 deletions

View File

@@ -16,6 +16,9 @@
package io.element.android.features.lockscreen.impl.pin.model
import androidx.compose.runtime.Immutable
@Immutable
sealed interface PinDigit {
data object Empty : PinDigit
data class Filled(val value: Char) : PinDigit

View File

@@ -170,9 +170,8 @@ private fun CustomSheetState.getIntOffset(): Int? = try {
null
}
private sealed class Slot {
data class SheetContent(val key: Int?) : Slot()
data object DragHandle : Slot()
data object Scaffold : Slot()
private sealed interface Slot {
data class SheetContent(val key: Int?) : Slot
data object DragHandle : Slot
data object Scaffold : Slot
}

View File

@@ -20,6 +20,7 @@ import android.Manifest
import android.annotation.SuppressLint
import android.net.Uri
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
@@ -409,6 +410,7 @@ class MessageComposerPresenter @Inject constructor(
}
}
@Immutable
sealed interface RoomMemberSuggestion {
data object Room : RoomMemberSuggestion
data class Member(val roomMember: RoomMember) : RoomMemberSuggestion

View File

@@ -32,6 +32,7 @@ import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -103,11 +104,12 @@ fun MessagesReactionButton(
}
}
sealed class MessagesReactionsButtonContent {
data class Text(val text: String) : MessagesReactionsButtonContent()
data class Icon(@DrawableRes val resourceId: Int) : MessagesReactionsButtonContent()
@Immutable
sealed interface MessagesReactionsButtonContent {
data class Text(val text: String) : MessagesReactionsButtonContent
data class Icon(@DrawableRes val resourceId: Int) : MessagesReactionsButtonContent
data class Reaction(val reaction: AggregatedReaction) : MessagesReactionsButtonContent()
data class Reaction(val reaction: AggregatedReaction) : MessagesReactionsButtonContent
val isHighlighted get() = this is Reaction && reaction.isHighlighted
}

View File

@@ -16,6 +16,9 @@
package io.element.android.features.messages.impl.timeline.model.event
import androidx.compose.runtime.Immutable
@Immutable
sealed interface TimelineItemStateContent : TimelineItemEventContent {
val body: String
}

View File

@@ -16,8 +16,10 @@
package io.element.android.features.messages.impl.timeline.model.event
import androidx.compose.runtime.Immutable
import org.jsoup.nodes.Document
@Immutable
sealed interface TimelineItemTextBasedContent : TimelineItemEventContent {
val body: String
val htmlDocument: Document?

View File

@@ -24,9 +24,9 @@ import android.os.SystemClock
class FirstThrottler(private val minimumInterval: Long = 800) {
private var lastDate = 0L
sealed class CanHandleResult {
data object Yes : CanHandleResult()
data class No(val shouldWaitMillis: Long) : CanHandleResult()
sealed interface CanHandleResult {
data object Yes : CanHandleResult
data class No(val shouldWaitMillis: Long) : CanHandleResult
fun waitMillis(): Long {
return when (this) {

View File

@@ -18,14 +18,16 @@ package io.element.android.libraries.designsystem.components.button
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import io.element.android.libraries.designsystem.theme.components.Button
import io.element.android.libraries.designsystem.theme.components.IconButton
import io.element.android.libraries.designsystem.theme.components.IconSource
import io.element.android.libraries.designsystem.theme.components.TextButton
/**
* A sealed class that represents the different visual styles that a button can have.
* A sealed interface that represents the different visual styles that a button can have.
*/
@Immutable
sealed interface ButtonVisuals {
val action: () -> Unit

View File

@@ -19,6 +19,7 @@ package io.element.android.libraries.designsystem.components.list
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.DpSize
@@ -34,6 +35,7 @@ import io.element.android.libraries.designsystem.theme.components.Text as TextCo
/**
* This is a helper to set default leading and trailing content for [ListItem]s.
*/
@Immutable
sealed interface ListItemContent {
/**
* Default Switch content for [ListItem].

View File

@@ -37,6 +37,7 @@ import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -238,6 +239,7 @@ private fun ButtonInternal(
}
}
@Immutable
sealed interface IconSource {
val contentDescription: String?

View File

@@ -23,6 +23,7 @@ import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@@ -134,6 +135,7 @@ fun ListItem(
/**
* The style to use for a [ListItem].
*/
@Immutable
sealed interface ListItemStyle {
data object Default : ListItemStyle
data object Primary : ListItemStyle

View File

@@ -20,6 +20,7 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.ExperimentalTextApi
@@ -82,6 +83,7 @@ fun ListSupportingText(
object ListSupportingTextDefaults {
/** Specifies the padding to use for the supporting text. */
@Immutable
sealed interface Padding {
/** No padding. */
data object None : Padding

View File

@@ -30,6 +30,7 @@ import androidx.compose.material3.SearchBarColors
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
@@ -180,6 +181,7 @@ object ElementSearchBarDefaults {
)
}
@Immutable
sealed interface SearchBarResultState<in T> {
/** No search results are available yet (e.g. because the user hasn't entered a search term). */
class NotSearching<T> : SearchBarResultState<T>

View File

@@ -15,7 +15,7 @@
*/
plugins {
id("io.element.android-library")
id("io.element.android-compose-library")
id("kotlin-parcelize")
alias(libs.plugins.anvil)
kotlin("plugin.serialization") version "1.9.10"

View File

@@ -22,14 +22,14 @@ import android.net.Uri
* This sealed class represents all the permalink cases.
* You don't have to instantiate yourself but should use [PermalinkParser] instead.
*/
sealed class PermalinkData {
sealed interface PermalinkData {
data class RoomLink(
val roomIdOrAlias: String,
val isRoomAlias: Boolean,
val eventId: String?,
val viaParameters: List<String>
) : PermalinkData()
) : PermalinkData
/*
* &room_name=Team2
@@ -47,9 +47,9 @@ sealed class PermalinkData {
val token: String,
val privateKey: String,
val roomType: String?
) : PermalinkData()
) : PermalinkData
data class UserLink(val userId: String) : PermalinkData()
data class UserLink(val userId: String) : PermalinkData
data class FallbackLink(val uri: Uri, val isLegacyGroupLink: Boolean = false) : PermalinkData()
data class FallbackLink(val uri: Uri, val isLegacyGroupLink: Boolean = false) : PermalinkData
}

View File

@@ -16,6 +16,9 @@
package io.element.android.libraries.matrix.api.room
import androidx.compose.runtime.Immutable
@Immutable
sealed interface MatrixRoomMembersState {
data object Unknown : MatrixRoomMembersState
data class Pending(val prevRoomMembers: List<RoomMember>? = null) : MatrixRoomMembersState

View File

@@ -28,9 +28,9 @@ import kotlin.time.Duration
* Can be retrieved from [RoomListService] methods.
*/
interface RoomList {
sealed class LoadingState {
data object NotLoaded : LoadingState()
data class Loaded(val numberOfRooms: Int) : LoadingState()
sealed interface LoadingState {
data object NotLoaded : LoadingState
data class Loaded(val numberOfRooms: Int) : LoadingState
}
/**

View File

@@ -25,16 +25,16 @@ import kotlinx.coroutines.flow.StateFlow
*/
interface RoomListService {
sealed class State {
data object Idle : State()
data object Running : State()
data object Error : State()
data object Terminated : State()
sealed interface State {
data object Idle : State
data object Running : State
data object Error : State
data object Terminated : State
}
sealed class SyncIndicator {
data object Show : SyncIndicator()
data object Hide : SyncIndicator()
sealed interface SyncIndicator {
data object Show : SyncIndicator
data object Hide : SyncIndicator
}
/**

View File

@@ -16,7 +16,7 @@
package io.element.android.libraries.matrix.api.tracing
sealed class WriteToFilesConfiguration {
data object Disabled : WriteToFilesConfiguration()
data class Enabled(val directory: String, val filenamePrefix: String) : WriteToFilesConfiguration()
sealed interface WriteToFilesConfiguration {
data object Disabled : WriteToFilesConfiguration
data class Enabled(val directory: String, val filenamePrefix: String) : WriteToFilesConfiguration
}

View File

@@ -20,8 +20,10 @@ import android.net.Uri
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Immutable
import io.element.android.libraries.core.mimetype.MimeTypes
@Immutable
sealed interface PickerType<Input, Output> {
fun getContract(): ActivityResultContract<Input, Output>
fun getDefaultRequest(): Input

View File

@@ -17,11 +17,13 @@
package io.element.android.libraries.textcomposer.model
import android.os.Parcelable
import androidx.compose.runtime.Immutable
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.TransactionId
import io.element.android.libraries.matrix.ui.components.AttachmentThumbnailInfo
import kotlinx.parcelize.Parcelize
@Immutable
sealed interface MessageComposerMode : Parcelable {
@Parcelize
data object Normal: MessageComposerMode

View File

@@ -16,8 +16,8 @@
package io.element.android.libraries.textcomposer.model
sealed class PressEvent {
data object PressStart: PressEvent()
data object Tapped: PressEvent()
data object LongPressEnd: PressEvent()
sealed interface PressEvent {
data object PressStart: PressEvent
data object Tapped: PressEvent
data object LongPressEnd: PressEvent
}

View File

@@ -16,11 +16,11 @@
package io.element.android.libraries.textcomposer.model
sealed class VoiceMessagePlayerEvent {
data object Play: VoiceMessagePlayerEvent()
data object Pause: VoiceMessagePlayerEvent()
sealed interface VoiceMessagePlayerEvent {
data object Play: VoiceMessagePlayerEvent
data object Pause: VoiceMessagePlayerEvent
data class Seek(
val position: Float
): VoiceMessagePlayerEvent()
): VoiceMessagePlayerEvent
}

View File

@@ -16,11 +16,13 @@
package io.element.android.libraries.textcomposer.model
import androidx.compose.runtime.Immutable
import kotlinx.collections.immutable.ImmutableList
import kotlin.time.Duration
sealed class VoiceMessageState {
data object Idle: VoiceMessageState()
@Immutable
sealed interface VoiceMessageState {
data object Idle: VoiceMessageState
data class Preview(
val isSending: Boolean,
@@ -29,10 +31,10 @@ sealed class VoiceMessageState {
val playbackProgress: Float,
val time: Duration,
val waveform: ImmutableList<Float>,
): VoiceMessageState()
): VoiceMessageState
data class Recording(
val duration: Duration,
val levels: ImmutableList<Float>,
): VoiceMessageState()
): VoiceMessageState
}

View File

@@ -19,13 +19,13 @@ package io.element.android.libraries.textcomposer.utils
/**
* State of a press gesture.
*/
internal sealed class PressState {
internal sealed interface PressState {
data class Idle(
val lastPress: Pressing?
) : PressState()
) : PressState
sealed class Pressing : PressState()
data object Tapping : Pressing()
data object LongPressing : Pressing()
sealed interface Pressing : PressState
data object Tapping : Pressing
data object LongPressing : Pressing
}

View File

@@ -19,11 +19,11 @@ package io.element.android.libraries.voicerecorder.api
import java.io.File
import kotlin.time.Duration
sealed class VoiceRecorderState {
sealed interface VoiceRecorderState {
/**
* The recorder is idle and not recording.
*/
data object Idle : VoiceRecorderState()
data object Idle : VoiceRecorderState
/**
* The recorder is currently recording.
@@ -31,7 +31,7 @@ sealed class VoiceRecorderState {
* @property elapsedTime The elapsed time since the recording started.
* @property levels The current audio levels of the recording as a fraction of 1.
*/
data class Recording(val elapsedTime: Duration, val levels: List<Float>) : VoiceRecorderState()
data class Recording(val elapsedTime: Duration, val levels: List<Float>) : VoiceRecorderState
/**
* The recorder has finished recording.
@@ -46,5 +46,5 @@ sealed class VoiceRecorderState {
val mimeType: String,
val waveform: List<Float>,
val duration: Duration,
) : VoiceRecorderState()
) : VoiceRecorderState
}

View File

@@ -16,13 +16,31 @@
package io.element.android.libraries.voicerecorder.impl.audio
sealed class Audio {
class Data(
sealed interface Audio {
data class Data(
val readSize: Int,
val buffer: ShortArray,
) : Audio()
) : Audio {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Data
if (readSize != other.readSize) return false
if (!buffer.contentEquals(other.buffer)) return false
return true
}
override fun hashCode(): Int {
var result = readSize
result = 31 * result + buffer.contentHashCode()
return result
}
}
data class Error(
val audioRecordErrorCode: Int
) : Audio()
) : Audio
}

View File

@@ -15,7 +15,7 @@
*/
plugins {
id("io.element.android-library")
id("io.element.android-compose-library")
}
android {

View File

@@ -16,6 +16,9 @@
package io.element.android.services.apperror.api
import androidx.compose.runtime.Immutable
@Immutable
sealed interface AppErrorState {
data object NoError : AppErrorState

View File

@@ -16,12 +16,19 @@
package io.element.android.tests.konsist
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.api.ext.list.constructors
import com.lemonappdev.konsist.api.ext.list.modifierprovider.withSealedModifier
import com.lemonappdev.konsist.api.ext.list.parameters
import com.lemonappdev.konsist.api.ext.list.withAnnotationOf
import com.lemonappdev.konsist.api.ext.list.withNameEndingWith
import com.lemonappdev.konsist.api.ext.list.withoutAnnotationOf
import com.lemonappdev.konsist.api.ext.list.withoutConstructors
import com.lemonappdev.konsist.api.ext.list.withoutName
import com.lemonappdev.konsist.api.ext.list.withoutParents
import com.lemonappdev.konsist.api.verify.assertEmpty
import com.lemonappdev.konsist.api.verify.assertTrue
import org.junit.Test
@@ -55,4 +62,33 @@ class KonsistArchitectureTest {
.withNameEndingWith("Events")
.assertEmpty(additionalMessage = "Events class MUST be sealed interface")
}
@Test
fun `Sealed class without constructor and without parent MUST be sealed interface`() {
Konsist.scopeFromProject()
.classes()
.withSealedModifier()
.withoutConstructors()
.withoutParents()
.assertEmpty(additionalMessage = "Sealed class without constructor MUST be sealed interface")
}
@Test
fun `Sealed interface used in Composable MUST be Immutable or Stable`() {
// List all sealed interface without Immutable nor Stable annotation in the project
val forbiddenInterfacesForComposableParameter = Konsist.scopeFromProject()
.interfaces()
.withSealedModifier()
.withoutAnnotationOf(Immutable::class, Stable::class)
.map { it.fullyQualifiedName }
Konsist.scopeFromProject()
.functions()
.withAnnotationOf(Composable::class)
.assertTrue(additionalMessage = "Consider adding the @Immutable or @Stable annotation to the sealed interface") {
it.parameters.all { param ->
param.type.fullyQualifiedName !in forbiddenInterfacesForComposableParameter
}
}
}
}