mirror of
https://github.com/hmalik144/EasyCC_Master.git
synced 2026-01-31 02:41:47 +00:00
@@ -5,16 +5,12 @@ import com.appttude.h_mal.easycc.models.CurrencyModelInterface
|
|||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
data class CurrencyResponse(
|
data class CurrencyResponse(
|
||||||
|
|
||||||
@field:SerializedName("date")
|
@field:SerializedName("date")
|
||||||
val date: String? = null,
|
val date: String? = null,
|
||||||
|
|
||||||
@field:SerializedName("amount")
|
@field:SerializedName("amount")
|
||||||
val amount: Double? = null,
|
val amount: Double? = null,
|
||||||
|
|
||||||
@field:SerializedName("rates")
|
@field:SerializedName("rates")
|
||||||
var rates: Map<String, Double>? = null,
|
var rates: Map<String, Double>? = null,
|
||||||
|
|
||||||
@field:SerializedName("base")
|
@field:SerializedName("base")
|
||||||
val base: String? = null
|
val base: String? = null
|
||||||
) : CurrencyModelInterface {
|
) : CurrencyModelInterface {
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
|||||||
|
|
||||||
private val appContext = context.applicationContext
|
private val appContext = context.applicationContext
|
||||||
|
|
||||||
// Instance of Shared preferences
|
|
||||||
private val preference: SharedPreferences =
|
private val preference: SharedPreferences =
|
||||||
PreferenceManager.getDefaultSharedPreferences(appContext)
|
PreferenceManager.getDefaultSharedPreferences(appContext)
|
||||||
|
|
||||||
@@ -28,7 +27,6 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
|||||||
context.resources.getStringArray(R.array.currency_arrays)[0]
|
context.resources.getStringArray(R.array.currency_arrays)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save currency pairs into prefs
|
|
||||||
fun saveConversionPair(s1: String, s2: String) {
|
fun saveConversionPair(s1: String, s2: String) {
|
||||||
preference.edit()
|
preference.edit()
|
||||||
.putString(CURRENCY_ONE, s1)
|
.putString(CURRENCY_ONE, s1)
|
||||||
@@ -36,8 +34,6 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
|||||||
.apply()
|
.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve Currency pairs from prefs
|
|
||||||
// Returns Pairs
|
|
||||||
fun getConversionPair(): Pair<String?, String?> {
|
fun getConversionPair(): Pair<String?, String?> {
|
||||||
val fromString = getConversionString(CURRENCY_ONE)
|
val fromString = getConversionString(CURRENCY_ONE)
|
||||||
val toString = getConversionString(CURRENCY_TWO)
|
val toString = getConversionString(CURRENCY_TWO)
|
||||||
@@ -45,18 +41,15 @@ class PreferenceProvider @Inject constructor(@ApplicationContext context: Contex
|
|||||||
return Pair(fromString, toString)
|
return Pair(fromString, toString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun getConversionString(conversionName: String): String? {
|
private fun getConversionString(conversionName: String): String? {
|
||||||
return preference
|
return preference
|
||||||
.getString(conversionName, defaultRate)
|
.getString(conversionName, defaultRate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save currency pairs for widget
|
|
||||||
fun saveWidgetConversionPair(
|
fun saveWidgetConversionPair(
|
||||||
fromString: String,
|
fromString: String,
|
||||||
toString: String, appWidgetId: Int
|
toString: String, appWidgetId: Int
|
||||||
) {
|
) {
|
||||||
|
|
||||||
preference.edit()
|
preference.edit()
|
||||||
.putString("${appWidgetId}_$CURRENCY_ONE", fromString)
|
.putString("${appWidgetId}_$CURRENCY_ONE", fromString)
|
||||||
.putString("${appWidgetId}_$CURRENCY_TWO", toString)
|
.putString("${appWidgetId}_$CURRENCY_TWO", toString)
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ class WidgetHelper @Inject constructor(
|
|||||||
val repository: Repository
|
val repository: Repository
|
||||||
) {
|
) {
|
||||||
|
|
||||||
suspend fun getWidgetData(): CurrencyModel? {
|
suspend fun getWidgetData(appWidgetId: Int): CurrencyModel? {
|
||||||
try {
|
try {
|
||||||
val pair = repository.getConversionPair()
|
val pair = repository.getWidgetConversionPairs(appWidgetId)
|
||||||
val s1 = pair.first?.trimToThree() ?: return null
|
val s1 = pair.first?.trimToThree() ?: return null
|
||||||
val s2 = pair.second?.trimToThree() ?: return null
|
val s2 = pair.second?.trimToThree() ?: return null
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import javax.inject.Inject
|
|||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class WidgetServiceIntent : JobIntentService() {
|
class WidgetServiceIntent : JobIntentService() {
|
||||||
|
|
||||||
//DI with kodein to use in CurrencyAppWidgetKotlin
|
|
||||||
@Inject
|
@Inject
|
||||||
lateinit var helper: WidgetHelper
|
lateinit var helper: WidgetHelper
|
||||||
|
|
||||||
@@ -45,7 +44,7 @@ class WidgetServiceIntent : JobIntentService() {
|
|||||||
val views = RemoteViews(context.packageName, R.layout.currency_app_widget)
|
val views = RemoteViews(context.packageName, R.layout.currency_app_widget)
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
val exchangeResponse = helper.getWidgetData()
|
val exchangeResponse = helper.getWidgetData(appWidgetId)
|
||||||
|
|
||||||
exchangeResponse?.let {
|
exchangeResponse?.let {
|
||||||
val titleString = "${it.from}${it.to}"
|
val titleString = "${it.from}${it.to}"
|
||||||
@@ -63,7 +62,7 @@ class WidgetServiceIntent : JobIntentService() {
|
|||||||
val configPendingIntent =
|
val configPendingIntent =
|
||||||
PendingIntent.getActivity(
|
PendingIntent.getActivity(
|
||||||
context, appWidgetId, clickIntentTemplate,
|
context, appWidgetId, clickIntentTemplate,
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
|
FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
|
||||||
)
|
)
|
||||||
views.setOnClickPendingIntent(R.id.widget_view, configPendingIntent)
|
views.setOnClickPendingIntent(R.id.widget_view, configPendingIntent)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,8 +79,7 @@ class RepositoryNetworkTest {
|
|||||||
val ioExceptionReturned = assertFailsWith<IOException> {
|
val ioExceptionReturned = assertFailsWith<IOException> {
|
||||||
repository.getDataFromApi(s1, s2)
|
repository.getDataFromApi(s1, s2)
|
||||||
}
|
}
|
||||||
assertNotNull(ioExceptionReturned)
|
assertEquals(ioExceptionReturned.message, "Error Code: 0")
|
||||||
assertNotNull(ioExceptionReturned.message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ import com.appttude.h_mal.easycc.data.repository.Repository
|
|||||||
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
||||||
import com.appttude.h_mal.easycc.utils.MainCoroutineRule
|
import com.appttude.h_mal.easycc.utils.MainCoroutineRule
|
||||||
import com.appttude.h_mal.easycc.utils.observeOnce
|
import com.appttude.h_mal.easycc.utils.observeOnce
|
||||||
|
import com.nhaarman.mockitokotlin2.doAnswer
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.Assert.*
|
import org.junit.Assert.*
|
||||||
@@ -18,7 +19,9 @@ import org.mockito.Mock
|
|||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.mockito.Mockito.mock
|
import org.mockito.Mockito.mock
|
||||||
import org.mockito.MockitoAnnotations
|
import org.mockito.MockitoAnnotations
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class MainViewModelTest {
|
class MainViewModelTest {
|
||||||
|
|
||||||
// Run tasks synchronously
|
// Run tasks synchronously
|
||||||
@@ -70,24 +73,25 @@ class MainViewModelTest {
|
|||||||
@Test
|
@Test
|
||||||
fun initiate_invalidBundleValues_successfulResponse() = runBlocking {
|
fun initiate_invalidBundleValues_successfulResponse() = runBlocking {
|
||||||
//GIVEN
|
//GIVEN
|
||||||
val currencyOne = "AUD - Australian Dollar"
|
val currencyOne = "corrupted data"
|
||||||
val currencyTwo = "GBP - British Pound"
|
val currencyTwo = "corrupted data again"
|
||||||
val pair = Pair(currencyOne, currencyTwo)
|
val bundle = mock(Bundle()::class.java)
|
||||||
val responseObject = mock(ResponseObject::class.java)
|
val error = "Corrupted data found"
|
||||||
|
|
||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(repository.getConversionPair()).thenReturn(pair)
|
Mockito.`when`(bundle.getString("parse_1")).thenReturn(currencyOne)
|
||||||
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo))
|
Mockito.`when`(bundle.getString("parse_2")).thenReturn(currencyTwo)
|
||||||
.thenReturn(responseObject)
|
Mockito.`when`(helper.getDataFromApi(currencyOne, currencyTwo))
|
||||||
|
.doAnswer { throw IOException(error) }
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.initiate(null)
|
viewModel.initiate(bundle)
|
||||||
viewModel.operationStartedListener.observeOnce {
|
viewModel.operationStartedListener.observeOnce {
|
||||||
assertEquals(true, it)
|
assertEquals(true, it)
|
||||||
}
|
}
|
||||||
viewModel.operationFinishedListener.observeOnce {
|
viewModel.operationFinishedListener.observeOnce {
|
||||||
assertEquals(true, it.first)
|
assertEquals(false, it.first)
|
||||||
assertNull(it.second)
|
assertEquals(it.second, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user