Retrolambda+bolts

25
অ砇䲆 Bolts + RetroLambda 疆ᒌ苢 碝ᒓ嘨蜦 2015.11.26

Transcript of Retrolambda+bolts

Bolts + RetroLambda

2015.11.26

• Bolts-Android

• Design API Based On Bolts

• Retrolabmda

• Refactor

Why use ?

• spec.

member func() {

Serialize Asynchronous Tasks

callback {

callback {

callback{ }

then {

then {

then {

} } } }

member func() {

}}}

• Bolts-Android

• Design API Based On Bolts

• Retrolabmda

• Refactor

Exampleshow dialog

press “YES” button Cancel

Do something

NO

YES

Example 1/4

public class RunnableCB implements Runnable { public void run() { this.run(); }}

private AlertDialog simpleAlertDialog(String message, final RunnableCB cb) { if (cb == null) { final RunnableCB emptyCallback= new RunnableCB() { public void run() { } }; return buildDialog(message, "OK", emptyCallback); } else { return buildDialog(message, "OK", cb); }}

Example 2/4

private AlertDialog buildDialog(String message, String cb1Option, final RunnableCB cb1) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message); builder.setPositiveButton(cb1Option, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); cb1.run(); } }); return builder.create();}

Example 3/4

final RunnableCB cbSwitchOff = new RunnableCB() { public void run() { ((CompoundButton) bleSwitch).setChecked(false); }}; String str = getResources().getString(R.string.no_ble_device_available); simpleAlertDialog(str, cbSwitchOff).show();

Example 4/4

Refactor with Boltspublic class Dialog { static Task<Integer> dlg(String msg, String opt1, String opt2) { final Task<Integer>.TaskCompletionSource tcs = Task.create(); AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setMessage(msg); if (opt1 != null && !opt1.isEmpty()) { builder.setPositiveButton(opt1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tcs.setResult(0); } }); } if (opt2 != null && !opt2.isEmpty()) { builder.setPositiveButton(opt2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tcs.setResult(1); } }); } AlertDialog d = builder.create(); d.show(); return tcs.getTask(); }}

String str = getResources().getString(R.string.no_ble_device_available); mDialog.show(str, "OK", null).continueWith(new Continuation<Integer, Void>() { @Override public Void then(Task<Integer> task) throws Exception { ((CompoundButton) bleSwitch).setChecked(false); return null; }});

Calling API we made

RunnableCB

,

they !! ( Java)

• Bolts-Android

• Design API Based On Bolts

• Retrolabmda

• Refactor

What’s Retrolambda

• Back port Java8 to Java 7, 6, 5

• Syntax Sugar ( )

• Good introduction : Ingram Chen http://ingramchen.io/blog/2014/10/retromlambda.html

Retrolambda Limitation

• Does not back port Java 8 APIs https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

gradle

• https://github.com/evant/gradle-retrolambda

// defined in the SDK interface OnClickListener { public void onClick(View v); }

// your code mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do something here } });

syntax sugar 1/2

mButton.setOnClickListener((View v) -> { // do something here });

syntax sugar 2/2

• Bolts-Android

• Design API Based On Bolts

• Code Example

• Retrolabmda

• Refactor

public class Dialog { static Task<Integer> dlg(String msg, String opt1, String opt2) { final Task<Integer>.TaskCompletionSource tcs = Task.create(); AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setMessage(msg); if (opt1 != null && !opt1.isEmpty()) { builder.setPositiveButton(opt1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tcs.setResult(0); } }); } if (opt2 != null && !opt2.isEmpty()) { builder.setPositiveButton(opt2, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tcs.setResult(1); } }); } AlertDialog d = builder.create(); d.show(); return tcs.getTask(); }}

->

->

String str = getResources().getString(R.string.no_ble_device_available); mDialog.show(str, "OK", null).continueWith(new Continuation<Integer, Void>() { @Override public Void then(Task<Integer> task) throws Exception { ((CompoundButton) bleSwitch).setChecked(false); return null; }});

->

public Task<Integer> show(String msg, String opt1, String opt2) { final Task.TaskCompletionSource tcs = Task.create(); final AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setMessage(msg); if (opt1 != null && !opt1.isEmpty()) { builder.setPositiveButton(opt1, (DialogInterface dialog, int which) -> { tcs.setResult(0); }); } if (opt2 != null && !opt2.isEmpty()) { builder.setNegativeButton(opt2, (DialogInterface dialog, int which) -> { tcs.setResult(1); }); } AlertDialog d = builder.create(); d.show(); return tcs.getTask();}

Result 1/2

mDialog.show1(R.string.no_ble_device_available).continueWith(task -> { ((CompoundButton) bleSwitch).setChecked(false); gotoScanPage(); return null;});

Result 2/2