Skip to content

Instantly share code, notes, and snippets.

@ClaudeHangui
Created May 23, 2021 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClaudeHangui/aa2177025f63f9c730e8b8d28853d4d5 to your computer and use it in GitHub Desktop.
Save ClaudeHangui/aa2177025f63f9c730e8b8d28853d4d5 to your computer and use it in GitHub Desktop.
Repository which provides the latest currency exchange rates for every country. The USD is the base currency
package com.changui.penguinpay
import android.content.Context
import android.net.ConnectivityManager
interface ExchangeRateRepoInt {
suspend fun getExchangeRates(): ExchangeRates?
}
interface NetworkAccessInt {
fun isConnectedToInternet(): Boolean
}
class NetworkAccessImpl(private val context: Context): NetworkAccessInt {
override fun isConnectedToInternet(): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = cm.activeNetworkInfo
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting
}
}
class ExchangeRateRepoImpl(private val api: RateExchangeApi, private val networkAccessInt: NetworkAccessInt): ExchangeRateRepoInt {
override suspend fun getExchangeRates(): ExchangeRates {
if (networkAccessInt.isConnectedToInternet()) {
val response = api.getLatestRates()
return response?.rates ?: throw NoDataException()
} else throw InternetErrorException()
}
}
class NoDataException : Exception()
class InternetErrorException : Exception()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment