Skip to content

Instantly share code, notes, and snippets.

@pakoito
Created March 31, 2019 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pakoito/7802ab6a2389721519a1f0d6da691d31 to your computer and use it in GitHub Desktop.
Save pakoito/7802ab6a2389721519a1f0d6da691d31 to your computer and use it in GitHub Desktop.
Refactor
private fun loggedInMenuUi(menu: Menu, rootView: View, checkoutCartStream: Stream<Bla>, startActivity: () -> Unit) {
val alertMenuItem = menu.findItem(R.id.activity_main_alerts_menu_item)
val rootView = (alertMenuItem.actionView as FrameLayout).apply {
setOnClickListener {
startActivity()
}
}
val redCircle: FrameLayout = rootView.findViewById(R.id.view_alert_red_circle)
val countTextView: TextView = rootView.findViewById(R.id.view_alert_count_textview)
suspend fun updateItemCount(itemsInCart: List<Item>) {
withContext(Dispatchers.Main) {
val itemsInCartCount = itemsInCart.size
if (itemsInCartCount == 0) {
redCircle.visibility = View.GONE
} else {
redCircle.visibility = View.VISIBLE
countTextView.text = itemsInCartCount.toString()
}
}
}
launch {
checkoutCart
.itemsInCartStream()
.consumeEach { itemsInCart ->
updateItemCount(itemsInCart)
}
}
}
@handstandsam
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment