Skip to content

Instantly share code, notes, and snippets.

View CryptoAdapter.kt
class CryptoAdapter : PagingDataAdapter {
fun bind(cryptoCurrency : CryptoCurrency?) {
cryptoCurrency?.let {
cryptoName.text = it.name
cryptoSymbol.text = it.symbol
cryptoPrice.text = ...
}
}
}
View CryptoListFragment.kt
lifecycleScope.launch {
cryptoViewModel.cryptos.collectLatest {
cryptoAdapter.submitData(it)
}
}
@Garyteck
Garyteck / CryptoViewModel.kt
Created Aug 14, 2020
ViewModel Crypto List app
View CryptoViewModel.kt
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 Aug 14, 2020
Pager With RemoteMediator
View PagerRemote.kt
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 Aug 14, 2020
CryptoCurrency Entity with Room
View CryptoCurrency.kt
@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?,
View Cryptocurrencies.json
"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 Aug 16, 2020
Network Interface of Coincap
View CoincapNetwork.kt
interface NetworkInterface {
companion object {
const val ENDPOINT = "https://api.coincap.io/v2/"
}
data class Cryptos (
val data : List<CryptoCurrency>
)
You can’t perform that action at this time.