In project I am implementing "Add to favorite" that when adding have to will record in server DB.
I am getting list of product from json
{
"id": 37,
"name": "Apple",
"price": 205,
"photo": "~/IMG/Full/20166117138.jpg",
"favorite": false
},
If user added item "to favorite" - I am do POST Request using Volley.
URL?user_id=1095&product_id=37
After this POST Request the parameter "favorite" will be changed to "true"
When user add some item to favorite, I am checking connecting timeout I using timer
waitTimer = new CountDownTimer(15000,300) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Toast.makeText(mContext,"Check connecting to internet ",Toast.LENGTH_SHORT).show();
}
}.start();
After 15 sec if mobile not connected with internet, I show message "Check connecting to internet ". If mobile is connected and got response from server timer is stoped and item added to favorite, changing icon.
Problem: All this work fine when user connected with WI-FI or with faster mobile internet, but when weakly internet connecting although the requset comes from server it stayed show message "Check connecting to internet ".
I am use RecyclerView
.
public class PrudactAdapterC extends RecyclerView.Adapter<PrudactAdapterC.PrudactViewHolder> {
@Override
public void onBindViewHolder(final PrudactViewHolder prudactViewHolder, final int i) {
prudactViewHolder.btn_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prudactViewHolder.btn_favorite.setEnabled(false);
prudactViewHolder.ll_favorite.setEnabled(false);
if (p.getFav()==false){
myFadeInAnimation = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
prudactViewHolder.btn_favorite.startAnimation(myFadeInAnimation);
waitTimer = new CountDownTimer(15000,300) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Toast.makeText(mContext,"Check connecting to internet ",Toast.LENGTH_SHORT).show();
prudactViewHolder.btn_favorite.clearAnimation();
myFadeInAnimation.cancel();
prudactViewHolder.btn_favorite.setEnabled(true);
prudactViewHolder.ll_favorite.setEnabled(true);
/*p.setFav(false);
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.icon_favorite_out);*/
}
}.start();
addToFav(p.getId(), prudactViewHolder, p);
}
else {
//
}
}
});
}
void addToFav(final int id, final PrudactViewHolder prudactViewHolder, final PrudactModel p){
String url = URLADD + "userid="+getClientId()+"&tovarid="+id;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("1"))
{
if(waitTimer != null) {
waitTimer.cancel();
waitTimer = null;
}
prudactViewHolder.btn_favorite.clearAnimation();
myFadeInAnimation.cancel();
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.icon_favorite_in);
p.setFav(true);
prudactViewHolder.ll_favorite.setEnabled(true);
prudactViewHolder.btn_favorite.setEnabled(true);
}
else {
if(waitTimer != null) {
waitTimer.cancel();
waitTimer = null;
}
prudactViewHolder.btn_favorite.clearAnimation();
myFadeInAnimation.cancel();
/* prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.icon_favorite_out);
p.setFav(false);*/
prudactViewHolder.ll_favorite.setEnabled(true);
prudactViewHolder.btn_favorite.setEnabled(true);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(mContext,"Check connecting to internet",Toast.LENGTH_SHORT).show();
if(waitTimer != null) {
waitTimer.cancel();
waitTimer = null;
}
prudactViewHolder.btn_favorite.clearAnimation();
myFadeInAnimation.cancel();
// prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.icon_favorite_out);
// p.setFav(false);
prudactViewHolder.ll_favorite.setEnabled(true);
prudactViewHolder.btn_favorite.setEnabled(true);
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
requestQueue.add(stringRequest);
}
}
If my method to implementing "Add to favorite" not right... let me know))