Skip to content

Instantly share code, notes, and snippets.

@ebubekirsezer

ebubekirsezer/Libraries.kt

Last active Aug 10, 2020
Embed
What would you like to do?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#F29F05"/>
<size android:width="80dp"
android:height="80dp"/>
</shape>
</item>
</selector>
package com.ess.currencyapp.model
import com.google.gson.annotations.SerializedName
data class Currency(
@SerializedName("base")
val base: String,
@SerializedName("date")
val date: String,
@SerializedName("rates")
val rates: Rates
)
data class Rates(
val AUD: Double,
val BGN: Double,
val BRL: Double,
val CAD: Double,
val CHF: Double,
val CNY: Double,
val CZK: Double,
val DKK: Double,
val GBP: Double,
val HKD: Double,
val HRK: Double,
val HUF: Double,
val IDR: Double,
val ILS: Double,
val INR: Double,
val ISK: Double,
val JPY: Double,
val KRW: Double,
val MXN: Double,
val MYR: Double,
val NOK: Double,
val NZD: Double,
val PHP: Double,
val PLN: Double,
val RON: Double,
val RUB: Double,
val SEK: Double,
val SGD: Double,
val THB: Double,
val TRY: Double,
val USD: Double,
val ZAR: Double
)
package com.ess.currencyapp.service
import com.ess.currencyapp.model.Currency
import retrofit2.Call
import retrofit2.http.GET
interface CurrencyAPI {
@GET("/latest")
fun getCurrency() : Call<Currency>
}
package com.ess.currencyapp.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ess.currencyapp.R
import kotlinx.android.synthetic.main.item_currency.view.*
class CurrencyRecyclerViewAdapter(private val currencyNameList: ArrayList<String>, private val currencyValueList: ArrayList<Double>)
: RecyclerView.Adapter<CurrencyRecyclerViewAdapter.RowHolder>() {
class RowHolder(view: View) : RecyclerView.ViewHolder(view){
fun bind(currencyName: String,currencyValue: Double){
itemView.currency_name.text = currencyName
itemView.currency_value.text = currencyValue.toString()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_currency,parent,false)
return RowHolder(view)
}
override fun getItemCount(): Int {
return currencyNameList.count()
}
override fun onBindViewHolder(holder: RowHolder, position: Int) {
holder.bind(currencyNameList[position],currencyValueList[position])
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".view.HomeFragment"
android:background="#180E26">
<LinearLayout
android:layout_margin="10dp"
android:id="@+id/euro_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/euro"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAlignment="center"
android:gravity="center"
android:textColor="#F29F05"
android:text="EURO EXCHANGE"
android:textSize="28dp"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/currencyList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
/>
</LinearLayout>
package com.ess.currencyapp.view
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.ess.currencyapp.R
import com.ess.currencyapp.adapter.CurrencyRecyclerViewAdapter
import com.ess.currencyapp.model.Currency
import com.ess.currencyapp.model.Rates
import com.ess.currencyapp.service.CurrencyAPI
import kotlinx.android.synthetic.main.fragment_home.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class HomeFragment : Fragment() {
val BASE_URL = "https://api.exchangeratesapi.io/"
val currencyNames : ArrayList<String> = arrayListOf("AUD","BGN","BRL","CAD","CHF","CNY","CZK","DKK","GBP","HKD","HRK","HUF","IDR",
"ILS","INR","ISK","JPY","KRW","MXN","MYR","NOK","NZD","PHP","PLN","RON","RUB","SEK","SGD","THB","TRY","USD","ZAR")
val currencyValues: ArrayList<Double> = arrayListOf()
var currencyRecyclerViewAdapter: CurrencyRecyclerViewAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
currencyList.layoutManager = LinearLayoutManager(context)
getCurrencyRates()
}
fun getCurrencyRates(){
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(CurrencyAPI::class.java)
val call = service.getCurrency()
call.enqueue(object : Callback<Currency>{
override fun onFailure(call: Call<Currency>, t: Throwable) {
t.printStackTrace()
}
override fun onResponse(call: Call<Currency>, response: Response<Currency>) {
if(response.isSuccessful){
response.body()?.let {
addAllCurrencyValues(it.rates)
currencyRecyclerViewAdapter = CurrencyRecyclerViewAdapter(currencyNames,currencyValues)
currencyList.adapter = currencyRecyclerViewAdapter
}
}
}
})
}
fun addAllCurrencyValues(rateModel: Rates){
currencyValues.add(rateModel.AUD)
currencyValues.add(rateModel.BGN)
currencyValues.add(rateModel.BRL)
currencyValues.add(rateModel.CAD)
currencyValues.add(rateModel.CHF)
currencyValues.add(rateModel.CNY)
currencyValues.add(rateModel.CZK)
currencyValues.add(rateModel.DKK)
currencyValues.add(rateModel.GBP)
currencyValues.add(rateModel.HKD)
currencyValues.add(rateModel.HRK)
currencyValues.add(rateModel.HUF)
currencyValues.add(rateModel.IDR)
currencyValues.add(rateModel.ILS)
currencyValues.add(rateModel.INR)
currencyValues.add(rateModel.ISK)
currencyValues.add(rateModel.JPY)
currencyValues.add(rateModel.KRW)
currencyValues.add(rateModel.MXN)
currencyValues.add(rateModel.MYR)
currencyValues.add(rateModel.NOK)
currencyValues.add(rateModel.NZD)
currencyValues.add(rateModel.PHP)
currencyValues.add(rateModel.PLN)
currencyValues.add(rateModel.RON)
currencyValues.add(rateModel.RUB)
currencyValues.add(rateModel.SEK)
currencyValues.add(rateModel.SGD)
currencyValues.add(rateModel.THB)
currencyValues.add(rateModel.TRY)
currencyValues.add(rateModel.USD)
currencyValues.add(rateModel.ZAR)
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<Button
android:id="@+id/currency_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle_button"
android:textColor="#180E26"
android:text="TRY"/>
<TextView
android:id="@+id/currency_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="8.5019"
android:textAlignment="center"
android:textColor="#F29F05"
android:textSize="24dp" />
</LinearLayout>
def retrofitVersion = '2.8.1'
def rxJavaVersion = '2.1.1'
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
implementation "io.reactivex.rxjava2:rxandroid:$rxJavaVersion"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment