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.
This commit is contained in:
Severiano Jaramillo 2019-01-09 17:19:49 -06:00
parent e16d651a63
commit 941a558a08
2 changed files with 25 additions and 10 deletions

View file

@ -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<String>()
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() {

View file

@ -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)
}