don't reinvent the wheel modern android stack

59
Don’t reinvent the wheel Modern Android Stack

Upload: vukien

Post on 27-Dec-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Don't reinvent the wheel Modern Android Stack

Don’t reinvent the wheel

Modern Android Stack

Page 2: Don't reinvent the wheel Modern Android Stack

Paweł Junak

Page 3: Don't reinvent the wheel Modern Android Stack

Libs:

● Timber● Butterknife● Picasso● RoboSpice

+ Retrofit

● ORMLite● Lombok● Parceler● Dagger2

Page 4: Don't reinvent the wheel Modern Android Stack

Open Source Bar

Page 5: Don't reinvent the wheel Modern Android Stack

Timber

never again:deleting logs fromproduction app

Page 6: Don't reinvent the wheel Modern Android Stack

Timber

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

DebugTree());}

Page 7: Don't reinvent the wheel Modern Android Stack

Timber

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

Page 8: Don't reinvent the wheel Modern Android Stack

Butterknife

never again:findViewById()

Page 9: Don't reinvent the wheel Modern Android Stack

Butterknife

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

Page 10: Don't reinvent the wheel Modern Android Stack

Butterknife

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

Page 11: Don't reinvent the wheel Modern Android Stack

Butterknife

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

Page 12: Don't reinvent the wheel Modern Android Stack

Butterknife

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

Page 13: Don't reinvent the wheel Modern Android Stack

Butterknife

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

Page 14: Don't reinvent the wheel Modern Android Stack

Butterknife

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

button.setText(); }

Page 15: Don't reinvent the wheel Modern Android Stack

Butterknife

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

Page 16: Don't reinvent the wheel Modern Android Stack

Picasso

never again:AsyncTask with HttpConnectiondownloading image

Page 17: Don't reinvent the wheel Modern Android Stack

Picasso

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

Page 18: Don't reinvent the wheel Modern Android Stack

Picasso

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

Page 19: Don't reinvent the wheel Modern Android Stack

Picasso

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

Page 20: Don't reinvent the wheel Modern Android Stack

RoboSpice + Retrofit

never again:AsyncTask with HttpConnection

Page 21: Don't reinvent the wheel Modern Android Stack

RoboSpice

never again:AsyncTask

Page 22: Don't reinvent the wheel Modern Android Stack

RoboSpice

Page 23: Don't reinvent the wheel Modern Android Stack

RoboSpice

Page 24: Don't reinvent the wheel Modern Android Stack

Retrofit

never again:HttpConnection

Page 25: Don't reinvent the wheel Modern Android Stack

Retrofit

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

}

Page 26: Don't reinvent the wheel Modern Android Stack

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 Modern Android Stack

Retrofit

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

Page 28: Don't reinvent the wheel Modern Android Stack

Retrofit

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

@Query("sort") String sort);

Page 29: Don't reinvent the wheel Modern Android Stack

Retrofit

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

Page 30: Don't reinvent the wheel Modern Android Stack

Retrofit

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

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

Page 31: Don't reinvent the wheel Modern Android Stack

RetrofitPOJO

public class Repo {int id;String name;

}

Page 32: Don't reinvent the wheel Modern Android Stack

RoboSpice + Retrofit

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

Page 33: Don't reinvent the wheel Modern Android Stack

ORMLite

never again:SQL

Page 34: Don't reinvent the wheel Modern Android Stack

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

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

}

Page 35: Don't reinvent the wheel Modern Android Stack

ORMLite

No Args Constructor needed

public Repo() {};

Page 36: Don't reinvent the wheel Modern Android Stack

ORMLite

class DbHelper extends OrmLiteSqliteOpenHelper

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

}

Page 37: Don't reinvent the wheel Modern Android Stack

ORMLite

class DbHelper extends OrmLiteSqliteOpenHelper

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

}

Page 38: Don't reinvent the wheel Modern Android Stack

ORMLite

class MainAct extends OrmLiteBaseActivity<DbHelper>

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

Page 39: Don't reinvent the wheel Modern Android Stack

ORMLite

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

Page 40: Don't reinvent the wheel Modern Android Stack

ORMLite

Repo repo = repoDao.queryForId(id);

Page 41: Don't reinvent the wheel Modern Android Stack

ORMLite

module in RoboSpice :)

Page 42: Don't reinvent the wheel Modern Android Stack

Lombok

never again:boilerplate getters,setters, constructors

Page 43: Don't reinvent the wheel Modern Android Stack

Lombok

@Getter @Setter

public class Repo {int id;String name;

}

Page 44: Don't reinvent the wheel Modern Android Stack

Lombok

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

}

Page 45: Don't reinvent the wheel Modern Android Stack

Lombok

@NoArgsConstructorpublic class Repo {

int id;String name;

}

Page 46: Don't reinvent the wheel Modern Android Stack

Lombok

@Datapublic class Repo {

int id;String name;

}

Page 47: Don't reinvent the wheel Modern Android Stack

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 48: Don't reinvent the wheel Modern Android Stack

Parceler

@Parcelpublic class Repo

Page 49: Don't reinvent the wheel Modern Android Stack

Dagger2

never again:init SharedPreferences

Page 50: Don't reinvent the wheel Modern Android Stack

Dagger2

@InjectSharedPreferences prefs;

Page 51: Don't reinvent the wheel Modern Android Stack

Dagger2@Moduleclass Utils

@ProvidesSharedPreferences provideSharedPreferences(){

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

}

Page 52: Don't reinvent the wheel Modern Android Stack

Dagger2

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

Page 53: Don't reinvent the wheel Modern Android Stack

Dagger2

@ProvidesTools provideTools(){

return new Tools();}

Page 54: Don't reinvent the wheel Modern Android Stack

Dagger2

@InjectTools tools;

Page 55: Don't reinvent the wheel Modern Android Stack

Summary:

● Butterknife● Picasso● RoboSpice

+ Retrofit● ORMLite

● Lombok● Parceler● Dagger2● Timber

github.com/Polidea/OpenSourceBar-Android

Page 56: Don't reinvent the wheel Modern Android Stack

@Bonus

Page 57: Don't reinvent the wheel Modern Android Stack

Cellular Network Simulator

Page 58: Don't reinvent the wheel Modern Android Stack

Q&A

Page 59: Don't reinvent the wheel Modern Android Stack

Thanks