Skip to content

Commit

Permalink
Merge pull request #5 from Trendyol/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
yigitozgumus authored Apr 29, 2024
2 parents bc17ee4 + 8ebd27d commit aef33b4
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 69 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ graph TD;
Transformers are responsible for handling `signal`s and `effect`s. They have an inner class called `TransmissionHataHolder` which can hold any Data type that is extended from `Transmission.Data`. Any update to the dataHolder automatically publishes the latest version to Data Channel.

```kotlin
class InputTransformer @Inject constructor() : Transformer() {
class InputTransformer @Inject constructor() : Transformer<Transmission.Data>() {

private val holder = TransmissionDataHolder(InputUiState())

override val signalHandler: SignalHandler = SignalHandler { signal ->
override val signalHandler = buildGenericSignalHandler { signal ->
when (signal) {
is InputSignal.InputUpdate -> {
holder.update { it.copy(writtenText = signal.value) }
Expand All @@ -69,7 +69,7 @@ class InputTransformer @Inject constructor() : Transformer() {
}
}

override val effectHandler: EffectHandler = EffectHandler { effect ->
override val effectHandler = buildGenericEffectHandler { effect ->
when (effect) {
is ColorPickerEffect.BackgroundColorUpdate -> {
holder.update { it.copy(backgroundColor = effect.color) }
Expand Down Expand Up @@ -97,27 +97,29 @@ The TransmissionRouter takes a set of `Transformer`s as a parameter. Building th
interface FeaturesModule {

@Multibinds
fun bindTransformerSet(): Set<Transformer>
fun bindTransformerSet(): Set<Transformer<Transmission.Data>>

@Binds
@IntoSet
fun bindInputTransformer(impl: InputTransformer): Transformer
fun bindInputTransformer(impl: InputTransformer): Transformer<Transmission.Data>

@Binds
@IntoSet
fun bindOutputTransformer(impl: OutputTransformer): Transformer
fun bindOutputTransformer(impl: OutputTransformer): Transformer<Transmission.Data>

@Binds
@IntoSet
fun bindColorPickerTransformer(impl: ColorPickerTransformer): Transformer
fun bindColorPickerTransformer(impl: ColorPickerTransformer): Transformer<Transmission.Data>

@Binds
@IntoSet
fun bindMultiOutputTransformer(impl: MultiOutputTransformer): Transformer
fun bindMultiOutputTransformer(impl: MultiOutputTransformer): Transformer<Transmission.Data>

companion object {
@Provides
fun provideRouter(transformerSet: @JvmSuppressWildcards Set<Transformer>): TransmissionRouter {
fun provideRouter(
transformerSet: @JvmSuppressWildcards Set<Transformer<Transmission.Data>>
): TransmissionRouter<Transmission.Data>{
return TransmissionRouter(transformerSet)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import javax.inject.Inject

@HiltViewModel
class SampleViewModel @Inject constructor(
private val transmissionRouter: TransmissionRouter
private val transmissionRouter: TransmissionRouter<Transmission.Data>
) : ViewModel() {

private val _uiState = MutableStateFlow(SampleScreenUiState())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.transmission.features

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.TransmissionRouter
import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer
import com.trendyol.transmission.features.input.InputTransformer
Expand All @@ -19,27 +20,29 @@ import dagger.multibindings.Multibinds
interface FeaturesModule {

@Multibinds
fun bindTransformerSet(): Set<Transformer>
fun bindTransformerSet(): Set<Transformer<Transmission.Data>>

@Binds
@IntoSet
fun bindInputTransformer(impl: InputTransformer): Transformer
fun bindInputTransformer(impl: InputTransformer): Transformer<Transmission.Data>

@Binds
@IntoSet
fun bindOutputTransformer(impl: OutputTransformer): Transformer
fun bindOutputTransformer(impl: OutputTransformer): Transformer<Transmission.Data>

@Binds
@IntoSet
fun bindColorPickerTransformer(impl: ColorPickerTransformer): Transformer
fun bindColorPickerTransformer(impl: ColorPickerTransformer): Transformer<Transmission.Data>

@Binds
@IntoSet
fun bindMultiOutputTransformer(impl: MultiOutputTransformer): Transformer
fun bindMultiOutputTransformer(impl: MultiOutputTransformer): Transformer<Transmission.Data>

companion object {
@Provides
fun provideRouter(transformerSet: @JvmSuppressWildcards Set<Transformer>): TransmissionRouter {
fun provideRouter(
transformerSet: @JvmSuppressWildcards Set<Transformer<Transmission.Data>>
): TransmissionRouter<Transmission.Data> {
return TransmissionRouter(transformerSet)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.trendyol.transmission.features.colorpicker

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.handler.EffectHandler
import com.trendyol.transmission.transformer.handler.SignalHandler
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.transformer.handler.buildGenericSignalHandler
import com.trendyol.transmission.ui.ColorPickerUiState
import javax.inject.Inject

class ColorPickerTransformer @Inject constructor() : Transformer() {
class ColorPickerTransformer @Inject constructor() : Transformer<Transmission.Data>() {

private val holder = TransmissionDataHolder(ColorPickerUiState())

override val signalHandler: SignalHandler = buildGenericSignalHandler { signal ->
override val signalHandler = buildGenericSignalHandler { signal ->
when (signal) {
is ColorPickerSignal.SelectColor -> {
holder.update { it.copy(selectedColorIndex = signal.index) }
Expand All @@ -24,7 +23,8 @@ class ColorPickerTransformer @Inject constructor() : Transformer() {
}
}

override val effectHandler: EffectHandler = buildGenericEffectHandler { effect ->

override val effectHandler = buildGenericEffectHandler { effect ->
when (effect) {
is ColorPickerEffect.BackgroundColorUpdate -> {
holder.update {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.trendyol.transmission.features.input

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.handler.EffectHandler
import com.trendyol.transmission.transformer.handler.SignalHandler
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.transformer.handler.buildTypedSignalHandler
import com.trendyol.transmission.ui.InputUiState
import javax.inject.Inject

class InputTransformer @Inject constructor() : Transformer() {
class InputTransformer @Inject constructor() : Transformer<Transmission.Data>() {

private val holder = TransmissionDataHolder(InputUiState())

override val signalHandler: SignalHandler = buildTypedSignalHandler<InputSignal> { signal ->
override val signalHandler = buildTypedSignalHandler<Transmission.Data, InputSignal> { signal ->
when (signal) {
is InputSignal.InputUpdate -> {
holder.update { it.copy(writtenText = signal.value) }
Expand All @@ -22,7 +21,7 @@ class InputTransformer @Inject constructor() : Transformer() {
}
}

override val effectHandler: EffectHandler = buildGenericEffectHandler { effect ->
override val effectHandler = buildGenericEffectHandler { effect ->
when (effect) {
is ColorPickerEffect.BackgroundColorUpdate -> {
holder.update { it.copy(backgroundColor = effect.color) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.trendyol.transmission.features.multioutput

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.features.input.InputEffect
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.handler.EffectHandler
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.ui.MultiOutputUiState
import javax.inject.Inject

class MultiOutputTransformer @Inject constructor() : Transformer() {
class MultiOutputTransformer @Inject constructor() : Transformer<Transmission.Data>() {

private val holder = TransmissionDataHolder(MultiOutputUiState())

override val effectHandler: EffectHandler = buildGenericEffectHandler { effect ->
override val effectHandler = buildGenericEffectHandler { effect ->
when (effect) {
is InputEffect.InputUpdate -> {
holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.trendyol.transmission.features.output

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.features.input.InputEffect
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.handler.EffectHandler
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.ui.OutputUiState
import javax.inject.Inject

class OutputTransformer @Inject constructor() : Transformer() {
class OutputTransformer @Inject constructor() : Transformer<Transmission.Data>() {

private val holder = TransmissionDataHolder(OutputUiState())

override val effectHandler: EffectHandler = buildGenericEffectHandler { effect ->
override val effectHandler = buildGenericEffectHandler { effect ->
when (effect) {
is InputEffect.InputUpdate -> {
holder.update { it.copy(outputText = effect.value) }
Expand Down
2 changes: 1 addition & 1 deletion transmission/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ publishing {
create<MavenPublication>("release") {
groupId = "com.trendyol"
artifactId = "transmission"
version = "0.0.4"
version = "0.0.5"
afterEvaluate {
from(components["java"])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch

class TransmissionRouter(
private val transformerSet: Set<Transformer>,
class TransmissionRouter<D: Transmission.Data>(
private val transformerSet: Set<Transformer<D>>,
private val dispatcher: CoroutineDispatcher = Dispatchers.Default
) {

Expand Down Expand Up @@ -44,10 +44,10 @@ class TransmissionRouter(

// endregion

private val outGoingDataChannel = Channel<Transmission.Data>(capacity = Channel.BUFFERED)
private val outGoingDataChannel = Channel<D>(capacity = Channel.BUFFERED)

fun initialize(
onData: ((Transmission.Data) -> Unit),
onData: ((D) -> Unit),
onEffect: (Transmission.Effect) -> Unit = {},
) {
if (transformerSet.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) {
open class Transformer<D: Transmission.Data>(dispatcher: CoroutineDispatcher = Dispatchers.Default) {

private val dataChannel: Channel<Transmission.Data> = Channel(capacity = Channel.UNLIMITED)
private val dataChannel: Channel<D> = Channel(capacity = Channel.UNLIMITED)
private val effectChannel: Channel<Transmission.Effect> = Channel(capacity = Channel.UNLIMITED)

private val jobList: MutableList<Job?> = mutableListOf()

open val signalHandler: SignalHandler? = null
open val signalHandler: SignalHandler<D>? = null

open val effectHandler: EffectHandler? = null
open val effectHandler: EffectHandler<D>? = null

private val coroutineScope = CoroutineScope(SupervisorJob() + dispatcher)

private val handlerScope: HandlerScope = object : HandlerScope {
override fun publishData(data: Transmission.Data?) {
private val handlerScope: HandlerScope<D> = object : HandlerScope<D> {
override fun publishData(data: D?) {
data?.let { dataChannel.trySend(it) }
}

Expand All @@ -43,7 +43,7 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) {
fun initialize(
incomingSignal: SharedFlow<Transmission.Signal>,
incomingEffect: SharedFlow<Transmission.Effect>,
outGoingData: SendChannel<Transmission.Data>,
outGoingData: SendChannel<D>,
outGoingEffect: SendChannel<Transmission.Effect>,
) {
jobList += coroutineScope.launch {
Expand All @@ -64,7 +64,7 @@ open class Transformer(dispatcher: CoroutineDispatcher = Dispatchers.Default) {
}
}

protected inner class TransmissionDataHolder<T : Transmission.Data?>(initialValue: T) {
protected inner class TransmissionDataHolder<T : D?>(initialValue: T) {

private val holder = MutableStateFlow(initialValue)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.trendyol.transmission.transformer.handler

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.transformer.Transformer

fun interface EffectHandler {
suspend fun HandlerScope.onEffect(effect: Transmission.Effect)
fun interface EffectHandler<D: Transmission.Data> {
suspend fun HandlerScope<D>.onEffect(effect: Transmission.Effect)
}

fun buildGenericEffectHandler(
onEffect: HandlerScope.(effect: Transmission.Effect) -> Unit
): EffectHandler {
fun<D: Transmission.Data> Transformer<D>.buildGenericEffectHandler(
onEffect: HandlerScope<D>.(effect: Transmission.Effect) -> Unit
): EffectHandler<D> {
return EffectHandler { effect -> onEffect(effect) }
}

inline fun <reified E : Transmission.Effect> buildTypedEffectHandler(
crossinline onEffect: suspend HandlerScope.(effect: E) -> Unit
): EffectHandler {
inline fun <D: Transmission.Data, reified E : Transmission.Effect> Transformer<D>.buildTypedEffectHandler(
crossinline onEffect: suspend HandlerScope<D>.(effect: E) -> Unit
): EffectHandler<D> {
return EffectHandler { incomingEffect ->
incomingEffect
.takeIf { it is E }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.trendyol.transmission.transformer.handler

import com.trendyol.transmission.Transmission

interface HandlerScope {
fun publishData(data: Transmission.Data?)
interface HandlerScope<D: Transmission.Data> {
fun publishData(data: D?)
fun publishEffect(effect: Transmission.Effect)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.trendyol.transmission.transformer.handler

import com.trendyol.transmission.Transmission
import com.trendyol.transmission.transformer.Transformer

fun interface SignalHandler {
suspend fun HandlerScope.onSignal(signal: Transmission.Signal)
fun interface SignalHandler<D: Transmission.Data> {
suspend fun HandlerScope<D>.onSignal(signal: Transmission.Signal)
}

fun buildGenericSignalHandler(
onSignal: suspend HandlerScope.(signal: Transmission.Signal) -> Unit
): SignalHandler {
fun<D: Transmission.Data> Transformer<D>.buildGenericSignalHandler(
onSignal: suspend HandlerScope<D>.(signal: Transmission.Signal) -> Unit
): SignalHandler<D> {
return SignalHandler { signal -> onSignal(signal) }
}

inline fun <reified S : Transmission.Signal> buildTypedSignalHandler(
crossinline onSignal: suspend HandlerScope.(signal: S) -> Unit
): SignalHandler {
inline fun <D: Transmission.Data, reified S : Transmission.Signal> buildTypedSignalHandler(
crossinline onSignal: suspend HandlerScope<D>.(signal: S) -> Unit
): SignalHandler<D> {
return SignalHandler { incomingSignal ->
incomingSignal
.takeIf { it is S }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlin.test.assertEquals
@OptIn(ExperimentalCoroutinesApi::class)
class TransmissionRouterTest {

private lateinit var sut: TransmissionRouter
private lateinit var sut: TransmissionRouter<Transmission.Data>

@get:Rule
val testCoroutineRule = TestCoroutineRule()
Expand Down
Loading

0 comments on commit aef33b4

Please sign in to comment.