狠狠撸

狠狠撸Share 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

What's hot (20)

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

Similar to Retrolambda+bolts (7)

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

More from Tom Sun (8)

PDF
Pioc
Tom Sun
?
PDF
Cloud radio 閃電秀
Tom Sun
?
PDF
健康报告:德国饮食
Tom Sun
?
PDF
Linux usb2ether
Tom Sun
?
PDF
iOs app 101
Tom Sun
?
PDF
Whos Fault
Tom Sun
?
PDF
小巫婆丽特拉
Tom Sun
?
PDF
Serial Pnp
Tom Sun
?
Pioc
Tom Sun
?
Cloud radio 閃電秀
Tom Sun
?
健康报告:德国饮食
Tom Sun
?
Linux usb2ether
Tom Sun
?
iOs app 101
Tom Sun
?
Whos Fault
Tom Sun
?
小巫婆丽特拉
Tom Sun
?
Serial Pnp
Tom Sun
?
Ad

Recently uploaded (20)

PDF
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
?
PPTX
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
?
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
?
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
?
PPTX
Introduction to Python Programming Language
merlinjohnsy
?
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipi?a González-de-Artaza
?
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
?
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
?
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
?
PPTX
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
?
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
?
PPTX
Computer network Computer network Computer network Computer network
Shrikant317689
?
PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
?
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
?
PDF
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
?
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
?
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
?
PPT
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
?
PPTX
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
?
PDF
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
?
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
?
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
?
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
?
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
?
Introduction to Python Programming Language
merlinjohnsy
?
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipi?a González-de-Artaza
?
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
?
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
?
FINAL plumbing code for board exam passer
MattKristopherDiaz
?
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
?
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
?
Computer network Computer network Computer network Computer network
Shrikant317689
?
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
?
PRIZ Academy - Process functional modelling
PRIZ Guru
?
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
?
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
?
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
?
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
?
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
?
How to Buy Verified CashApp Accounts IN 2025
Buy Verified CashApp Accounts
?

Retrolambda+bolts

  • 2. ? Bolts-Android ? Design API Based On Bolts ? Retrolabmda ? Refactor
  • 3. Why use ? ? spec.
  • 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