61
Don’t reinvent the wheel Modern Android Stack

Don't reinvent the wheel, use libraries

  • Upload
    polidea

  • View
    253

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Don't reinvent the wheel, use libraries

Don’t reinvent the wheel

Modern Android Stack

Page 2: Don't reinvent the wheel, use libraries

Paweł Junak

Page 3: Don't reinvent the wheel, use libraries

Libs:

● Timber● Butterknife● Picasso● RoboSpice

+ Retrofit

● ORMLite● Lombok● Parceler● Dagger2

Page 4: Don't reinvent the wheel, use libraries

Open Source Bar

Page 5: Don't reinvent the wheel, use libraries

Timber

never again:deleting logs fromproduction app

Page 6: Don't reinvent the wheel, use libraries

Timber

if (BuildConfig.DEBUG) {Timber.plant(new Timber.

DebugTree());}

Page 7: Don't reinvent the wheel, use libraries

Timber

Timber.d("Downloading URL: %s", url);

Page 8: Don't reinvent the wheel, use libraries

Butterknife

never again:findViewById()

Page 9: Don't reinvent the wheel, use libraries

Butterknife

EditText email = (EditText) findViewById(R.id.email)

Page 10: Don't reinvent the wheel, use libraries

Butterknife

@Bind(R.id.email)EditText email;

Page 11: Don't reinvent the wheel, use libraries

Butterknife

never again:button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doStuff() } });

Page 12: Don't reinvent the wheel, use libraries

Butterknife

@onClick(R.id.button)public void doStuff(View v)

Page 13: Don't reinvent the wheel, use libraries

Butterknife

@onClick(R.id.button)public void doStuff()

Page 14: Don't reinvent the wheel, use libraries

Butterknife

@onClick(R.id.button)public void doStuff(Button b) {

button.setText(); }

Page 15: Don't reinvent the wheel, use libraries

Butterknife

ButterKnife.bind(this);● OnCreate ButterKnife.bind(this, view);● onViewCreated(View v, Bundle b),● ViewHolder(View v):

Page 16: Don't reinvent the wheel, use libraries

Picasso

never again:AsyncTask with HttpConnectiondownloading image

Page 17: Don't reinvent the wheel, use libraries

Picasso

Picasso.with(context).load(url).into(imageView);

Page 18: Don't reinvent the wheel, use libraries

Picasso

Picasso.with(context).load(url).placeholder(R.drawable.placeholder).error(R.drawable.placeholder_error).into(imageView);

Page 19: Don't reinvent the wheel, use libraries

Picasso

Picasso.with(context).load(url).resize(50, 50).centerCrop().into(imageView);

Page 20: Don't reinvent the wheel, use libraries

RoboSpice + Retrofit

never again:AsyncTask with HttpConnection

Page 21: Don't reinvent the wheel, use libraries

RoboSpice

never again:AsyncTask

Page 22: Don't reinvent the wheel, use libraries

RoboSpice

Page 23: Don't reinvent the wheel, use libraries

RoboSpice

Page 24: Don't reinvent the wheel, use libraries

Retrofit

never again:HttpConnection

Page 25: Don't reinvent the wheel, use libraries

Retrofit

public interface GitHubService {@GET("/users/{user}/repos")List<Repo> listRep(@Path("user") String u);

}

Page 26: Don't reinvent the wheel, use libraries

Retrofit

@Headers("Cache-Control: max-age=640000")

@Headers({ "Accept: application/vnd.github.v3.full+json", "User-Agent: Retrofit-Sample-App"

})

Page 27: Don't reinvent the wheel, use libraries

Retrofit

@GET(/users)void getUser(@Header("Auth") String auth);

Page 28: Don't reinvent the wheel, use libraries

Retrofit

@GET("/group/{id}/users")List<User> groupList(@Path("id") int groupId,

@Query("sort") String sort);

Page 29: Don't reinvent the wheel, use libraries

Retrofit

RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build();

Page 30: Don't reinvent the wheel, use libraries

Retrofit

GitHubService serv = RestAdapter.create(GitHubService.class);

List<Repo> repos = service.listRepos("octocat");

Page 31: Don't reinvent the wheel, use libraries

RetrofitPOJO

public class Repo {int id;String name;

}

Page 32: Don't reinvent the wheel, use libraries

RoboSpice + Retrofit

@Overridepublic void onRequestSuccess(Repo repo) { // TODO forkAll }@Overridepublic void onRequestFailure(SpiceException ex) { // TODO fail }

Page 33: Don't reinvent the wheel, use libraries

Mention

Retrofit 2 coming

Page 34: Don't reinvent the wheel, use libraries

ORMLite

never again:SQL

Page 35: Don't reinvent the wheel, use libraries

ORMLite@DatabaseTable(tableName = "repo")public class Repo {

@DatabaseField(id = true)int id;@DatabaseField(canBeNull = false)String name;

}

Page 36: Don't reinvent the wheel, use libraries

ORMLite

No Args Constructor needed

public Repo() {};

Page 37: Don't reinvent the wheel, use libraries

ORMLite

class DbHelper extends OrmLiteSqliteOpenHelper

onCreate(SQLiteDatabase db, ConnectionSource cS) {TableUtils.createTable(cS, Repo.class);

}

Page 38: Don't reinvent the wheel, use libraries

ORMLite

class DbHelper extends OrmLiteSqliteOpenHelper

Dao<Repo,Integer> getDao() {return getDao(Repo.class)

}

Page 39: Don't reinvent the wheel, use libraries

ORMLite

class MainAct extends OrmLiteBaseActivity<DbHelper>

Dao<Repo,Integer> repoDao = getHelper.getDao();

Page 40: Don't reinvent the wheel, use libraries

ORMLite

Repo repo = new Repo(id, name);repoDao.create(repo);

Page 41: Don't reinvent the wheel, use libraries

ORMLite

Repo repo = repoDao.queryForId(id);

Page 42: Don't reinvent the wheel, use libraries

ORMLite

module in RoboSpice :)

Page 43: Don't reinvent the wheel, use libraries

Mention

yahoo - squidbsquare - sqlbrite

Page 44: Don't reinvent the wheel, use libraries

Lombok

never again:boilerplate getters,setters, constructors

Page 45: Don't reinvent the wheel, use libraries

Lombok

@Getter @Setter

public class Repo {int id;String name;

}

Page 46: Don't reinvent the wheel, use libraries

Lombok

public class Repo {@Getterint id;@SetterString name;

}

Page 47: Don't reinvent the wheel, use libraries

Lombok

@NoArgsConstructorpublic class Repo {

int id;String name;

}

Page 48: Don't reinvent the wheel, use libraries

Lombok

@Datapublic class Repo {

int id;String name;

}

Page 49: Don't reinvent the wheel, use libraries

Parceler

never again:public static final Creator<Repo> CREATOR =

new Creator<Repo> {public Repo createFromParcel(Parcel source);public Repo[] newArray(int size);}public void writeToParcel(Parcel dest, int flags);

Page 50: Don't reinvent the wheel, use libraries

Parceler

@Parcelpublic class Repo

Page 51: Don't reinvent the wheel, use libraries

Parceler

Parcelable pr = Parcel.wrap(new Repo())Repo repo = Parcel.unwrap(pr)

Page 52: Don't reinvent the wheel, use libraries

Lombok@Getter

@Setter@NoArgsConstructor@Parcel@DatabaseTable(tableName = "repo")public class Repo {

@DatabaseField(id = true)int id;@DatabaseField(canBeNull = false)String name;

}

Page 53: Don't reinvent the wheel, use libraries

Dagger2

never again:init SharedPreferences

Page 54: Don't reinvent the wheel, use libraries

Dagger2

@InjectSharedPreferences prefs;

Page 55: Don't reinvent the wheel, use libraries

Dagger2@Moduleclass Utils

@ProvidesSharedPreferences provideSharedPreferences(){

return context.getSharedPreferences("pref",Context.MODE_PRIVATE);

}

Page 56: Don't reinvent the wheel, use libraries

Dagger2

tools.saveMail(mail);tools.getMail();tools.saveToken(token);tools.getToken();

Page 57: Don't reinvent the wheel, use libraries

Dagger2

@ProvidesTools provideTools(){

return new Tools();}

Page 58: Don't reinvent the wheel, use libraries

Dagger2

@InjectTools tools;

Page 59: Don't reinvent the wheel, use libraries

Summary:

● Butterknife● Picasso● RoboSpice

+ Retrofit● ORMLite

● Lombok● Parceler● Dagger2● Timber

github.com/Polidea/OpenSourceBar-Android

Page 60: Don't reinvent the wheel, use libraries

Q&A

Page 61: Don't reinvent the wheel, use libraries

Thanks