mirror of
https://github.com/hmalik144/EasyCC_Master.git
synced 2026-01-31 02:41:47 +00:00
Added BackupCurrencyApi.kt to supplement currency sources in case of no working api
This commit is contained in:
5
.idea/assetWizardSettings.xml
generated
5
.idea/assetWizardSettings.xml
generated
@@ -33,9 +33,10 @@
|
|||||||
</option>
|
</option>
|
||||||
<option name="values">
|
<option name="values">
|
||||||
<map>
|
<map>
|
||||||
|
<entry key="assetSourceType" value="FILE" />
|
||||||
<entry key="color" value="ffffff" />
|
<entry key="color" value="ffffff" />
|
||||||
<entry key="outputName" value="ic_refresh_white_24dp" />
|
<entry key="outputName" value="ic_background" />
|
||||||
<entry key="sourceFile" value="C:\Users\h_mal" />
|
<entry key="sourceFile" value="D:\Downloads\untitled (1).svg" />
|
||||||
</map>
|
</map>
|
||||||
</option>
|
</option>
|
||||||
</PersistentState>
|
</PersistentState>
|
||||||
|
|||||||
BIN
.idea/caches/build_file_checksums.ser
generated
BIN
.idea/caches/build_file_checksums.ser
generated
Binary file not shown.
16
.idea/codeStyles/Project.xml
generated
16
.idea/codeStyles/Project.xml
generated
@@ -1,6 +1,22 @@
|
|||||||
<component name="ProjectCodeStyleConfiguration">
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
<code_scheme name="Project" version="173">
|
<code_scheme name="Project" version="173">
|
||||||
<JetCodeStyleSettings>
|
<JetCodeStyleSettings>
|
||||||
|
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
|
||||||
|
<value>
|
||||||
|
<package name="java.util" alias="false" withSubpackages="false" />
|
||||||
|
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
|
||||||
|
<package name="io.ktor" alias="false" withSubpackages="true" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="PACKAGES_IMPORT_LAYOUT">
|
||||||
|
<value>
|
||||||
|
<package name="" alias="false" withSubpackages="true" />
|
||||||
|
<package name="java" alias="false" withSubpackages="true" />
|
||||||
|
<package name="javax" alias="false" withSubpackages="true" />
|
||||||
|
<package name="kotlin" alias="false" withSubpackages="true" />
|
||||||
|
<package name="" alias="true" withSubpackages="true" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
|
||||||
</JetCodeStyleSettings>
|
</JetCodeStyleSettings>
|
||||||
<codeStyleSettings language="XML">
|
<codeStyleSettings language="XML">
|
||||||
|
|||||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -39,7 +39,7 @@
|
|||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectType">
|
<component name="ProjectType">
|
||||||
|
|||||||
4
.idea/modules.xml
generated
4
.idea/modules.xml
generated
@@ -2,8 +2,8 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<modules>
|
||||||
<module fileurl="file://$PROJECT_DIR$/Personal_Project---Currency_Converter-master.iml" filepath="$PROJECT_DIR$/Personal_Project---Currency_Converter-master.iml" group="Personal_Project---Currency_Converter-master" />
|
<module fileurl="file://$PROJECT_DIR$/.idea/modules/EasyCC.iml" filepath="$PROJECT_DIR$/.idea/modules/EasyCC.iml" group="EasyCC" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" group="Personal_Project---Currency_Converter-master/app" />
|
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" group="EasyCC/app" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -4,13 +4,13 @@ apply plugin: 'kotlin-android-extensions'
|
|||||||
apply plugin: 'kotlin-kapt'
|
apply plugin: 'kotlin-kapt'
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 29
|
compileSdkVersion 30
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.appttude.h_mal.easycc"
|
applicationId "com.appttude.h_mal.easycc"
|
||||||
minSdkVersion 23
|
minSdkVersion 21
|
||||||
targetSdkVersion 29
|
targetSdkVersion 30
|
||||||
versionCode 2
|
versionCode 5
|
||||||
versionName "1.1"
|
versionName "4.1"
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
buildTypes {
|
buildTypes {
|
||||||
@@ -24,6 +24,15 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
viewBinding.enabled = true
|
viewBinding.enabled = true
|
||||||
|
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = "1.8"
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -40,8 +49,10 @@ dependencies {
|
|||||||
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
|
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
|
||||||
|
|
||||||
//Retrofit and GSON
|
//Retrofit and GSON
|
||||||
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
|
def retrofit_ver = "2.8.1"
|
||||||
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
|
implementation "com.squareup.retrofit2:retrofit:$retrofit_ver"
|
||||||
|
implementation "com.squareup.retrofit2:converter-gson:$retrofit_ver"
|
||||||
|
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"
|
||||||
|
|
||||||
//Kotlin Coroutines
|
//Kotlin Coroutines
|
||||||
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.appttude.h_mal.easycc;
|
|
||||||
|
|
||||||
|
|
||||||
public class MainActivityTest {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
package com.appttude.h_mal.easycc.application
|
package com.appttude.h_mal.easycc.application
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import com.appttude.h_mal.easycc.data.network.api.BackupCurrencyApi
|
||||||
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
||||||
import com.appttude.h_mal.easycc.data.network.interceptors.NetworkConnectionInterceptor
|
import com.appttude.h_mal.easycc.data.network.interceptors.NetworkConnectionInterceptor
|
||||||
import com.appttude.h_mal.easycc.data.network.interceptors.QueryInterceptor
|
import com.appttude.h_mal.easycc.data.network.interceptors.QueryInterceptor
|
||||||
|
import com.appttude.h_mal.easycc.data.network.interceptors.loggingInterceptor
|
||||||
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
||||||
import com.appttude.h_mal.easycc.data.repository.RepositoryImpl
|
import com.appttude.h_mal.easycc.data.repository.RepositoryImpl
|
||||||
|
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
||||||
|
import com.appttude.h_mal.easycc.helper.WidgetHelper
|
||||||
import com.appttude.h_mal.easycc.ui.main.MainViewModelFactory
|
import com.appttude.h_mal.easycc.ui.main.MainViewModelFactory
|
||||||
import com.appttude.h_mal.easycc.ui.widget.WidgetViewModelFactory
|
import com.appttude.h_mal.easycc.ui.widget.WidgetViewModelFactory
|
||||||
import org.kodein.di.Kodein
|
import org.kodein.di.Kodein
|
||||||
@@ -18,17 +22,20 @@ import org.kodein.di.generic.singleton
|
|||||||
|
|
||||||
class AppClass : Application(), KodeinAware {
|
class AppClass : Application(), KodeinAware {
|
||||||
|
|
||||||
// Kodein Dependecy Injection created in Application class
|
// Kodein Dependecy Injection singletons and providers created
|
||||||
override val kodein by Kodein.lazy {
|
override val kodein by Kodein.lazy {
|
||||||
import(androidXModule(this@AppClass))
|
import(androidXModule(this@AppClass))
|
||||||
|
|
||||||
// instance() can be context or other binding created
|
|
||||||
bind() from singleton { NetworkConnectionInterceptor(instance()) }
|
bind() from singleton { NetworkConnectionInterceptor(instance()) }
|
||||||
|
bind() from singleton { loggingInterceptor() }
|
||||||
bind() from singleton { QueryInterceptor(instance()) }
|
bind() from singleton { QueryInterceptor(instance()) }
|
||||||
bind() from singleton { CurrencyApi(instance(),instance()) }
|
bind() from singleton { CurrencyApi(instance(), instance(), instance()) }
|
||||||
|
bind() from singleton { BackupCurrencyApi(instance(),instance()) }
|
||||||
bind() from singleton { PreferenceProvider(instance()) }
|
bind() from singleton { PreferenceProvider(instance()) }
|
||||||
bind() from singleton { RepositoryImpl(instance(), instance(), instance()) }
|
bind() from singleton { RepositoryImpl(instance(), instance(), instance()) }
|
||||||
bind() from provider { MainViewModelFactory(instance()) }
|
bind() from singleton { CurrencyDataHelper(instance()) }
|
||||||
|
bind() from singleton { WidgetHelper(instance(), instance()) }
|
||||||
|
bind() from provider { MainViewModelFactory(instance(), instance()) }
|
||||||
bind() from provider { WidgetViewModelFactory(instance()) }
|
bind() from provider { WidgetViewModelFactory(instance()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ abstract class SafeApiRequest {
|
|||||||
.append(errorMessageString)
|
.append(errorMessageString)
|
||||||
.toString()
|
.toString()
|
||||||
}
|
}
|
||||||
|
print(log)
|
||||||
|
|
||||||
// Log.e("Api Response Error", log)
|
// Log.e("Api Response Error", log)
|
||||||
|
|
||||||
//return error message
|
//return error message
|
||||||
@@ -58,7 +60,7 @@ abstract class SafeApiRequest {
|
|||||||
//extract ["error"] from error body
|
//extract ["error"] from error body
|
||||||
return JSONObject(it).getString("error")
|
return JSONObject(it).getString("error")
|
||||||
} catch (e: JSONException) {
|
} catch (e: JSONException) {
|
||||||
Log.e(TAG, e.message)
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.appttude.h_mal.easycc.data.network.api
|
||||||
|
|
||||||
|
import com.appttude.h_mal.easycc.data.network.interceptors.NetworkConnectionInterceptor
|
||||||
|
import com.appttude.h_mal.easycc.data.network.response.CurrencyResponse
|
||||||
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
|
import retrofit2.Response
|
||||||
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.Query
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrofit2 Network class to create network requests
|
||||||
|
*/
|
||||||
|
interface BackupCurrencyApi {
|
||||||
|
|
||||||
|
// Get rate from server with arguments passed in Repository
|
||||||
|
@GET("latest?")
|
||||||
|
suspend fun getCurrencyRate(
|
||||||
|
@Query("from") currencyFrom: String,
|
||||||
|
@Query("to") currencyTo: String
|
||||||
|
): Response<CurrencyResponse>
|
||||||
|
|
||||||
|
// interface invokation to be used in application class
|
||||||
|
companion object{
|
||||||
|
operator fun invoke(
|
||||||
|
networkConnectionInterceptor: NetworkConnectionInterceptor,
|
||||||
|
interceptor: HttpLoggingInterceptor
|
||||||
|
) : BackupCurrencyApi{
|
||||||
|
|
||||||
|
val okkHttpclient = OkHttpClient.Builder()
|
||||||
|
.addInterceptor(interceptor)
|
||||||
|
.addNetworkInterceptor(networkConnectionInterceptor)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
// Build retrofit
|
||||||
|
return Retrofit.Builder()
|
||||||
|
.client(okkHttpclient)
|
||||||
|
.baseUrl("https://api.frankfurter.app/")
|
||||||
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
|
.build()
|
||||||
|
.create(BackupCurrencyApi::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import com.appttude.h_mal.easycc.data.network.interceptors.NetworkConnectionInte
|
|||||||
import com.appttude.h_mal.easycc.data.network.interceptors.QueryInterceptor
|
import com.appttude.h_mal.easycc.data.network.interceptors.QueryInterceptor
|
||||||
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
@@ -22,26 +23,25 @@ interface CurrencyApi {
|
|||||||
// interface invokation to be used in application class
|
// interface invokation to be used in application class
|
||||||
companion object{
|
companion object{
|
||||||
operator fun invoke(
|
operator fun invoke(
|
||||||
networkConnectionInterceptor: NetworkConnectionInterceptor,
|
networkConnectionInterceptor: NetworkConnectionInterceptor,
|
||||||
queryInterceptor: QueryInterceptor
|
queryInterceptor: QueryInterceptor,
|
||||||
|
interceptor: HttpLoggingInterceptor
|
||||||
) : CurrencyApi{
|
) : CurrencyApi{
|
||||||
|
|
||||||
// okkHttpclient with injected interceptors
|
// okkHttpclient with injected interceptors
|
||||||
val okkHttpclient = OkHttpClient.Builder()
|
val okkHttpclient = OkHttpClient.Builder()
|
||||||
.addInterceptor(queryInterceptor)
|
.addInterceptor(interceptor)
|
||||||
.addNetworkInterceptor(networkConnectionInterceptor)
|
.addInterceptor(queryInterceptor)
|
||||||
.build()
|
.addNetworkInterceptor(networkConnectionInterceptor)
|
||||||
|
.build()
|
||||||
|
|
||||||
// Build retrofit
|
// Build retrofit
|
||||||
return Retrofit.Builder()
|
return Retrofit.Builder()
|
||||||
.client(okkHttpclient)
|
.client(okkHttpclient)
|
||||||
.baseUrl("https://free.currencyconverterapi.com/api/v3/")
|
.baseUrl("https://free.currencyconverterapi.com/api/v3/")
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
.build()
|
.build()
|
||||||
.create(CurrencyApi::class.java)
|
.create(CurrencyApi::class.java)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.appttude.h_mal.easycc.data.network.interceptors
|
||||||
|
|
||||||
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
|
|
||||||
|
fun loggingInterceptor() = run {
|
||||||
|
val httpLoggingInterceptor = HttpLoggingInterceptor()
|
||||||
|
httpLoggingInterceptor.apply {
|
||||||
|
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@ class QueryInterceptor(
|
|||||||
|
|
||||||
override fun intercept(chain: Interceptor.Chain): Response {
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
val original: Request = chain.request()
|
val original: Request = chain.request()
|
||||||
val originalHttpUrl: HttpUrl = original.url()
|
val originalHttpUrl: HttpUrl = original.url
|
||||||
|
|
||||||
val url = originalHttpUrl.newBuilder()
|
val url = originalHttpUrl.newBuilder()
|
||||||
.addQueryParameter("apiKey", context.getString(R.string.apiKey))
|
.addQueryParameter("apiKey", context.getString(R.string.apiKey))
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.appttude.h_mal.easycc.data.network.response
|
||||||
|
|
||||||
|
import com.appttude.h_mal.easycc.models.CurrencyModel
|
||||||
|
import com.appttude.h_mal.easycc.models.CurrencyModelInterface
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class CurrencyResponse(
|
||||||
|
|
||||||
|
@field:SerializedName("date")
|
||||||
|
val date: String? = null,
|
||||||
|
|
||||||
|
@field:SerializedName("amount")
|
||||||
|
val amount: Double? = null,
|
||||||
|
|
||||||
|
@field:SerializedName("rates")
|
||||||
|
var rates : Map<String, Double>? = null,
|
||||||
|
|
||||||
|
@field:SerializedName("base")
|
||||||
|
val base: String? = null
|
||||||
|
): CurrencyModelInterface {
|
||||||
|
|
||||||
|
override fun getCurrencyModel(): CurrencyModel {
|
||||||
|
return CurrencyModel(
|
||||||
|
base,
|
||||||
|
rates?.iterator()?.next()?.key,
|
||||||
|
rates?.iterator()?.next()?.value ?: 0.0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,24 @@
|
|||||||
package com.appttude.h_mal.easycc.data.network.response
|
package com.appttude.h_mal.easycc.data.network.response
|
||||||
|
|
||||||
|
import com.appttude.h_mal.easycc.models.CurrencyModel
|
||||||
|
import com.appttude.h_mal.easycc.models.CurrencyModelInterface
|
||||||
import com.appttude.h_mal.easycc.models.CurrencyObject
|
import com.appttude.h_mal.easycc.models.CurrencyObject
|
||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
class ResponseObject(
|
data class ResponseObject(
|
||||||
@SerializedName("query")
|
@field:SerializedName("query")
|
||||||
var query : Any,
|
var query : Any? = null,
|
||||||
@SerializedName("results")
|
@field:SerializedName("results")
|
||||||
var results : Map<String, CurrencyObject>?
|
var results : Map<String, CurrencyObject>? = null
|
||||||
)
|
): CurrencyModelInterface {
|
||||||
|
|
||||||
|
override fun getCurrencyModel(): CurrencyModel {
|
||||||
|
val res = results?.iterator()?.next()?.value
|
||||||
|
return CurrencyModel(
|
||||||
|
res?.fr,
|
||||||
|
res?.to,
|
||||||
|
res?.value ?: 0.0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.appttude.h_mal.easycc.data.repository
|
package com.appttude.h_mal.easycc.data.repository
|
||||||
|
|
||||||
|
import com.appttude.h_mal.easycc.data.network.response.CurrencyResponse
|
||||||
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -7,13 +8,15 @@ import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
|||||||
*/
|
*/
|
||||||
interface Repository {
|
interface Repository {
|
||||||
|
|
||||||
suspend fun getData(fromCurrency: String, toCurrency: String): ResponseObject
|
suspend fun getDataFromApi(fromCurrency: String, toCurrency: String): ResponseObject
|
||||||
|
|
||||||
|
suspend fun getBackupDataFromApi(fromCurrency: String, toCurrency: String): CurrencyResponse
|
||||||
|
|
||||||
fun getConversionPair(): Pair<String?, String?>
|
fun getConversionPair(): Pair<String?, String?>
|
||||||
|
|
||||||
fun setConversionPair(fromCurrency: String, toCurrency: String)
|
fun setConversionPair(fromCurrency: String, toCurrency: String)
|
||||||
|
|
||||||
fun getArrayList(): Array<String>
|
fun getCurrenciesList(): Array<String>
|
||||||
|
|
||||||
fun getWidgetConversionPairs(appWidgetId: Int): Pair<String?, String?>
|
fun getWidgetConversionPairs(appWidgetId: Int): Pair<String?, String?>
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package com.appttude.h_mal.easycc.data.repository
|
package com.appttude.h_mal.easycc.data.repository
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.res.Resources
|
||||||
import com.appttude.h_mal.easycc.R
|
import com.appttude.h_mal.easycc.R
|
||||||
import com.appttude.h_mal.easycc.data.network.SafeApiRequest
|
import com.appttude.h_mal.easycc.data.network.SafeApiRequest
|
||||||
|
import com.appttude.h_mal.easycc.data.network.api.BackupCurrencyApi
|
||||||
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
||||||
|
import com.appttude.h_mal.easycc.data.network.response.CurrencyResponse
|
||||||
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
||||||
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
||||||
import com.appttude.h_mal.easycc.utils.convertPairsListToString
|
import com.appttude.h_mal.easycc.utils.convertPairsListToString
|
||||||
@@ -12,20 +14,27 @@ import com.appttude.h_mal.easycc.utils.convertPairsListToString
|
|||||||
* Default implementation of [Repository]. Single entry point for managing currency' data.
|
* Default implementation of [Repository]. Single entry point for managing currency' data.
|
||||||
*/
|
*/
|
||||||
class RepositoryImpl (
|
class RepositoryImpl (
|
||||||
private val api: CurrencyApi,
|
private val api: CurrencyApi,
|
||||||
private val prefs: PreferenceProvider,
|
private val backUpApi: BackupCurrencyApi,
|
||||||
context: Context
|
private val prefs: PreferenceProvider
|
||||||
):Repository, SafeApiRequest(){
|
):Repository, SafeApiRequest(){
|
||||||
|
|
||||||
private val appContext = context.applicationContext
|
override suspend fun getDataFromApi(
|
||||||
|
fromCurrency: String,
|
||||||
override suspend fun getData(fromCurrency: String, toCurrency: String
|
toCurrency: String
|
||||||
): ResponseObject{
|
): ResponseObject{
|
||||||
// Set currency pairs as correct string for api query eg. AUD_GBP
|
// Set currency pairs as correct string for api query eg. AUD_GBP
|
||||||
val currencyPair = convertPairsListToString(fromCurrency, toCurrency)
|
val currencyPair = convertPairsListToString(fromCurrency, toCurrency)
|
||||||
return responseUnwrap{ api.getCurrencyRate(currencyPair)}
|
return responseUnwrap{ api.getCurrencyRate(currencyPair)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun getBackupDataFromApi(
|
||||||
|
fromCurrency: String,
|
||||||
|
toCurrency: String
|
||||||
|
): CurrencyResponse {
|
||||||
|
return responseUnwrap{ backUpApi.getCurrencyRate(fromCurrency, toCurrency)}
|
||||||
|
}
|
||||||
|
|
||||||
override fun getConversionPair(): Pair<String?, String?> {
|
override fun getConversionPair(): Pair<String?, String?> {
|
||||||
return prefs.getConversionPair()
|
return prefs.getConversionPair()
|
||||||
}
|
}
|
||||||
@@ -34,8 +43,8 @@ class RepositoryImpl (
|
|||||||
prefs.saveConversionPair(fromCurrency, toCurrency)
|
prefs.saveConversionPair(fromCurrency, toCurrency)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getArrayList(): Array<String> =
|
override fun getCurrenciesList(): Array<String> =
|
||||||
appContext.resources.getStringArray(R.array.currency_arrays)
|
Resources.getSystem().getStringArray(R.array.currency_arrays)
|
||||||
|
|
||||||
override fun getWidgetConversionPairs(appWidgetId: Int): Pair<String?, String?> =
|
override fun getWidgetConversionPairs(appWidgetId: Int): Pair<String?, String?> =
|
||||||
prefs.getWidgetConversionPair(appWidgetId)
|
prefs.getWidgetConversionPair(appWidgetId)
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.appttude.h_mal.easycc.helper
|
||||||
|
|
||||||
|
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||||
|
import com.appttude.h_mal.easycc.models.CurrencyModelInterface
|
||||||
|
import java.lang.Exception
|
||||||
|
|
||||||
|
class CurrencyDataHelper (
|
||||||
|
val repository: Repository
|
||||||
|
){
|
||||||
|
|
||||||
|
suspend fun getDataFromApi(from: String, to: String): CurrencyModelInterface{
|
||||||
|
return try {
|
||||||
|
repository.getDataFromApi(from, to)
|
||||||
|
}catch (e: Exception){
|
||||||
|
e.printStackTrace()
|
||||||
|
repository.getBackupDataFromApi(from, to)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.appttude.h_mal.easycc.helper
|
||||||
|
|
||||||
|
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||||
|
import com.appttude.h_mal.easycc.models.CurrencyModel
|
||||||
|
import com.appttude.h_mal.easycc.utils.trimToThree
|
||||||
|
|
||||||
|
import kotlin.Exception
|
||||||
|
|
||||||
|
class WidgetHelper (
|
||||||
|
val helper: CurrencyDataHelper,
|
||||||
|
val repository: Repository
|
||||||
|
){
|
||||||
|
|
||||||
|
suspend fun getWidgetData(): CurrencyModel? {
|
||||||
|
try {
|
||||||
|
val pair = repository.getConversionPair()
|
||||||
|
val s1 = pair.first?.trimToThree() ?: return null
|
||||||
|
val s2 = pair.second?.trimToThree() ?: return null
|
||||||
|
|
||||||
|
return helper.getDataFromApi(s1, s2).getCurrencyModel()
|
||||||
|
}catch (e: Exception){
|
||||||
|
e.printStackTrace()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeWidgetData(id: Int){
|
||||||
|
repository.removeWidgetConversionPairs(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.appttude.h_mal.easycc.models
|
||||||
|
|
||||||
|
data class CurrencyModel(
|
||||||
|
val from: String?,
|
||||||
|
val to: String?,
|
||||||
|
var rate: Double = 0.0
|
||||||
|
)
|
||||||
|
|
||||||
|
interface CurrencyModelInterface{
|
||||||
|
fun getCurrencyModel(): CurrencyModel
|
||||||
|
}
|
||||||
@@ -22,31 +22,30 @@ import org.kodein.di.generic.instance
|
|||||||
|
|
||||||
class MainActivity : AppCompatActivity(), KodeinAware, View.OnClickListener {
|
class MainActivity : AppCompatActivity(), KodeinAware, View.OnClickListener {
|
||||||
|
|
||||||
override val kodein by kodein()
|
|
||||||
// Retrieve MainViewModelFactory via dependency injection
|
// Retrieve MainViewModelFactory via dependency injection
|
||||||
|
override val kodein by kodein()
|
||||||
private val factory: MainViewModelFactory by instance()
|
private val factory: MainViewModelFactory by instance()
|
||||||
|
|
||||||
companion object {
|
lateinit var viewModel: MainViewModel
|
||||||
lateinit var viewModel: MainViewModel
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
// Keyboard is not overlapping views
|
// Keyboard is not overlapping views
|
||||||
this.window.setSoftInputMode(
|
window.setSoftInputMode(
|
||||||
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN or
|
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN or
|
||||||
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
|
||||||
|
)
|
||||||
|
|
||||||
viewModel = ViewModelProviders.of(this, factory)
|
viewModel = ViewModelProviders.of(this, factory)
|
||||||
.get(MainViewModel::class.java)
|
.get(MainViewModel::class.java)
|
||||||
|
|
||||||
// Bind viewmodel to layout with view binding
|
// Bind viewmodel to layout with view binding
|
||||||
DataBindingUtil
|
DataBindingUtil
|
||||||
.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
|
.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
|
||||||
.apply {
|
.apply {
|
||||||
viewmodel = viewModel
|
viewmodel = viewModel
|
||||||
lifecycleOwner = this@MainActivity
|
lifecycleOwner = this@MainActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModel.initiate(intent.extras)
|
viewModel.initiate(intent.extras)
|
||||||
|
|
||||||
@@ -56,24 +55,23 @@ class MainActivity : AppCompatActivity(), KodeinAware, View.OnClickListener {
|
|||||||
|
|
||||||
private fun setUpObservers() {
|
private fun setUpObservers() {
|
||||||
viewModel.operationStartedListener.observe(this, Observer {
|
viewModel.operationStartedListener.observe(this, Observer {
|
||||||
// Show progress bar
|
|
||||||
progressBar.hideView(false)
|
progressBar.hideView(false)
|
||||||
})
|
})
|
||||||
viewModel.operationFinishedListener.observe(this, Observer { pair ->
|
viewModel.operationFinishedListener.observe(this, Observer { pair ->
|
||||||
// hide progress bar
|
// hide progress bar
|
||||||
progressBar.hideView(true)
|
progressBar.hideView(true)
|
||||||
if (pair.first){
|
if (pair.first) {
|
||||||
// Operation was successful remove text in EditTexts
|
// Operation was successful remove text in EditTexts
|
||||||
bottomInsertValues.clearEditText()
|
bottomInsertValues.clearEditText()
|
||||||
topInsertValue.clearEditText()
|
topInsertValue.clearEditText()
|
||||||
}else{
|
} else {
|
||||||
// Display Toast with error message returned from Viewmodel
|
// Display Toast with error message returned from Viewmodel
|
||||||
pair.second?.let { displayToast(it) }
|
pair.second?.let { displayToast(it) }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUpListeners(){
|
private fun setUpListeners() {
|
||||||
topInsertValue.addTextChangedListener(textWatcherClass)
|
topInsertValue.addTextChangedListener(textWatcherClass)
|
||||||
bottomInsertValues.addTextChangedListener(textWatcherClass2)
|
bottomInsertValues.addTextChangedListener(textWatcherClass2)
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package com.appttude.h_mal.easycc.ui.main
|
package com.appttude.h_mal.easycc.ui.main
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import com.appttude.h_mal.easycc.data.repository.Repository
|
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||||
|
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
||||||
import com.appttude.h_mal.easycc.utils.toTwoDpString
|
import com.appttude.h_mal.easycc.utils.toTwoDpString
|
||||||
|
import com.appttude.h_mal.easycc.utils.trimToThree
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -14,9 +15,8 @@ import java.io.IOException
|
|||||||
/**
|
/**
|
||||||
* ViewModel for the task Main Activity Screen
|
* ViewModel for the task Main Activity Screen
|
||||||
*/
|
*/
|
||||||
private const val TAG = "MainViewModel"
|
|
||||||
class MainViewModel(
|
class MainViewModel(
|
||||||
// Repository injected via Viewmodel factory
|
private val currencyDataHelper: CurrencyDataHelper,
|
||||||
private val repository: Repository
|
private val repository: Repository
|
||||||
) : ViewModel(){
|
) : ViewModel(){
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ class MainViewModel(
|
|||||||
private fun getExchangeRate(){
|
private fun getExchangeRate(){
|
||||||
operationStartedListener.postValue(true)
|
operationStartedListener.postValue(true)
|
||||||
|
|
||||||
// Null check on currency values
|
// view binded exchange rates selected null checked
|
||||||
if (rateIdFrom.isNullOrEmpty() || rateIdTo.isNullOrEmpty()){
|
if (rateIdFrom.isNullOrEmpty() || rateIdTo.isNullOrEmpty()){
|
||||||
operationFinishedListener.postValue(Pair(false, "Select currencies"))
|
operationFinishedListener.postValue(Pair(false, "Select currencies"))
|
||||||
return
|
return
|
||||||
@@ -48,18 +48,19 @@ class MainViewModel(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open Coroutine on IO thread to carry out async task
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
try {
|
try {
|
||||||
// Non-null assertion (!!) as values have been null checked and have not changed
|
// Non-null assertion (!!) as values have been null checked and have not changed
|
||||||
val exchangeResponse = repository.getData(rateIdFrom!!, rateIdTo!!)
|
val exchangeResponse = currencyDataHelper.getDataFromApi(
|
||||||
|
rateIdFrom!!.trimToThree(),
|
||||||
|
rateIdTo!!.trimToThree()
|
||||||
|
)
|
||||||
|
|
||||||
exchangeResponse.results?.iterator()?.next()?.value?.let {
|
exchangeResponse.getCurrencyModel().let {
|
||||||
// Response Successful and contains @param CurrencyObject
|
conversionRate = it.rate
|
||||||
repository.setConversionPair(rateIdFrom!!, rateIdTo!!)
|
repository.setConversionPair(rateIdFrom!!, rateIdTo!!)
|
||||||
|
|
||||||
operationFinishedListener.postValue(Pair(true, null))
|
operationFinishedListener.postValue(Pair(true, null))
|
||||||
conversionRate = it.value
|
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
}catch(e: IOException){
|
}catch(e: IOException){
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.appttude.h_mal.easycc.ui.main
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import com.appttude.h_mal.easycc.data.repository.RepositoryImpl
|
import com.appttude.h_mal.easycc.data.repository.RepositoryImpl
|
||||||
|
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Viewmodel factory for [MainViewModel]
|
* Viewmodel factory for [MainViewModel]
|
||||||
@@ -10,10 +11,11 @@ import com.appttude.h_mal.easycc.data.repository.RepositoryImpl
|
|||||||
*/
|
*/
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
class MainViewModelFactory (
|
class MainViewModelFactory (
|
||||||
private val repository: RepositoryImpl
|
private val repository: RepositoryImpl,
|
||||||
|
private val helper: CurrencyDataHelper
|
||||||
): ViewModelProvider.NewInstanceFactory(){
|
): ViewModelProvider.NewInstanceFactory(){
|
||||||
|
|
||||||
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||||
return MainViewModel(repository) as T
|
return MainViewModel(helper, repository) as T
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ class WidgetViewModel(
|
|||||||
private val repository: Repository
|
private val repository: Repository
|
||||||
) : ViewModel(){
|
) : ViewModel(){
|
||||||
|
|
||||||
private val defaultCurrency: String by lazy { repository.getArrayList()[0] }
|
private val defaultCurrency: String by lazy { repository.getCurrenciesList()[0] }
|
||||||
var appWidgetId: Int? = null
|
var appWidgetId: Int? = null
|
||||||
|
|
||||||
// data binding to @R.layout.currency_app_widget_configure
|
// data binding to @R.layout.currency_app_widget_configure
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.appttude.h_mal.easycc.widget
|
package com.appttude.h_mal.easycc.widget
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.appwidget.AppWidgetManager
|
import android.appwidget.AppWidgetManager
|
||||||
import android.appwidget.AppWidgetProvider
|
import android.appwidget.AppWidgetProvider
|
||||||
@@ -8,9 +9,8 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
import android.widget.Toast
|
|
||||||
import com.appttude.h_mal.easycc.R
|
import com.appttude.h_mal.easycc.R
|
||||||
import com.appttude.h_mal.easycc.data.repository.RepositoryImpl
|
import com.appttude.h_mal.easycc.helper.WidgetHelper
|
||||||
import com.appttude.h_mal.easycc.ui.main.MainActivity
|
import com.appttude.h_mal.easycc.ui.main.MainActivity
|
||||||
import com.appttude.h_mal.easycc.utils.transformIntToArray
|
import com.appttude.h_mal.easycc.utils.transformIntToArray
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -19,7 +19,6 @@ import kotlinx.coroutines.launch
|
|||||||
import org.kodein.di.KodeinAware
|
import org.kodein.di.KodeinAware
|
||||||
import org.kodein.di.LateInitKodein
|
import org.kodein.di.LateInitKodein
|
||||||
import org.kodein.di.generic.instance
|
import org.kodein.di.generic.instance
|
||||||
import java.io.IOException
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,15 +26,21 @@ import java.io.IOException
|
|||||||
* App Widget Configuration implemented in [CurrencyAppWidgetConfigureActivityKotlin]
|
* App Widget Configuration implemented in [CurrencyAppWidgetConfigureActivityKotlin]
|
||||||
*/
|
*/
|
||||||
private const val TAG = "CurrencyAppWidgetKotlin"
|
private const val TAG = "CurrencyAppWidgetKotlin"
|
||||||
class CurrencyAppWidgetKotlin : AppWidgetProvider() {
|
|
||||||
|
class CurrencyAppWidgetKotlin : AppWidgetProvider() {
|
||||||
|
|
||||||
//DI with kodein to use in CurrencyAppWidgetKotlin
|
//DI with kodein to use in CurrencyAppWidgetKotlin
|
||||||
private val kodein = LateInitKodein()
|
private val kodein = LateInitKodein()
|
||||||
private val repository : RepositoryImpl by kodein.instance()
|
private val repository: WidgetHelper by kodein.instance()
|
||||||
|
|
||||||
//update trigger either on timed update or from from first start
|
//update trigger either on timed update or from from first start
|
||||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
override fun onUpdate(
|
||||||
Log.i(TAG,"onUpdate() appWidgetIds = ${appWidgetIds.size}")
|
context: Context,
|
||||||
|
appWidgetManager: AppWidgetManager,
|
||||||
|
appWidgetIds: IntArray
|
||||||
|
) {
|
||||||
|
kodein.baseKodein = (context.applicationContext as KodeinAware).kodein
|
||||||
|
Log.i(TAG, "onUpdate() appWidgetIds = ${appWidgetIds.size}")
|
||||||
// There may be multiple widgets active, so update all of them
|
// There may be multiple widgets active, so update all of them
|
||||||
for (appWidgetId in appWidgetIds) {
|
for (appWidgetId in appWidgetIds) {
|
||||||
updateAppWidget(context, appWidgetManager, appWidgetId)
|
updateAppWidget(context, appWidgetManager, appWidgetId)
|
||||||
@@ -44,75 +49,66 @@ class CurrencyAppWidgetKotlin : AppWidgetProvider() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||||
|
kodein.baseKodein = (context.applicationContext as KodeinAware).kodein
|
||||||
// When the user deletes the widget, delete the preference associated with it.
|
// When the user deletes the widget, delete the preference associated with it.
|
||||||
for (appWidgetId in appWidgetIds) {
|
for (appWidgetId in appWidgetIds) {
|
||||||
repository.removeWidgetConversionPairs(appWidgetId)
|
repository.removeWidgetData(appWidgetId)
|
||||||
}
|
}
|
||||||
super.onDeleted(context, appWidgetIds)
|
super.onDeleted(context, appWidgetIds)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onEnabled(context: Context) {
|
override fun onEnabled(context: Context) {
|
||||||
|
kodein.baseKodein = (context.applicationContext as KodeinAware).kodein
|
||||||
// Enter relevant functionality for when the first widget is created
|
// Enter relevant functionality for when the first widget is created
|
||||||
AppWidgetManager.getInstance(context).apply {
|
AppWidgetManager.getInstance(context).apply {
|
||||||
val thisAppWidget = ComponentName(context.packageName, CurrencyAppWidgetKotlin::class.java.name)
|
val thisAppWidget =
|
||||||
|
ComponentName(context.packageName, CurrencyAppWidgetKotlin::class.java.name)
|
||||||
val appWidgetIds = getAppWidgetIds(thisAppWidget)
|
val appWidgetIds = getAppWidgetIds(thisAppWidget)
|
||||||
onUpdate(context, this, appWidgetIds)
|
onUpdate(context, this, appWidgetIds)
|
||||||
}
|
}
|
||||||
super.onEnabled(context)
|
super.onEnabled(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
|
||||||
if (context == null){ return }
|
|
||||||
kodein.baseKodein = (context.applicationContext as KodeinAware).kodein
|
|
||||||
|
|
||||||
super.onReceive(context, intent)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onDisabled(context: Context) {
|
override fun onDisabled(context: Context) {
|
||||||
kodein.baseKodein = (context.applicationContext as KodeinAware).kodein
|
kodein.baseKodein = (context.applicationContext as KodeinAware).kodein
|
||||||
// Enter relevant functionality for when the last widget is disabled
|
// Enter relevant functionality for when the last widget is disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
|
private fun updateAppWidget(
|
||||||
val stringList = repository.getWidgetConversionPairs(appWidgetId)
|
context: Context,
|
||||||
val s1 = stringList.first
|
appWidgetManager: AppWidgetManager,
|
||||||
val s2 = stringList.second
|
appWidgetId: Int
|
||||||
|
) {
|
||||||
// Construct the RemoteViews object
|
// Construct the RemoteViews object
|
||||||
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 {
|
||||||
try {
|
val exchangeResponse = repository.getWidgetData()
|
||||||
val response = repository.getData(s1!!.substring(0,3),s2!!.substring(0,3))
|
|
||||||
|
|
||||||
response.results?.iterator()?.next()?.value?.let {
|
exchangeResponse?.let {
|
||||||
val titleString = "${it.fr}${it.to}"
|
val titleString = "${it.from}${it.to}"
|
||||||
views.setTextViewText(R.id.exchangeName, titleString)
|
views.setTextViewText(R.id.exchangeName, titleString)
|
||||||
views.setTextViewText(R.id.exchangeRate, it.value.toString())
|
views.setTextViewText(R.id.exchangeRate, it.rate.toString())
|
||||||
}
|
|
||||||
}catch (io : IOException){
|
setUpdateIntent(context, appWidgetId).let { intent ->
|
||||||
Log.i("WidgetClass",io.message ?: "Failed")
|
|
||||||
Toast.makeText(context,io.message, Toast.LENGTH_LONG).show()
|
|
||||||
}finally {
|
|
||||||
setUpdateIntent(context, appWidgetId).let {
|
|
||||||
//set the pending intent to the icon
|
//set the pending intent to the icon
|
||||||
views.setImageViewResource(R.id.refresh_icon, R.drawable.ic_refresh_white_24dp)
|
views.setImageViewResource(R.id.refresh_icon, R.drawable.ic_refresh_white_24dp)
|
||||||
views.setOnClickPendingIntent(R.id.refresh_icon, it)
|
views.setOnClickPendingIntent(R.id.refresh_icon, intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
val clickIntentTemplate = clickingIntent(context, s1, s2)
|
val clickIntentTemplate = clickingIntent(context)
|
||||||
|
|
||||||
val configPendingIntent =
|
val configPendingIntent =
|
||||||
PendingIntent.getActivity(
|
PendingIntent.getActivity(
|
||||||
context, appWidgetId, clickIntentTemplate,
|
context, appWidgetId, clickIntentTemplate,
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT)
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
|
)
|
||||||
views.setOnClickPendingIntent(R.id.widget_view, configPendingIntent)
|
views.setOnClickPendingIntent(R.id.widget_view, configPendingIntent)
|
||||||
|
|
||||||
// Instruct the widget manager to update the widget
|
|
||||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Instruct the widget manager to update the widget
|
||||||
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -120,22 +116,26 @@ class CurrencyAppWidgetKotlin : AppWidgetProvider() {
|
|||||||
private fun setUpdateIntent(context: Context, appWidgetId: Int): PendingIntent? {
|
private fun setUpdateIntent(context: Context, appWidgetId: Int): PendingIntent? {
|
||||||
//Create update intent for refresh icon
|
//Create update intent for refresh icon
|
||||||
val updateIntent = Intent(
|
val updateIntent = Intent(
|
||||||
context, CurrencyAppWidgetKotlin::class.java).apply {
|
context, CurrencyAppWidgetKotlin::class.java
|
||||||
|
).apply {
|
||||||
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
|
||||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, transformIntToArray(appWidgetId))
|
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, transformIntToArray(appWidgetId))
|
||||||
}
|
}
|
||||||
//add previous intent to this pending intent
|
//add previous intent to this pending intent
|
||||||
return PendingIntent.getBroadcast(
|
return PendingIntent.getBroadcast(
|
||||||
context,
|
context,
|
||||||
appWidgetId,
|
appWidgetId,
|
||||||
updateIntent,
|
updateIntent,
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT)
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clickingIntent(
|
private fun clickingIntent(
|
||||||
context: Context,
|
context: Context
|
||||||
s1: String?, s2: String?
|
|
||||||
): Intent {
|
): Intent {
|
||||||
|
val pair = repository.repository.getConversionPair()
|
||||||
|
val s1 = pair.first
|
||||||
|
val s2 = pair.second
|
||||||
return Intent(context, MainActivity::class.java).apply {
|
return Intent(context, MainActivity::class.java).apply {
|
||||||
action = Intent.ACTION_MAIN
|
action = Intent.ACTION_MAIN
|
||||||
addCategory(Intent.CATEGORY_LAUNCHER)
|
addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
@@ -144,5 +144,27 @@ class CurrencyAppWidgetKotlin : AppWidgetProvider() {
|
|||||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun <T: Activity> clickingIntent(
|
||||||
|
context: Context,
|
||||||
|
activity: Class<T>,
|
||||||
|
vararg argPairs: Pair<String, Any?>
|
||||||
|
): Intent {
|
||||||
|
|
||||||
|
return Intent(context, activity::class.java).apply {
|
||||||
|
action = Intent.ACTION_MAIN
|
||||||
|
addCategory(Intent.CATEGORY_LAUNCHER)
|
||||||
|
argPairs.forEach {
|
||||||
|
putExtra(it.first, it.second)
|
||||||
|
}
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T: Any> Intent.putExtra(s: String, second: T?) {
|
||||||
|
when(second){
|
||||||
|
is String -> putExtra(s,second)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
app/src/main/res/drawable/ic_background.xml
Normal file
21
app/src/main/res/drawable/ic_background.xml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:aapt="http://schemas.android.com/aapt"
|
||||||
|
android:width="108dp"
|
||||||
|
android:height="192dp"
|
||||||
|
android:viewportWidth="1080"
|
||||||
|
android:viewportHeight="1920">
|
||||||
|
<path
|
||||||
|
android:pathData="M0,0L1080,0L1080,1920L0,1920L0,0Z">
|
||||||
|
<aapt:attr name="android:fillColor">
|
||||||
|
<gradient
|
||||||
|
android:startY="0"
|
||||||
|
android:startX="1080"
|
||||||
|
android:endY="1920"
|
||||||
|
android:endX="0"
|
||||||
|
android:type="linear">
|
||||||
|
<item android:offset="0" android:color="#FF315659"/>
|
||||||
|
<item android:offset="1" android:color="#FF2978A0"/>
|
||||||
|
</gradient>
|
||||||
|
</aapt:attr>
|
||||||
|
</path>
|
||||||
|
</vector>
|
||||||
126
app/src/main/res/layout-v26/activity_main.xml
Normal file
126
app/src/main/res/layout-v26/activity_main.xml
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<data>
|
||||||
|
|
||||||
|
<variable
|
||||||
|
name="viewmodel"
|
||||||
|
type="com.appttude.h_mal.easycc.ui.main.MainViewModel" />
|
||||||
|
</data>
|
||||||
|
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@drawable/ic_background"
|
||||||
|
android:focusable="false"
|
||||||
|
android:focusableInTouchMode="true"
|
||||||
|
android:orientation="vertical"
|
||||||
|
tools:context=".ui.main.MainActivity">
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_margin="12dp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintLeft_toLeftOf="parent"
|
||||||
|
app:layout_constraintRight_toRightOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/whole_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="18dp"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<androidx.cardview.widget.CardView style="@style/cardview_theme">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/currency_one"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_margin="12dp"
|
||||||
|
android:autoSizeMaxTextSize="12dp"
|
||||||
|
android:tag="top"
|
||||||
|
android:text="@={viewmodel.rateIdFrom}"
|
||||||
|
android:textColor="@color/colour_five"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
</androidx.cardview.widget.CardView>
|
||||||
|
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/topInsertValue"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="6dp"
|
||||||
|
android:layout_marginBottom="6dp"
|
||||||
|
android:background="@drawable/round_edit_text"
|
||||||
|
android:ems="10"
|
||||||
|
android:hint="insert value one"
|
||||||
|
android:inputType="numberDecimal"
|
||||||
|
android:padding="12dp"
|
||||||
|
android:selectAllOnFocus="true"
|
||||||
|
android:tag="from"
|
||||||
|
android:textColorHighlight="#608d91" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical">
|
||||||
|
|
||||||
|
<androidx.cardview.widget.CardView style="@style/cardview_theme">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/currency_two"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_margin="12dp"
|
||||||
|
android:tag="bottom"
|
||||||
|
android:text="@={viewmodel.rateIdTo}"
|
||||||
|
android:textColor="@color/colour_five"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
</androidx.cardview.widget.CardView>
|
||||||
|
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/bottomInsertValues"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="6dp"
|
||||||
|
android:layout_weight="7"
|
||||||
|
android:background="@drawable/round_edit_text"
|
||||||
|
android:ems="10"
|
||||||
|
android:hint="insert value two"
|
||||||
|
android:inputType="numberDecimal"
|
||||||
|
android:padding="12dp"
|
||||||
|
android:selectAllOnFocus="true"
|
||||||
|
android:tag="to"
|
||||||
|
android:textColorHighlight="#608d91" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progressBar"
|
||||||
|
style="?android:attr/progressBarStyle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerInParent="true"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:indeterminateTint="@color/colour_four"
|
||||||
|
android:indeterminateTintMode="src_atop"
|
||||||
|
android:visibility="gone" />
|
||||||
|
</RelativeLayout>
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
||||||
|
</layout>
|
||||||
@@ -1,119 +1,128 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<data>
|
<data>
|
||||||
|
|
||||||
<variable
|
<variable
|
||||||
name="viewmodel"
|
name="viewmodel"
|
||||||
type="com.appttude.h_mal.easycc.ui.main.MainViewModel" />
|
type="com.appttude.h_mal.easycc.ui.main.MainViewModel" />
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
<RelativeLayout
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical"
|
android:background="@drawable/ic_background"
|
||||||
android:focusable="false"
|
android:focusable="false"
|
||||||
android:focusableInTouchMode="true"
|
android:focusableInTouchMode="true"
|
||||||
|
android:orientation="vertical"
|
||||||
tools:context=".ui.main.MainActivity">
|
tools:context=".ui.main.MainActivity">
|
||||||
|
|
||||||
<RelativeLayout
|
<LinearLayout
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerInParent="true"
|
app:layout_constraintWidth_percent=".9"
|
||||||
android:layout_margin="12dp">
|
android:layout_marginBottom="9dp"
|
||||||
|
android:orientation="vertical"
|
||||||
|
app:layout_constraintBottom_toTopOf="@id/middle"
|
||||||
|
app:layout_constraintLeft_toLeftOf="parent"
|
||||||
|
app:layout_constraintRight_toRightOf="parent">
|
||||||
|
|
||||||
<LinearLayout
|
<androidx.cardview.widget.CardView style="@style/cardview_theme">
|
||||||
android:id="@+id/whole_view"
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/currency_one"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_margin="12dp"
|
||||||
|
android:tag="top"
|
||||||
|
android:text="@={viewmodel.rateIdFrom}"
|
||||||
|
android:textColor="@color/colour_five"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
</androidx.cardview.widget.CardView>
|
||||||
|
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/topInsertValue"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="vertical">
|
android:layout_marginTop="6dp"
|
||||||
|
android:layout_marginBottom="6dp"
|
||||||
|
android:background="@drawable/round_edit_text"
|
||||||
|
android:ems="10"
|
||||||
|
android:hint="insert value one"
|
||||||
|
android:inputType="numberDecimal"
|
||||||
|
android:padding="12dp"
|
||||||
|
android:selectAllOnFocus="true"
|
||||||
|
android:tag="from"
|
||||||
|
android:textColorHighlight="#608d91" />
|
||||||
|
|
||||||
<LinearLayout
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
|
<android.widget.Space
|
||||||
|
android:id="@+id/middle"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="1dp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintWidth_percent=".9"
|
||||||
|
android:layout_marginTop="9dp"
|
||||||
|
android:orientation="vertical"
|
||||||
|
app:layout_constraintLeft_toLeftOf="parent"
|
||||||
|
app:layout_constraintRight_toRightOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/middle">
|
||||||
|
|
||||||
|
<androidx.cardview.widget.CardView style="@style/cardview_theme">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/currency_two"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="18dp"
|
android:layout_margin="12dp"
|
||||||
android:orientation="vertical">
|
android:tag="bottom"
|
||||||
<androidx.cardview.widget.CardView
|
android:text="@={viewmodel.rateIdTo}"
|
||||||
style="@style/cardview_theme">
|
android:textColor="@color/colour_five"
|
||||||
|
android:textSize="18sp" />
|
||||||
<TextView
|
</androidx.cardview.widget.CardView>
|
||||||
android:tag="top"
|
|
||||||
android:id="@+id/currency_one"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_margin="12dp"
|
|
||||||
android:text="@={viewmodel.rateIdFrom}"
|
|
||||||
android:textColor="@color/colour_five"
|
|
||||||
android:textSize="18sp" />
|
|
||||||
</androidx.cardview.widget.CardView>
|
|
||||||
|
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/topInsertValue"
|
android:id="@+id/bottomInsertValues"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginBottom="6dp"
|
|
||||||
android:layout_marginTop="6dp"
|
|
||||||
android:background="@drawable/round_edit_text"
|
|
||||||
android:ems="10"
|
|
||||||
android:hint="insert value one"
|
|
||||||
android:textColorHighlight="#608d91"
|
|
||||||
android:inputType="numberDecimal"
|
|
||||||
android:padding="12dp"
|
|
||||||
android:tag="from"
|
|
||||||
android:selectAllOnFocus="true" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<LinearLayout
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:orientation="vertical">
|
|
||||||
|
|
||||||
<androidx.cardview.widget.CardView style="@style/cardview_theme">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/currency_two"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_margin="12dp"
|
|
||||||
android:text="@={viewmodel.rateIdTo}"
|
|
||||||
android:tag="bottom"
|
|
||||||
android:textColor="@color/colour_five"
|
|
||||||
android:textSize="18sp" />
|
|
||||||
</androidx.cardview.widget.CardView>
|
|
||||||
|
|
||||||
|
|
||||||
<EditText
|
|
||||||
android:id="@+id/bottomInsertValues"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_marginTop="6dp"
|
|
||||||
android:layout_weight="7"
|
|
||||||
android:background="@drawable/round_edit_text"
|
|
||||||
android:ems="10"
|
|
||||||
android:hint="insert value two"
|
|
||||||
android:textColorHighlight="#608d91"
|
|
||||||
android:inputType="numberDecimal"
|
|
||||||
android:padding="12dp"
|
|
||||||
android:tag="to"
|
|
||||||
android:selectAllOnFocus="true" />
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
|
|
||||||
<ProgressBar
|
|
||||||
android:id="@+id/progressBar"
|
|
||||||
style="?android:attr/progressBarStyle"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_centerInParent="true"
|
android:layout_marginTop="6dp"
|
||||||
android:indeterminate="true"
|
android:layout_weight="7"
|
||||||
android:indeterminateTint="@color/colour_four"
|
android:background="@drawable/round_edit_text"
|
||||||
android:indeterminateTintMode="src_atop"
|
android:ems="10"
|
||||||
android:visibility="gone"/>
|
android:hint="insert value two"
|
||||||
</RelativeLayout>
|
android:inputType="numberDecimal"
|
||||||
</RelativeLayout>
|
android:padding="12dp"
|
||||||
|
android:selectAllOnFocus="true"
|
||||||
|
android:tag="to"
|
||||||
|
android:textColorHighlight="#608d91" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progressBar"
|
||||||
|
style="?android:attr/progressBarStyle"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:indeterminate="true"
|
||||||
|
android:indeterminateTint="@color/colour_four"
|
||||||
|
android:indeterminateTintMode="src_atop"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintLeft_toLeftOf="parent"
|
||||||
|
app:layout_constraintRight_toRightOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
|
||||||
</layout>
|
</layout>
|
||||||
|
|||||||
@@ -7,17 +7,17 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:background="#4D000000">
|
android:background="#4D000000">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<RelativeLayout
|
<RelativeLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="wrap_content">
|
||||||
android:layout_weight="1"
|
|
||||||
android:layout_marginBottom="3dp">
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/exchangeName"
|
android:id="@+id/exchangeName"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:autoSizeMaxTextSize="100sp"
|
android:autoSizeMaxTextSize="20sp"
|
||||||
android:autoSizeMinTextSize="6sp"
|
android:autoSizeMinTextSize="6sp"
|
||||||
android:autoSizeStepGranularity="2sp"
|
android:autoSizeStepGranularity="2sp"
|
||||||
android:autoSizeTextType="uniform"
|
android:autoSizeTextType="uniform"
|
||||||
@@ -26,8 +26,8 @@
|
|||||||
tools:text="AUDGBP" />
|
tools:text="AUDGBP" />
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/refresh_icon"
|
android:id="@+id/refresh_icon"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="18dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="18dp"
|
||||||
tools:src="@drawable/ic_refresh_white_24dp"
|
tools:src="@drawable/ic_refresh_white_24dp"
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:layout_alignParentRight="true"
|
android:layout_alignParentRight="true"
|
||||||
@@ -39,13 +39,26 @@
|
|||||||
android:id="@+id/exchangeRate"
|
android:id="@+id/exchangeRate"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
android:layout_weight="2"
|
android:layout_weight="1"
|
||||||
|
android:layout_marginLeft="6dp"
|
||||||
|
android:layout_marginRight="6dp"
|
||||||
android:autoSizeMaxTextSize="100sp"
|
android:autoSizeMaxTextSize="100sp"
|
||||||
android:autoSizeMinTextSize="6sp"
|
android:autoSizeMinTextSize="6sp"
|
||||||
android:autoSizeStepGranularity="2sp"
|
android:autoSizeStepGranularity="2sp"
|
||||||
android:autoSizeTextType="uniform"
|
android:autoSizeTextType="uniform"
|
||||||
|
android:maxLines="1"
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:textColor="#ffffff"
|
android:textColor="#ffffff"
|
||||||
android:textStyle="bold"
|
android:textStyle="bold"
|
||||||
tools:text="0.526462" />
|
tools:text="0.52646215" />
|
||||||
|
|
||||||
|
<!-- <TextView-->
|
||||||
|
<!-- android:id="@+id/lastUpdated"-->
|
||||||
|
<!-- android:layout_width="match_parent"-->
|
||||||
|
<!-- android:layout_height="wrap_content"-->
|
||||||
|
<!-- android:gravity="center"-->
|
||||||
|
<!-- android:textSize="8sp"-->
|
||||||
|
<!-- android:textColor="#ffffff"-->
|
||||||
|
<!-- android:text=" "-->
|
||||||
|
<!-- tools:text="Last updated: 03:18 Wed" />-->
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
<item name="colorPrimary">@color/colour_two</item>
|
<item name="colorPrimary">@color/colour_two</item>
|
||||||
<item name="colorPrimaryDark">@color/colour_two</item>
|
<item name="colorPrimaryDark">@color/colour_two</item>
|
||||||
<item name="colorAccent">@color/colour_two</item>
|
<item name="colorAccent">@color/colour_two</item>
|
||||||
<item name="android:windowBackground">@drawable/gradient_colour</item>
|
<item name="android:windowBackground">@drawable/ic_background</item>
|
||||||
<item name="windowActionBar">false</item>
|
<item name="windowActionBar">false</item>
|
||||||
<item name="windowNoTitle">true</item>
|
<item name="windowNoTitle">true</item>
|
||||||
<item name="android:editTextColor">@color/colour_five</item>
|
<item name="android:editTextColor">@color/colour_five</item>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package com.appttude.h_mal.easycc.repository
|
package com.appttude.h_mal.easycc.repository
|
||||||
|
|
||||||
import android.content.Context
|
import com.appttude.h_mal.easycc.data.network.api.BackupCurrencyApi
|
||||||
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
||||||
|
import com.appttude.h_mal.easycc.data.network.response.CurrencyResponse
|
||||||
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
||||||
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
||||||
import com.appttude.h_mal.easycc.data.repository.Repository
|
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||||
@@ -29,31 +30,31 @@ class RepositoryNetworkTest{
|
|||||||
@Mock
|
@Mock
|
||||||
lateinit var api: CurrencyApi
|
lateinit var api: CurrencyApi
|
||||||
@Mock
|
@Mock
|
||||||
lateinit var prefs: PreferenceProvider
|
lateinit var apiBackup: BackupCurrencyApi
|
||||||
@Mock
|
@Mock
|
||||||
lateinit var context: Context
|
lateinit var prefs: PreferenceProvider
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
MockitoAnnotations.initMocks(this)
|
MockitoAnnotations.initMocks(this)
|
||||||
repository = RepositoryImpl(api, prefs, context)
|
repository = RepositoryImpl(api, apiBackup, prefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun getRateFromApi_positiveResponse() = runBlocking {
|
fun getRateFromApi_positiveResponse() = runBlocking {
|
||||||
//GIVEN - Create query string
|
//GIVEN - Create query string
|
||||||
val s1 = "AUD - Australian Dollar"
|
val s1 = "AUD"
|
||||||
val s2 = "GBP - British Pound"
|
val s2 = "GBP"
|
||||||
val query = convertPairsListToString(s1, s2)
|
|
||||||
//create a successful retrofit response
|
//create a successful retrofit response
|
||||||
val mockCurrencyResponse = mock(ResponseObject::class.java)
|
val mockCurrencyResponse = mock(ResponseObject::class.java)
|
||||||
val re = Response.success(mockCurrencyResponse)
|
val re = Response.success(mockCurrencyResponse)
|
||||||
|
|
||||||
//WHEN - loginApiRequest to return a successful response
|
//WHEN - loginApiRequest to return a successful response
|
||||||
Mockito.`when`(api.getCurrencyRate(query)).thenReturn(re)
|
val currencyPair = convertPairsListToString(s1, s2)
|
||||||
|
Mockito.`when`(api.getCurrencyRate(currencyPair)).thenReturn(re)
|
||||||
|
|
||||||
//THEN - the unwrapped login response contains the correct values
|
//THEN - the unwrapped login response contains the correct values
|
||||||
val currencyResponse = repository.getData(s1,s2)
|
val currencyResponse = repository.getDataFromApi(s1,s2)
|
||||||
assertNotNull(currencyResponse)
|
assertNotNull(currencyResponse)
|
||||||
assertEquals(currencyResponse, mockCurrencyResponse)
|
assertEquals(currencyResponse, mockCurrencyResponse)
|
||||||
}
|
}
|
||||||
@@ -61,20 +62,21 @@ class RepositoryNetworkTest{
|
|||||||
@Test
|
@Test
|
||||||
fun loginUser_negativeResponse() = runBlocking {
|
fun loginUser_negativeResponse() = runBlocking {
|
||||||
//GIVEN
|
//GIVEN
|
||||||
val s1 = "AUD - Australian Dollar"
|
val s1 = "AUD"
|
||||||
val s2 = "GBP - British Pound"
|
val s2 = "GBP"
|
||||||
val query = convertPairsListToString(s1, s2)
|
|
||||||
//mock retrofit error response
|
//mock retrofit error response
|
||||||
val mockBody = mock(ResponseBody::class.java)
|
val mockBody = mock(ResponseBody::class.java)
|
||||||
val mockRaw = mock(okhttp3.Response::class.java)
|
val mockRaw = mock(okhttp3.Response::class.java)
|
||||||
val re = Response.error<String>(mockBody, mockRaw)
|
val re = Response.error<String>(mockBody, mockRaw)
|
||||||
|
|
||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(api.getCurrencyRate(query)).thenAnswer { re }
|
val currencyPair = convertPairsListToString(s1, s2)
|
||||||
|
Mockito.`when`(api.getCurrencyRate(currencyPair)).thenAnswer { re }
|
||||||
|
|
||||||
//THEN - assert exception is not null
|
//THEN - assert exception is not null
|
||||||
val ioExceptionReturned = assertFailsWith<IOException> {
|
val ioExceptionReturned = assertFailsWith<IOException> {
|
||||||
repository.getData(s1, s2)
|
repository.getDataFromApi(s1, s2)
|
||||||
}
|
}
|
||||||
assertNotNull(ioExceptionReturned)
|
assertNotNull(ioExceptionReturned)
|
||||||
assertNotNull(ioExceptionReturned.message)
|
assertNotNull(ioExceptionReturned.message)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.appttude.h_mal.easycc.repository
|
package com.appttude.h_mal.easycc.repository
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import com.appttude.h_mal.easycc.data.network.api.BackupCurrencyApi
|
||||||
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
import com.appttude.h_mal.easycc.data.network.api.CurrencyApi
|
||||||
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
import com.appttude.h_mal.easycc.data.prefs.PreferenceProvider
|
||||||
import com.appttude.h_mal.easycc.data.repository.Repository
|
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||||
@@ -19,14 +20,14 @@ class RepositoryStorageTest {
|
|||||||
@Mock
|
@Mock
|
||||||
lateinit var api: CurrencyApi
|
lateinit var api: CurrencyApi
|
||||||
@Mock
|
@Mock
|
||||||
lateinit var prefs: PreferenceProvider
|
lateinit var apiBackup: BackupCurrencyApi
|
||||||
@Mock
|
@Mock
|
||||||
lateinit var context: Context
|
lateinit var prefs: PreferenceProvider
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
MockitoAnnotations.initMocks(this)
|
MockitoAnnotations.initMocks(this)
|
||||||
repository = RepositoryImpl(api, prefs, context)
|
repository = RepositoryImpl(api, apiBackup, prefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import android.util.Log
|
|||||||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
|
||||||
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
import com.appttude.h_mal.easycc.data.network.response.ResponseObject
|
||||||
import com.appttude.h_mal.easycc.data.repository.Repository
|
import com.appttude.h_mal.easycc.data.repository.Repository
|
||||||
import com.appttude.h_mal.easycc.models.CurrencyObject
|
import com.appttude.h_mal.easycc.helper.CurrencyDataHelper
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import com.appttude.h_mal.easycc.utils.observeOnce
|
import com.appttude.h_mal.easycc.utils.observeOnce
|
||||||
@@ -16,7 +16,6 @@ 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 kotlin.time.seconds
|
|
||||||
|
|
||||||
class MainViewModelTest {
|
class MainViewModelTest {
|
||||||
|
|
||||||
@@ -29,10 +28,13 @@ class MainViewModelTest {
|
|||||||
@Mock
|
@Mock
|
||||||
lateinit var repository: Repository
|
lateinit var repository: Repository
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
lateinit var helper: CurrencyDataHelper
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
MockitoAnnotations.initMocks(this)
|
MockitoAnnotations.initMocks(this)
|
||||||
viewModel = MainViewModel(repository)
|
viewModel = MainViewModel(helper, repository)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -46,7 +48,7 @@ class MainViewModelTest {
|
|||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(bundle.getString("parse_1")).thenReturn(currencyOne)
|
Mockito.`when`(bundle.getString("parse_1")).thenReturn(currencyOne)
|
||||||
Mockito.`when`(bundle.getString("parse_2")).thenReturn(currencyTwo)
|
Mockito.`when`(bundle.getString("parse_2")).thenReturn(currencyTwo)
|
||||||
Mockito.`when`(repository.getData(currencyOne, currencyTwo)).thenReturn(responseObject)
|
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo)).thenReturn(responseObject)
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.initiate(bundle)
|
viewModel.initiate(bundle)
|
||||||
@@ -55,7 +57,6 @@ class MainViewModelTest {
|
|||||||
}
|
}
|
||||||
viewModel.operationFinishedListener.observeOnce {
|
viewModel.operationFinishedListener.observeOnce {
|
||||||
assertEquals(true, it.first)
|
assertEquals(true, it.first)
|
||||||
Log.i("tag", "${it.first} ${it.second}")
|
|
||||||
assertNull(it.second)
|
assertNull(it.second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,7 +71,7 @@ class MainViewModelTest {
|
|||||||
|
|
||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(repository.getConversionPair()).thenReturn(pair)
|
Mockito.`when`(repository.getConversionPair()).thenReturn(pair)
|
||||||
Mockito.`when`(repository.getData(currencyOne, currencyTwo)).thenReturn(responseObject)
|
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo)).thenReturn(responseObject)
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.initiate(null)
|
viewModel.initiate(null)
|
||||||
@@ -138,7 +139,7 @@ class MainViewModelTest {
|
|||||||
val responseObject = mock(ResponseObject::class.java)
|
val responseObject = mock(ResponseObject::class.java)
|
||||||
|
|
||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(repository.getData(currencyOne, currencyTwo)).thenReturn(responseObject)
|
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo)).thenReturn(responseObject)
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.setCurrencyName(tag, currencyOne)
|
viewModel.setCurrencyName(tag, currencyOne)
|
||||||
@@ -162,7 +163,7 @@ class MainViewModelTest {
|
|||||||
val responseObject = mock(ResponseObject::class.java)
|
val responseObject = mock(ResponseObject::class.java)
|
||||||
|
|
||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(repository.getData(currencyOne, currencyTwo)).thenReturn(responseObject)
|
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo)).thenReturn(responseObject)
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.setCurrencyName(tag, currencyOne)
|
viewModel.setCurrencyName(tag, currencyOne)
|
||||||
@@ -184,7 +185,7 @@ class MainViewModelTest {
|
|||||||
val responseObject = mock(ResponseObject::class.java)
|
val responseObject = mock(ResponseObject::class.java)
|
||||||
|
|
||||||
//WHEN
|
//WHEN
|
||||||
Mockito.`when`(repository.getData(currencyOne, currencyTwo)).thenReturn(responseObject)
|
Mockito.`when`(repository.getDataFromApi(currencyOne, currencyTwo)).thenReturn(responseObject)
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.setCurrencyName(tag, currencyOne)
|
viewModel.setCurrencyName(tag, currencyOne)
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class WidgetViewModelTest {
|
|||||||
Mockito.`when`(repository.getWidgetConversionPairs(appId)).thenAnswer { pair }
|
Mockito.`when`(repository.getWidgetConversionPairs(appId)).thenAnswer { pair }
|
||||||
Mockito.`when`(repository.getWidgetConversionPairs(appId).first).thenReturn(null)
|
Mockito.`when`(repository.getWidgetConversionPairs(appId).first).thenReturn(null)
|
||||||
Mockito.`when`(repository.getWidgetConversionPairs(appId).second).thenReturn(null)
|
Mockito.`when`(repository.getWidgetConversionPairs(appId).second).thenReturn(null)
|
||||||
Mockito.`when`(repository.getArrayList()).thenReturn(array)
|
Mockito.`when`(repository.getCurrenciesList()).thenReturn(array)
|
||||||
|
|
||||||
//THEN
|
//THEN
|
||||||
viewModel.initiate(123)
|
viewModel.initiate(123)
|
||||||
|
|||||||
Reference in New Issue
Block a user