Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitozgumus committed Jun 28, 2024
2 parents 0ccb574 + ea7e295 commit 77d2172
Show file tree
Hide file tree
Showing 44 changed files with 1,069 additions and 590 deletions.
420 changes: 211 additions & 209 deletions sample/src/main/java/com/trendyol/transmission/SampleActivity.kt

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions sample/src/main/java/com/trendyol/transmission/SampleViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.trendyol.transmission
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.trendyol.transmission.effect.RouterEffect
import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer
import com.trendyol.transmission.features.input.InputTransformer
import com.trendyol.transmission.router.onEach
import com.trendyol.transmission.router.toState
import com.trendyol.transmission.ui.ColorPickerUiState
import com.trendyol.transmission.ui.InputUiState
import com.trendyol.transmission.ui.MultiOutputUiState
import com.trendyol.transmission.ui.OutputUiState
import com.trendyol.transmission.ui.SampleScreenUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -19,28 +22,33 @@ import kotlin.time.Duration.Companion.seconds

@HiltViewModel
class SampleViewModel @Inject constructor(
private val transmissionRouter: TransmissionRouter
private val router: TransmissionRouter
) : ViewModel() {

private val _uiState = MutableStateFlow(SampleScreenUiState())
val uiState = _uiState.asStateFlow()
val inputUiState = router.dataStream
.onEach<InputUiState> { _transmissionList.value = listOf() }
.toState(viewModelScope, InputUiState())

val outputUiState = router.dataStream.toState(viewModelScope, OutputUiState())
val colorPickerUiState = router.dataStream.toState(viewModelScope, ColorPickerUiState())
val multiOutputUiState = router.dataStream.toState(viewModelScope, MultiOutputUiState())

private val _transmissionList = MutableStateFlow<List<String>>(emptyList())
val transmissionList = _transmissionList.asStateFlow()

init {
viewModelScope.launch {
launch {
transmissionRouter.dataStream.collect(::onData)
router.dataStream.collect(::onData)
}
launch {
transmissionRouter.effectStream.collect(::onEffect)
router.effectStream.collect(::onEffect)
}
}
}

fun processSignal(signal: Transmission.Signal) {
transmissionRouter.processSignal(signal)
router.processSignal(signal)
_transmissionList.update { it.plus("Signal: $signal") }
}

Expand All @@ -53,28 +61,20 @@ class SampleViewModel @Inject constructor(
}
}
}
val inputData = transmissionRouter.queryHelper.queryData<InputUiState>("InputUiState")
val inputData = router.requestHelper.getData(InputTransformer.holderContract)
delay(1.seconds)
val colorPicker =
transmissionRouter.queryHelper.queryData<ColorPickerUiState>("ColorPickerUiState")
router.requestHelper.getData(ColorPickerTransformer.holderContract)
_transmissionList.update { it.plus("Current InputData: $inputData") }
_transmissionList.update { it.plus("Current ColorPickerData: $colorPicker") }
}


private fun onData(data: Transmission.Data) {
when (data) {
is InputUiState -> _uiState.update { it.copy(inputState = data) }
is OutputUiState -> _uiState.update { it.copy(outputState = data) }
is MultiOutputUiState -> _uiState.update { it.copy(multiOutputState = data) }
is ColorPickerUiState -> _uiState.update { it.copy(colorPickerState = data) }
}
_transmissionList.update { it.plus("Data: $data") }
}


override fun onCleared() {
transmissionRouter.clear()
router.clear()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.buildDataHolder
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.transformer.handler.buildGenericSignalHandler
import com.trendyol.transmission.transformer.request.buildDataContract
import com.trendyol.transmission.ui.ColorPickerUiState
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Inject
Expand All @@ -14,7 +15,7 @@ class ColorPickerTransformer @Inject constructor(
@DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher
) : Transformer(defaultDispatcher) {

private val holder = buildDataHolder(ColorPickerUiState(), key = "ColorPickerUiState")
private val holder = buildDataHolder(ColorPickerUiState(), holderContract)

override val signalHandler = buildGenericSignalHandler { signal ->
when (signal) {
Expand All @@ -40,4 +41,8 @@ class ColorPickerTransformer @Inject constructor(
}
}
}

companion object {
val holderContract = buildDataContract<ColorPickerUiState>("ColorPickerUiState")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.buildDataHolder
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.transformer.handler.buildTypedSignalHandler
import com.trendyol.transmission.transformer.query.registerComputation
import com.trendyol.transmission.transformer.request.buildComputationContract
import com.trendyol.transmission.transformer.request.buildComputationContractWithArgs
import com.trendyol.transmission.transformer.request.buildDataContract
import com.trendyol.transmission.transformer.request.computation.registerComputation
import com.trendyol.transmission.ui.InputUiState
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
Expand All @@ -17,13 +20,16 @@ class InputTransformer @Inject constructor(
@DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher
) : Transformer(defaultDispatcher) {

private val holder = buildDataHolder(InputUiState(), key = "InputUiState")
private val holder = buildDataHolder(InputUiState(), holderContract)

init {
registerComputation<WrittenInput>(key = "WrittenInput") {
registerComputation(writtenInputContract) {
delay(1.seconds)
WrittenInput(holder.getValue().writtenText)
}
registerComputation(writtenInputWithArgs) {
WrittenInput(it)
}
}

override val signalHandler = buildTypedSignalHandler<InputSignal> { signal ->
Expand All @@ -42,4 +48,11 @@ class InputTransformer @Inject constructor(
}
}
}

companion object {
val writtenInputWithArgs =
buildComputationContractWithArgs<String, WrittenInput>("WrittenInputWithArgs")
val writtenInputContract = buildComputationContract<WrittenInput>("WrittenInput")
val holderContract = buildDataContract<InputUiState>("InputUiState")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.trendyol.transmission.features.multioutput
import com.trendyol.transmission.DefaultDispatcher
import com.trendyol.transmission.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.features.input.InputEffect
import com.trendyol.transmission.features.output.OutputCalculationResult
import com.trendyol.transmission.features.output.OutputTransformer
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.buildDataHolder
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
Expand All @@ -21,7 +21,7 @@ class MultiOutputTransformer @Inject constructor(
when (effect) {
is InputEffect.InputUpdate -> {
holder.update { it.copy(writtenUppercaseText = effect.value.uppercase()) }
val result = queryComputation<OutputCalculationResult>("OutputCalculation")
val result = compute(OutputTransformer.outputCalculationContract)
holder.update {
it.copy(writtenUppercaseText = it.writtenUppercaseText + " ${result?.result}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import com.trendyol.transmission.effect.RouterEffect
import com.trendyol.transmission.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.features.colorpicker.ColorPickerTransformer
import com.trendyol.transmission.features.input.InputEffect
import com.trendyol.transmission.features.input.WrittenInput
import com.trendyol.transmission.features.input.InputTransformer
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.dataholder.buildDataHolder
import com.trendyol.transmission.transformer.handler.buildGenericEffectHandler
import com.trendyol.transmission.transformer.query.registerComputation
import com.trendyol.transmission.transformer.request.buildComputationContract
import com.trendyol.transmission.transformer.request.buildExecutionContract
import com.trendyol.transmission.transformer.request.computation.registerComputation
import com.trendyol.transmission.transformer.request.execution.registerExecution
import com.trendyol.transmission.ui.ColorPickerUiState
import com.trendyol.transmission.ui.OutputUiState
import com.trendyol.transmission.ui.theme.Pink80
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import javax.inject.Inject
Expand All @@ -27,36 +31,48 @@ class OutputTransformer @Inject constructor(
private val holder2 = buildDataHolder(ColorPickerUiState(), publishUpdates = false)

init {
registerComputation<OutputCalculationResult>(key = "OutputCalculation") {
registerComputation(outputCalculationContract) {
delay(2.seconds)
val data = queryData<ColorPickerUiState>("ColorPickerUiState")?.selectedColorIndex
val writtenOutput = queryComputation<WrittenInput>(key = "WrittenInput")
val data = getData(ColorPickerTransformer.holderContract)?.selectedColorIndex
val writtenOutput = compute(InputTransformer.writtenInputContract)
val result = Random.nextInt(5, 15) * Random.nextInt(5, 15)
OutputCalculationResult("result is $result with ($writtenOutput) and $data")
}
registerExecution(outputExecutionContract) {
delay(4.seconds)
communicationScope.publish(ColorPickerEffect.BackgroundColorUpdate(Pink80))
}
}

override val effectHandler = buildGenericEffectHandler { effect ->
when (effect) {
is InputEffect.InputUpdate -> {
holder.update { it.copy(outputText = effect.value) }
delay(3.seconds)
val selectedColor = queryData<ColorPickerUiState>("ColorPickerUiState")
val selectedColor = getData(ColorPickerTransformer.holderContract)
selectedColor ?: return@buildGenericEffectHandler
holder.update {
it.copy(outputText = it.outputText + " and Selected color index is ${selectedColor.selectedColorIndex}")
}
publish(effect = RouterEffect(holder.getValue()))
delay(1.seconds)
send(
effect = ColorPickerEffect.BackgroundColorUpdate(holder2.getValue().backgroundColor),
to = ColorPickerTransformer::class
)
execute(outputExecutionContract)
publish(effect = RouterEffect(holder.getValue()))
}

is ColorPickerEffect.BackgroundColorUpdate -> {
holder.update { it.copy(backgroundColor = effect.color) }
}
}
}

companion object {
val outputCalculationContract =
buildComputationContract<OutputCalculationResult>("OutputCalculationResult")
val outputExecutionContract =
buildExecutionContract("outputExecutionContract")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ package com.trendyol.transmission.ui
import androidx.compose.ui.graphics.Color
import com.trendyol.transmission.Transmission


data class SampleScreenUiState(
val inputState: InputUiState = InputUiState(),
val outputState: OutputUiState = OutputUiState(),
val colorPickerState: ColorPickerUiState = ColorPickerUiState(),
val multiOutputState: MultiOutputUiState = MultiOutputUiState()
)

data class InputUiState(
val writtenText: String = "",
val backgroundColor: Color = Color.White
Expand Down
17 changes: 0 additions & 17 deletions sample/src/test/java/com/trendyol/transmission/ExampleUnitTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ class ColorPickerTransformerTest {
assertTrue { effectStream.last().effect is ColorPickerEffect.SelectedColorUpdate }
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.trendyol.transmission.features.output

import com.trendyol.transmission.features.colorpicker.ColorPickerEffect
import com.trendyol.transmission.effect.RouterEffect
import com.trendyol.transmission.features.input.InputEffect
import com.trendyol.transmission.transformer.util.TestCoroutineRule
import com.trendyol.transmission.ui.ColorPickerUiState
Expand Down Expand Up @@ -44,7 +44,7 @@ class OutputTransformerTest {
addQueryData(ColorPickerUiState(), key = "ColorPickerUiState")
}) {
assertEquals(OutputUiState(outputText = "test"), dataStream[1])
assertTrue(effectStream.last().effect is ColorPickerEffect.BackgroundColorUpdate)
assertTrue(effectStream.last().effect is RouterEffect)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ class RegistryScopeImpl : RegistryScope {
override fun <D : Transmission.Data> addComputation(data: D, key: String) {
computationMap[key] = data
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.trendyol.transmission.Transmission
import com.trendyol.transmission.effect.EffectWrapper
import com.trendyol.transmission.router.createBroadcast
import com.trendyol.transmission.transformer.Transformer
import com.trendyol.transmission.transformer.query.Query
import com.trendyol.transmission.transformer.query.QueryResult
import com.trendyol.transmission.transformer.request.Query
import com.trendyol.transmission.transformer.request.QueryResult
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
Expand All @@ -19,7 +19,7 @@ import kotlinx.coroutines.launch
internal class TestRouter(
private val registry: RegistryScopeImpl,
private val transformer: Transformer,
private val dispatcher: CoroutineDispatcher
dispatcher: CoroutineDispatcher
) {
private val testScope = CoroutineScope(dispatcher)

Expand All @@ -37,7 +37,7 @@ internal class TestRouter(

private val outGoingQueryChannel: Channel<Query> = Channel(capacity = Channel.BUFFERED)

private val queryResultChannel: Channel<QueryResult<Transmission.Data>> =
private val queryResultChannel: Channel<QueryResult> =
Channel(capacity = Channel.BUFFERED)

private val incomingQueryResponse = queryResultChannel.receiveAsFlow()
Expand Down Expand Up @@ -82,6 +82,9 @@ internal class TestRouter(
when (query) {
is Query.Computation -> processComputationQuery(query)
is Query.Data -> processDataQuery(query)
is Query.ComputationWithArgs<*> -> processComputationQueryWithArgs(query)
is Query.Execution -> {}
is Query.ExecutionWithArgs<*> -> {}
}
}

Expand Down Expand Up @@ -109,4 +112,15 @@ internal class TestRouter(
queryResultChannel.trySend(computationToSend)
}

private fun <A : Any> processComputationQueryWithArgs(
query: Query.ComputationWithArgs<A>
) = testScope.launch {
val computationToSend = QueryResult.Computation(
owner = query.sender,
data = registry.computationMap[query.key],
key = query.key
)
queryResultChannel.trySend(computationToSend)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ fun Transformer.testWithSignal(
}
}

suspend fun Transformer.waitProcessingToFinish() {
currentSignalProcessing?.join()
currentEffectProcessing?.join()
}
Loading

0 comments on commit 77d2172

Please sign in to comment.