Skip to content

Instantly share code, notes, and snippets.

@laurentyhuel
Created May 9, 2022 08:33
Show Gist options
  • Save laurentyhuel/5f7e9b9d9e99f3455cc81e43b79b7e40 to your computer and use it in GitHub Desktop.
Save laurentyhuel/5f7e9b9d9e99f3455cc81e43b79b7e40 to your computer and use it in GitHub Desktop.
2x screen CustomerDisplayPresentation
class CustomerDisplayPresentation(
context: Context,
display: Display,
private var lifecycleOwner: LifecycleOwner?,
private var customerDisplayViewModel: CustomerDisplayViewModel?
) : Presentation(context, display) {
private val adapter = OrderItemPresentationAdapter(context)
private lateinit var binding: PresentationCustomerDisplayBinding
private var cartItems: List<OrderItemModel>
init {
setCancelable(false)
cartItems = emptyList()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.presentation_customer_display, null, false)
setContentView(binding.root)
//cart layout
binding.orderSummaryOrderedItems.layoutManager = LinearLayoutManager(context)
binding.orderSummaryOrderedItems.adapter = adapter
//confirm print layout
binding.confirmPrintReceiptActionCancel.setOnClickListener {
onClick(false)
}
binding.confirmPrintReceiptAction.setOnClickListener {
onClick(true)
}
ifLet(customerDisplayViewModel, lifecycleOwner) { customerDisplayViewModel, lifecycleOwner ->
customerDisplayViewModel.cartItemsLiveData.observe(lifecycleOwner) {
cartItems = it ?: emptyList()
adapter.setData(cartItems)
refreshUI()
}
customerDisplayViewModel.stateLiveData.observe(lifecycleOwner) {
binding.viewFlipper.displayedChild = it.flipperViewIndex
}
}
refreshUI()
}
private fun refreshUI() {
if (::binding.isInitialized) {
binding.orderSummaryOrderedItems.isVisible = cartItems.isNotEmpty()
binding.emptyCartText.isVisible = cartItems.isEmpty()
binding.emptyCartImage.isVisible = cartItems.isEmpty()
if (cartItems.size > 1) {
//scroll to last items
binding.orderSummaryOrderedItems.scrollToPosition(cartItems.size - 1)
}
}
}
private fun onClick(shouldPrint: Boolean) {
customerDisplayViewModel?.confirmPrintReceiptListener?.onConfirmPrint(shouldPrint)
}
fun clear() {
lifecycleOwner = null
customerDisplayViewModel = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment