0

I am trying to make generic implementation for adding and showing progress bar in fragment / activity when there are multiple calls.

Please check image for reference

Anyone have any better solution rather than taking two references of progress bar and toggling its visibility? The implementation should be generic which can be applied to any views.

The90sArtist
  • 177
  • 1
  • 1
  • 9

1 Answers1

1

Add this interface in your Project....

public interface RetrofitCallback {

    <T> void getSuccessCallBack(Response<T> response, int position);

    void getErrorCallBack(String message, int position);

}

add these methods in your Utility or RetrofitUtility.

public static <T> void callRetrofit(final Context context, final Fragment fragment, Call<T> call, final int position, final RetrofitCallback callback) {

        final ProgressDialog pDialog = CommonMethod.showProgressDialog(context, context.getResources().getString(R.string.loading));// progress for whole application
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                pDialog.dismiss();
                if (response.isSuccessful()) {
                    ResultBody result = (ResultBody) response.body();
                    if (result.isSuccess())
                        callback.getSuccessCallBack(response, position);
                    else
                        callback.getErrorCallBack(result.getMessage(), position);

                } else
                    Toast.makeText(context, response.message(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                pDialog.dismiss();
                if (CommonMethod.checkconnection(context)) { // checking internet connection
                    Toast.makeText(context, "Server_error", Toast.LENGTH_SHORT).show();
                } else {
                    CommonMethod.showconnectiondialog(context, fragment);
                }
            }
        });
    }

  public static Retrofit getClient() {

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl("XXXXXXXXXXXXX")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }

        return retrofit;
    }

All Retrofit API

public interface RetrofitApi {

    @POST("allData")
    Call<UserData> getAllData();

}

Implement RetrofitCallback in your Activity and Fragment

Call Api like this

 Call<UserData> call = ApiClient.getClient().create(RetrofitApi.class).getAllData();
 ApiClient.callRetrofit(context, this, call, 0, this);

and you will get data from RetrofitCallback override methods below

 @Override
    public <T> void getSuccessCallBack(Response<T> response, int position) {
        UserData userData = ((UserData) response.body());
        // do your operation over here 
    }

    @Override
    public void getErrorCallBack(String message, int position) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

Here is all models file....

ResultBody.class

public class ResultBody {
    private String message;
    private boolean success;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}

UserData.class

public class UserData extends ResultBody {

    private User userData;



    public User getUserData() {
        return userData;
    }

    public void setUserData(User user) {
        this.userData = user;
    }
}

Let me know if you stuck anywhere.....

Relative Posts or Questions

Android:dynamically pass model class to retrofit callback

sushildlh
  • 8,986
  • 4
  • 33
  • 77