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

NMA-615 | Contacts Page | Unregistered Users #519

Merged
merged 21 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0698f10
Removing wallet fragment before recreating activity
desamtralized Jul 29, 2020
6f08871
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Jul 30, 2020
6bbb759
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Aug 5, 2020
42c99c4
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Aug 6, 2020
af16683
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Aug 18, 2020
4dde9e0
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Sep 8, 2020
909ef0b
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Sep 11, 2020
e4ce62c
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Sep 21, 2020
262c0bf
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Sep 24, 2020
80e5932
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Sep 29, 2020
088bbbb
Merge remote-tracking branch 'upstream/evonet-develop' into evonet-de…
desamtralized Oct 1, 2020
da1f349
Upgrade to Evolution page.
desamtralized Oct 1, 2020
de3d53e
Removed commented code
desamtralized Oct 1, 2020
964b74e
Using Async version of db fetch
desamtralized Oct 2, 2020
a50998a
Fixed stuck contacts icon
desamtralized Oct 2, 2020
ef6db8a
Replaced old school callbacks-based communication model between activ…
tomasz-ludek Oct 2, 2020
3cc66d3
Further improvements of MainActivityViewModel
tomasz-ludek Oct 2, 2020
620cc98
Minor code refactoring
tomasz-ludek Oct 2, 2020
cd1d505
Merge branch 'master-into-evonet-develop' into dashpay-contacts-page-…
tomasz-ludek Oct 6, 2020
5f7afd4
Merge branch 'evonet-develop' into dashpay-contacts-page-no-user
tomasz-ludek Oct 6, 2020
b912e2c
Final improvements of handling Join DashPay button visibility.
tomasz-ludek Oct 6, 2020
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
59 changes: 20 additions & 39 deletions wallet/src/de/schildbach/wallet/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.google.common.collect.ImmutableList
import de.schildbach.wallet.AppDatabase
import de.schildbach.wallet.Constants
import de.schildbach.wallet.WalletBalanceWidgetProvider
import de.schildbach.wallet.data.BlockchainIdentityData
import de.schildbach.wallet.data.BlockchainState
import de.schildbach.wallet.data.PaymentIntent
import de.schildbach.wallet.livedata.Resource
import de.schildbach.wallet.livedata.Status
import de.schildbach.wallet.ui.InputParser.BinaryInputParser
import de.schildbach.wallet.ui.PaymentsFragment.Companion.ACTIVE_TAB_RECENT
import de.schildbach.wallet.ui.RestoreFromFileHelper.OnRestoreWalletListener
Expand All @@ -42,7 +38,6 @@ import de.schildbach.wallet.ui.dashpay.ContactsFragment
import de.schildbach.wallet.ui.dashpay.ContactsFragment.Companion.MODE_SEARCH_CONTACTS
import de.schildbach.wallet.ui.dashpay.ContactsFragment.Companion.MODE_SELECT_CONTACT
import de.schildbach.wallet.ui.dashpay.ContactsFragment.Companion.MODE_VIEW_REQUESTS
import de.schildbach.wallet.ui.dashpay.DashPayViewModel
import de.schildbach.wallet.ui.dashpay.UpgradeToEvolutionFragment
import de.schildbach.wallet.ui.widget.UpgradeWalletDisclaimerDialog
import de.schildbach.wallet.util.CrashReporter
Expand All @@ -64,7 +59,6 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
UpgradeWalletDisclaimerDialog.OnUpgradeConfirmedListener,
EncryptNewKeyChainDialogFragment.OnNewKeyChainEncryptedListener,
PaymentsPayFragment.OnSelectContactToPayListener, WalletFragment.OnSelectPaymentTabListener,
WalletFragment.OnWalletFragmentViewCreatedListener,
ContactSearchResultsAdapter.OnViewAllRequestsListener,
UpgradeToEvolutionFragment.OnUpgradeBtnClicked {

Expand All @@ -87,16 +81,14 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
}
}

private lateinit var viewModel: MainActivityViewModel

private var isRestoringBackup = false
private var showBackupWalletDialog = false
private val config: Configuration by lazy { walletApplication.configuration }
private var fingerprintHelper: FingerprintHelper? = null

private var blockchainState: BlockchainState? = null
private var dashPayViewModel: DashPayViewModel? = null
private var isPlatformAvailable: Boolean = false
private var hasIdentity: Boolean = false
private lateinit var walletFragment: WalletFragment

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -106,7 +98,7 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
}
setContentView(R.layout.activity_main)

walletFragment = supportFragmentManager.findFragmentByTag("wallet_fragment") as WalletFragment
initViewModel()

if (savedInstanceState == null) {
checkAlerts()
Expand All @@ -123,6 +115,21 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
setupBottomNavigation()
}

fun initViewModel() {
viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java]
viewModel.blockchainStateData.observe(this, Observer {
// just to trigger data loading
})
viewModel.isPlatformAvailableData.observe(this, Observer {
// just to trigger data loading
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those empty observers could be replaced by other way of triggering data loading but let's leave it as it is
I'll fix it when working on "JoinDash" shortcut when #516 in merged (it will be merged after this PR)

viewModel.blockchainIdentityData.observe(this, Observer {
if (it != null) {
hasIdentity = it.creationState == BlockchainIdentityData.CreationState.DONE
}
})
}

override fun onResume() {
super.onResume()
showBackupWalletDialogIfNeeded()
Expand All @@ -136,32 +143,6 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
handleIntent(intent!!)
}

override fun onWalletFragmentViewCreated() {
AppDatabase.getAppDatabase().blockchainStateDao().load().observe(this, Observer {
blockchainState = it
walletFragment.setBlockchainState(blockchainState)
})
dashPayViewModel = ViewModelProvider(this)[DashPayViewModel::class.java]
dashPayViewModel!!.isPlatformAvailableLiveData.observe(this, Observer<Resource<Boolean?>?> { status ->
if (status?.status === Status.SUCCESS) {
if (status.data != null) {
isPlatformAvailable = status.data
}
} else {
isPlatformAvailable = false
}
walletFragment.setPlatformAvailability(isPlatformAvailable)
})
AppDatabase.getAppDatabase().blockchainIdentityDataDaoAsync().loadBase().observe(this,
Observer {
if (it != null) {
hasIdentity = it.creationState == BlockchainIdentityData.CreationState.DONE
}
walletFragment.setBlockchainIdentity(it)
}
)
}

//BIP44 Wallet Upgrade Dialog Dismissed (Ok button pressed)
override fun onUpgradeConfirmed() {
if (isRestoringBackup) {
Expand Down Expand Up @@ -233,7 +214,7 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
private fun showContacts(mode: Int = MODE_SEARCH_CONTACTS) {
bottom_navigation.menu.findItem(R.id.contacts)?.isChecked = true

val isSynced = blockchainState?.isSynced() ?: false
val isSynced = viewModel.blockchainState?.isSynced() ?: false
if (hasIdentity) {
val contactsFragment = ContactsFragment.newInstance(mode)
if (mode == MODE_VIEW_REQUESTS) {
Expand All @@ -242,7 +223,7 @@ class MainActivity : AbstractBindServiceActivity(), ActivityCompat.OnRequestPerm
replaceFragment(contactsFragment)
}
} else {
val readyToUpgrade = isPlatformAvailable && isSynced &&
val readyToUpgrade = viewModel.isPlatformAvailable && isSynced &&
wallet.canAffordIdentityCreation()
replaceFragment(UpgradeToEvolutionFragment.newInstance(readyToUpgrade))
}
Expand Down
35 changes: 35 additions & 0 deletions wallet/src/de/schildbach/wallet/ui/MainActivityViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.schildbach.wallet.ui

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.liveData
import de.schildbach.wallet.AppDatabase
import de.schildbach.wallet.data.BlockchainIdentityBaseData
import de.schildbach.wallet.data.BlockchainState
import de.schildbach.wallet.livedata.Status
import de.schildbach.wallet.ui.dashpay.PlatformRepo
import kotlinx.coroutines.Dispatchers

class MainActivityViewModel(application: Application) : AndroidViewModel(application) {

private val platformRepo = PlatformRepo.getInstance()

val isPlatformAvailableData = liveData(Dispatchers.IO) {
val status = platformRepo.isPlatformAvailable()
if (status.status == Status.SUCCESS && status.data != null) {
emit(status.data)
} else {
emit(false)
}
}
val isPlatformAvailable: Boolean
get() = isPlatformAvailableData.value ?: false

val blockchainStateData = AppDatabase.getAppDatabase().blockchainStateDao().load()
val blockchainState: BlockchainState?
get() = blockchainStateData.value

val blockchainIdentityData = AppDatabase.getAppDatabase().blockchainIdentityDataDaoAsync().loadBase()
val blockchainIdentity: BlockchainIdentityBaseData?
get() = blockchainIdentityData.value
}
63 changes: 27 additions & 36 deletions wallet/src/de/schildbach/wallet/ui/WalletFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.appbar.AppBarLayout.Behavior.DragCallback
import de.schildbach.wallet.WalletApplication
import de.schildbach.wallet.data.BlockchainIdentityBaseData
import de.schildbach.wallet.data.BlockchainIdentityData
import de.schildbach.wallet.data.BlockchainState
import de.schildbach.wallet.data.PaymentIntent
import de.schildbach.wallet.ui.CheckPinDialog.Companion.show
import de.schildbach.wallet.ui.InputParser.StringInputParser
Expand Down Expand Up @@ -64,10 +63,10 @@ import org.dash.wallet.integration.uphold.ui.UpholdAccountActivity

class WalletFragment : Fragment() {

private lateinit var mainActivityViewModel: MainActivityViewModel

private var clipboardManager: ClipboardManager? = null
private var blockchainState: BlockchainState? = null
private var syncComplete = false
private var isPlatformAvailable = false
private var noIdentityCreatedOrInProgress = true
private var retryCreationIfInProgress = true

Expand All @@ -87,6 +86,7 @@ class WalletFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initViewModel()
initView()
clipboardManager = activity?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?

Expand All @@ -105,7 +105,20 @@ class WalletFragment : Fragment() {
})

registerOnCoinsSentReceivedListener()
(requireActivity() as OnWalletFragmentViewCreatedListener).onWalletFragmentViewCreated()
}

fun initViewModel() {
mainActivityViewModel = ViewModelProvider(requireActivity())[MainActivityViewModel::class.java]
mainActivityViewModel.isPlatformAvailableData.observe(viewLifecycleOwner, Observer {
showHideJoinDashPayAction()
})
mainActivityViewModel.blockchainStateData.observe(viewLifecycleOwner, Observer {
updateSyncState()
showHideJoinDashPayAction()
})
mainActivityViewModel.blockchainIdentityData.observe(viewLifecycleOwner, Observer {
setBlockchainIdentity(it)
})
}

override fun onDestroyView() {
Expand All @@ -119,27 +132,7 @@ class WalletFragment : Fragment() {
showHideJoinDashPayAction()
}

fun setBlockchainState(blockchainState: BlockchainState?) {
this.blockchainState = blockchainState
if (isDetached || !isVisible) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be needed when using LiveDatas

return
}
updateSyncState()
showHideJoinDashPayAction()
}

fun setPlatformAvailability(available: Boolean) {
isPlatformAvailable = available
if (isDetached || !isVisible) {
return
}
showHideJoinDashPayAction()
}

fun setBlockchainIdentity(identityData: BlockchainIdentityBaseData?) {
if (isDetached || !isVisible) {
return
}
private fun setBlockchainIdentity(identityData: BlockchainIdentityBaseData?) {
if (identityData != null) {
noIdentityCreatedOrInProgress = identityData.creationState == BlockchainIdentityData.CreationState.NONE
showHideJoinDashPayAction()
Expand All @@ -166,17 +159,19 @@ class WalletFragment : Fragment() {
}

private fun updateSyncState() {
if (blockchainState == null) {
if (mainActivityViewModel.blockchainState == null) {
return
}
val blockchainState = mainActivityViewModel.blockchainState

var percentage: Int = blockchainState!!.percentageSync
if (blockchainState!!.replaying && blockchainState!!.percentageSync == 100) {
if (blockchainState.replaying && blockchainState.percentageSync == 100) {
//This is to prevent showing 100% when using the Rescan blockchain function.
//The first few broadcasted blockchainStates are with percentage sync at 100%
percentage = 0
}
val syncProgressView = sync_status_progress
if (blockchainState != null && blockchainState!!.syncFailed()) {
if (blockchainState.syncFailed()) {
updateSyncPaneVisibility(R.id.sync_status_pane, true)
sync_progress_pane.visibility = View.GONE
sync_error_pane.visibility = View.VISIBLE
Expand All @@ -189,7 +184,7 @@ class WalletFragment : Fragment() {
syncProgressView.progress = percentage
val syncPercentageView = sync_status_percentage
syncPercentageView.text = "$percentage%"
syncComplete = blockchainState!!.isSynced()
syncComplete = blockchainState.isSynced()
if (syncComplete) {
syncPercentageView.setTextColor(resources.getColor(R.color.success_green))
syncStatusTitle.setText(R.string.sync_status_sync_title)
Expand All @@ -216,7 +211,7 @@ class WalletFragment : Fragment() {
}

fun showHideJoinDashPayAction() {
if (noIdentityCreatedOrInProgress && syncComplete && isPlatformAvailable) {
if (noIdentityCreatedOrInProgress && syncComplete && mainActivityViewModel.isPlatformAvailable) {
val visible = wallet.canAffordIdentityCreation() && config.showJoinDashPay
join_dashpay_action.visibility = if (visible) View.VISIBLE else View.GONE
} else {
Expand All @@ -231,7 +226,7 @@ class WalletFragment : Fragment() {
}

private fun handleVerifySeed() {
val checkPinSharedModel = ViewModelProviders.of(this)[CheckPinSharedModel::class.java]
val checkPinSharedModel = ViewModelProvider(this)[CheckPinSharedModel::class.java]
checkPinSharedModel.onCorrectPinCallback.observe(viewLifecycleOwner, Observer<Pair<Int?, String?>?> { data ->
if (data?.second != null) {
startVerifySeedActivity(data.second!!)
Expand Down Expand Up @@ -346,8 +341,4 @@ class WalletFragment : Fragment() {
interface OnSelectPaymentTabListener {
fun onSelectPaymentTab(mode: Int)
}

interface OnWalletFragmentViewCreatedListener {
fun onWalletFragmentViewCreated()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ open class DashPayViewModel(application: Application) : AndroidViewModel(applica
contactsLiveData.value = UsernameSearch(text, orderBy)
}

val isPlatformAvailableLiveData = liveData(Dispatchers.IO) {
emit(Resource.loading(null))
emit(platformRepo.isPlatformAvailable())
}

fun searchNotifications(text: String) {
notificationsLiveData.query = text
}
Expand Down