question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

About controller's usage

See original GitHub issue

Excuse me for interrupting you again,I have a problem in usage of controller。 Here is the usage scenario: first,i add a loadingModel to controller,like this: ` @AutoModel LoadingEpoxyModel loadingModel;

@Override
protected void buildModels(List<String> data) {
    loadingModel.addTo(this);
}`

the view display is a progress full screen. At the same time,i request data from server,in response to the data from the server,i can do:

  1. if the status code returned from the server is ok,and the data is not empty,i want to remove/hide loadingModel,add dataModels.
  2. if the status code returned from the server is ok,but the data is empty,i want to remove/hide loadingModel,add emptyModel,user click this emptyModel,i want to remove/hie emptyModel,add loadingModel.
  3. if the status code returned from the server is error,may be due to the bad internet connection,i want to remove/hide loadingModel,add errorModel,user click this errorModel,i want to remove/hie emptyModel,add loadingModel.

My confusion is here,the wiki said :controller not include remove/hide methods about models,so i can not remove/hide loadingModel.i try to use addIf,Unfortunately,i found that it have limits in supporting three case above. Maybe Typed Controllers can solve this problem.But i have no idea. I know Epoxy Adapter can solve this problem,but i want to use controller follow you. dou you have some suggestion? finally,please forgive me for my bad English if it annoys you.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tschuchortdevcommented, May 19, 2017

@ailuoyi basically, the idea is to get rid of complex state and mutability. Instead of your view being some stateful thing that you tell to change (typical MVP) you can think of the view as some kind of function-like blackbox that takes in a POJO (your UI state) and returns the appropriate user interface. Every time an event happens, you create a new POJO that contains all of your current app state and tell the view layer to render that. In this case, you construct a list of POJOs/EpoxyModels in buildModels and give it to Epoxy, which will render this new state, regardless of which models were in it before.

This idea comes from functional languages and can give you a very unidirectional, predictable data flow without side effects. In a language like Haskell or Elm you can relatively easily construct your whole app as a single function composed of other functions that only transform a single flow of data:

class State(color = BLACK, counter = 0)

// presenter
fun handleEvent(currentState: State, event: Event): State =
    when(action) {
         COUNTER_CLICK-> currentState.copy(counter = currentState.counter + 1)
         COLOR_CLICK -> currentState.copy(color = getRandomColor())
    }   
    
// view
fun render(state: State): Ui =
    ui {
        backgroundColor = state.color

        label {
            text = counter
        }

        button { 
            text = "increase counter"
            event = COUNTER_CLICK;
        }

        button { 
            text = "change color"
            event = COLOR_CLICK;
        }
    }

then your whole progam is basically: render(handleEvent(state, event))

2reactions
elihartcommented, May 18, 2017

Happy to help, and no worries about your English, it’s good enough 😃

If I’m understanding your case, I think you can do something like this:

class DataClass {
    boolean isLoading;
    boolean error;
    List<String> strings = Collections.emptyList(); // empty list by default
  }

  @Override
  protected void buildModels(DataClass data) {
    loadingModel.addIf(data.isLoading);
    errorModel
        .onClick(() -> // callback to retry the request, update the data, and requestModelBuild)
        .addIf(data.error);

    for (String s : data.strings) {
      new DataModel_()
          .data(s)
          .addTo(this);
    }
  }

The reason why the controller doesn’t support removing models is that every time buildModels is called you start with a completely empty model state and rebuild it from scratch. Ideally you have some sort of Data object representing all of the state and you can decide what models to add to reflect that state.

Epoxy doesn’t force you to use a certain data pattern, so you might use an activity/fragment/presenter/whatever, but that presenter should coordinate the network request, update the data state as necessary, and set that data on the controller when it changes so the controller can rebuild models.

Hope that helps! let me know if I can explain it more

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a Controller? - Definition from Techopedia
A controller is a program component that serves as a mediator between a user and application and handles business-related tasks triggered in ASP.NET...
Read more >
Financial Controller Roles, Duties, Skillset, Career Path
A controller acts as an overseer of a company's financial health by taking ownership of the financial reporting process. A controller oversees ...
Read more >
Types of Controls and Controllers - A ThomasNet Buying Guide
Controls and Controllers are mechanical, electro-mechanical, or electronic devices which use input signals to change conditions or values in processes or ...
Read more >
Game controller - Wikipedia
A game controller, gaming controller, or simply controller, is an input device used with video games or entertainment systems to provide input to...
Read more >
How to Choose the Right Game Controller for Your PC - PCMag
If the controller was manufactured in the last five years, it can pair wirelessly with your computer over Bluetooth. If you want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found