Created
May 23, 2021 12:25
-
-
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
This file contains hidden or 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
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