Skip to content

Instantly share code, notes, and snippets.

View mgp's full-sized avatar
🤗

Michael Parker mgp

🤗
View GitHub Profile
package org.khanacademy.core.topictree.models;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
/**
* Uniquely identifies an {@link Exercise} instance.
*/
public final class ExerciseDescriptor {
package org.khanacademy.core.topictree.models;
/**
* An enumeration over all exercise frameworks.
*/
public enum ExerciseFramework {
KHAN_EXERCISES,
PERSEUS,
}
@mgp
mgp / gist:3da4b06d308f5b1d0490
Created March 12, 2015 01:16
Load an article
/*
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content, TopicTutorialListFragment.newInstance(subject))
.addToBackStack("subject")
.commit();
*/
System.out.println("navigating");
Article article = new Article("xbdcfe503", "An Introduction to the Protestant Reformation");
@mgp
mgp / gist:dfe381800ee2cc3fa3e2
Created April 9, 2015 00:33
Failed use of an anonymous inner class
override func loadView() {
// Create a KAExerciseControlsDelegate implementation that invokes methods on the delegate
// of this instance.
weak var weakSelf = self
class ViewDelegate: KAExerciseControlsDelegate {
// Invokes the given block with the view controller and its delegate, if both are still
// allocated.
private func callControllerDelegate(caller: (ExerciseControlsViewDelegate, ExerciseControlsViewController) -> Void) {
if let strongSelf = weakSelf, strongDelegate = strongSelf.delegate {
caller(strongDelegate, strongSelf)
@mgp
mgp / gist:8a175c90edb864c847ab
Created April 9, 2015 23:10
Previous RAC to dim
let exerciseTransitioningSignal = exerciseTransitioningSubject
.startWithBoxedValue(false)
.takeUntil(rac_willDeallocSignal())
controlsEnabledSubscription = exerciseTransitioningSignal
.combineLatestWith(isReachableSignal)
.map {
let tuple = $0 as! RACTuple
let isExerciseTransitioning = (tuple.first as! Box<Bool>).value
let isReachable = (tuple.second as! Box<Bool>).value
@mgp
mgp / ObservableUtils.java
Created June 3, 2015 15:33
ObservableUtils.java
package org.khanacademy.core.util;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Optional;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;
@mgp
mgp / ObservableUtilsTest.java
Created June 3, 2015 15:33
ObservableUtilsTest.java
package org.khanacademy.core.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.khanacademy.core.util.test_util.Observables.collectAll;
import static org.khanacademy.core.util.test_util.Observables.collectFirst;
import org.khanacademy.core.base.BaseTestCase;
import org.khanacademy.core.exceptions.TestException;
package org.khanacademy.android.ui.videos;
import org.khanacademy.android.logging.Logger;
import org.khanacademy.android.ui.videos.VideoViewActivity.VideoPlayer;
import org.khanacademy.core.progress.ProgressUpdater;
import org.khanacademy.core.progress.models.VideoUserProgress;
import org.khanacademy.core.topictree.identifiers.ContentItemIdentifier;
import org.khanacademy.core.util.ObservableUtils;
import com.google.common.base.Preconditions;
/**
* Describes the expected observed values by an observable, for testing that an observable
* emits those described values.
*/
public static final class ExpectedObservedEvents<E> {
public enum TerminalEvent {
COMPLETED,
ERROR
}
@mgp
mgp / android-development-best-practices.md
Created June 19, 2015 01:51
Android development best practices

Prefer Immutability

Our principal goal as programmers is to reduce complexity. At any point in a program, we can reason about the state of a constant trivially -- its state is the state upon assignment. By contrast, the state of a variable can change endlessly.

With judicious use, immutable objects can lead to quicker execution speed and lower memory usage. The hash value of a String, the query parameters of an immutable URL, and the distance traced by an immutable sequence of points are all immutable as well. We can cache their values for later use instead of recompute them on every access. We can also share an immutable object with clients without requiring those clients to defensively copy it.

An immutable value is safe in many ways. For example, String is immutable and so its hashCode value is immutable. Consequently, it is safe to use as a String as a key in a Map: The lower order bits of that hash will never change, and so an inserted key will never reside in the wrong bucket. An immutab