Skip to content

Commit

Permalink
Resolving detekt issues #56
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidGarousi committed Mar 17, 2023
1 parent 954c740 commit 349303e
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 117 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ data class StockListDto(
val min: Double? = null,
val max: Double? = null,
val ref: Double? = null,
val open: Double? = null,
val open: Double? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import android.util.Log
import com.lightstreamer.client.ItemUpdate
import com.lightstreamer.client.Subscription
import com.lightstreamer.client.SubscriptionListener
import garousi.dev.lightstreamer.connection.LightStreamerConnection
import garousi.dev.lightstreamer.models.SubscriptionMode
import garousi.dev.lightstreamer.service.LightStreamerService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -16,15 +18,18 @@ import javax.inject.Inject

class StockListLightStreamerService @Inject constructor(
private val connection: LightStreamerConnection,
private val externalScope: CoroutineScope
) : LightStreamerService<StockListDto> {

private val scope = CoroutineScope(SupervisorJob())
private var dto = StockListDto()
private val _flow: MutableStateFlow<StockListDto> = MutableStateFlow(StockListDto())
override val flow = _flow.asStateFlow()
private val _stockListStream: MutableStateFlow<StockListDto> = MutableStateFlow(StockListDto())
override val stream = _stockListStream.asStateFlow()

init {
connection.connect(serverAddress = serverAddress, adapterSet = adapterSet)
connection.connect(
serverAddress = serverAddress,
adapterSet = adapterSet
)
}

companion object {
Expand Down Expand Up @@ -152,13 +157,13 @@ class StockListLightStreamerService @Inject constructor(
ref = ref,
open = open,
)
scope.launch(Dispatchers.IO) {
_flow.emit(dto)
externalScope.launch(Dispatchers.IO) {
_stockListStream.emit(dto)
}
}

override fun unsubscribe() {
connection.unsubscribe()
scope.cancel()
externalScope.cancel()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dev.garousi.stockwatcher.feature.watchlist.data.LightStreamerConnection
import dev.garousi.stockwatcher.feature.watchlist.data.LightStreamerConnectionImpl
import garousi.dev.lightstreamer.connection.DefaultLightStreamerConnection
import garousi.dev.lightstreamer.connection.LightStreamerConnection
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class LightStreamerModule {
@Binds
@Singleton
abstract fun bindsLightStreamerService(impl: LightStreamerConnectionImpl): LightStreamerConnection
abstract fun bindsLightStreamerService(impl: DefaultLightStreamerConnection): LightStreamerConnection
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
package dev.garousi.stockwatcher.feature.watchlist.di

import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import dev.garousi.stockwatcher.feature.watchlist.data.LightStreamerConnection
import dev.garousi.stockwatcher.feature.watchlist.data.LightStreamerService
import dev.garousi.stockwatcher.feature.watchlist.data.StockListDto
import dev.garousi.stockwatcher.feature.watchlist.data.StockListLightStreamerService
import garousi.dev.lightstreamer.service.LightStreamerService
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object WatchlistListModule {
// @Provides
// fun provideGetStockListUseCase(
// repository: StockRepository
// ): GetStockListUseCase {
// return GetStockListUseCase(repository)
// }
//
// @Provides
// fun provideStockListUseCases(
// getStockListUseCase: GetStockListUseCase
// ): StockListUseCases {
// return StockListUseCases(getStockList = getStockListUseCase)
// }

@Provides
interface WatchlistListModule {
@Binds
@Singleton
fun provideStockListLightStreamerService(
connection: LightStreamerConnection,
): LightStreamerService<StockListDto> {
return StockListLightStreamerService(connection = connection)
}
fun bindsStockListLightStreamerService(
impl: StockListLightStreamerService
): LightStreamerService<StockListDto>
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
@file:Suppress("CyclomaticComplexMethod")

package dev.garousi.stockwatcher.feature.watchlist.presentation

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.garousi.stockwatcher.feature.watchlist.data.LightStreamerService
import dev.garousi.stockwatcher.feature.watchlist.data.StockListDto
import garousi.dev.lightstreamer.service.LightStreamerService
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
Expand All @@ -30,41 +28,34 @@ class WatchlistViewModel @Inject constructor(
}

private fun subscribeToDemoAdapterSet() {
stockListLightStreamerService
.subscribe()
.flow
.map { stock ->
_uiState.update { it.copy(isLoading = false) }
stock.itemPos?.let { updatedItemIndex ->
_uiState.update {
it.copy(
stocks = it.stocks.toMutableList().apply {
this[updatedItemIndex - 1] = this[updatedItemIndex - 1].copy(
name = stock.name ?: it.stocks[updatedItemIndex].name,
last = stock.last ?: it.stocks[updatedItemIndex].last,
time = stock.time ?: it.stocks[updatedItemIndex].time,
change = stock.change ?: it.stocks[updatedItemIndex].change,
bidSize = stock.bidSize ?: it.stocks[updatedItemIndex].bidSize,
bid = stock.bid ?: it.stocks[updatedItemIndex].bid,
ask = stock.ask ?: it.stocks[updatedItemIndex].ask,
askSize = stock.askSize ?: it.stocks[updatedItemIndex].askSize,
min = stock.min ?: it.stocks[updatedItemIndex].min,
max = stock.max ?: it.stocks[updatedItemIndex].max,
ref = stock.ref ?: it.stocks[updatedItemIndex].ref,
open = stock.open ?: it.stocks[updatedItemIndex].open,
)
},
)
viewModelScope.launch {
stockListLightStreamerService
.subscribe()
.stream
.collectLatest { stock ->
stock.itemPos?.let { updatedItemIndex ->
_uiState.update {
it.copy(
stocks = it.stocks.toMutableList().apply {
this[updatedItemIndex - 1] = this[updatedItemIndex - 1].copy(
name = stock.name ?: it.stocks[updatedItemIndex].name,
last = stock.last ?: it.stocks[updatedItemIndex].last,
time = stock.time ?: it.stocks[updatedItemIndex].time,
change = stock.change ?: it.stocks[updatedItemIndex].change,
bidSize = stock.bidSize ?: it.stocks[updatedItemIndex].bidSize,
bid = stock.bid ?: it.stocks[updatedItemIndex].bid,
ask = stock.ask ?: it.stocks[updatedItemIndex].ask,
askSize = stock.askSize ?: it.stocks[updatedItemIndex].askSize,
min = stock.min ?: it.stocks[updatedItemIndex].min,
max = stock.max ?: it.stocks[updatedItemIndex].max,
ref = stock.ref ?: it.stocks[updatedItemIndex].ref,
open = stock.open ?: it.stocks[updatedItemIndex].open,
)
}
)
}
}
}
}
.catch { cause: Throwable ->
Log.i("LOGGER", "" + cause.localizedMessage.orEmpty())
}
.stateIn(
scope = viewModelScope,
initialValue = StockListDto(),
started = SharingStarted.Eagerly,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package garousi.dev.light_streamer.connection
package garousi.dev.lightstreamer.connection

import android.util.Log
import com.lightstreamer.client.ClientListener
import com.lightstreamer.client.LightstreamerClient
import com.lightstreamer.client.Subscription
import com.lightstreamer.client.SubscriptionListener
import garousi.dev.light_streamer.listeners.EmptyClientListener
import garousi.dev.light_streamer.models.SubscriptionMode
import garousi.dev.lightstreamer.listeners.EmptyClientListener
import garousi.dev.lightstreamer.models.SubscriptionMode

class DefaultLightStreamerConnection : LightStreamerConnection {
override var lsClient: LightstreamerClient? = null
Expand Down Expand Up @@ -117,4 +117,4 @@ class DefaultLightStreamerConnection : LightStreamerConnection {
) {
subscription = Subscription(subscriptionMode.name, itemName, fieldNames)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package garousi.dev.light_streamer.connection
package garousi.dev.lightstreamer.connection

import com.lightstreamer.client.LightstreamerClient
import com.lightstreamer.client.Subscription
import com.lightstreamer.client.SubscriptionListener
import garousi.dev.light_streamer.models.SubscriptionMode
import garousi.dev.lightstreamer.models.SubscriptionMode

interface LightStreamerConnection {
val lsClient: LightstreamerClient?
Expand Down Expand Up @@ -45,4 +45,4 @@ interface LightStreamerConnection {

fun setRequestedSnapshot(requestedSnapshot: String?)
fun setRequestedMaxFrequency(requestedMaxFrequency: String?)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package garousi.dev.light_streamer.listeners
package garousi.dev.lightstreamer.listeners

import com.lightstreamer.client.ClientListener
import com.lightstreamer.client.LightstreamerClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package garousi.dev.light_streamer.models
package garousi.dev.lightstreamer.models

enum class SubscriptionMode {
Merge,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package garousi.dev.light_streamer.service
package garousi.dev.lightstreamer.service

import com.lightstreamer.client.ItemUpdate
import kotlinx.coroutines.flow.StateFlow
Expand Down

0 comments on commit 349303e

Please sign in to comment.