Salimov Azimjon 316-18 guruh Mavzu: Android dasturlarida xabarlarni yuborish va almashtirish uchun foydalanuvchi interfeysini yaratish



Download 24,18 Kb.
bet4/5
Sana31.12.2021
Hajmi24,18 Kb.
#238279
1   2   3   4   5
Bog'liq
3-TOPSHIRIQ MI

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)

Download 24,18 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish