Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[On hold] Port Volley to Retrofit #132

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ dependencies {
implementation "android.arch.persistence.room:runtime:$lifecycleVersion"
annotationProcessor "android.arch.persistence.room:compiler:$lifecycleVersion"
kapt "android.arch.persistence.room:compiler:$lifecycleVersion"

// Retrofit
implementation('com.squareup.retrofit2:retrofit:2.5.0') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
implementation('com.squareup.retrofit2:converter-gson:2.5.0') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
exclude group: 'com.google.code.gson', module: 'gson'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.widget.Toast
import com.blockeq.stellarwallet.R
import com.blockeq.stellarwallet.interfaces.SuccessErrorCallback
import com.blockeq.stellarwallet.models.HorizonException
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.networking.Horizon
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.utils.NetworkUtils
import kotlinx.android.synthetic.main.activity_add_asset.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ import com.blockeq.stellarwallet.interfaces.ChangeTrustlineListener
import com.blockeq.stellarwallet.interfaces.OnLoadAccount
import com.blockeq.stellarwallet.interfaces.SuccessErrorCallback
import com.blockeq.stellarwallet.models.HorizonException
import com.blockeq.stellarwallet.models.SupportedAsset
import com.blockeq.stellarwallet.models.SupportedAssetType
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.responses.SupportedAssetResponse
import com.blockeq.stellarwallet.responses.SupportedAssetType
import com.blockeq.stellarwallet.networking.Horizon
import com.blockeq.stellarwallet.networking.RetrofitClient
import com.blockeq.stellarwallet.networking.api.SupportedAssetAPI
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.utils.NetworkUtils
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.content_assets_activity.*
import org.stellar.sdk.Asset
import org.stellar.sdk.requests.ErrorResponse
import org.stellar.sdk.responses.AccountResponse
import retrofit2.Call
import retrofit2.Callback

class AssetsActivity : BasePopupActivity(), ChangeTrustlineListener {

private var map: Map<String, SupportedAsset>? = null
private var map: Map<String, SupportedAssetResponse>? = null
private var assetsList: ArrayList<Any> = ArrayList()
private lateinit var context : Context
private lateinit var adapter : AssetsRecyclerViewAdapter
Expand Down Expand Up @@ -83,13 +85,13 @@ class AssetsActivity : BasePopupActivity(), ChangeTrustlineListener {
//endregion

private fun convertBalanceToSupportedAsset(balances: Array<AccountResponse.Balance>,
supportedAssetsMap: Map<String, SupportedAsset>) : List<SupportedAsset> {
supportedAssetsMap: Map<String, SupportedAssetResponse>) : List<SupportedAssetResponse> {

val lumenSupportedAsset = SupportedAsset(0, Constants.LUMENS_ASSET_CODE, Constants.LUMENS_IMAGE_URL,
val lumenSupportedAsset = SupportedAssetResponse(0, Constants.LUMENS_ASSET_CODE, Constants.LUMENS_IMAGE_URL,
"", "", Constants.LUMENS_ASSET_NAME, "", "",
"0", SupportedAssetType.ADDED, null)

val list = ArrayList<SupportedAsset>()
val list = ArrayList<SupportedAssetResponse>()
list.add(lumenSupportedAsset)

if (balances.isNotEmpty()) {
Expand All @@ -107,7 +109,7 @@ class AssetsActivity : BasePopupActivity(), ChangeTrustlineListener {
return@map asset
}
else -> {
val asset = SupportedAsset(0, it.assetCode.toLowerCase(), "",
val asset = SupportedAssetResponse(0, it.assetCode.toLowerCase(), "",
it.assetIssuer.accountId, it.limit, it.assetCode, "",
"", it.balance, SupportedAssetType.ADDED, it.asset)
return@map asset
Expand All @@ -117,36 +119,35 @@ class AssetsActivity : BasePopupActivity(), ChangeTrustlineListener {

// This cast is guaranteed to succeed
@Suppress("UNCHECKED_CAST")
list.addAll((nullableAssets.filter { it != null }) as List<SupportedAsset>)
list.addAll((nullableAssets.filter { it != null }) as List<SupportedAssetResponse>)
}

return list
}

private fun getFilteredSupportedAssets(map: Map<String, SupportedAsset>): List<SupportedAsset> {
private fun getFilteredSupportedAssets(map: Map<String, SupportedAssetResponse>): List<SupportedAssetResponse> {
return map.values.filter { it ->
it.code.toUpperCase() !in WalletApplication.localStore.balances!!.map { it.assetCode }
}
}

private fun loadSupportedAssets() {
val queue = Volley.newRequestQueue(this)

// TODO: Use retrofit and dagger
val request = JsonObjectRequest(Request.Method.GET, Constants.BLOCKEQ_BASE_URL, null,
Response.Listener { response ->
// display response
val gson = GsonBuilder().create()
val token = object : TypeToken<Map<String, SupportedAsset>>(){}.type

map = gson.fromJson<Map<String, SupportedAsset>>(response.toString(), token)
updateAdapter()
},
Response.ErrorListener {
Toast.makeText(this, getString(R.string.error_supported_assets_message), Toast.LENGTH_SHORT).show()
})

queue.add(request)
val retrofitClient = RetrofitClient.getRetrofitClient(Constants.BLOCKEQ_BASE_URL)
val service = retrofitClient.create(SupportedAssetAPI::class.java)

val call = service.requestSupportedAssets()

call.enqueue(object : Callback<Map<String, SupportedAssetResponse>>{
override fun onFailure(call: Call<Map<String, SupportedAssetResponse>>, t: Throwable) {
Toast.makeText(this@AssetsActivity, getString(R.string.error_supported_assets_message), Toast.LENGTH_SHORT).show()
}

override fun onResponse(call: Call<Map<String, SupportedAssetResponse>>, response: retrofit2.Response<Map<String, SupportedAssetResponse>>) {
map = response.body()
updateAdapter()
}

})
}

//region Call backs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.blockeq.stellarwallet.R
import com.blockeq.stellarwallet.helpers.Constants
import com.blockeq.stellarwallet.interfaces.SuccessErrorCallback
import com.blockeq.stellarwallet.models.HorizonException
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.networking.Horizon
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.utils.NetworkUtils
import kotlinx.android.synthetic.main.activity_inflation.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import com.blockeq.stellarwallet.interfaces.SuccessErrorCallback
import com.blockeq.stellarwallet.models.ExchangeApiModel
import com.blockeq.stellarwallet.models.HorizonException
import com.blockeq.stellarwallet.models.PinType
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.networking.Horizon
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.utils.NetworkUtils
import com.blockeq.stellarwallet.utils.StringFormat.Companion.getNumDecimals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.blockeq.stellarwallet.fragments.WalletFragment
import com.blockeq.stellarwallet.interfaces.OnLoadAccount
import com.blockeq.stellarwallet.interfaces.OnLoadEffects
import com.blockeq.stellarwallet.models.MinimumBalance
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.networking.Horizon
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.utils.KeyboardUtils
import com.blockeq.stellarwallet.utils.NetworkUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import com.blockeq.stellarwallet.WalletApplication
import com.blockeq.stellarwallet.activities.InflationActivity
import com.blockeq.stellarwallet.helpers.Constants
import com.blockeq.stellarwallet.interfaces.ChangeTrustlineListener
import com.blockeq.stellarwallet.models.SupportedAsset
import com.blockeq.stellarwallet.models.SupportedAssetType
import com.blockeq.stellarwallet.responses.SupportedAssetResponse
import com.blockeq.stellarwallet.responses.SupportedAssetType
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.utils.StringFormat
import com.squareup.picasso.Picasso
Expand Down Expand Up @@ -53,8 +53,8 @@ class AssetsRecyclerViewAdapter(var context: Context, private var listener: Chan

override fun getItemViewType(position: Int): Int {
return when {
items[position] is SupportedAsset && (items[position] as SupportedAsset).type == SupportedAssetType.ADDED -> TYPE_ASSET
items[position] is SupportedAsset -> TYPE_SUPPORTED_ASSET
items[position] is SupportedAssetResponse && (items[position] as SupportedAssetResponse).type == SupportedAssetType.ADDED -> TYPE_ASSET
items[position] is SupportedAssetResponse -> TYPE_SUPPORTED_ASSET
else -> TYPE_HEADER
}
}
Expand Down Expand Up @@ -107,7 +107,7 @@ class AssetsRecyclerViewAdapter(var context: Context, private var listener: Chan
//region Bind View Holders

private fun configureAssetViewHolder(viewHolder : AssetViewHolder, position : Int) {
val asset = items[position] as SupportedAsset
val asset = items[position] as SupportedAssetResponse

viewHolder.assetButton.visibility = View.VISIBLE
viewHolder.assetName.text = asset.name
Expand Down Expand Up @@ -165,7 +165,7 @@ class AssetsRecyclerViewAdapter(var context: Context, private var listener: Chan
}

private fun configureSupportedAssetViewHolder(viewHolder: SupportedAssetViewHolder, position: Int) {
val asset = items[position] as SupportedAsset
val asset = items[position] as SupportedAssetResponse
val trustLineAsset = Asset.createNonNativeAsset(asset.code.toUpperCase(), KeyPair.fromAccountId(asset.issuer))

viewHolder.assetName.text = String.format(context.getString(R.string.asset_template),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.blockeq.stellarwallet.models.AssetUtil
import com.blockeq.stellarwallet.models.Currency
import com.blockeq.stellarwallet.models.MyOffer
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.utils.AccountUtils
import kotlinx.android.synthetic.main.fragment_tab_my_offers.*
import org.stellar.sdk.responses.OfferResponse
import timber.log.Timber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.blockeq.stellarwallet.adapters.OrderBooksAdapter
import com.blockeq.stellarwallet.interfaces.OnUpdateTradingCurrencies
import com.blockeq.stellarwallet.models.*
import com.blockeq.stellarwallet.remote.Horizon
import com.brandongogetap.stickyheaders.StickyLayoutManager
import kotlinx.android.synthetic.main.fragment_tab_order_book.*
import org.stellar.sdk.responses.OrderBookResponse
import timber.log.Timber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.blockeq.stellarwallet.R
import com.blockeq.stellarwallet.WalletApplication
import com.blockeq.stellarwallet.interfaces.OnTradeCurrenciesChanged
import com.blockeq.stellarwallet.models.*
import com.blockeq.stellarwallet.remote.Horizon
import com.blockeq.stellarwallet.networking.Horizon
import com.blockeq.stellarwallet.utils.AccountUtils
import com.blockeq.stellarwallet.vmodels.TradingViewModel
import kotlinx.android.synthetic.main.fragment_tab_trade.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Constants {
const val INFLATION_DESTINATION = "GCCD6AJOYZCUAQLX32ZJF2MKFFAUJ53PVCFQI3RHWKL3V47QYE2BNAUT"

const val LUMENS_IMAGE_URL = "https://firebasestorage.googleapis.com/v0/b/blockeq-wallet.appspot.com/o/icon-stellar.png?alt=media&token=38b70165-5255-4113-a15e-3c72bd4fab9f"
const val BLOCKEQ_BASE_URL = "https://api.blockeq.com/directory/assets"
const val BLOCKEQ_BASE_URL = "https://api.blockeq.com"
const val BLOCKEQ_ASSETS_URL = "https://api.blockeq.com/directory/assets"
const val BLOCKEQ_EXCHANGES_URL = "https://api.blockeq.com/directory/exchanges?asArray"
const val BLOCKEQ_DIAGNOSTIC_URL = "https://api.blockeq.com/diagnostic"

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blockeq.stellarwallet.remote
package com.blockeq.stellarwallet.networking

import android.os.AsyncTask
import android.os.Handler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blockeq.stellarwallet.remote
package com.blockeq.stellarwallet.networking

import android.os.AsyncTask
import com.blockeq.stellarwallet.interfaces.OnLoadAccount
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.blockeq.stellarwallet.networking

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitClient {

companion object {
fun getRetrofitClient(baseUrl : String) : Retrofit {
return Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.blockeq.stellarwallet.networking.api

import com.blockeq.stellarwallet.responses.SupportedAssetResponse
import retrofit2.Call
import retrofit2.http.GET

interface SupportedAssetAPI {

@GET("/directory/assets")
fun requestSupportedAssets() : Call<Map<String, SupportedAssetResponse>>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.blockeq.stellarwallet.responses

import org.stellar.sdk.Asset

/**
* Class which provides a model for SupportedAssetResponse
* @constructor Sets all properties of the SupportedAssetResponse
* @property id the unique identifier of this asset
* @property code the asset code
* @property image the link to icon of asset
* @property issuer the public address of the issuer of this asset
* @property createdAt the iso instant time stamp of when the asset was created
* @property updatedAt the iso instant time stamp of when the asset was updated
* @property amount the amount of balance of asset
* @property type either added or not added asset
*/

enum class SupportedAssetType {
ADDED, NOT_ADDED
}

data class SupportedAssetResponse (var id: Int, var code: String, var image: String, var issuer: String,
var limit: String, var name: String, var createdAt: String,
var updatedAt: String, var amount: String?,
var type: SupportedAssetType? = SupportedAssetType.NOT_ADDED,
var asset: Asset?)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package com.blockeq.stellarwallet.vmodels
import android.app.Application
import android.arch.lifecycle.AndroidViewModel
import android.arch.lifecycle.MutableLiveData
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import com.blockeq.stellarwallet.models.*
import com.blockeq.stellarwallet.remote.Horizon
import org.stellar.sdk.requests.RequestBuilder
import com.blockeq.stellarwallet.networking.Horizon
import org.stellar.sdk.responses.OrderBookResponse
import timber.log.Timber
import java.util.*
Expand Down