Is show() method Static method or not? - android-studio

I had seen that the Toast in android->
Toast toast = Toast.makeText(context, text, duration);
toast.show();
From seen that, I think that makeText a static method and show method non-static method.
But after that seen->
Toast.makeText(context, text, duration).show();
I am get confused.
How show() is called without creating an object of Toast class?(If show method is non-static-according to first code)

The two pieces of code you show are the same.
The second one calls Toast.makeText(context, text, duration), takes the return from that (the Toast instance) and calls show() on it.
The first code block makes a local variable called toast but the behavior is the same in both.

Show() is not a static method. Show() can be called because MakeText() returns a Toast object.

Related

How do I get a parameter to not just display in a component, but also be recognized inside of OnInitializedAsync()?

I'm working on a blazor server-side project and I have a component that gets passed a model (pickedWeek) as a parameter. I can use the model fine in-line with the html, but OnInitializedAsync always thinks that the model is null.
I have passed native types in as parameters, from the Page into a component, this way without an issue. I use a NullWeek as a default parameter, so the number getting used in OnInitializedAsync only ever appears to be from the NullWeek. In case this is related, there is a sibling component that is returning the Week model to the Page through an .InvokeAsync call, where StateHasChanged() is being called after the update. It appears that the new Week is getting updated on the problem component, but that OnInitializeAsync() either doesn't see it, or just never fires again- which maybe is my problem, but I didn't think it worked that way.
For instance, the below code will always show "FAILURE" but it will show the correct Week.Number. Code below:
<div>#pickedWeek.Number</div>
#if(dataFromService != null)
{
<div>SUCCESS</div>
}
else
{
<div>FAILURE</div>
}
#code{
[Parameter]
public Week pickedWeek { get; set; }
protected IEnumerable<AnotherModel> dataFromService { get; set; }
protected override async Task OnInitializedAsync()
{
if (pickedWeek.Number > 0)
{
dataFromService = await _injectedService.MakeACall(pickedWeek.Id);
}
}
}
#robsta has this correct in the comments, you can use OnParametersSet for this. Then, you will run into another issue, in that each rerender will set your parameters again and generate another call to your service. I've gotten around this by using a flag field along with the the OnParametersSet method. Give this a shot and report back.
private bool firstRender = true;
protected override async Task OnParametersSetAsync()
{
if (pickedWeek.Number > 0 && firstRender)
{
dataFromService = await _injectedService.MakeACall(pickedWeek.Id);
firstRender = false;
// MAYBE call this if it doesn't work without
StateHasChanged();
}
}
Another alternative is to use the OnAfterRender override, which supplies a firstRender bool in the the method signature, and you can do similar logic. I tend to prefer the first way though, as this second way allows it to render, THEN sets the value of your list, THEN causes another rerender, which seems like more chatter than is needed to me. However if your task is long running, use this second version and build up a loading message to display while the list is null, and another to display if the service call fails. "FAILURE" is a bit misleading as you have it as it's being displayed before the call completes.
I've also found that a call to await Task.Delay(1); placed before your service call can be useful in that it breaks the UI thread loose from the service call awaiter and allows your app to render in a loading state until the data comes back.

RxAndroid + Retrofit 2, getting a list of books

So, let's say I need to get a list of favorite books for an Android app.
I have the list of ids, but I can only get one book at a time, so, I don't really have a bookAPI.getFavoriteBooks(listOfFavoriteIds) method call (the server doesn't have an endpoint for that), but instead I'd have to call bookAPI.getBook(id) for each id in the list to get all the favorite books, and after I get them I should return a list with the results.
The answers I've found so far assume that there's a method Observable<List<Book>> getFavoriteBooks(List<Integer> ids)I could call, but in this case I don't have that.
Is there a way of solving this question using RxAndroid and Retrofit 2?
It's hard to say from your question if this fits your needs, but you could try:
Observable.fromIterable(listOfIds)
.flatMap(new Function<Integer, ObservableSource<Book>>() {
#Override
public ObservableSource<Book> apply(Integer integer)
throws Exception {
return bookApi.getBook(id);
}
})
.toList()
Let me explain what's happening here. fromIterable creates an observable that emits each element in the iterable as an event. In this case it will emit each book id.
You then flat map said observable to your api observable. So in this case you're mapping each emitted Id to an observable that emits each book from the api.
Finally, you collect all the emitted books as a list. Once you subscribe to this stream it will have a list as a single event:
// whatever way you get the above stream
.subscribe(new Consumer<List<Book>>() {
#Override
public void accept(List<Book> result) throws Exception {
// do whatever you want with result
}
});
Just make sure to use the right schedulers for your use case.
(Careful because this subscribe call doesn't handle errors, but I guess you can figure that out pretty easily)

UML Class Method From Sequence Diagram

How does the direction of a message could determine the method of a class (of a sequence diagram actor) ? I'd say the actor sending the message is the one having the method. Am I correct ?
And the classes For that are
Am I right or is it the other way around ?
"Sending a message" is in most cases the same as "calling a method", which means that if an actor sends a message to the computer, then the computer needs to understand it / implement a method.
There is a difference (see here 1) but essentially you "send a message" to an object, and the object decides what to do with it, in most cases it implements an appropriate method.
So to answer your question, the receiver of the message send should implement a method for it, not the sender.
Perhaps a pseudocode can also illustrate it:
class A {
function hello() {
b.someMessage();
c.otherMessage();
}
}
class B {
function someMessage() {
this.selfMessage();
}
}

Capturing a javafx Change event with mockito throws an IllegalStateException

I want to test using mockito, that some events are triggered when a javafx ObservableList is modified.
I have tried to to it this way :
#Test
public void handleListChanged() throws Exception {
// [given]
ObservableList<String> list = FXCollections.observableArrayList();
ListChangeListener<String> listener = mock(ListChangeListener.class);
list.addListener(listener);
// [when]
list.add("test");
// [then]
ArgumentCaptor<Change> argument = ArgumentCaptor.forClass(Change.class);
verify(listener).onChanged(argument.capture());
assertTrue(argument.getValue().wasAdded());
}
bu an IllegalStateException raises on the last line :
java.lang.IllegalStateException
at com.sun.javafx.collections.NonIterableChange.checkState(NonIterableChange.java:101)
at com.sun.javafx.collections.NonIterableChange.getPermutation(NonIterableChange.java:81)
at javafx.collections.ListChangeListener$Change.wasPermutated(ListChangeListener.java:156)
at javafx.collections.ListChangeListener$Change.wasAdded(ListChangeListener.java:165)
at FXCollectionsTest.handleListChanged(FXCollectionsTest.java:28
The documentation of the wasAdded() method warns that an IllegalArgumentException could be thrown "if this Change is in initial state" but i don't understand it.
Why my code is not working ?
Is there a solution for testing this code using mockito ?
You must call the next method first on the Change object before you can call any other method like wasAdded.
Unit test
assertTrue(argument.getValue().next());
assertTrue(argument.getValue().wasAdded());
Documentation
public abstract boolean next()
Go to the next change. In initial state is invalid a require a call to next() before calling other methods. The first next() call will make this object represent the first change.
Source

Mockito testing events

I have a class which generates events e.g.
public class EventSource{
public addEventListener(EventListener listener).....
public raiseEvent(){
Event e=....
listener.handle(e);
}
}
I'm using Mockito to mock an EventListener and want to do an equality on the event object (e.g. event.getTime()..event.getMessage() etc). The event object doesnt have an equals method so I cant easily create another object and do an assert.
Whats the normal way of doing this with Mockito?
It looks like you want to use an argument captor.
For example:
ArgumentCaptor<Event> argument = ArgumentCaptor.forClass(Event.class);
verify(mockedListener).handle(argument.capture());
assertEquals("ExpectedMessage", argument.getValue().getMessage());

Resources