This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CryptoAdapter : PagingDataAdapter { | |
fun bind(cryptoCurrency : CryptoCurrency?) { | |
cryptoCurrency?.let { | |
cryptoName.text = it.name | |
cryptoSymbol.text = it.symbol | |
cryptoPrice.text = ... | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lifecycleScope.launch { | |
cryptoViewModel.cryptos.collectLatest { | |
cryptoAdapter.submitData(it) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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?, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"data": [ | |
{ | |
"id": "bitcoin", | |
"rank": "1", | |
"symbol": "BTC", | |
"name": "Bitcoin", | |
"supply": "17193925.0000000000000000", | |
"maxSupply": "21000000.0000000000000000", | |
"marketCapUsd": "119150835874.4699281625807300", | |
"volumeUsd24Hr": "2927959461.1750323310959460", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface NetworkInterface { | |
companion object { | |
const val ENDPOINT = "https://api.coincap.io/v2/" | |
} | |
data class Cryptos ( | |
val data : List<CryptoCurrency> | |
) |