Android Libraries

 

 

 

 


In Real Life

Jolanda Verhoef

Android Developer @ Blendle

Which libraries?

Timber

Butterknife

GSON

Universal Image Loader

OkHttp

RxJava

Mosby MVP

Dagger 2

Why this

talk?

Easy Logging

Timber.d("There are %s apples", x);
Log.d(TAG, "There are " + x + " apples);
Timber.w(e, "Reason for the error");
Log.w("Reason for the error");
e.printStackTrace();

Butterknife

Easy View Binding

private TextView mTextView;
private ImageView mImageView;
private RelativeLayout mContainerView;

public void onCreate(Bundle savedInstanceState) {
  mTextView = (TextView) this.findViewById(R.id.textView);
  mImageView = (ImageView) this.findViewById(R.id.imageView);
  mContainerView = (RelativeLayout) this.findViewById(R.id.container);
}
@Bind(R.id.textView) TextView mTextView;
@Bind(R.id.imageView) ImageView mImageView;
@Bind(R.id.container) RelativeLayout mContainerView;
private TextView mTextView;

public void onCreate(Bundle savedInstanceState) {
  mTextView = (TextView) this.findViewById(R.id.textView);

  mTextView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      ((TextView) v).setText("Hello World!");
    }
  });
}
@OnClick(R.id.textview)
public void textViewClicked(TextView tv) {
    tv.setText("Hello World!");
}

GSON

Easy JSON Parsing

{
  "page_preview": {
    "href": "https://static.blendle.nl/...6b831ee.jpg",
    "width": 412,
    "height": 550
  },
  "id": "bnl-soigneur-20150601",
  "initial_publication_time": "2015-06-01T00:00:00+00:00"
}
public class Issue {
  @SerializedName("page_preview")
  PagePreview pagePreview;

  @SerializedName("inital_publication_date")
  Date publicationDate; 

  String id;

  public static class PagePreview {
    String href;
    int width;
    int height;
  }
}
Issue i = new Gson().fromJson(jsonString, Issue.class);

Universal Image Loader

Easy Image Loading

Advantages

Asynchronous

Memory management

Disk caching

SOLVED PROBLEM!

OkHttp

Easy Network Requests

Request request = new Request.Builder()
    .url(url)
    .build();

Response response = new OkHttpClient().newCall(request).execute();
String body = response.body().string();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
String body = EntityUtils.toString(response.getEntity());

Advantages of OkHttp

Sharing sockets

Connection pooling

Transparent GZIP

Caching for repeated requests

Silent recovery

Reactive components

RxJava

Browsing Newspapers & Magazines in Blendle

  • Retrieving your favorites
  • Retrieving popular issues
  • Retrieving issues per category

Retrieving latest issues of your favorite titles

  • Retrieve a list of urls
  • Each url points to a title issue
  • Concurrently retrieve issues
Observable<List<Issue>> myObservable = mDataManager
  .getUserFavourites()
  .flatMap(Observable::from)
  .map(UserFavourite::getLatestIssueLink)
  .concatMap(this::getIssue)
  .toList();
myObservable.subscribe(
  this::showIssues,
  this::showError);

Mosby MVP

It's all about intentions

Presenter

View

public interface LoginView extends MvpView {
  void showLoginLoading();
  void loginSucceeded();
  void loginFailed();
}


public class LoginPresenter implements MvpPresenter<LoginView> {
  public void cancel() {}
  public void forgotPassword() {}
  public void login(String username, String password) {}
}

Complex Example

Why a library?

Dagger 2

Dependency Injection

In software engineering, dependency injection

is a software design pattern that implements

inversion of control for resolving dependencies.

Dependency injection means giving an object

its instance variables. Really. That's it.

~ Wikipedia

DataManager mDataManager;
Analytics mAnalytics;
Session mSessionl
KioskService mKioskService;

public KioskPresenter(DataManager dataManager,
        Analytics analytics, Session session,
        KioskService kioskService) {
  mDataManager = dataManager;
  mAnalytics = analytics;
  mSession = session;
  mKioskService = kioskService;
}
@Inject DataManager mDataManager;
@Inject Analytics mAnalytics;
@Inject Session mSession;
@Inject KioskService mKioskService;

public KioskPresenter(Context context) {
  MyApp.get(context).getSessionComponent().inject(this);
}


@Module
public class SessionModule {
  @Provides
  public OkHttpClient provideHttpClient() {
    return new OkHttpClient();
  }

  @Provides
  public DataManager provideDataManager(client) {
    return new DataManager(client);
}
DataManager mDataManager;
Analytics mAnalytics;
Session mSessionl
KioskService mKioskService;

public KioskPresenter(Context context) {
  mDataManager = DataManager.getInstance();
  mAnalytics = new Analytics(context);
  mSession = Session.retrieveSession();
  mKioskService = new KioskService(context);
}

Which libraries?

Timber

Butterknife

GSON

Universal Image Loader

OkHttp

RxJava

Mosby MVP

Dagger 2

@lojanda

jolanda@blendle.com

Made with Slides.com