Git Product home page Git Product logo

gigigo-interactorexecutor-library-android's Introduction

gigigo-interactor-executor-library

Build Status

Execute asynchronous tasks in other thread in interactor

Initialitation

We neew to instance some classes to use this library. The ideal would be to define these classes in a dagger module.

InteractorPriorityBlockingQueue blockingQueue = new InteractorPriorityBlockingQueue(100);

InteractorOutputThreadFactory threadFactory =
        new InteractorOutputThreadFactory();

PriorizableThreadPoolExecutor executorService =
        new PriorizableThreadPoolExecutor(CONCURRENT_INTERACTORS, CONCURRENT_INTERACTORS, 0L,
            TimeUnit.MILLISECONDS, blockingQueue, threadFactory);

InteractorInvokerImp interactorInvoker = new InteractorInvokerImp(executorService, new LogExceptionHandler());

Use

Presenter

We have to call our interactor in the presenter like this:

InteractorExample interactor = new InteractorExample();

    new InteractorExecution<>(interactor)
    .result(new InteractorResult<Boolean>() {

      @Override public void onResult(Boolean result) {
        Log.d(TAG, "onSuccess: " + result);
      }
    })
    .error(InteractorErrorExample.class, new InteractorResult<InteractorErrorExample>() {
      @Override public void onResult(InteractorErrorExample result) {
        Log.d(TAG, "onError: " + result.toString());
      }
    }).execute(interactorInvoker);

Interactor response is returned in the result or error callback methods.

Domain

Our interactors look like:

public class InteractorExample implements Interactor<InteractorResponse<Boolean>> {

  @Override public InteractorResponse<Boolean> call() throws Exception {
    BusinessObject<Boolean> boBoolean =
        new BusinessObject<>(true, BusinessError.createOKInstance());

    //BusinessObject<Boolean> boBooleanError =
    //    new BusinessObject<>(null, BusinessError.createKoInstance(""));

    if (boBoolean.isSuccess()) {
      return new InteractorResponse<>(true);
    } else {
      return new InteractorResponse<>(new InteractorErrorExample());
    }
  }
}

Interactor Errors

We have to implements the InteractorError interface to have new classes which define our interactor errror.

public class InteractorErrorExample implements InteractorError {

  @Override public BusinessError getError() {
    return null;
  }
}

Change thread

Change to Ui Thread when interactor task is finished we have to implement the Presenter interface in out presenter class.

public class MainPresenter extends Presenter<MainView> 

The interface which implements out main activity have to be like this:

@ThreadDecoratedView
public interface MainView {

  @NotDecorated
  void initUi();

  void append(String s);
}

Where @ThreadDecoratedView annotation indicates the calls to the method in this interface will be done in a new thread. The @NotDecorated annotation indicates this method is executed in the same thread which is called.

To return to new thread we have to call

   getView().append("");

In our Activity we have to attach the view so we have to call

   presenter.attachView(this);

and when the view is attached we have to call in our presenter and initialize all the UI elements

@Override public void onViewAttached() {
    getView().initUi();
    Log.i(TAG + 2, Thread.currentThread().getName());
  }

Gradle dependency

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:2.1.2'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
	compile 'com.github.Gigigo-Android-Devs.gigigo-interactor-executor-library:interactorexecutor-invoker:1.0.6'
	apt 'com.github.Gigigo-Android-Devs.gigigo-interactor-executor-library:threaddecoratedview-compiler:1.0.6'
	compile 'com.github.Gigigo-Android-Devs.gigigo-interactor-executor-library:threaddecoratedview-common:1.0.6'
	}

TODO

  • Library has to notify user in the middle of a proccess, for example, the remaining time to finish the task.

License

Copyright 2016 Gigigo.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

gigigo-interactorexecutor-library-android's People

Contributors

beni84 avatar pedroamador avatar ruimiguel avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.