-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert RetrofitCallback to Kotlin and some refactoring #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package `in`.projecteka.jataayu.network.utils | ||
|
||
import `in`.projecteka.jataayu.network.model.Error | ||
import `in`.projecteka.jataayu.network.model.ErrorResponse | ||
import androidx.lifecycle.MutableLiveData | ||
import com.google.gson.Gson | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
import retrofit2.Response | ||
import java.net.HttpURLConnection.HTTP_FORBIDDEN | ||
import java.net.HttpURLConnection.HTTP_UNAUTHORIZED | ||
|
||
private const val DEFAULT_ERROR_CODE = -1 | ||
private const val ERROR_CODE_UNAUTHORIZED = 1017 | ||
|
||
abstract class RetrofitCallback<T> : Callback<T?> { | ||
private var responseCallback: ResponseCallback? = null | ||
|
||
constructor() | ||
|
||
constructor(responseCallback: ResponseCallback?) { | ||
this.responseCallback = responseCallback | ||
} | ||
|
||
protected abstract fun observableLiveData(): MutableLiveData<T?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can instead make this a part of constructor, since that is the contract anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, there are cases where I don't want a callback/progress bar. e.g. Search. It was kept optional for that only. |
||
override fun onResponse(call: Call<T?>, response: Response<T?>) { | ||
if (response.isSuccessful) { | ||
if (responseCallback != null) responseCallback!!.onSuccess(response.body()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can use ?.let operator instead of null check or !! |
||
if (response.body() != null) { | ||
observableLiveData().value = response.body() | ||
} | ||
} else { | ||
if (response.errorBody() != null && responseCallback != null) { | ||
try { | ||
val errorResponse: ErrorResponse = Gson() | ||
.fromJson<ErrorResponse>(response.errorBody()!!.string(), ErrorResponse::class.java) | ||
responseCallback!!.onFailure(errorResponse) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
var errorCode = DEFAULT_ERROR_CODE | ||
if (response.code() in arrayOf(HTTP_FORBIDDEN, HTTP_UNAUTHORIZED)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good way of checking :) +1 |
||
errorCode = ERROR_CODE_UNAUTHORIZED | ||
} | ||
responseCallback!!.onFailure(ErrorResponse(Error(errorCode, e.message!!))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onFailure(call: Call<T?>, t: Throwable) { | ||
if (responseCallback != null) responseCallback!!.onFailure(t) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,34 +81,40 @@ class ConfirmPinFragment : BaseDialogFragment(), OtpSubmissionClickHandler, OtpC | |
bundle.getString(PIN)?.let { pin -> | ||
if (confirmedPin == pin) { | ||
showProgressBar(true) | ||
viewModel.createPinResponse.observe(this, Observer { | ||
viewModel.createPinResponse.observe(this, Observer { payloadResource -> | ||
|
||
when (it) { | ||
is Loading -> showProgressBar(it.isLoading, getString(R.string.creating_pin)) | ||
when (payloadResource) { | ||
is Loading -> showProgressBar(payloadResource.isLoading, getString(R.string.creating_pin)) | ||
is Success -> { | ||
activity?.let { | ||
it.setPinCreated(true) | ||
activity?.let { fragmentActivity -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead just do activity?.setPinCreated() or activity?.setResult, activity?.setFinish(). let creates a scoped function, so might not be useful considering the observer below has to be moved out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have just added existing logic inside |
||
fragmentActivity.setPinCreated(true) | ||
showProgressBar(true) | ||
viewModel.userVerificationResponse.observe(this, Observer { userVerificationResponse -> | ||
activity?.setConsentTempToken(userVerificationResponse.temporaryToken) | ||
EventBus.getDefault().post(MessageEventType.USER_VERIFIED) | ||
it.setResult(Activity.RESULT_OK) | ||
it.finish() | ||
}) | ||
viewModel.userVerificationResponse.observe( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be outside this observer. Nesting observers leads to reregistering of observers everytime the parent observer observes an emission There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
this, | ||
Observer { userVerificationResponse -> | ||
userVerificationResponse?.temporaryToken?.let { | ||
activity?.setConsentTempToken(it) | ||
EventBus.getDefault().post(MessageEventType.USER_VERIFIED) | ||
fragmentActivity.setResult(Activity.RESULT_OK) | ||
fragmentActivity.finish() | ||
} | ||
}) | ||
viewModel.verifyUser(pin, this) | ||
|
||
} | ||
} | ||
is PartialFailure -> { | ||
context?.showAlertDialog(getString(R.string.failure), it.error?.message, | ||
getString(android.R.string.ok)) | ||
context?.showAlertDialog( | ||
getString(R.string.failure), payloadResource.error?.message, | ||
getString(android.R.string.ok) | ||
) | ||
} | ||
} | ||
}) | ||
if (context?.getConsentPinCreationAPIintegrationStatus()!!){ | ||
if (context?.getConsentPinCreationAPIintegrationStatus()!!) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can write this as if(context?.getConsentPinCreationAPIintegrationStatus() == true){...} to remove the force unwrapping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you have a boolean that is nullable and you need to check it, rather than
|
||
showProgressBar(true) | ||
viewModel.createPin(confirmedPin) | ||
} else{ | ||
} else { | ||
activity?.let { | ||
it.setResult(Activity.RESULT_OK) | ||
it.finish() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,7 +102,7 @@ class PatientAccountsFragment : BaseFragment(), ItemClickCallback, PatientAccoun | |
viewModel.linkAccountsResponse.observe(this, linkAccountsObserver) | ||
} | ||
|
||
private val linkAccountsObserver = Observer<LinkAccountsResponse> { _ -> | ||
private val linkAccountsObserver = Observer<LinkAccountsResponse?> { _ -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to remove optionals in generics. |
||
(activity as ProviderActivity).showVerifyOtpScreen() | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,9 @@ import androidx.lifecycle.ViewModel | |
import java.util.regex.Pattern | ||
|
||
class UserAccountsViewModel(private val repository: UserAccountsRepository) : ViewModel() { | ||
var linkedAccountsResponse = liveDataOf<LinkedAccountsResponse>() | ||
var createAccountResponse = liveDataOf<CreateAccountResponse>() | ||
var myProfileResponse = liveDataOf<MyProfile>() | ||
var linkedAccountsResponse = liveDataOf<LinkedAccountsResponse?>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be made immutable |
||
var createAccountResponse = liveDataOf<CreateAccountResponse?>() | ||
var myProfileResponse = liveDataOf<MyProfile?>() | ||
|
||
fun getUserAccounts(responseCallback: ResponseCallback) { | ||
repository.getUserAccounts().observeOn(linkedAccountsResponse, responseCallback) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not required to mark generic Optional. While casting, it will anyway become an optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generics enforced me to make it nullable. But I didn't put much effort to remove that nullable part. Pls remove it if not required.