Skip to content

Instantly share code, notes, and snippets.

@dubeboy
Created September 15, 2019 22:02
Show Gist options
  • Save dubeboy/ee2e0dc57bad3c725148374917ecc803 to your computer and use it in GitHub Desktop.
Save dubeboy/ee2e0dc57bad3c725148374917ecc803 to your computer and use it in GitHub Desktop.
private fun postNewGroceryItemToServer(dialog: DialogInterface, view: View) {
val etItemName = view.findViewById<EditText>(R.id.et_grocery_item_name)
val checkBoxIsAvailable = view.findViewById<CheckBox>(R.id.checkbox_grocery_item_is_available)
val groceryItem = GroceryItem(etItemName.text.toString(), checkBoxIsAvailable.isChecked)
swipe_refresh_grocery_items.isRefreshing = true
GroceryServiceFactory.makeService().addGroceryItem(groceryItem).enqueue(object: Callback<StatusResponseEntity<GroceryItem>?> {
override fun onFailure(call: Call<StatusResponseEntity<GroceryItem>?>, t: Throwable) {
swipe_refresh_grocery_items.isRefreshing = false
Toast.makeText(this@MainActivity, "Oops something went wrong please check your internet connection", Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<StatusResponseEntity<GroceryItem>?>,
response: Response<StatusResponseEntity<GroceryItem>?>
) {
swipe_refresh_grocery_items.isRefreshing = false
if (response.body() != null && response.body()?.status == true) { // our business rule is that if status is true then entity is not null
groceryItemAdapter.add(response.body()!!.entity!!)
} else {
Toast.makeText(this@MainActivity, "Something went terribly wrong", Toast.LENGTH_LONG).show()
}
}
})
dialog.dismiss()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment