From 941a558a08c8951a619e3499a71a8517a07dfe41 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Wed, 9 Jan 2019 17:19:49 -0600 Subject: [PATCH] Force the app to start updating the account information just after it is imported/created. This wasn't happening automatically the first time the account was imported, the the user had to close and reopen the app for the transfers update to take place. The problem was that the account information was only fetched from the shared preferences when the app started, thus a method to force the fetch was introduced just after the user imports/creates his user account. --- .../activities/ConnectedActivity.kt | 29 ++++++++++++------- .../fragments/BaseAccountFragment.kt | 6 ++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/ConnectedActivity.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/ConnectedActivity.kt index 1e43ab6..c7f7c8e 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/ConnectedActivity.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/ConnectedActivity.kt @@ -94,10 +94,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val userId = PreferenceManager.getDefaultSharedPreferences(this) - .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") - if (userId != "") - mCurrentAccount = UserAccount(userId) + getUserAccount() mAssetRepository = AssetRepository(this) @@ -144,6 +141,17 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { .subscribe { handleIncomingMessage(it) } } + /** + * Obtains the userId from the shared preferences and creates a [UserAccount] instance. + * Created as a public function, so that it can be called from its Fragments. + */ + fun getUserAccount() { + val userId = PreferenceManager.getDefaultSharedPreferences(this) + .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") ?: "" + if (userId != "") + mCurrentAccount = UserAccount(userId) + } + private fun handleIncomingMessage(message: Any?) { if (message is JsonRpcResponse<*>) { @@ -200,7 +208,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { if (latestOpCount == 0L) { Log.d(TAG, "The node returned 0 total_ops for current account and may not have installed the history plugin. " + "\nAsk the NetworkService to remove the node from the list and connect to another one.") - mNetworkService!!.removeCurrentNodeAndReconnect() + mNetworkService?.removeCurrentNodeAndReconnect() } else if (storedOpCount == -1L) { // Initial case when the app starts storedOpCount = latestOpCount @@ -293,7 +301,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { } private fun updateBalances() { - if (mNetworkService!!.isConnected) { + if (mNetworkService?.isConnected == true) { val id = mNetworkService!!.sendMessage(GetAccountBalances(mCurrentAccount, ArrayList()), GetAccountBalances.REQUIRED_API) @@ -306,7 +314,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { */ private val mRequestMissingUserAccountsTask = object : Runnable { override fun run() { - if (mNetworkService!!.isConnected) { + if (mNetworkService?.isConnected == true) { val id = mNetworkService!!.sendMessage(GetAccounts(missingUserAccounts), GetAccounts.REQUIRED_API) responseMap[id] = RESPONSE_GET_ACCOUNTS @@ -321,7 +329,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { */ private val mRequestMissingAssetsTask = object : Runnable { override fun run() { - if (mNetworkService!!.isConnected) { + if (mNetworkService?.isConnected == true) { val id = mNetworkService!!.sendMessage(GetAssets(missingAssets), GetAssets.REQUIRED_API) responseMap[id] = RESPONSE_GET_ASSETS @@ -336,7 +344,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { */ private val mCheckMissingPaymentsTask = object : Runnable { override fun run() { - if (mNetworkService != null && mNetworkService!!.isConnected) { + if (mNetworkService?.isConnected == true) { if (mCurrentAccount != null) { val userAccounts = ArrayList() userAccounts.add(mCurrentAccount!!.objectId) @@ -359,7 +367,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { private val mRequestBlockMissingTimeTask = object : Runnable { override fun run() { - if (mNetworkService != null && mNetworkService!!.isConnected) { + if (mNetworkService?.isConnected == true) { val id = mNetworkService!!.sendMessage(GetBlockHeader(blockNumberWithMissingTime), GetBlockHeader.REQUIRED_API) @@ -389,6 +397,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection { mHandler.removeCallbacks(mCheckMissingPaymentsTask) mHandler.removeCallbacks(mRequestMissingUserAccountsTask) mHandler.removeCallbacks(mRequestMissingAssetsTask) + mHandler.removeCallbacks(mRequestBlockMissingTimeTask) } override fun onResume() { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BaseAccountFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BaseAccountFragment.kt index b45f613..5a10ead 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BaseAccountFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BaseAccountFragment.kt @@ -13,6 +13,8 @@ import cy.agorise.graphenej.BrainKey import cy.agorise.graphenej.PublicKey import cy.agorise.graphenej.models.AccountProperties import org.bitcoinj.core.ECKey +import cy.agorise.bitsybitshareswallet.activities.ConnectedActivity + abstract class BaseAccountFragment : ConnectedFragment() { @@ -72,6 +74,10 @@ abstract class BaseAccountFragment : ConnectedFragment() { } } + // Force [ConnectedActivity] to refresh the userId from the SharedPreferences, so that the app can immediately + // to fetch the account's transactions. + (activity as ConnectedActivity).getUserAccount() + // Send the user back to HomeFragment findNavController().navigate(R.id.home_action) }