Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rajkiran20
Copy link
Contributor

I have ran unit and UI tests. Since UI tests are running fine there shouldn't be any problems with this (unless UI tests were missing).

I would still request you to do a round of sanity after merging this PR.

protected abstract fun observableLiveData(): MutableLiveData<T?>
override fun onResponse(call: Call<T?>, response: Response<T?>) {
if (response.isSuccessful) {
if (responseCallback != null) responseCallback!!.onSuccess(response.body())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use ?.let operator instead of null check or !!

private const val DEFAULT_ERROR_CODE = -1
private const val ERROR_CODE_UNAUTHORIZED = 1017

abstract class RetrofitCallback<T> : Callback<T?> {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

this.responseCallback = responseCallback
}

protected abstract fun observableLiveData(): MutableLiveData<T?>
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

} catch (e: Exception) {
e.printStackTrace()
var errorCode = DEFAULT_ERROR_CODE
if (response.code() in arrayOf(HTTP_FORBIDDEN, HTTP_UNAUTHORIZED)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good way of checking :) +1

it.setResult(Activity.RESULT_OK)
it.finish()
})
viewModel.userVerificationResponse.observe(
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

is Success -> {
activity?.let {
it.setPinCreated(true)
activity?.let { fragmentActivity ->
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just added existing logic inside .let everywhere. If the said change is required, it was required before also.

}
}
})
if (context?.getConsentPinCreationAPIintegrationStatus()!!){
if (context?.getConsentPinCreationAPIintegrationStatus()!!) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

== true why? It will anyway go inside if it's true. Instead we can extract out the variable outside so it is unwrapped only once. But it's just one occurrence.

Copy link
Contributor

@rajivrajan rajivrajan Apr 3, 2020

Choose a reason for hiding this comment

The 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 !! force unwrapping or using the let block, you can evaluate that against a true / false value.

context?.getConsentPinCreationAPIintegrationStatus() gives a Boolean? because context is nullable here. if you don't want to force unwrap, you could write this as
context?.getConsentPinCreationAPIintegrationStatus()?.let { if(it){...}}
or
if(context?.getConsentPinCreationAPIintegrationStatus() == true){...}
This doesn't always evaluate true because
if the value is null, null != true.
if the value is false, false == true
if the value is true, true == true

@@ -102,7 +102,7 @@ class PatientAccountsFragment : BaseFragment(), ItemClickCallback, PatientAccoun
viewModel.linkAccountsResponse.observe(this, linkAccountsObserver)
}

private val linkAccountsObserver = Observer<LinkAccountsResponse> { _ ->
private val linkAccountsObserver = Observer<LinkAccountsResponse?> { _ ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to remove optionals in generics.

var linkedAccountsResponse = liveDataOf<LinkedAccountsResponse>()
var createAccountResponse = liveDataOf<CreateAccountResponse>()
var myProfileResponse = liveDataOf<MyProfile>()
var linkedAccountsResponse = liveDataOf<LinkedAccountsResponse?>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be made immutable

@@ -18,9 +18,9 @@ class RegistrationViewModel(private val repository: AuthenticationRepository) :
const val MOBILE_IDENTIFIER_TYPE = "mobile"
}

var requestVerificationResponse = liveDataOf<RequestVerificationResponse>()
var requestVerificationResponse = liveDataOf<RequestVerificationResponse?>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be made immutable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants