Office Hours — Today, April 20

Saturday, April 17

Apr 20
8:25 AM
Mark M.
has entered the room
Mark M.
turned on guest access
8:35 AM
Kumar V.
has entered the room
Kumar V.
Hi Mark.
Mark M.
hello, Kumar!
how can I help you today?
Kumar V.
View paste
This is my requirement. 
I have to refresh my screen after certain timeout (dynamic value) and have to pause and resume when the screen goes out of visibility and comes back.

Which would be the right component to go with ?
I tried to use Handler in ViewModel and sending the callbacks via LiveData, but don't know how to pause the timer here.
Mark M.
by "use Handler", do you mean that is what you were using for the timeout (e.g., postDelayed())? if not, what are you using for the timeout?
Kumar V.
Yes Handler with postDelayed
8:40 AM
Mark M.
you would probably need to cancel the postDelayed() via removeCallbacks() and make note of how much time is remaining -- then, when appropriate, call postDelayed() for that remaining time
Kumar V.
Okay. This seems to be common use case, is there any elegant, standard way that you are aware of, to get this done.
Mark M.
"This seems to be common use case" -- um, I'll take your word for that
there are many ways of implementing a timer, including options in reactive frameworks (e.g., RxJava, Kotlin coroutines)
Eric
has entered the room
Eric
morning mark!
Mark M.
I do not know of any off the top of my head with built in pause/resume logic
Shikhar D.
has entered the room
Mark M.
(BTW, hello, Eric -- I will be with you shortly!)
(hello, Shikhar! I will be with you in a bit, after Kumar and Eric!)
Kumar V.
Okay. But do you see any issues if I go ahead with Handler with background thread ?
Mark M.
personally, I would be using RxJava or coroutines
I have never tried Handler in a ViewModel
I do not know of any problems with that, but I do not have practical experience with that combination
Kumar V.
What should I be looking in coroutines for this ?
8:45 AM
Mark M.
delay() in a suspend function, or I think there is also a delay() operator on Flow
let me take questions from the others, and I will come back to you in a little while
Eric: your turn! how can I help you today?
Kumar V.
View paste
Okay, haven't explored Flow, will check on that. 
Ya Sure.
Eric
You mentioned using paging library version 3 with rxjava. Can you refer me to an example, or is it in your commonsware material?
Mark M.
neither -- it is in a project that I working on for a consulting client
(er, I am working on)
Eric
ok, yea I didn't see it iin elements of jetpack so far
Mark M.
I will add some Paging 3 material to *Elements of Android Room* eventually, though not pertaining to RxJava
Eric
ok
I will look online instead
I appreciate it, take it easy :)
Mark M.
you too!
Eric
bye
Mark M.
Shikhar: your turn! how can I help you today?
Eric
has left the room
Shikhar D.
Hey Mark, 1st time using chat option. Thanks for the opportunity
Mark M.
I'm happy to try to help!
Shikhar D.
my question is related to ScopeStorage policy. Are you aware of the deadline. I got a mail which says May5 but target android 11 is Nov21
8:50 AM
Mark M.
I personally have not received the email, as I do not distribute anything myself on the Play Store -- however, I have seen copies of that email
Shikhar D.
I have some materials with me. Shall I send it to you now?
Mark M.
I am uncertain what you mean by "materials", sorry
I assume you are referring to an email like the one posted at: https://www.reddit.com/r/androiddev/comments/mq...
Shikhar D.
So the thing is ours is a chat App and we are still targeting api29 and using requestLegacyStorage flag
excatly..oh by materials I mean the things I googled related to policy
above is the policy mail screenshot
Mark M.
as is noted in that Reddit thread, the email is very poorly written
and android:requestLegacyExternalStorage="true" is specifically supposed to be usable for Android 10
Shikhar D.
yes. email is not clear whether it applies when you target android 11 as well as when you ask for all files permission
because if May5 is the deadline then I have to start the migration work right now
8:55 AM
Mark M.
I have no visibility into Google's decision-making process, nor how they intend to act on May 5
Shikhar D.
I understand. Still, What's your suggestion ?
Mark M.
if your targetSdkVersion is still 29, though, https://support.google.com/googleplay/android-d... indicates that you should still be OK for now
Shikhar D.
that was also my assumption, but I got confused when I saw the mail in my play console. As I am not targeting android 11, then I dont know why they sent me the mail. May be they have sent it to all dev accounts
Mark M.
well, at some point, you are going to need to do the work to deal with android:requestLegacyExternalStorage no longer being honored once your targetSdkVersion reaches 30
Shikhar D.
there is some preserveFlag as well. I still dont understand its use
Mark M.
I think that they sent it to all dev accounts -- my customer's app has targetSdkVersion 29, and the person handling the Play Store distribution received this email
I do not understand that preserve flag either
anyway, if you have the time in your development schedule to try to expedite no longer needing android:requestLegacyExternalStorage in the next few weeks, that would be the safest course of action
so for example, we will be double-checking things in my customer's project, though we should be close to OK in our current state
Shikhar D.
almost most of the answers on StackOverflow has same description of that flag which is written in official documentation
9:00 AM
Mark M.
let me take another question from Kumar, and I will be back with you in a bit
Kumar: back to you! do you have another question?
Shikhar D.
thanks for the suggestion. I will start working on the migration then. Will you be checking more on this?
sure
Kumar V.
View paste
Yes one last one. 

I want to add a imageview in between of text, found that there is this SpannableString, I can use ImageSpan and set the image. But had less control in terms of setting width and height of the image and scalability. The option is there, but doesn't seem elegant, should I break the textview into 2 and add imageview in between or go with SpannableString
Mark M.
that will depend on what you want to do with things like text flow control (e.g., word-wrapping)
if the two pieces of text are truly independent, having two TextView widgets should be fine
Kumar V.
It is a short one, a single line.
Mark M.
well, "single line" might depend on screen size, font scale, etc.
Kumar V.
It is a single text, they aren't indepedent.
Mark M.
if you are absolutely certain that either you will not need word-wrapping, or you are willing to consider this text as two separate pieces for word-wrapping, then two TextViews should be fine
9:05 AM
Kumar V.
I mean the text is very short, of 3 words with default font size
Mark M.
then probably you can get away with two TextViews
Kumar V.
Okay.
Can I get reference point for that coroutines / flow with timer
Mark M.
OK, I must have have mis-remembered -- I do not see a delay() operator for Flow
Kumar V.
With the coroutine ?
Mark M.
I show delay() in a coroutine in many examples in *Elements of Kotlin Coroutines*
Kumar V.
Okay, will check and try that.
Mark M.
delay() is a top-level function, available inside of any suspend function, to delay for a period of time
9:10 AM
Mark M.
let me take another question from Shikhar, and I will try to get back to you before the end of the chat
Shkihar: over to you! do you have another question?
Kumar V.
but does it have pause / resume functionality ?
Mark M.
(Kumar: no, but as I said, I do not know of anything that does -- you would have to implement that sort of logic yourself, such as by cancelling the coroutine and scheduling it later for the remaining time)
Shikhar D.
1 more. So in MVVM java project how do you communicate between Repo and ViewModel
For simplicity, lets consider Repo doesn't have caching and it fetches data from API
I am using Rx. So the Repo should return Single<>, Observable<>
Mark M.
for work that takes time, have the repository return something reactive, such as an RxJava Observable/Single/Completable
correct, and which of those depends on whether the repository will be publishing changes over time
Shikhar D.
I was under the impression that subsribeOn(), observeOn() which we write in Rx pipeline should be in Repo
Mark M.
so, for example, a simple Web service call probably would get modeled as a Single or Completable, depending on whether you are expecting any results back
I have seen that code either be in the repository or the viewmodel
Shikhar D.
but if its in Repo then how will you communicate the result back to ViewModel
Mark M.
return the Single, etc.
subscribe {} would be in the ViewModel
subscribeOn() != subscribe {}
9:15 AM
Mark M.
subscribeOn() says "this is the Scheduler that should be used for the work"
subscribe() says "OK, send me the results"
Shikhar D.
so this is how I am doing
Mark M.
if the repository calls subscribeOn() and observeOn(), it controls the choice of schedulers, but it still has a Single, Observable, etc. to return to the viewmodel
Shikhar D.
View paste (15 more lines)
ViewModel:

public void fetchSentUsers() {
        setStateForEmptyDataAndNotify(StateData.LOADING);

        contactsRepo.getSentUsers(contactsRepo.getSentUsersPayload(MainActivity.getAppHelper().getAT()))
                .subscribeOn(Schedulers.io())
                .delay(1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<List<SentUsersApiModel>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        compositeDisposable.add(d);
                    }

...
View paste
Repo:

 public Single<List<SentUsersApiModel>> getSentUsers(JSONObject payload) {
        return getContactsApi().getSentUsers(getRequestBody(payload));
    }
View paste
if the repository calls subscribeOn() and observeOn(), it controls the choice of schedulers, but it still has a Single, Observable, etc. to return to the viewmodel
--> in that case the repo will need viewmodel reference as well.
Mark M.
no
View paste
public Single<List<SentUsersApiModel>> getSentUsers(JSONObject payload) {
        return getContactsApi()
          .getSentUsers(getRequestBody(payload))
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread());
    }
subscribeOn() returns the Single
observeOn() returns the Single
so getSentUsers() returns the Single
the subscribe() call remains in the viewmodel
Shikhar D.
ok...subscribe(new SingleObserver<> with override callbacks will be in ViewModel
Mark M.
correct
9:20 AM
Shikhar D.
is there any issue if ViewModel controls the Schedulers. Which way you will suggest
Mark M.
I have seen both be used
mostly, I have put the schedulers in the viewmodel
Shikhar D.
same..Most of the code samples I found uses the same approach. But I was confused because of theory which says Repo should fetch the data and If repo is fetching the data then I was assuming that it should be handling schedulers and just returning the results to ViewModel
Mark M.
I would argue that the code with the subscribe() call should be the one with the observeOn() call, to control what thread is used for those subscribe() callbacks
that implies that observeOn() would be handled by the viewmodel
I can see arguments for why subscribeOn() should be handled by the repository
but, I cannot tell you that there is One True Correct Answer for this -- mostly, it seems to be a matter of project style
OK, at this point, if either of you have any questions, go ahead! I will try to answer whatever I can in the remaining time
Shikhar D.
I am done. Thanks for the help. Really appreciate it
9:25 AM
Mark M.
you're welcome!
Kumar V.
That's it Mark. Still unclear about which one to use Handlers / Coroutines for that timer thing.
Mark M.
BTW, the chat transcript will be posted to https://commonsware.com/office-hours/ shortly after the end of the chat
Kumar: if you are using coroutines for other stuff, I would stick to coroutines over a Handler for the timer
Shikhar D.
has left the room
Mark M.
Kumar V.
Yes we are using coroutines.
Okay, will first try with coroutines.
Thanks for the help Mark.
Mark M.
you're welcome!
Kumar V.
Good day. Bye.
Mark M.
have a pleasant day!
Kumar V.
has left the room
9:30 AM
Mark M.
turned off guest access

Saturday, April 17

 

Office Hours

People in this transcript

  • Eric
  • Kumar Velu
  • Mark Murphy
  • Shikhar Deep

Files in this transcript