diff --git a/app/build.gradle b/app/build.gradle index ba6c4f30..301382b6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' + } } diff --git a/app/src/main/java/com/blockeq/stellarwallet/activities/AddAssetActivity.kt b/app/src/main/java/com/blockeq/stellarwallet/activities/AddAssetActivity.kt index 807c5731..62eae59f 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/activities/AddAssetActivity.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/activities/AddAssetActivity.kt @@ -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.* diff --git a/app/src/main/java/com/blockeq/stellarwallet/activities/AssetsActivity.kt b/app/src/main/java/com/blockeq/stellarwallet/activities/AssetsActivity.kt index 0129f16d..09b43833 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/activities/AssetsActivity.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/activities/AssetsActivity.kt @@ -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? = null + private var map: Map? = null private var assetsList: ArrayList = ArrayList() private lateinit var context : Context private lateinit var adapter : AssetsRecyclerViewAdapter @@ -83,13 +85,13 @@ class AssetsActivity : BasePopupActivity(), ChangeTrustlineListener { //endregion private fun convertBalanceToSupportedAsset(balances: Array, - supportedAssetsMap: Map) : List { + supportedAssetsMap: Map) : List { - 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() + val list = ArrayList() list.add(lumenSupportedAsset) if (balances.isNotEmpty()) { @@ -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 @@ -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) + list.addAll((nullableAssets.filter { it != null }) as List) } return list } - private fun getFilteredSupportedAssets(map: Map): List { + private fun getFilteredSupportedAssets(map: Map): List { 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>(){}.type - - map = gson.fromJson>(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>{ + override fun onFailure(call: Call>, t: Throwable) { + Toast.makeText(this@AssetsActivity, getString(R.string.error_supported_assets_message), Toast.LENGTH_SHORT).show() + } + + override fun onResponse(call: Call>, response: retrofit2.Response>) { + map = response.body() + updateAdapter() + } + + }) } //region Call backs diff --git a/app/src/main/java/com/blockeq/stellarwallet/activities/InflationActivity.kt b/app/src/main/java/com/blockeq/stellarwallet/activities/InflationActivity.kt index 79cc48c8..3de9f9c4 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/activities/InflationActivity.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/activities/InflationActivity.kt @@ -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.* diff --git a/app/src/main/java/com/blockeq/stellarwallet/activities/SendActivity.kt b/app/src/main/java/com/blockeq/stellarwallet/activities/SendActivity.kt index fb76b15e..324fd589 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/activities/SendActivity.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/activities/SendActivity.kt @@ -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 diff --git a/app/src/main/java/com/blockeq/stellarwallet/activities/WalletActivity.kt b/app/src/main/java/com/blockeq/stellarwallet/activities/WalletActivity.kt index 21884928..d4bebab7 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/activities/WalletActivity.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/activities/WalletActivity.kt @@ -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 diff --git a/app/src/main/java/com/blockeq/stellarwallet/adapters/AssetsRecyclerViewAdapter.kt b/app/src/main/java/com/blockeq/stellarwallet/adapters/AssetsRecyclerViewAdapter.kt index 3d61fe46..1bdbbf41 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/adapters/AssetsRecyclerViewAdapter.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/adapters/AssetsRecyclerViewAdapter.kt @@ -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 @@ -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 } } @@ -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 @@ -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), diff --git a/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/MyOffersTabFragment.kt b/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/MyOffersTabFragment.kt index 76396dec..dcd6565d 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/MyOffersTabFragment.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/MyOffersTabFragment.kt @@ -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 diff --git a/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/OrderBookTabFragment.kt b/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/OrderBookTabFragment.kt index dc384c6e..33ec84a4 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/OrderBookTabFragment.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/OrderBookTabFragment.kt @@ -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 diff --git a/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/TradeTabFragment.kt b/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/TradeTabFragment.kt index 9011b63a..6b56bc57 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/TradeTabFragment.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/fragments/tabs/TradeTabFragment.kt @@ -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.* diff --git a/app/src/main/java/com/blockeq/stellarwallet/helpers/Constants.kt b/app/src/main/java/com/blockeq/stellarwallet/helpers/Constants.kt index a7d0e499..04fc6abf 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/helpers/Constants.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/helpers/Constants.kt @@ -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" diff --git a/app/src/main/java/com/blockeq/stellarwallet/models/SupportedAsset.kt b/app/src/main/java/com/blockeq/stellarwallet/models/SupportedAsset.kt deleted file mode 100644 index 9969155e..00000000 --- a/app/src/main/java/com/blockeq/stellarwallet/models/SupportedAsset.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.blockeq.stellarwallet.models - -import org.stellar.sdk.Asset - -/** - * Class which provides a model for SupportedAsset - * @constructor Sets all properties of the SupportedAsset - * @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 SupportedAsset (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?) diff --git a/app/src/main/java/com/blockeq/stellarwallet/remote/Horizon.kt b/app/src/main/java/com/blockeq/stellarwallet/networking/Horizon.kt similarity index 99% rename from app/src/main/java/com/blockeq/stellarwallet/remote/Horizon.kt rename to app/src/main/java/com/blockeq/stellarwallet/networking/Horizon.kt index 0ac1def1..9feae534 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/remote/Horizon.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/networking/Horizon.kt @@ -1,4 +1,4 @@ -package com.blockeq.stellarwallet.remote +package com.blockeq.stellarwallet.networking import android.os.AsyncTask import android.os.Handler diff --git a/app/src/main/java/com/blockeq/stellarwallet/remote/HorizonTasks.kt b/app/src/main/java/com/blockeq/stellarwallet/networking/HorizonTasks.kt similarity index 97% rename from app/src/main/java/com/blockeq/stellarwallet/remote/HorizonTasks.kt rename to app/src/main/java/com/blockeq/stellarwallet/networking/HorizonTasks.kt index ddf841de..6809a4f7 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/remote/HorizonTasks.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/networking/HorizonTasks.kt @@ -1,4 +1,4 @@ -package com.blockeq.stellarwallet.remote +package com.blockeq.stellarwallet.networking import android.os.AsyncTask import com.blockeq.stellarwallet.interfaces.OnLoadAccount diff --git a/app/src/main/java/com/blockeq/stellarwallet/networking/RetrofitClient.kt b/app/src/main/java/com/blockeq/stellarwallet/networking/RetrofitClient.kt new file mode 100644 index 00000000..dddfe401 --- /dev/null +++ b/app/src/main/java/com/blockeq/stellarwallet/networking/RetrofitClient.kt @@ -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() + } + } +} diff --git a/app/src/main/java/com/blockeq/stellarwallet/networking/api/SupportedAssetAPI.kt b/app/src/main/java/com/blockeq/stellarwallet/networking/api/SupportedAssetAPI.kt new file mode 100644 index 00000000..847cec3b --- /dev/null +++ b/app/src/main/java/com/blockeq/stellarwallet/networking/api/SupportedAssetAPI.kt @@ -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> + +} diff --git a/app/src/main/java/com/blockeq/stellarwallet/responses/SupportedAssetResponse.kt b/app/src/main/java/com/blockeq/stellarwallet/responses/SupportedAssetResponse.kt new file mode 100644 index 00000000..f92000c7 --- /dev/null +++ b/app/src/main/java/com/blockeq/stellarwallet/responses/SupportedAssetResponse.kt @@ -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?) diff --git a/app/src/main/java/com/blockeq/stellarwallet/vmodels/TradingViewModel.kt b/app/src/main/java/com/blockeq/stellarwallet/vmodels/TradingViewModel.kt index 98260dc6..2d30b842 100644 --- a/app/src/main/java/com/blockeq/stellarwallet/vmodels/TradingViewModel.kt +++ b/app/src/main/java/com/blockeq/stellarwallet/vmodels/TradingViewModel.kt @@ -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.*