konmik / nucleus

Nucleus is an Android library, which utilizes the Model-View-Presenter pattern to properly connect background tasks with visual parts of an application.
MIT License
1.97k stars 253 forks source link

Q/A (was: Some doubts) #42

Open mheras opened 9 years ago

mheras commented 9 years ago

Hi!

I'm working on an app I want to use Nucleus for MVP+Rx.

I have some doubts, and I couldn't find any solution in the documentation/wiki. Could you please help me?:

1) How can I use two instances of the same view, and make each of them have their own instances of their presenters? Is it possible by telling the PresenterStorage with ID to use? Or by any other way?

2) Why is the presenter being destroyed and pointed to null in the onPause method instead of doing so in onDestroy? The documentation seems to be out of date. What if I want to stack several activities of the same type, and want them to keep their presenters while not destroyed by the system? Is that working because you pass isFinishing() to the delegate?

3) I think we need more documentation about the restartable* methods in RxPresenter. I know that it is documented in the code with Javadoc, but I think it would be great to add some explanation in the wiki. Something like the deliver* methods documentation (btw, some of them seem to be out of date since you released version 2.0.1).

Thank you very much. Your library is great. Cheers, M.

inorichi commented 8 years ago

@konmik, Do you think it's worth to cache the presenter class from the annotation RequiresPresenter in a map so that reflection is used only once per view class? I can work on it if you like this.

konmik commented 8 years ago

@inorichi Thanks for the suggestion. But I think it doesn't. The performance benefit will be less than 1 ms.

PaulWoitaschek commented 8 years ago

@inorichi I'm always using the factory methods to eliminate reflection and use the constructor to set the factory.

So I have

/**
 * Base class that forces setting a presenter factory so reflection will be eliminated.
 *
 * @author Paul Woitaschek
 */
public abstract class NucleusBaseActivity<V, P extends Presenter<V>> extends NucleusAppCompatActivity<P> {

   public NucleusBaseActivity() {
      setPresenterFactory(newFactory());
   }

   @NonNull protected abstract PresenterFactory<P> newFactory();
}
kshivaram commented 8 years ago

@konmik, while using pagination, what is the expected behavior when an activity is restarted? Suppose I have populated my recyclerview with 4 pages of 20 items each, when the activity is restarted again, does the presenter redeliver 4 request results for the 4 different pages to the view with 20 items each?

konmik commented 8 years ago

@KScoder83

When an activity is restarted due to process restoration RxPager must re-fetch only the first page.

When the activity is recreated due to config change the pager must just pass all the cached pages to the activity.

I will probably update the paging code soon, I already have a much better (simpler, flexible) implementation.

konmik commented 8 years ago

I've just got some free time and implemented this: https://github.com/konmik/nucleus/tree/master/nucleus-example-real-life

Adds: Dagger 2, Butterknife, Retrolambda, Icepick, paging with a progress indicator.

wisien92 commented 8 years ago

@konmik how should I setup my view from presenter? right now I fetch data in onCreate in presenter then I run setUp methods in onTakeView, but I would like to make these methods called only once ... request from view would be better?

emwno commented 8 years ago

@konmik hi. Could you tell me why you extend NucleusAppCompatActivity, when the presenter is actually for a fragment hosted by the activity? thanks

konmik commented 8 years ago

@wisien92 you do not setup your view from presenter. Nucleus way is absolutely opposite: your View uses presenter to fetch data. View is the King. Presenter just presents data. It does not controls View, otherwise I would call it Controller or something like that. MVC does not work well - just look at the failure of Fragments API. Fragment is a classical controller, it has even more .lifecycles than the view itself, increasing complexity instead of reducing it.

konmik commented 8 years ago

@emwno Very often having a fragment is an unneeded complexity. Plain activities are simpler. Plain views are even more simpler. Less lifecycles = simpler = less bugs.

emwno commented 8 years ago

@konmik say for my use-case i have 3 fragments, each of which requests data (different) from a server backend and all request types are similar. Should I use a separate presenter for each fragment? And if so, should my activity extend NucleusAppCompatActivity<?> (and what to place at the ? ).

konmik commented 8 years ago

I would use a separate presenter for each fragment. One view - one presenter.

If you do not need a presenter for the activity itself, you don't need to extend NucleusAppCompatActivity. All view classes can work independently. I advise you to look inside them, they are very simple and must be very easy to read.

nbsith commented 8 years ago

Hello. I have a question about example https://github.com/konmik/nucleus/blob/master/nucleus-example/src/main/java/nucleus/example/main/MainPresenter.java

Parameter "name" from request(String name) is stored under "NAME_KEY" key in case of process restart. What happens if I call request() many times with different parameters and then process restarts?

In onCreate we will register restartableLatestCache using only latest value of "name" passed to request(). I would expect that after calling request() with different parameters, my Activity will receive all results (from calls with different parameters) after it's recreation, not only the latest one.

How it could be achieved in Nucleus?

konmik commented 8 years ago

Hi @nbsith ,

Nucleus can have only one restartable per restartableId. If you need a custom behavior - feel free to implement your own. Core functions that allow coupling data and view are public and can be reused easily.