SlideShare a Scribd company logo
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 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 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();

}

}


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
Retrolambda+bolts

More Related Content

PDF
JavaFX8 TestFX - CDI
PDF
vJUG - The JavaFX Ecosystem
PDF
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
PDF
中華電信 教育訓練
PDF
Sep Nasiri "Upwork PHP Architecture"
PDF
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
PDF
How to create an Angular builder
PDF
JSDC 2015 - TDD 的開發哲學,以 Node.js 為例
JavaFX8 TestFX - CDI
vJUG - The JavaFX Ecosystem
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
中華電信 教育訓練
Sep Nasiri "Upwork PHP Architecture"
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
How to create an Angular builder
JSDC 2015 - TDD 的開發哲學,以 Node.js 為例

What's hot (20)

ODP
Eclipse Mars News @JUG HH
PDF
"How to Use Bazel to Manage Monorepos: The Grammarly Front-End Team’s Experie...
PDF
Виталий Редько "React + Redux: performance & scalability"
PDF
Александр Махомет "Feature Flags. Уменьшаем риски при выпуске изменений"
PDF
Супер быстрая автоматизация тестирования на iOS
PDF
vienna.js - Automatic testing of (RESTful) API documentation
PDF
Continuous Integration for your Android projects
PDF
Rest, sockets em golang
PDF
Graphql usage
PDF
java8-patterns
PDF
Philip Shurpik "Architecting React Native app"
PDF
Viktor Turskyi "Effective NodeJS Application Development"
PDF
Introducing spring
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
Gradle起步走: 以CLI Application為例 @ JCConf 2014
PDF
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
PDF
Use React Patterns to Build Large Scalable App
PDF
Webinar - Unbox GitLab CI/CD
PDF
PPTX
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Eclipse Mars News @JUG HH
"How to Use Bazel to Manage Monorepos: The Grammarly Front-End Team’s Experie...
Виталий Редько "React + Redux: performance & scalability"
Александр Махомет "Feature Flags. Уменьшаем риски при выпуске изменений"
Супер быстрая автоматизация тестирования на iOS
vienna.js - Automatic testing of (RESTful) API documentation
Continuous Integration for your Android projects
Rest, sockets em golang
Graphql usage
java8-patterns
Philip Shurpik "Architecting React Native app"
Viktor Turskyi "Effective NodeJS Application Development"
Introducing spring
3 WAYS TO TEST YOUR COLDFUSION API
Gradle起步走: 以CLI Application為例 @ JCConf 2014
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
Use React Patterns to Build Large Scalable App
Webinar - Unbox GitLab CI/CD
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Ad

Similar to Retrolambda+bolts (7)

PDF
RxJava With retrolambda
PPTX
Lecture android best practices
PDF
[Ultracode Munich #4] Short introduction to the new Android build system incl...
PDF
Mobile Apps by Pure Go with Reverse Binding
PDF
Modern Android app library stack
PDF
RxJava и Android. Плюсы, минусы, подводные камни
PDF
Kotlin coroutine - the next step for RxJava developer?
RxJava With retrolambda
Lecture android best practices
[Ultracode Munich #4] Short introduction to the new Android build system incl...
Mobile Apps by Pure Go with Reverse Binding
Modern Android app library stack
RxJava и Android. Плюсы, минусы, подводные камни
Kotlin coroutine - the next step for RxJava developer?
Ad

More from Tom Sun (8)

PDF
Pioc
PDF
Cloud radio 閃電秀
PDF
健康報告:德國飲食
PDF
Linux usb2ether
PDF
iOs app 101
PDF
Whos Fault
PDF
小巫婆麗特拉
PDF
Serial Pnp
Pioc
Cloud radio 閃電秀
健康報告:德國飲食
Linux usb2ether
iOs app 101
Whos Fault
小巫婆麗特拉
Serial Pnp

Recently uploaded (20)

PPT
Total quality management ppt for engineering students
PDF
Soil Improvement Techniques Note - Rabbi
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
Management Information system : MIS-e-Business Systems.pptx
PPTX
CyberSecurity Mobile and Wireless Devices
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
communication and presentation skills 01
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
Current and future trends in Computer Vision.pptx
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
PPTX
Amdahl’s law is explained in the above power point presentations
Total quality management ppt for engineering students
Soil Improvement Techniques Note - Rabbi
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Management Information system : MIS-e-Business Systems.pptx
CyberSecurity Mobile and Wireless Devices
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Fundamentals of safety and accident prevention -final (1).pptx
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
communication and presentation skills 01
distributed database system" (DDBS) is often used to refer to both the distri...
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Current and future trends in Computer Vision.pptx
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
Amdahl’s law is explained in the above power point presentations

Retrolambda+bolts

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

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