Skip to content

Create custom Observer class to abstract common Loading logic #1205

@samtstern

Description

@samtstern

We have a lot of code like this:

mHandler.getOperation().observe(this, new Observer<Resource<IdpResponse>>() {
            @Override
            public void onChanged(Resource<IdpResponse> resource) {
                if (resource.getState() == State.LOADING) {
                    getDialogHolder().showLoadingDialog(R.string.fui_progress_dialog_signing_in);
                    return;
                }
                getDialogHolder().dismissDialog();

                if (resource.getState() == State.SUCCESS) {
                    // ...
                } else if (resource.getState() == State.FAILURE) {
                    // ...
                }
            }
        });

In particular the LOADING bits almost always look the same. We could make something like:

public class ResourceObserver<T> extends Observer<Resource<T>> {

          @Override
          public void onChanged(Resource<T> resource) {
                if (resource.getState() == State.LOADING) {
                    onLoading()
                } else {
                    onNotLoading(resource);
                }
          }


          protected void onLoading() {
             // .. Do standard stuf
          }

          protected abstract void onNotLoading(...);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions