Skip to content

Instantly share code, notes, and snippets.

View Zhuinden's full-sized avatar
🤔
Fighting Proguard

Gabor Varadi Zhuinden

🤔
Fighting Proguard
View GitHub Profile
@Zhuinden
Zhuinden / realm-newspost-model.java
Last active September 4, 2016 06:52
Realm News Post Example Model
public class NewsPost extends RealmObject {
@PrimaryKey
@Required
private long id;
// left nullable as default as example
private String author;
private String title;
@Zhuinden
Zhuinden / realm-ui-thread-singleton.java
Last active September 4, 2016 16:56
Realm Singleton instance for UI thread from Book Example
public class BooksScopeListener extends Fragment { // could go to base class
public BooksScopeListener() {
setRetainInstance(true);
RealmManager.incrementCount();
}
@Override
public void onDestroy() {
RealmManager.decrementCount();
super.onDestroy();
// initialization
Context baseContext = Flow.configure(baseContext, this) //
.defaultKey(LoginKey.create()) //
.dispatcher(flowDispatcher) //
.install();
super.attachBaseContext(baseContext);
// manipulation of backstack
Flow flow = Flow.get(this);
public interface Dispatcher {
/**
* Called when the history is about to change. Note that Flow does not consider the
* Traversal to be finished, and will not actually update the history, until the callback is
* triggered. Traversals cannot be canceled.
*
* @param callback Must be called to indicate completion of the traversal.
*/
void dispatch(@NonNull Traversal traversal, @NonNull TraversalCallback callback);
}
@Override
public void changeKey(State outgoingState, State incomingState, final Direction direction,
Map<Object, Context> incomingContexts, final TraversalCallback callback) {
// SAVE STATE FOR VIEW TO BE REMOVED
final View previousView = mainActivity.root.getChildAt(0);
if(outgoingState != null && previousView != null) {
outgoingState.save(previousView);
}
LayoutKey newKey = incomingState.getKey();
Context internalContext = incomingContexts.get(newKey);
public class BasicKeyParceler implements KeyParceler {
@Override
@NonNull
public Parcelable toParcelable(@NonNull Object key) {
return (Parcelable) key;
}
@Override
@NonNull
public Object toKey(@NonNull Parcelable parcelable) {
@Zhuinden
Zhuinden / flow-dispatcher-simple.java
Last active October 7, 2016 13:03
Flow Simple Dispatcher
public class SimpleDispatcher implements Dispatcher {
@Override
public void dispatch(@NonNull Traversal traversal, final @NonNull TraversalCallback callback) {
final LayoutKey newKey = traversal.destination.top();
final LayoutKey previousKey = traversal.origin.top();
if(newKey.equals(previousKey)) { // SHORT CIRCUIT ON SAME KEY
callback.onTraversalCompleted();
return;
}
public class Email extends RealmObject {
public static final String PROPERTY_SUBJECT = "subject";
public static final String PROPERTY_SENDER = "sender";
public static final String PROPERTY_BODY = "body";
public String subject;
public Sender sender;
public Body body;
public Date receivedDateTime;
public class Email extends RealmObject {
private String subject;
private String senderName;
private String bodyContent;
private String bodyContentType;
private Date receivedDateTime;
// getters, setters
}
public interface FeedItemInterface
extends RealmModel {
interface Repository {
FeedItemInterface findOne(Realm realm, long id);
RealmResults<FeedItemInterface> findAllSortedByRank(Realm realm);
FeedItemInterface findOneHighestRank(Realm realm);
FeedItemInterface findOneNewestUpdated(Realm realm);
}
long getId();