Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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
Example
show 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 emptyCal...




private AlertDialog buildDialog(String message,

String cb1Option, final RunnableCB cb1)
{



AlertDialog.Builder buil...


final RunnableCB cbSwitchOff = new RunnableCB() {

public void run() {

((CompoundButton) bleSwitch).setChecked(false);
...
Refactor with Bolts
public class Dialog {

static Task<Integer> dlg(String msg, String opt1, String opt2) {

final Task<In...


String str = getResources().getString(R.string.no_ble_device_available);


mDialog.show(str, "OK", null).continueWith(ne...
,
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://
ingramch...
Retrolambda Limitation
• Does not back port Java 8 APIs

https://docs.oracle.com/javase/8/docs/api/java/
util/function/pac...
gradle
•
• https://github.com/evant/gradle-retrolambda
// defined in the SDK
interface OnClickListener {
public void onClick(View v);
}
// your code
mButton.setOnClickListener(n...
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>.TaskCompletio...


String str = getResources().getString(R.string.no_ble_device_available);


mDialog.show(str, "OK", null).continueWith(ne...
public Task<Integer> show(String msg, String opt1, String opt2) {

final Task.TaskCompletionSource tcs = Task.create();

f...
mDialog.show1(R.string.no_ble_device_available).continueWith(task -> {

((CompoundButton) bleSwitch).setChecked(false);

g...
Retrolambda+bolts
Upcoming SlideShare
Loading in …5
×

Retrolambda+bolts

219 views

Published on

Language: Traditional Chinese
Bolts-Android & RetroLambda introduction

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Retrolambda+bolts

  1. 1. Bolts + RetroLambda 2015.11.26
  2. 2. • Bolts-Android • Design API Based On Bolts • Retrolabmda • Refactor
  3. 3. Why use ? • spec.
  4. 4. member func() { Serialize Asynchronous Tasks callback { callback { callback{ } then { then { then { } } } } member func() { } } }
  5. 5. • Bolts-Android • Design API Based On Bolts • Retrolabmda • Refactor
  6. 6. Example show dialog press “YES” button Cancel Do something NO YES
  7. 7. Example 1/4 public class RunnableCB implements Runnable {
 public void run() {
 this.run();
 }
 } 
 

  8. 8. 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
  9. 9. 
 
 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
  10. 10. 
 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
  11. 11. Refactor with Bolts 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();
 }
 }
  12. 12. 
 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
  13. 13. , they !! ( Java)
  14. 14. • Bolts-Android • Design API Based On Bolts • Retrolabmda • Refactor
  15. 15. 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
  16. 16. Retrolambda Limitation • Does not back port Java 8 APIs
 https://docs.oracle.com/javase/8/docs/api/java/ util/function/package-summary.html
  17. 17. gradle • • https://github.com/evant/gradle-retrolambda
  18. 18. // 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
  19. 19. mButton.setOnClickListener((View v) -> { // do something here }); syntax sugar 2/2
  20. 20. • Bolts-Android • Design API Based On Bolts • Code Example • Retrolabmda • Refactor
  21. 21. 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();
 }
 } -> ->
  22. 22. 
 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;
 }
 }); 
 ->
  23. 23. 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
  24. 24. mDialog.show1(R.string.no_ble_device_available).continueWith(task -> {
 ((CompoundButton) bleSwitch).setChecked(false);
 gotoScanPage();
 return null;
 }); Result 2/2

×