it.map { quote ->
stocks.add(quote)
}
}
}
fun saveStock(quote: Quote) = viewModelScope.launch(exceptionHandler) {
stocksRepository.saveStock(quote)
}
fun deleteStock(quote: Quote) = viewModelScope.launch(exceptionHandler) {
stocksRepository.deleteStock(quote)
}
fun liveUpdateStocks() = viewModelScope.launch(exceptionHandler) {
stocks.forEach {
ws.send("\"type\": \"subscribe\", \"symbol\": ${it.symbol}")
}
}
fun setUpWebSocket() {
val request = Request.Builder()
.url("wss://ws.finnhub.io?token=c15r63v48v6oal0lqon0")
.header("X-Finnhub-Secret", "c15r63v48v6oal0lqoo0")
.build()
ws = OkHttpClient()
.newWebSocket(request, object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
super.onOpen(webSocket, response)
}
override fun onMessage(webSocket: WebSocket, text: String) {
super.onMessage(webSocket, text)
val quote = Gson().fromJson(text,StockUpdate::class.java)
log("on message $quote")
_stockUpdate.value = Resource.Success(quote)
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
super.onFailure(webSocket, t, response)
log("on failure $t $response")
}
})
}
}
package uz.azim.stocks.ui.fragment.stocks
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import uz.azim.stocks.R
import uz.azim.stocks.databinding.FragmentStocksBinding
import uz.azim.stocks.model.Quote
import uz.azim.stocks.ui.fragment.BaseFragment
import uz.azim.stocks.ui.fragment.main.MainFragmentDirections
import uz.azim.stocks.ui.fragment.stocks.adapter.LoadFooterAdapter
import uz.azim.stocks.ui.fragment.stocks.adapter.QuoteAdapter
import uz.azim.stocks.ui.fragment.stocks.listener.OnStockClickListener
import uz.azim.stocks.ui.fragment.stocks.vm.StocksVM
import uz.azim.stocks.util.Resource
import uz.azim.stocks.util.anim.LikeAnimation
import uz.azim.stocks.util.getDrawable
import uz.azim.stocks.util.log
import uz.azim.stocks.util.navigate
import javax.inject.Inject
@AndroidEntryPoint
class StocksFragment : BaseFragment(R.layout.fragment_stocks) {
@Inject
lateinit var quoteAdapter: QuoteAdapter
private val stocksVM by viewModels()
override fun initViewBinding(view: View) = FragmentStocksBinding.bind(view)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setUpRv()
setUpListeners()
}
override fun subscribeObservers() {
lifecycleScope.launchWhenStarted {
stocksVM.quotes.collect {
if (it is Resource.Success) {
it.data?.let { list ->
quoteAdapter.submitData(list)
Do'stlaringiz bilan baham: |