zircles-android/app/src/main/java/cash/z/ecc/android/ui/receive/ReceiveFragment.kt

100 lines
3.6 KiB
Kotlin
Raw Normal View History

2019-11-26 12:29:16 -08:00
package cash.z.ecc.android.ui.receive
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import cash.z.android.qrecycler.QRecycler
2020-01-08 00:46:40 -08:00
import cash.z.ecc.android.R
import cash.z.ecc.android.databinding.FragmentReceiveNewBinding
import cash.z.ecc.android.di.viewmodel.viewModel
2020-01-08 00:46:40 -08:00
import cash.z.ecc.android.ext.onClickNavBack
import cash.z.ecc.android.ext.onClickNavTo
import cash.z.ecc.android.feedback.Report
import cash.z.ecc.android.feedback.Report.Tap.*
2019-11-26 12:29:16 -08:00
import cash.z.ecc.android.ui.base.BaseFragment
import cash.z.ecc.android.sdk.ext.toAbbreviatedAddress
import cash.z.ecc.android.sdk.ext.twig
2019-11-26 12:29:16 -08:00
import kotlinx.coroutines.launch
import kotlin.math.roundToInt
2019-11-26 12:29:16 -08:00
2020-01-08 00:46:40 -08:00
class ReceiveFragment : BaseFragment<FragmentReceiveNewBinding>() {
override val screen = Report.Screen.RECEIVE
private val viewModel: ReceiveViewModel by viewModel()
2019-11-26 12:29:16 -08:00
lateinit var qrecycler: QRecycler
2020-01-08 00:46:40 -08:00
// lateinit var addressParts: Array<TextView>
2019-11-26 12:29:16 -08:00
2020-01-08 00:46:40 -08:00
override fun inflate(inflater: LayoutInflater): FragmentReceiveNewBinding =
FragmentReceiveNewBinding.inflate(inflater)
2019-11-26 12:29:16 -08:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2020-01-08 00:46:40 -08:00
// addressParts = arrayOf(
// text_address_part_1,
// text_address_part_2,
// text_address_part_3,
// text_address_part_4,
// text_address_part_5,
// text_address_part_6,
// text_address_part_7,
// text_address_part_8
// )
2020-01-09 23:53:16 -08:00
binding.buttonScan.setOnClickListener {
mainActivity?.maybeOpenScan(R.id.action_nav_receive_to_nav_scan).also { tapped(RECEIVE_SCAN) }
2020-01-09 23:53:16 -08:00
}
binding.backButtonHitArea.onClickNavBack() { tapped(RECEIVE_BACK) }
2019-11-26 12:29:16 -08:00
}
override fun onAttach(context: Context) {
qrecycler = QRecycler() // inject! :)
super.onAttach(context)
}
override fun onResume() {
super.onResume()
resumedScope.launch {
onAddressLoaded(viewModel.getAddress())
2019-11-26 12:29:16 -08:00
}
}
private fun onAddressLoaded(address: String) {
twig("address loaded: $address length: ${address.length}")
2019-11-26 12:29:16 -08:00
qrecycler.load(address)
.withQuietZoneSize(3)
.withCorrectionLevel(QRecycler.CorrectionLevel.MEDIUM)
2020-01-08 00:46:40 -08:00
.into(binding.receiveQrCode)
2019-11-26 12:29:16 -08:00
binding.receiveAddress.text = address.toAbbreviatedAddress(12, 12)
2020-01-08 00:46:40 -08:00
// address.distribute(8) { i, part ->
// setAddressPart(i, part)
// }
2019-11-26 12:29:16 -08:00
}
private fun <T> String.distribute(chunks: Int, block: (Int, String) -> T) {
val charsPerChunk = length / 8.0
val wholeCharsPerChunk = charsPerChunk.toInt()
val chunksWithExtra = ((charsPerChunk - wholeCharsPerChunk) * chunks).roundToInt()
repeat(chunks) { i ->
val part = if (i < chunksWithExtra) {
substring(i * (wholeCharsPerChunk + 1), (i + 1) * (wholeCharsPerChunk + 1))
} else {
substring(i * wholeCharsPerChunk + chunksWithExtra, (i + 1) * wholeCharsPerChunk + chunksWithExtra)
}
block(i, part)
}
}
2020-01-08 00:46:40 -08:00
// private fun setAddressPart(index: Int, addressPart: String) {
// Log.e("TWIG", "setting address for part $index) $addressPart")
// val thinSpace = "\u2005" // 0.25 em space
// val textSpan = SpannableString("${index + 1}$thinSpace$addressPart")
//
// textSpan.setSpan(AddressPartNumberSpan(), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
//
// addressParts[index].text = textSpan
// }
2019-11-26 12:29:16 -08:00
}