Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Garyteck
Garyteck / CryptoAdapter.kt
Created August 14, 2020 05:18
Crypto Adapter
class CryptoAdapter : PagingDataAdapter {
fun bind(cryptoCurrency : CryptoCurrency?) {
cryptoCurrency?.let {
cryptoName.text = it.name
cryptoSymbol.text = it.symbol
cryptoPrice.text = ...
}
}
}
@Garyteck
Garyteck / CryptoListFragment.kt
Created August 14, 2020 05:17
CryptoListfragment
lifecycleScope.launch {
cryptoViewModel.cryptos.collectLatest {
cryptoAdapter.submitData(it)
}
}
@Garyteck
Garyteck / CryptoViewModel.kt
Created August 14, 2020 05:08
ViewModel Crypto List app
class CryptoViewModel constructor( repository: Repository) : ViewModel()
val cryptos : Flow<PagingData<CryptoCurrency>> = repository.getCryptos()
}
// build.gradle file dependencies
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
@Garyteck
Garyteck / PagerRemote.kt
Created August 14, 2020 05:01
Pager With RemoteMediator
class Repository(val db: CryptoDatabase, val api : NetworkInterface) {
fun getCryptos() : Flow<PagingData<CryptoCurrency>> {
return Pager(
config = PagingConfig(PAGE_SIZE),
initialKey = 0,
remoteMediator = RepositoryMediator(db,api),
pagingSourceFactory = { db.cryptoDao().getCryptos() }
).flow
}
@Garyteck
Garyteck / CryptoCurrency.kt
Last active August 14, 2020 04:51
CryptoCurrency Entity with Room
@Entity(tableName = "CryptoCurrency")
data class CryptoCurrency(
@PrimaryKey
val id: String,
val changePercent24Hr: String?,
val marketCapUsd: String?,
val maxSupply: String?,
val name: String?,
val priceUsd: String?,
val rank: String?,
@Garyteck
Garyteck / Cryptocurrencies.json
Last active August 14, 2020 05:35
Coincap REST API
"data": [
{
"id": "bitcoin",
"rank": "1",
"symbol": "BTC",
"name": "Bitcoin",
"supply": "17193925.0000000000000000",
"maxSupply": "21000000.0000000000000000",
"marketCapUsd": "119150835874.4699281625807300",
"volumeUsd24Hr": "2927959461.1750323310959460",
@Garyteck
Garyteck / CoincapNetwork.kt
Last active August 16, 2020 08:10
Network Interface of Coincap
interface NetworkInterface {
companion object {
const val ENDPOINT = "https://api.coincap.io/v2/"
}
data class Cryptos (
val data : List<CryptoCurrency>
)