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

Fix part of #1633: Removed view dependency from ProfileEditViewModel #1925

Merged
merged 23 commits into from
Nov 24, 2020

Conversation

alokbharti
Copy link
Contributor

@alokbharti alokbharti commented Oct 2, 2020

Explanation

This PR fixes part of #1633
Fixes part of #1870

This PR contains following changes:

  • ProfileEditViewModel had a reference to activity variable to set its title but now an existing livedata variable is used to expose the title value and then ProfileEditActivityPresenter observe that variable to set the title for ProfileEditActivity.
  • Checked state of switch is now shifted to ProfileEditActivityPresenter
  • Instead of using ViewModelProvider, now ProfileEditViewModel is directly injected in ProfileEditActivityPresenter
  • Removed onRestoreInstanceState from ProfileEditActivity as viewmodel can survives configuration changes

Following tests are done to ensure that everything is working as it was before

  • Created multiple users and verified that the profile name is visible as the title of the activity
  • Verified the checked state of switch button in ProfileEditActivity
  • Verified above after rotating screen multiple times
  • Repeated above steps while Talkback is on
  • Ran existing test file related to ProfileEditActivity

Update

  • Fixed all the tests for ProfileEditActivity. (tested on pixel 2 emulator and Xioami Mi A3)

Roboelectric
roboelectric

Espresso
espresso

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes. (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • The PR explanation includes the words "Fixes #bugnum: ..." (or "Fixes part of #bugnum" if the PR only partially fixes an issue).
  • The PR follows the style guide.
  • The PR does not contain any unnecessary auto-generated code from Android Studio.
  • The PR is made from a branch that's not called "develop".
  • The PR is made from a branch that is up-to-date with "develop".
  • The PR's branch is based on "develop" and not on any other branch.
  • The PR is assigned to an appropriate reviewer in both the Assignees and the Reviewers sections.

- Used MutableLiveData to store the value of ProfileEditActivity Title
  and then expose it using a livedata variable in profleEditViewModel
  class
- Observe the livedata variable to set ProfileEditActivity Title in
  ProfileEditActivityPresenter class
@Arjupta
Copy link
Contributor

Arjupta commented Oct 3, 2020

@alokbharti you must ask for a review from any of the code maintainers else your PR will be noticed late. Like
@rt4914 sir were viewing the original issue so you can ask him for review here.

@alokbharti
Copy link
Contributor Author

@alokbharti you must ask for a review from any of the code maintainers else your PR will be noticed late. Like
@rt4914 sir were viewing the original issue so you can ask him for review here.

@Arjupta I already know this but I was waiting for the checks to be finished.

@alokbharti
Copy link
Contributor Author

@rt4914 PTAL

@@ -27,6 +25,9 @@ class ProfileEditViewModel @Inject constructor(

lateinit var profileName: String

private val _activityTitle = MutableLiveData<String>()
Copy link
Contributor

Choose a reason for hiding this comment

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

As per I understand from the issue and this committed file, we have to work on Switch.
@miaboloix Could you please correct me here?

Copy link
Contributor

Choose a reason for hiding this comment

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

@anandwana001 you might need to approach @miaboloix directly w.r.t. this.

Copy link
Member

Choose a reason for hiding this comment

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

@anandwana001 is correct, though there are actually two issues with this view model:

  1. It relies on a view directly (the Switch--view models should not depend on views)
  2. It relies on Activity, but its lifecycle is managed by AndroidX's ViewModelProvider which means it will leak references to the previous activity during configuration changes

#1633 is meant to fix (1), but if possible we should also fix (2) while here. The way we've been fixing (2) everywhere else is just by injecting the view model directly into the presenter rather than using ViewModelProvider.

@alokbharti you'll need to figure out a way to make (1) work--we don't want to depend on Switch which may require introducing a callback interface or a live data from this view model.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @BenHenning , thanks for the suggestions. I'll fix those issues one by one for this viewmodel. For now, as suggested I'm doing the changes for the switch part.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. While here, can you also do (2)? All that's needed is to switch the presenter from using ModelViewProvider to injecting the view model directly. There aren't straightforward tests that we can add for this, so we'll rely on existing tests to pass.

@rt4914
Copy link
Contributor

rt4914 commented Oct 6, 2020

Also, I have assigned this PR to @BenHenning too as I am not completely sure about this solution yet.

Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @alokbharti! Left some thoughts.

@@ -27,6 +25,9 @@ class ProfileEditViewModel @Inject constructor(

lateinit var profileName: String

private val _activityTitle = MutableLiveData<String>()
Copy link
Member

Choose a reason for hiding this comment

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

@anandwana001 is correct, though there are actually two issues with this view model:

  1. It relies on a view directly (the Switch--view models should not depend on views)
  2. It relies on Activity, but its lifecycle is managed by AndroidX's ViewModelProvider which means it will leak references to the previous activity during configuration changes

#1633 is meant to fix (1), but if possible we should also fix (2) while here. The way we've been fixing (2) everywhere else is just by injecting the view model directly into the presenter rather than using ViewModelProvider.

@alokbharti you'll need to figure out a way to make (1) work--we don't want to depend on Switch which may require introducing a callback interface or a live data from this view model.

- Added a livedata variable to expose the checked state of switch in
  ProfileViewModel through which ProfileEditActivityPresenter can
  observe and update the UI accordingly
- Removed switch variable initialization from viewmodel
- Added activity dependeny as it was before
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @alokbharti! Left two comments. I noticed that your PR description is out-of-date--could you update that to reflect the changes in this PR?

Also to confirm, have you manually tested the app with these changes to make sure that the profile switch still works correctly?

Further, can you please also verify or add tests to make sure the switch functionality is working correctly after the changes from this PR?

private lateinit var switch: Switch

private val _isAllowedDownloadAccess = MutableLiveData<Boolean>()
val isAllowedDownloadAccess: LiveData<Boolean> = _isAllowedDownloadAccess
Copy link
Member

Choose a reason for hiding this comment

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

@@ -27,6 +25,9 @@ class ProfileEditViewModel @Inject constructor(

lateinit var profileName: String

private val _activityTitle = MutableLiveData<String>()
Copy link
Member

Choose a reason for hiding this comment

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

Thanks. While here, can you also do (2)? All that's needed is to switch the presenter from using ModelViewProvider to injecting the view model directly. There aren't straightforward tests that we can add for this, so we'll rely on existing tests to pass.

Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Apologies, I meant to select 'request changes' in the last review.

@alokbharti
Copy link
Contributor Author

Thanks @alokbharti! Left two comments. I noticed that your PR description is out-of-date--could you update that to reflect the changes in this PR?

Also to confirm, have you manually tested the app with these changes to make sure that the profile switch still works correctly?

Further, can you please also verify or add tests to make sure the switch functionality is working correctly after the changes from this PR?

Thanks @BenHenning , I've tested the app manually & using talkback and It's working correctly. I'll add the second part too in the next commit i.e. changes for activity title and will update the PR.

- Removed activity variable from ProfileViewModel, as we can simply use
  existing profile livedata variable to update the title of the
  ProfileEditActivity in the ProfileEditActivityPresenter
- Instead of using viewModelProvider in ProfileEditActivityPresenter to
  get viewmodel, now it is directly injected
- Since viewmodel can survive configuration changes, there is no need to
  create an extra variable to store profile name to use it in
  onRestoreInstanceState
- Added comments for public variable in ProfileEditViewModel
Copy link
Contributor

@anandwana001 anandwana001 left a comment

Choose a reason for hiding this comment

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

nit change.

@alokbharti alokbharti assigned anandwana001 and unassigned alokbharti Nov 5, 2020
Copy link
Contributor

@anandwana001 anandwana001 left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks @alokbharti

@anandwana001 anandwana001 removed their assignment Nov 6, 2020
Copy link
Contributor

@rt4914 rt4914 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks.

@rt4914
Copy link
Contributor

rt4914 commented Nov 6, 2020

@anandwana001 Please resolve your comment threads. Also if you find any comment which I have not resolved feel free to add me again.

@alokbharti We will have to wait for @BenHenning review on this as the merging is blocked. Mostly by End of this month.

@rt4914 rt4914 assigned BenHenning and anandwana001 and unassigned rt4914 Nov 6, 2020
@anandwana001 anandwana001 removed their assignment Nov 9, 2020
Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @alokbharti! Really nice improvements to the tests.

Left a couple of comments, but the PR overall LGTM.

@BenHenning BenHenning assigned alokbharti and unassigned BenHenning Nov 17, 2020
@alokbharti alokbharti removed their assignment Nov 20, 2020
@rt4914
Copy link
Contributor

rt4914 commented Nov 23, 2020

@alokbharti If you are looking for another approval from Ben. Please assign him this PR. Thanks.

Copy link
Member

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

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

Thanks @alokbharti, LGTM!

@BenHenning
Copy link
Member

BenHenning commented Nov 24, 2020

@Sarthak2601 I'm not seeing your comments for some reason, so going ahead and merging this. Please follow-up with an issue if you have any additional requests for this implementation.

@BenHenning BenHenning merged commit ff74da4 into oppia:develop Nov 24, 2020
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.

7 participants