Skip to content

Commit 3e87511

Browse files
committed
Encapsulate missing key warning into reusable MissingKeyView
1 parent 4e8a550 commit 3e87511

7 files changed

Lines changed: 103 additions & 101 deletions

File tree

app/src/main/java/one/mixin/android/ui/wallet/ClassicWalletFragment.kt

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import kotlinx.coroutines.withContext
2727
import one.mixin.android.Constants
2828
import one.mixin.android.R
2929
import one.mixin.android.RxBus
30+
import one.mixin.android.crypto.CryptoWalletHelper
3031
import one.mixin.android.databinding.FragmentPrivacyWalletBinding
3132
import one.mixin.android.databinding.ViewWalletFragmentHeaderBinding
3233
import one.mixin.android.db.web3.vo.Web3TokenItem
@@ -35,7 +36,6 @@ import one.mixin.android.db.web3.vo.isWatch
3536
import one.mixin.android.event.QuoteColorEvent
3637
import one.mixin.android.event.WalletRefreshedEvent
3738
import one.mixin.android.extension.dp
38-
import one.mixin.android.extension.highlightStarTag
3939
import one.mixin.android.extension.dpToPx
4040
import one.mixin.android.extension.mainThread
4141
import one.mixin.android.extension.numberFormat2
@@ -204,21 +204,6 @@ class ClassicWalletFragment : BaseFragment(R.layout.fragment_privacy_wallet), He
204204
AnalyticsTracker.trackTradeStart(TradeWallet.WEB3, TradeSource.WALLET_HOME)
205205
SwapActivity.show(requireActivity(), inMixin = false, walletId = walletId)
206206
}
207-
208-
importKeyBtn.setOnClickListener {
209-
lifecycleScope.launch {
210-
val wallet = web3ViewModel.findWalletById(walletId)
211-
val chainId = web3ViewModel.getAddresses(walletId).firstOrNull()?.chainId
212-
if (chainId != null) {
213-
val mode = if (wallet?.category == WalletCategory.IMPORTED_MNEMONIC.value) {
214-
WalletSecurityActivity.Mode.RE_IMPORT_MNEMONIC
215-
} else {
216-
WalletSecurityActivity.Mode.RE_IMPORT_PRIVATE_KEY
217-
}
218-
WalletSecurityActivity.show(requireActivity(), mode, walletId = walletId, chainId = chainId)
219-
}
220-
}
221-
}
222207
}
223208
_headBinding?.pendingView?.isVisible = false
224209

@@ -498,19 +483,20 @@ class ClassicWalletFragment : BaseFragment(R.layout.fragment_privacy_wallet), He
498483
_headBinding?.watchLayout?.isVisible = isWatch
499484

500485
if (isMissingKey) {
501-
val isMnemonic = wallet?.category == WalletCategory.IMPORTED_MNEMONIC.value
502-
_headBinding?.importKeyBtn?.text = getString(
503-
if (isMnemonic) R.string.Import_Mnemonic_Phrase else R.string.import_private_key
504-
)
505-
val learn = getString(R.string.Learn_More)
506-
val info = getString(
507-
if (isMnemonic) R.string.Import_Mnemonic_Phrase_Desc else R.string.Import_Private_Key_Desc,
508-
learn
509-
)
510-
val learnUrl = getString(
511-
if (isMnemonic) R.string.import_mnemonic_phrase_url else R.string.import_private_key_url
512-
)
513-
_headBinding?.missingKeyTv?.highlightStarTag(info, arrayOf(learnUrl))
486+
val isMnemonic = wallet.category == WalletCategory.IMPORTED_MNEMONIC.value
487+
_headBinding?.missingKeyView?.setMissingKey(isMnemonic) {
488+
lifecycleScope.launch {
489+
val chainId = web3ViewModel.getAddresses(id).firstOrNull()?.chainId
490+
if (chainId != null) {
491+
val mode = if (isMnemonic) {
492+
WalletSecurityActivity.Mode.RE_IMPORT_MNEMONIC
493+
} else {
494+
WalletSecurityActivity.Mode.RE_IMPORT_PRIVATE_KEY
495+
}
496+
WalletSecurityActivity.show(requireActivity(), mode, walletId = id, chainId = chainId)
497+
}
498+
}
499+
}
514500
}
515501

516502
if (isWatch) {

app/src/main/java/one/mixin/android/web3/details/Web3TransactionsFragment.kt

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import one.mixin.android.db.web3.vo.solLamportToAmount
3535
import one.mixin.android.extension.buildAmountSymbol
3636
import one.mixin.android.extension.colorAttr
3737
import one.mixin.android.extension.colorFromAttribute
38-
import one.mixin.android.extension.highlightStarTag
3938
import one.mixin.android.extension.dp
4039
import one.mixin.android.extension.getClipboardManager
4140
import one.mixin.android.extension.getParcelableCompat
@@ -253,17 +252,6 @@ class Web3TransactionsFragment : BaseFragment(R.layout.fragment_web3_transaction
253252
)
254253
}
255254

256-
importKeyBtn.setOnClickListener {
257-
lifecycleScope.launch {
258-
val wallet = web3ViewModel.findWalletById(token.walletId)
259-
val mode = if (wallet?.category == WalletCategory.IMPORTED_MNEMONIC.value) {
260-
WalletSecurityActivity.Mode.RE_IMPORT_MNEMONIC
261-
} else {
262-
WalletSecurityActivity.Mode.RE_IMPORT_PRIVATE_KEY
263-
}
264-
WalletSecurityActivity.show(requireActivity(), mode, walletId = token.walletId, chainId = token.chainId)
265-
}
266-
}
267255

268256
transactionsTitleLl.setOnClickListener {
269257
view.navigate(
@@ -535,18 +523,14 @@ class Web3TransactionsFragment : BaseFragment(R.layout.fragment_web3_transaction
535523

536524
if (isMissingKey) {
537525
val isMnemonic = wallet?.category == WalletCategory.IMPORTED_MNEMONIC.value
538-
binding.importKeyBtn.text = getString(
539-
if (isMnemonic) R.string.Import_Mnemonic_Phrase else R.string.import_private_key
540-
)
541-
val learn = getString(R.string.Learn_More)
542-
val info = getString(
543-
if (isMnemonic) R.string.Import_Mnemonic_Phrase_Desc else R.string.Import_Private_Key_Desc,
544-
learn
545-
)
546-
val learnUrl = getString(
547-
if (isMnemonic) R.string.import_mnemonic_phrase_url else R.string.import_private_key_url
548-
)
549-
binding.missingKeyTv.highlightStarTag(info, arrayOf(learnUrl))
526+
binding.missingKeyView.setMissingKey(isMnemonic) {
527+
val mode = if (isMnemonic) {
528+
WalletSecurityActivity.Mode.RE_IMPORT_MNEMONIC
529+
} else {
530+
WalletSecurityActivity.Mode.RE_IMPORT_PRIVATE_KEY
531+
}
532+
WalletSecurityActivity.show(requireActivity(), mode, walletId = token.walletId, chainId = token.chainId)
533+
}
550534
}
551535

552536
if (token.isNativeSolToken() && wallet != null && (wallet.category == WalletCategory.CLASSIC.value || (wallet.isImported() && wallet.hasLocalPrivateKey))) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package one.mixin.android.widget
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import android.view.LayoutInflater
6+
import android.widget.LinearLayout
7+
import one.mixin.android.R
8+
import one.mixin.android.databinding.ViewMissingKeyBinding
9+
import one.mixin.android.extension.highlightStarTag
10+
11+
class MissingKeyView @JvmOverloads constructor(
12+
context: Context,
13+
attrs: AttributeSet? = null,
14+
defStyleAttr: Int = 0
15+
) : LinearLayout(context, attrs, defStyleAttr) {
16+
17+
private val binding: ViewMissingKeyBinding
18+
19+
init {
20+
binding = ViewMissingKeyBinding.inflate(LayoutInflater.from(context), this)
21+
orientation = VERTICAL
22+
}
23+
24+
fun setMissingKey(isMnemonic: Boolean, onImportClick: () -> Unit) {
25+
binding.importKeyBtn.text = context.getString(
26+
if (isMnemonic) R.string.Import_Mnemonic_Phrase else R.string.import_private_key
27+
)
28+
29+
val learn = context.getString(R.string.Learn_More)
30+
val info = context.getString(
31+
if (isMnemonic) R.string.Import_Mnemonic_Phrase_Desc else R.string.Import_Private_Key_Desc,
32+
"**$learn**"
33+
)
34+
val learnUrl = context.getString(
35+
if (isMnemonic) R.string.import_mnemonic_phrase_url else R.string.import_private_key_url
36+
)
37+
38+
binding.missingKeyTv.highlightStarTag(info, arrayOf(learnUrl))
39+
binding.importKeyBtn.setOnClickListener { onImportClick() }
40+
}
41+
42+
}

app/src/main/res/layout/fragment_web3_transactions.xml

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,33 +143,14 @@
143143
android:layout_marginEnd="8dp"
144144
android:layout_marginBottom="24dp" />
145145

146-
<LinearLayout
146+
<one.mixin.android.widget.MissingKeyView
147+
android:id="@+id/missing_key_view"
147148
android:layout_width="match_parent"
148149
android:layout_height="wrap_content"
149150
android:layout_marginStart="20dp"
150151
android:layout_marginEnd="20dp"
151152
android:layout_marginTop="30dp"
152-
android:layout_marginBottom="24dp"
153-
android:orientation="vertical">
154-
155-
<Button
156-
android:id="@+id/import_key_btn"
157-
style="@style/AppTheme.MaterialButton"
158-
android:layout_width="match_parent"
159-
android:layout_height="wrap_content"
160-
android:text="@string/import_private_key" />
161-
162-
<TextView
163-
android:id="@+id/missing_key_tv"
164-
android:layout_width="match_parent"
165-
android:layout_height="wrap_content"
166-
android:layout_marginTop="12dp"
167-
android:textColor="?attr/text_assist"
168-
android:textSize="14sp"
169-
android:lineSpacingExtra="2dp"
170-
android:gravity="center_horizontal"
171-
tools:text="The encrypted private key for this wallet is missing from this device. Please re-import to continue. Learn More" />
172-
</LinearLayout>
153+
android:layout_marginBottom="24dp" />
173154

174155
</ViewAnimator>
175156

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<merge xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
tools:parentTag="android.widget.LinearLayout">
7+
8+
<Button
9+
android:id="@+id/import_key_btn"
10+
style="@style/AppTheme.MaterialButton"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:text="@string/import_private_key" />
14+
15+
<TextView
16+
android:id="@+id/missing_key_tv"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:layout_marginTop="12dp"
20+
android:textColor="?attr/text_assist"
21+
android:textSize="14sp"
22+
android:lineSpacingExtra="2dp"
23+
android:gravity="center_horizontal"
24+
android:clickable="true"
25+
android:focusable="true"
26+
tools:text="The encrypted private key for this wallet is missing from this device. Please re-import to continue. Learn More" />
27+
28+
</merge>

app/src/main/res/layout/view_wallet_fragment_header.xml

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,14 @@
107107
android:layout_marginStart="8dp"
108108
android:layout_marginEnd="8dp" />
109109

110-
<LinearLayout
110+
<one.mixin.android.widget.MissingKeyView
111+
android:id="@+id/missing_key_view"
111112
android:layout_width="match_parent"
112113
android:layout_height="wrap_content"
113-
android:layout_marginStart="36dp"
114-
android:layout_marginEnd="36dp"
114+
android:layout_marginStart="20dp"
115+
android:layout_marginEnd="20dp"
115116
android:layout_marginTop="30dp"
116-
android:layout_marginBottom="20dp"
117-
android:orientation="vertical">
118-
119-
<Button
120-
android:id="@+id/import_key_btn"
121-
style="@style/AppTheme.MaterialButton"
122-
android:layout_width="match_parent"
123-
android:layout_height="wrap_content"
124-
android:text="@string/import_private_key" />
125-
126-
<TextView
127-
android:id="@+id/missing_key_tv"
128-
android:layout_width="match_parent"
129-
android:layout_height="wrap_content"
130-
android:layout_marginTop="12dp"
131-
android:textColor="?attr/text_assist"
132-
android:textSize="14sp"
133-
android:lineSpacingExtra="2dp"
134-
android:gravity="center_horizontal"
135-
tools:text="The encrypted private key for this wallet is missing from this device. Please re-import to continue. Learn More" />
136-
</LinearLayout>
117+
android:layout_marginBottom="20dp" />
137118

138119
</ViewAnimator>
139120

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,8 +2107,8 @@
21072107
<string name="NoKey">NoKey</string>
21082108
<string name="Watching">Watching</string>
21092109
<string name="error_too_many_wallets">ERROR 10632: Maximum wallet limit reached</string>
2110-
<string name="Import_Private_Key_Desc">The encrypted private key for this wallet is missing from this device. Please re-import to continue. **%1$s**</string>
2111-
<string name="Import_Mnemonic_Phrase_Desc">The encrypted mnemonic phrase for this wallet is missing from this device. Please re-import to continue. **%1$s**</string>
2110+
<string name="Import_Private_Key_Desc">The encrypted private key for this wallet is missing from this device. Please re-import to continue. %1$s</string>
2111+
<string name="Import_Mnemonic_Phrase_Desc">The encrypted mnemonic phrase for this wallet is missing from this device. Please re-import to continue. %1$s</string>
21122112
<string name="Import_Mnemonic_Phrase">Import Mnemonic Phrase</string>
21132113
<string name="reimport_private_key_error">The imported private key does not match the wallet.</string>
21142114
<string name="reimport_mnemonic_phrase_error">The imported mnemonic phrase does not match the wallet.</string>

0 commit comments

Comments
 (0)