Added the behavior to include the senders address

Addresses #63
This commit is contained in:
Kevin Gorham 2020-01-09 12:58:57 -05:00
parent cb9e6cc4b4
commit 83969f0eb4
No known key found for this signature in database
GPG Key ID: CCA55602DF49FC38
5 changed files with 92 additions and 16 deletions

View File

@ -6,6 +6,10 @@ import cash.z.ecc.android.ui.MainActivity
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.channelFlow
fun View.gone() = goneIf(true)
fun View.invisible() = invisibleIf(true)
fun View.goneIf(isGone: Boolean) {
visibility = if (isGone) GONE else VISIBLE
}

View File

@ -36,8 +36,8 @@ class SendConfirmFragment : BaseFragment<FragmentSendConfirmBinding>() {
"Send ${sendViewModel.zatoshiAmount.convertZatoshiToZecString(8)} ZEC to ${sendViewModel?.toAddress.toAbbreviatedAddress()}?"
}
sendViewModel.memo.trim().isNotEmpty().let { hasMemo ->
binding.radioIncludeAddress.isChecked = hasMemo
binding.radioIncludeAddress.goneIf(!hasMemo)
binding.radioIncludeAddress.isChecked = hasMemo || sendViewModel.includeFromAddress
binding.radioIncludeAddress.goneIf(!(hasMemo || sendViewModel.includeFromAddress))
}
}

View File

@ -7,6 +7,8 @@ import android.view.inputmethod.EditorInfo
import cash.z.ecc.android.R
import cash.z.ecc.android.databinding.FragmentSendMemoBinding
import cash.z.ecc.android.di.viewmodel.activityViewModel
import cash.z.ecc.android.ext.gone
import cash.z.ecc.android.ext.goneIf
import cash.z.ecc.android.ext.onClickNavTo
import cash.z.ecc.android.ui.base.BaseFragment
@ -23,21 +25,17 @@ class SendMemoFragment : BaseFragment<FragmentSendMemoBinding>() {
onAddMemo()
}
binding.buttonSkip.setOnClickListener {
binding.inputMemo.setText("")
sendViewModel.memo = ""
mainActivity?.navController?.navigate(R.id.action_nav_send_memo_to_send_confirm)
onSkip()
}
R.id.action_nav_send_memo_to_nav_send_address.let {
binding.backButtonHitArea.onClickNavTo(it)
onBackPressNavTo(it)
}
binding.radioIncludeAddress.setOnClickListener {
if (binding.radioIncludeAddress.isActivated) {
binding.radioIncludeAddress.isChecked = false
} else {
binding.radioIncludeAddress.isActivated = true
}
binding.checkIncludeAddress.setOnCheckedChangeListener { _, _->
onIncludeMemo(binding.checkIncludeAddress.isChecked)
}
binding.inputMemo.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
onAddMemo()
@ -46,11 +44,39 @@ class SendMemoFragment : BaseFragment<FragmentSendMemoBinding>() {
false
}
}
binding.radioIncludeAddress.requestFocus()
sendViewModel.afterInitFromAddress {
binding.textIncludedAddress.text = "sent from ${sendViewModel.fromAddress}"
}
binding.textIncludedAddress.gone()
applyModel()
}
private fun applyModel() {
binding.inputMemo.setText(sendViewModel.memo)
binding.checkIncludeAddress.isChecked = sendViewModel.includeFromAddress
}
private fun onIncludeMemo(checked: Boolean) {
binding.textIncludedAddress.goneIf(!checked)
sendViewModel.includeFromAddress = checked
if (checked) binding.inputMemo.setHint("") else binding.inputMemo.setHint("Add a memo here")
}
private fun onSkip() {
binding.inputMemo.setText("")
sendViewModel.memo = ""
sendViewModel.includeFromAddress = false
onNext()
}
private fun onAddMemo() {
sendViewModel.memo = binding.inputMemo.text.toString()
onNext()
}
private fun onNext() {
mainActivity?.navController?.navigate(R.id.action_nav_send_memo_to_send_confirm)
}
}

View File

@ -1,6 +1,7 @@
package cash.z.ecc.android.ui.send
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cash.z.ecc.android.lockbox.LockBox
import cash.z.ecc.android.ui.setup.WalletSetupViewModel
import cash.z.wallet.sdk.Initializer
@ -11,6 +12,7 @@ import cash.z.wallet.sdk.ext.twig
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
class SendViewModel @Inject constructor() : ViewModel() {
@ -25,6 +27,7 @@ class SendViewModel @Inject constructor() : ViewModel() {
lateinit var initializer: Initializer
fun send(): Flow<PendingTransaction> {
val memoToSend = if (includeFromAddress) "$memo\nsent from\n$fromAddress" else memo
val keys = initializer.deriveSpendingKeys(
lockBox.getBytes(WalletSetupViewModel.LockBoxKey.SEED)!!
)
@ -32,7 +35,7 @@ class SendViewModel @Inject constructor() : ViewModel() {
keys[0],
zatoshiAmount,
toAddress,
memo
memoToSend
).onEach {
twig(it.toString())
}
@ -54,7 +57,23 @@ class SendViewModel @Inject constructor() : ViewModel() {
}
}
fun afterInitFromAddress(block: () -> Unit) {
viewModelScope.launch {
fromAddress = synchronizer.getAddress()
block()
}
}
var fromAddress: String = ""
var toAddress: String = ""
var memo: String = ""
var zatoshiAmount: Long = -1L
var includeFromAddress: Boolean = false
set(value) {
require(!value || (value && !fromAddress.isNullOrEmpty())) {
"Error: from address was empty while attempting to include it in the memo. Verify" +
" that initFromAddress() has previously been called on this viewmodel."
}
field = value
}
}

View File

@ -60,11 +60,38 @@
app:layout_constraintWidth_percent="0.8"
tools:text="This is my memo" />
<RadioButton
android:id="@+id/radio_include_address"
<TextView
android:id="@+id/text_included_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="6dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:textColor="@color/text_light_dimmed"
tools:text="sent from z23lk4jjl2k3jl43kkj542l3kl4hj2l3k1j41l2kjk423lkj423lklhk2jrhiuhrh2j4hh2hkj23hkj4"
app:layout_constraintStart_toStartOf="@id/input_memo"
app:layout_constraintEnd_toEndOf="@id/input_memo"
app:layout_constraintBottom_toBottomOf="@id/input_memo" />
<View
android:layout_width="0dp"
android:layout_height="1px"
android:elevation="6dp"
android:layout_marginBottom="4dp"
android:background="@color/text_light_dimmed"
app:layout_constraintStart_toStartOf="@id/text_included_address"
app:layout_constraintEnd_toEndOf="@id/text_included_address"
app:layout_constraintTop_toTopOf="@id/text_included_address"/>
<CheckBox
android:id="@+id/check_include_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:padding="0dp"
android:layout_marginRight="0dp"
android:text="Include your sending address in memo"
app:layout_constraintStart_toStartOf="@+id/input_memo"
app:layout_constraintTop_toBottomOf="@+id/input_memo" />
@ -79,7 +106,7 @@
android:text="Your transaction is shielded and your address is unavailable to recipient"
app:layout_constraintEnd_toEndOf="@id/input_memo"
app:layout_constraintStart_toStartOf="@id/input_memo"
app:layout_constraintTop_toBottomOf="@id/radio_include_address" />
app:layout_constraintTop_toBottomOf="@id/check_include_address" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button_next"