Updated on 2026-08-14

This commit is contained in:
Tangem 2020-04-01 15:11:19 +03:00
parent 8b1440146c
commit 8954738212
3 changed files with 35 additions and 5 deletions

View file

@ -36,6 +36,7 @@ abstract class BaseCardActionFragment : Fragment(), LayoutHolder {
}
protected val viewModel: ParamsViewModel by viewModels { ActionViewModelFactory(itemsManager) }
protected lateinit var mainView: View
protected val widgetList = mutableListOf<ParameterWidget>()
private val itemsManager: ItemsManager by lazy { ParamsManagerFactory.createFactory().get(getAction())!! }
@ -51,7 +52,7 @@ abstract class BaseCardActionFragment : Fragment(), LayoutHolder {
viewModel.setCardManager(CardManager.init(requireActivity()))
initFab()
createWidgets { subscribeToViewModelChanges(it) }
createWidgets { subscribeToViewModelChanges() }
}
private fun initFab() {
@ -59,29 +60,40 @@ abstract class BaseCardActionFragment : Fragment(), LayoutHolder {
mainView.find<FloatingActionButton>(R.id.fab_action)?.setOnClickListener { viewModel.invokeMainAction() }
}
private fun createWidgets(callback: (List<ParameterWidget>) -> Unit) {
private fun createWidgets(widgetCreatedCallback: () -> Unit) {
Log.d(this, "createWidgets")
viewModel.ldParams.observe(viewLifecycleOwner, Observer { itemList ->
val widgetList = mutableListOf<ParameterWidget>()
itemList.forEach { param ->
val widget = ParameterWidget(inflateParamView(incomingParamsContainer), param)
widget.onValueChanged = { id, value -> viewModel.userChangedItem(id, value) }
widget.onActionBtnClickListener = viewModel.getItemAction(param.id)
widgetList.add(widget)
}
callback(widgetList)
widgetCreatedCallback()
})
}
private fun subscribeToViewModelChanges(widgetList: List<ParameterWidget>) {
private fun subscribeToViewModelChanges() {
Log.d(this, "subscribeToViewModelChanges")
listenResponse()
listenError()
listenChangedItems()
listenDescriptionSwitchChanges()
}
protected open fun listenResponse() {
val tvResponse by lazy { mainView.findViewById<TextView>(R.id.tv_action_response_json) }
viewModel.ldResponse.observe(viewLifecycleOwner, Observer {
Log.d(this, "action response: ${if (it.length > 50) it.substring(0..50) else it}")
tvResponse.text = it
})
}
protected open fun listenError() {
viewModel.seError.observe(viewLifecycleOwner, Observer { showSnackbarMessage(it) })
}
protected open fun listenChangedItems() {
viewModel.seChangedItems.observe(viewLifecycleOwner, Observer { itemList ->
itemList.forEach { item ->
Log.d(this, "item changed from VM - name: ${item.id}")
@ -90,6 +102,9 @@ abstract class BaseCardActionFragment : Fragment(), LayoutHolder {
widgetList.firstOrNull { it.id == item.id }?.changeParamValue(item.viewModel.data)
}
})
}
protected open fun listenDescriptionSwitchChanges() {
val mainActViewModel by activityViewModels<MainViewModel>()
mainActViewModel.ldDescriptionSwitch.observe(viewLifecycleOwner, Observer { isEnabled ->
widgetList.forEach { it.toggleDescriptionVisibility(isEnabled) }

View file

@ -1,8 +1,12 @@
package com.tangem.tangemtest.ucase.variants.scan.ui
import android.widget.TextView
import androidx.lifecycle.Observer
import com.tangem.tangemtest.R
import com.tangem.tangemtest.ucase.resources.ActionType
import com.tangem.tangemtest.ucase.ui.BaseCardActionFragment
import ru.dev.gbixahue.eu4d.lib.android._android.views.show
import ru.dev.gbixahue.eu4d.lib.android.global.log.Log
/**
[REDACTED_AUTHOR]
@ -12,4 +16,14 @@ class ScanActionFragment : BaseCardActionFragment() {
override fun getLayoutId(): Int = R.layout.fg_action_card_scan
override fun getAction(): ActionType = ActionType.Scan
override fun listenResponse() {
val tvStub = mainView.findViewById<TextView>(R.id.tv_screen_stub)
val tvResponse by lazy { mainView.findViewById<TextView>(R.id.tv_action_response_json) }
viewModel.ldResponse.observe(viewLifecycleOwner, Observer {
Log.d(this, "action response: ${if (it.length > 50) it.substring(0..50) else it}")
tvStub.show(false)
tvResponse.text = it
})
}
}

View file

@ -29,6 +29,7 @@
</ScrollView>
<TextView
android:id="@+id/tv_screen_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"