Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DawnImpulse/c7881d868201200f747443a3e66d0f0d to your computer and use it in GitHub Desktop.
Save DawnImpulse/c7881d868201200f747443a3e66d0f0d to your computer and use it in GitHub Desktop.
/**
* Layout for ingredients in recipe info
*/
fun ingredientLayout(ingredientsList: List<RecipeIngredientsPojo>) {
var i = 1
mutableListIngredients.clear()
ingredientLayout.removeAllViews()
if (!ingredientsList.isEmpty()) {
for (ingredient in ingredientsList) {
val layout = layoutInflater.inflate(R.layout.dynamic_ingredients, null)
val checkBox = layout.findViewById<View>(R.id.ingredientCheck) as CheckImageView
val ingredientQuantity = layout.findViewById<View>(R.id.ingredientQuantity) as TextView
val ingredientName = layout.findViewById<View>(R.id.ingredientName) as TextView
checkBox.id = i
ingredientQuantity.id = 100 + i
ingredientName.id = 200 + i
nonDarkColor?.let {
checkBox.setColorFilter(nonDarkColor!!)
}
ingredient.ingredientId?.let { id ->
IngredientsModel().getItem(id, object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null) {
response?.let { response1 ->
val pojo = response1 as IngredientPojo
mutableListIngredients.add(pojo)
pojo.name?.let { names ->
ingredientName.text = " ${names[Const.EN]}"
}
ingredient.ingredientQuantity?.let { quantity ->
ingredientQuantity.text = "${quantity[selectedPeopleNo.toString()].toString()} ${pojo.unit}"
}
buyList?.let {
buyList!!.ingredients?.let {
buyList!!.ingredients!!
.filter { id == it.ingredientId }
.forEach { checkBox.setViewChecked(true) }
}
}
}
}
}
}, Const.CALLBACK_4)
}
val clickListener = View.OnClickListener() { view ->
when (view.id) {
in 1..100 -> {
if (Prefs.contains(Const.LOGIN)) {
var ch = layout.findViewById<View>(view.id) as CheckImageView
var newBuyList: BuyListPojo?
if (!ch.checked) {
var ingredientPojo = BuyListIngredientsPojo(ingredientsList[view.id - 1].ingredientId,
recipePojo!!.recipeIngredientsPojo!![view.id - 1].ingredientQuantity?.get(selectedPeopleNo.toString()), false)
newBuyList = if (buyList == null || buyList!!.ingredients == null) {
var tempBuyList: MutableList<BuyListIngredientsPojo> = ArrayList()
tempBuyList.add(ingredientPojo)
BuyListPojo(userId, tempBuyList)
} else {
var oldBuyList = buyList!!.ingredients
oldBuyList!!.add(ingredientPojo)
BuyListPojo(userId, oldBuyList)
}
ch.setViewChecked(true)
} else {
var oldBuyList = buyList!!.ingredients
newBuyList = if (oldBuyList!!.size > 1) {
val k = oldBuyList
.takeWhile { it.ingredientId != ingredientsList[view.id - 1].ingredientId }
.count()
oldBuyList.removeAt(k)
BuyListPojo(userId, oldBuyList)
} else
null
ch.setViewChecked(false)
}
if (newBuyList == null) {
BuyListModel().removeList("$recipeId$userId", object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null)
//L.d(NAME, response.toString())
else
L.e(NAME, error.getString(Const.ERROR_MESSAGE))
}
}, Const.CALLBACK_10)
} else {
BuyListModel().updateBuyList("$recipeId$userId", newBuyList, object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null)
//L.d(NAME, response.toString())
else
L.e(NAME, error!!.getString(Const.ERROR_MESSAGE))
}
}, Const.CALLBACK_10)
}
} else {
Toast.makeText(this, "Please sign in", Toast.LENGTH_SHORT).show()
}
}
in 200..299 -> {
var json = JSONObject()
var iObject = mutableListIngredients[view.id - 201]
iObject?.let {
json.put(Const.NAME, iObject.name?.get("en"))
json.put(Const.IMAGE, iObject.imageUrl)
json.put(Const.DESCRIPTION, iObject.remarks)
Dialog.ingredientDialog(this, json)
}
}
}
}
ingredientName.setOnClickListener(clickListener)
checkBox.setOnClickListener(clickListener)
ingredientLayout.addView(layout)
i++
}
}
}
/**
* Layout for buy list
*/
fun buyList(buyList: BuyListPojo) {
buyListLayout.removeAllViews()
var i = 1
buyList.ingredients?.let {
if (!buyList.ingredients!!.isEmpty()) {
for (ingredient in buyList.ingredients!!) {
val layout = layoutInflater.inflate(R.layout.dynamic_buy_list, null)
val checkBox = layout.findViewById<View>(R.id.ingredientCheck) as CheckImageView
val ingredientQuantity = layout.findViewById<View>(R.id.ingredientQuantity) as TextView
val ingredientName = layout.findViewById<View>(R.id.ingredientName) as TextView
val listCross = layout.findViewById<View>(R.id.listCross) as AppCompatImageView
checkBox.id = i
ingredientQuantity.id = 100 + i
ingredientName.id = 200 + i
listCross.id = 300 + i
nonDarkColor?.let { checkBox.setColorFilter(nonDarkColor!!) }
ingredient.ingredientId?.let { id ->
IngredientsModel().getItem(id, object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null) {
response?.let { response1 ->
val pojo = response1 as IngredientPojo
pojo.name?.let { names ->
ingredientName.text = " ${names[Const.EN]}"
}
ingredient.ingredientQuantity?.let { quantity ->
ingredientQuantity.text = "${quantity.toString()} ${pojo.unit}"
}
if (ingredient.ingredientUsed) {
Config.textStrike(ingredientQuantity)
Config.textStrike(ingredientName)
checkBox.setViewChecked(true)
} else {
Config.removeTextStrike(ingredientQuantity)
Config.removeTextStrike(ingredientName)
checkBox.setViewChecked(false)
}
}
}
}
}, Const.CALLBACK_4)
}
val clickListener = View.OnClickListener() { view ->
when (view.id) {
in 1..100 -> {
var ch = layout.findViewById<View>(view.id) as CheckImageView
var iQ = layout.findViewById<View>(100 + view.id) as TextView
var iN = layout.findViewById<View>(200 + view.id) as TextView
if (!ch.checked) {
Config.textStrike(iQ)
Config.textStrike(iN)
ch.setViewChecked(true)
buyList.ingredients!![view.id - 1].ingredientUsed = true
} else {
Config.removeTextStrike(iQ)
Config.removeTextStrike(iN)
ch.setViewChecked(false)
buyList.ingredients!![view.id - 1].ingredientUsed = false
}
BuyListModel().updateBuyList("$recipeId$userId", buyList, object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null) {
} else L.e(NAME, error.getString(Const.ERROR_MESSAGE))
}
}, Const.CALLBACK_13)
}
in 300..400 -> {
var oldBuyList = buyList!!.ingredients
if (oldBuyList!!.size > 1) {
val k = oldBuyList
.takeWhile { it.ingredientId != buyList.ingredients!![view.id - 301].ingredientId }
.count()
oldBuyList.removeAt(k)
BuyListModel().updateBuyList("$recipeId$userId", BuyListPojo(userId, oldBuyList), object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null)
//L.d(NAME, response.toString())i
else
L.e(NAME, error!!.getString(Const.ERROR_MESSAGE))
}
}, Const.CALLBACK_10)
} else {
BuyListModel().removeList("$recipeId$userId", object : OnCallbackListener {
override fun OnCallback(error: JSONObject?, response: Any?, callbackId: Int) {
if (error == null)
//L.d(NAME, response.toString())
else
L.e(NAME, error!!.getString(Const.ERROR_MESSAGE))
}
}, Const.CALLBACK_10)
}
}
}
}
checkBox.setOnClickListener(clickListener)
listCross.setOnClickListener(clickListener)
buyListLayout.addView(layout)
i++
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment