Skip to content

Instantly share code, notes, and snippets.

@jpetitto
jpetitto / gist:f01fa09af58189f335b5
Created April 30, 2015 19:24
Jake Wharton's Tips for Becoming a Better Android Developer
  1. Don't blindly accept that Google's approach to Android development is always the right way. Judge for yourself what the right approach is (often times it is Google's approach) based on your requirements and solution.

  2. Be comfortable with examining the Android source code. It's not magic, there's always an answer. Helps you solve problems that would otherwise show up on Stackoverflow.

  3. Be multi-discipline. Android can be your focus, but develop for other platforms and use other languages.

  4. Master the tools. Make it work for you.

  5. Be resourceful. Know when and why a library is the right choice over writing your own solution.

@jpetitto
jpetitto / gist:d9ffed9843a5e9bfa358
Created May 12, 2015 22:10
Anonymous Classes vs Lambdas
Observable.just("Hello, world!")
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
System.out.println(s);
}
});
Observable.just("Hello, world!")
.subscribe(s -> System.out.println(s));
@jpetitto
jpetitto / DependencyInjectionExample.java
Created August 12, 2015 03:39
Dependency Injection Example
// no DI
public class Refrigerator {
private PowerSource powerSource;
public Refrigerator() {
powerSource = new AcPowerSource();
}
}
// with DI (manual)
@jpetitto
jpetitto / MainActivity.java
Created August 19, 2015 16:12
Save Fragment state across config changes
public class MainActivity extends AppCompatActivity {
private Fragment savedFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
savedFragment = getSupportFragmentManager().getFragment(savedInstanceState, "content");
@jpetitto
jpetitto / GenericWildcards.java
Created September 28, 2015 17:25
"We simply cannot afford another wildcards" - Joshua Bloch
Enum<E extends Enum<E>> { ... }
<T extends Object & Comparable<? super T>> T Collections.max(Collection<? extends T>) { ... }
public <V extends Wrapper<? extends Comparable<T>>>
Comparator<V> comparator() { ... }
error: equalTo(Box<capture of ?>) in Box<capture of ?> cannot be applied to (Box<capture of ?>)
equal = unknownBox.equalTo(unknownBox)
@jpetitto
jpetitto / HackLoop.java
Last active October 29, 2015 15:20
Hack Week Loop
for (int i = 0, remaining = 5; i < colorBucket.size() && remaining > 0; i++, remaining--) {
...
}
@jpetitto
jpetitto / BetterLoop.java
Created October 29, 2015 15:33
Less Hacky Loop
private <T> int picker(List<T> src, List<T> dst, int limit) {
Iterator<T> iterator = src.iterator();
while (iterator.hasNext() && limit > 0) {
dst.add(iterator.next());
limit--;
}
return limit;
}
@jpetitto
jpetitto / BestLoop.java
Created October 29, 2015 15:41
Least Hacky Loop
List<Item> allBucket = ...
List<Item> genderBucket = ...
List<Item> sizeBucket = ...
List<Item> colorBucket = ...
List<Item> recommendations = new ArrayList<>();
picker(recommendations, limit, colorBucket, sizeBucket, genderBucket, allBucket);
private <T> int picker(List<T> src, List<T> dst, int limit) {
Iterator<T> iterator = src.iterator();
@jpetitto
jpetitto / RxDynamicPoll.java
Created November 5, 2015 16:35
Polling at Dynamic Rate with RxJava
BehaviorSubject<Integer> timerSubject = BehaviorSubject.create(1);
timerSubject
.switchMap(interval -> Observable.timer(interval, TimeUnit.SECONDS, Schedulers.newThread()))
.flatMap(i -> Observable.just(randomInt()))
.doOnNext(timerSubject::onNext)
.subscribe(System.out::println);
@jpetitto
jpetitto / RxGroupBy.java
Last active November 18, 2015 19:55
RxJava "groupBy" Operator
Observable.just(1, 2, 3, 4, 5)
.groupBy(i -> i % 2 == 0 ? "even" : "odd")
.subscribe(obs -> {
if (obs.getKey().equals("even")) {
obs.subscribe(i -> System.out.println("even: " + i));
} else {
obs.subscribe(i -> System.out.println("odd: " + i));
}
});