Created
March 21, 2022 15:54
-
-
Save laurentyhuel/baf81afd54f52a16260b9747ec735f42 to your computer and use it in GitHub Desktop.
2x screen CustomerDisplayPresentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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