Stan-for-Android-Core

Stan-for-Android

License

License

GroupId

GroupId

com.label305.stan
ArtifactId

ArtifactId

lib-utils
Last Version

Last Version

0.4.0
Release Date

Release Date

Type

Type

aar
Description

Description

Stan-for-Android-Core
Stan-for-Android
Project URL

Project URL

https://github.com/label305/Stan-for-Android
Source Code Management

Source Code Management

https://github.com/label305/Stan-for-Android

Download lib-utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.label305.stan/lib-utils/ -->
<dependency>
    <groupId>com.label305.stan</groupId>
    <artifactId>lib-utils</artifactId>
    <version>0.4.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.label305.stan/lib-utils/
implementation 'com.label305.stan:lib-utils:0.4.0'
// https://jarcasting.com/artifacts/com.label305.stan/lib-utils/
implementation ("com.label305.stan:lib-utils:0.4.0")
'com.label305.stan:lib-utils:aar:0.4.0'
<dependency org="com.label305.stan" name="lib-utils" rev="0.4.0">
  <artifact name="lib-utils" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.label305.stan', module='lib-utils', version='0.4.0')
)
libraryDependencies += "com.label305.stan" % "lib-utils" % "0.4.0"
[com.label305.stan/lib-utils "0.4.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.intellij : annotations jar +

Project Modules

There are no modules declared in this project.

AsyncTask-Android Download

AsyncTask made simpler.

Set up

Add the following to the dependencies section of your build.gradle:

compile 'com.label305:asynctask:x.x.x.'

SimpleAsyncTask

Use a SimpleAsyncTask when doing work that will not throw a checked Exception:

public class MyAsyncTask extends SimpleAsyncTask<String> {

  @Override
  protected String doInBackground() {
    return "Test";
  }

  @Override
  protected void onSuccess(final String result) {
    // Do something with the result
  }
}

AsyncTask

Use an AsyncTask when doing work that may throw a checked Exception:

public class MyAsyncTask extends AsyncTask<String, IOException> {

  @Override
  protected String doInBackground() throws IOException {
    return myWebservice.getSomeString();
  }

  @Override
  protected void onSuccess(final String result) {
    // Do something with the result
  }

  @Override
  protected void onException(final IOException e) {
    // Handle IOException
  }
}

Executing

Like Android's AsyncTask, you can call execute() on the AsyncTask instance to start it:

new MyAsyncTask().execute();

Alternatively, you can use an AsyncTaskExecutor to delegate the execution. A default implementation is available as AsyncTaskExecutor.DEFAULT_EXECUTOR, which is used by AsyncTask.execute().

AsyncTaskExecutor.DEFAULT_EXECUTOR.execute(new MyAsyncTask());

Testing

The AsyncTaskExecutor can come in handy while testing. When practicing dependency injection, you can inject a different implementation that executes the AsyncTask synchronously. TestAsyncTaskExecutor can be used for such tests. To access this class, add the following to your relevant build.gradle section:

testCompile 'com.label305:asynctask-test:x.x.x' // For unit tests
androidTestCompile 'com.label305:asynctask-test:x.x.x' // For instrumentation tests

For example, suppose we have the following class for asynchronously retrieving a User from some web service:

public class UserRetriever {

  private final WebService mWebService;

  private final AsyncTaskExecutor mExecutor;

  public UserRetriever(final WebService webService, final AsyncTaskExecutor executor) {
    mWebService = webService;
    mExecutor = executor;
  }

  /**
   * Retrieves the User asynchronously, and notifies given callback when the User is retrieved.
   */
  public void retrieveUser(final UserCallback callback) {
    mExecutor.execute(new RetrieveUserTask(callback));
  }

  public interface UserCallback {

    void onUserRetrieved(User user);
  }

  private class RetrieveUserTask extends SimpleAsyncTask<User> {

    private final UserCallback mCallback;

    private RetrieveUserTask(final UserCallback callback) {
      mCallback = callback;
    }

    @Override
    public User doInBackground() {
      return mWebService.retrieveUser();
    }

    @Override
    public void onSuccess(final User user) {
      mCallback.onUserRetrieved(user);
    }
  }
}

We can test this class using mock objects, in this case Mockito:

public class UserRetrieverTest {

  @Test
  public void retrieveUser_retrievesUser_andNotifiesCallback() {
    /* Given */
    User user = new User();

    WebService webService = mock(WebService.class);
    when(webService.retrieveUser()).thenReturn(user);

    UserCallback callback = mock(UserCallback.class);

    UserRetriever userRetriever = new UserRetriever(webService, TestAsyncTaskExecutor.instance());

    /* When */
    userRetriever.retrieveUser(callback);

    /* Then */
    verify(callback).onUserRetrieved(user);
  }
}

License

Copyright 2015 Label305 B.V.

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.

com.label305.stan

Label305

Versions

Version
0.4.0
0.3.1
0.3.0