Last active
January 27, 2022 06:55
-
-
Save m7mdra/8a01148edc8ccbb82f9dd2f03c4e97d7 to your computer and use it in GitHub Desktop.
Retrofit callback wrapper with extra method to indicate the state of the request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comtas.com.zoolvibs.network; | |
import javax.annotation.Nullable; | |
/** | |
* Created by m7mdra on 23/12/17. | |
*/ | |
public interface Resource<T> { | |
/** | |
* indicate the resource loading | |
* | |
* @param isLoading true if loading false otherwise | |
*/ | |
void onLoading(boolean isLoading); | |
/** | |
* called when an exception is thrown by the resource | |
* @param throwable | |
*/ | |
void onError(Throwable throwable); | |
/** | |
* @param t resource response , it could be null. | |
*/ | |
void onSuccess(@Nullable T t); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package comtas.com.zoolvibs.network; | |
import android.support.annotation.NonNull; | |
import okhttp3.Request; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
/** | |
* Created by m7mdra on 24/01/18. | |
*/ | |
public class Response<T> implements Callback<T> { | |
private Resource<T> res; | |
private Response(Resource<T> resource) { | |
res = resource; | |
} | |
public static <T> Response<T> create(Resource<T> resource) { | |
if (resource == null) | |
throw new NullPointerException("Resource interface can not be null"); | |
final Response<T> response = new Response<>(resource); | |
resource.onLoading(true); | |
return response; | |
} | |
public Response<T> start() { | |
res.onLoading(true); | |
return this; | |
} | |
@Override | |
public void onResponse(@NonNull Call<T> call, @NonNull retrofit2.Response<T> response) { | |
res.onLoading(false); | |
final T body = response.body(); | |
//add filtering here here , like http request status | |
//if(response.code()==401){ | |
//res.onError(new UnauthorizedUserException()); | |
// } | |
res.onSuccess(body); | |
} | |
@Override | |
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) { | |
res.onLoading(false); | |
res.onError(t); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createService(User.class).login(username, password, FirebaseInstanceId.getInstance().getToken()).enqueue(Response.create(new Resource<AuthResponse>() { | |
@Override | |
public void onLoading(boolean isLoading) { | |
//show loading progress | |
if (isLoading) | |
mProgressDialog.show(getFragmentManager(), ""); | |
else | |
mProgressDialog.dismiss(); | |
} | |
@Override | |
public void onError(Throwable throwable) { | |
//handle error here | |
if (throwable instanceof IOException) | |
//network/json parsing related error. | |
else if(throwable instanceof UnauthorizedUserException){ | |
startActivity(new Intent(getApplicationContext(),LoginActivity.class)) | |
finish(); | |
} | |
//other error | |
} | |
@Override | |
public void onSuccess(@Nullable AuthResponse authResponse) { | |
//on call ended , but its dose not means successful request. | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment