Feb 9 | 3:55 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Feb 9 | 4:00 PM |
Anshul V. | has entered the room |
Feb 9 | 4:05 PM |
Mark M. |
hello, Anshul!
|
Mark M. |
how can I help you today?
|
Anshul V. |
Hi Mark, how are you doing?
|
Mark M. |
OK, and you?
|
Anshul V. |
I am doing fine thank you!
|
Anshul V. |
I wanted to talk about the SO bump question we talked about yesterday
|
Mark M. |
do you mean https://stackoverflow.com/q/54581480/115145 ?
|
Anshul V. | |
Anshul V. |
yes
|
Mark M. |
OK, same post, different URLs :-)
|
Mark M. |
"So at the end when the user presses Continue, I get a List<InputData>, one for each fragment user created" -- isn't this a matter of the activity iterating over the fragments and calling giveMeMyInputDataKThxBye() on each
|
Mark M. |
then assembling that into a List<InputData>?
|
Anshul V. |
Well, yes, but I am not sure how to do that. I think I have got validation and passing the data confused and can't seem to get either
|
Mark M. |
your EditProfilePagerAdapter has a List<Fragment>
|
Feb 9 | 4:10 PM |
Mark M. |
change that to be a List<EditProfileFragment>
|
Mark M. |
then do a for loop over it
|
Mark M. |
for (EditProfileFragment fragment in fragments) { // TODO }
|
Mark M. |
where the TODO calls some method on the fragment to get the InputData and adds it to an ArrayList
|
Anshul V. |
okay, but how to I access that data in the activity? I have not linked the List<EditProfileFragment> in EditProfilePagerAdapter
|
Mark M. |
write a method on EditProfilePagerAdapter
|
Mark M. |
call it collectInputData() or something
|
Mark M. |
it does the for loop over the List<EditProfileFragment> and returns the List<InputData>
|
Mark M. |
and your activity calls collectInputData() when the user clicks the Continue button
|
Anshul V. |
hmm, so the InputData that is present in each fragment gets added to the list. I guess I make the InputData in each fragment accessible to the EditProfilePagerAdapter then?
|
Mark M. |
that was my proposed giveMeMyInputDataKThxBye() method from earlier
|
Mark M. |
though you might want to use a simpler name :-)
|
Feb 9 | 4:15 PM |
Anshul V. |
Haha..like it, so each fragment has a public giveMeMyInputDataKThxBye(), which it sends to EditProfilePagerAdapter when activity calls collectInputData() on continue button press?
|
Mark M. |
"sends" implies a different flow than I'm thinking -- "returns" would be what I would use
|
Mark M. |
but otherwise, yes
|
Anshul V. |
hmm, got it.
|
Feb 9 | 4:20 PM |
Anshul V. |
Now where does the validation fits in the picture? I want to validate a couple of input fields of the EditProfileFragment and want to raise error if that field is not filled? Since there can be many EditProfileFragment added via the Add Fragment button in Activity, I was thinking each time the Add Fragment button is clicked, that current fragments data gets validated and when that is correct, then only the data is "returned" to the EditProfilePagerAdapter? Am I going in the right direction?
|
Mark M. |
you would need to validate both when the user clicks "Add" and "Continue", as otherwise you will not validate the last fragment
|
Mark M. |
to do that, have a validate() method on the fragment that performs the validation and updates it UI, returning a boolean indicating whether the validation succeeded
|
Mark M. |
call that as part of processing Add and Continue clicks, not proceeding if validate() returns false
|
Mark M. |
you can find out the index of the current page from the ViewPager, and use that to look up the current fragment in your adapter
|
Feb 9 | 4:25 PM |
Anshul V. |
Hmm, I did not think about the last fragment. So, the flow would be -> When user clicks +Add/Continue, the pager adapter requests data for "current" fragment, and upon that request, the data before being returned to Adapter, is validated, if anything wrong, error is raised and data is not sent, if right the data is sent to adapter, which adds it to a List<InputData> which can later be inputted in collectInputData() call by activity.
|
Mark M. |
something like that, yes
|
Mark M. |
the fact that you are using a ViewPager makes things more complicated, as the user could have a valid fragment, add another one, go back to the first one, make a mistake there, swipe back to the last one, then click Continue
|
Mark M. |
your Continue validation probably needs to check all the fragments, and collect all the data at that point
|
Feb 9 | 4:30 PM |
Anshul V. |
Hmm, that makes sense. let's say if there is an error in say fragment 3, then pager adapter would load fragment 3 as a current fragment (on continue press) and communicate with fragment about the field that is not validated (still not sure how this would happen) and then the fragment would do setError() on that field of fragment 3.
|
Mark M. |
well, my gut instinct is that the fragments should do the validation themselves -- I'm not quite certain why you are having the activity do it
|
Mark M. |
so, on a Continue press, you would iterate over the fragments and call validate() -- if one returns false, you switch the pager back to that fragment
|
Mark M. |
if none return false, then all the data is valid, so you collect the data from each of the fragments and continue on your merry way
|
Feb 9 | 4:35 PM |
Anshul V. |
Ah, so validate() would be inside giveMeMyInputDataKThxBye(), so before data is returned to the collectInputData() in PagerAdapter, it is already validated, each time.
|
Mark M. |
that's a possibility, where giveMeMyInputDataKThxBye() returned null to indicate invalid data
|
Mark M. |
I had envisioned validate() and giveMeMyInputDataKThxBye() being separate, but either way could work
|
Anshul V. |
okay, if validate() and giveMeMyInputDataKThxBye() are different, what would be the trigger to call the validate()? Since there is no button/event inside fragment where we know that now user has entered the data and it can be validated.
|
Mark M. |
at minimum, you would call validate() when the user clicked Continue
|
Mark M. |
you could also call validate() when the user clicked Add, to confirm that the current fragment is OK, though due to the pager, you can't rely on it *staying* valid if the user goes back and make changes
|
Feb 9 | 4:40 PM |
Mark M. |
but, on a Continue click, you would:
|
Mark M. |
1. iterate over the fragments, calling validate() on each
|
Mark M. |
2. if a validate() call returns false, switch the pager to that fragment and you're done for now
|
Mark M. |
3. if all validate() calls return true, you iterate over the fragments and call giveMeMyInputDataKThxBye() to build your List of results, and you continue from there
|
Mark M. |
if giveMeMyInputDataKThxBye() is cheap, and you want to just have one iteration loop and use a null return value to signal invalid data, so you do not need a separate validate() method, you could do that
|
Anshul V. |
okay, I think I got it, both validate() and giveMeMyInputDataKThxBye() are going to be called by PagerAdapter, on Continue button press in activity
|
Mark M. |
yes
|
Mark M. |
(BTW, sorry about the delay there -- my computer froze, so I needed to reboot)
|
Feb 9 | 4:45 PM |
Anshul V. |
No problem, I didn't notice it..
|
Anshul V. |
okay, Mark. I should be able to make this work. Thank you very much for your time and input, I appreciate it.
|
Mark M. |
you're welcome!
|
Feb 9 | 4:50 PM |
Anshul V. |
I would be coming again to the office hours, I have a lot of questions regarding the app architecture where I think it can be improved. I have just started on my Android Development journey and I thank you for making such amazing books!
|
Anshul V. |
Have a good weekend!
|
Mark M. |
you too!
|
Mark M. |
and thanks for the kind words!
|
Feb 9 | 5:00 PM |
Mark M. |
that's a wrap for today's chat
|
Mark M. |
the transcript will be posted on https://commonsware.com/office-hours/ shortly
|
Mark M. |
the next chat is Tuesday at 7:30pm US Eastern
|
Mark M. |
have a pleasant day!
|
Anshul V. | has left the room |
Mark M. | turned off guest access |
Feb 9 | 5:20 PM |
Mark M. | has left the room |
Feb 9 | 5:50 PM |
Mark M. | has entered the room |