Skip to content
This repository has been archived by the owner on Nov 20, 2018. It is now read-only.

Commit

Permalink
Push of preview2 release
Browse files Browse the repository at this point in the history
  • Loading branch information
erikandre committed May 5, 2016
1 parent 62a2e95 commit d318980
Show file tree
Hide file tree
Showing 181 changed files with 4,081 additions and 4,253 deletions.
19 changes: 8 additions & 11 deletions Barf/src/main/java/com/badoo/barf/data/repo/BaseRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,30 @@
* Implementation of {@link Repository} which manages in progress queries. For this to work correctly there are two constraints, firstly
* the query must correctly implemented the equals method such that it can be compared to in progress queries. Secondly the Observable
* returned should be able to handle multiple subscription without causing long running operations such as network calls to restart.
*
* @param <Q> the query type.
* @param <R> the result type.
*/
public abstract class BaseRepository<Q extends Query, R> implements Repository<Q, R> {
public abstract class BaseRepository<T> implements Repository<T> {

private static final String TAG = BaseRepository.class.getSimpleName();
private static final boolean DEBUG = true;

private Map<Q, Observable<R>> mInProcessQueries = new ConcurrentHashMap<>();
private Map<Query<?>, Observable<?>> mInProcessQueries = new ConcurrentHashMap<>();

@NonNull
@Override
public Observable<R> query(@NonNull Q query) {
public <Result> Observable<Result> query(@NonNull Query<Result> query) {
Log.d(TAG, "Starting query: " + query);
if (DEBUG) {
assertQueryOverridesEqualsAndHashcode(query.getClass());
}

Observable<R> inProcessQuery = mInProcessQueries.get(query);
Observable<?> inProcessQuery = mInProcessQueries.get(query);
if (inProcessQuery != null) {
Log.d(TAG, "Query: " + query + " already in progress, ignoring");
return inProcessQuery;
//noinspection unchecked
return (Observable<Result>) inProcessQuery;
}

final Observable<R> queryObservable = createObservable(query)
final Observable<Result> queryObservable = createObservable(query)
.doOnTerminate(() -> {
Log.d(TAG, "Query: " + query + " terminated");
mInProcessQueries.remove(query);
Expand All @@ -53,7 +51,7 @@ public Observable<R> query(@NonNull Q query) {
* In the case an query isn't in progress, this method will be called to perform the query.
*/
@NonNull
protected abstract Observable<R> createObservable(@NonNull Q query);
protected abstract <Result> Observable<Result> createObservable(@NonNull Query<Result> query);

@VisibleForTesting
static boolean assertQueryOverridesEqualsAndHashcode(Class<?> clazz) {
Expand All @@ -68,5 +66,4 @@ static boolean assertQueryOverridesEqualsAndHashcode(Class<?> clazz) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
* @see com.badoo.barf.data.repo.annotations.Handles
* @see com.badoo.barf.data.repo.annotations.HandlesUtil
*/
public class DelegatingRepository<Q extends Query, R> extends BaseRepository<Q, R> {
public class DelegatingRepository<T> extends BaseRepository<T> {

public final Map<Class<?>, QueryHandler<?, ?>> mRegisteredHandlers = new HashMap<>();

/**
* Register a given query type against a handler.
*/
public void registerHandler(Class<Q> type, QueryHandler<Q, R> handler) {
public <Result> void registerHandler(Class<Query<Result>> type, QueryHandler<Query<Result>, Result> handler) {
mRegisteredHandlers.put(type, handler);
}

@NonNull
@Override
protected Observable<R> createObservable(@NonNull Q query) {
protected <Result> Observable<Result> createObservable(@NonNull Query<Result> query) {
final QueryHandler handler = mRegisteredHandlers.get(query.getClass());
if (handler == null) {
throw new IllegalArgumentException(String.format("No handler is registered for query %s", query.getClass()));
Expand Down
7 changes: 5 additions & 2 deletions Barf/src/main/java/com/badoo/barf/data/repo/Query.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.badoo.barf.data.repo;

/**
* A marker interface for repository queries
* A marker interface for repository queries. Any class implementing this must also provide implementations of equals() and hashCode()
* otherwise you might risk having queries executed multiple times (concurrently) since we cannot deduplicate them.
* <p>
* <R> the expected return type for this query
*/
public interface Query {
public interface Query<Result> {
}
60 changes: 0 additions & 60 deletions Barf/src/main/java/com/badoo/barf/data/repo/Repositories.java

This file was deleted.

4 changes: 2 additions & 2 deletions Barf/src/main/java/com/badoo/barf/data/repo/Repository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* A repository is a data store that can be used to both request and publish data. The repository may or may not be able to retrieve data
* from local and/or remote sources, but if so, it should follow the mode specified by the {@link Query#getMode()}.
*/
public interface Repository<Q extends Query, R> {
public interface Repository<DataType> {

/**
* Perform a query on the repository.
Expand All @@ -17,5 +17,5 @@ public interface Repository<Q extends Query, R> {
* {@link Query#getMode()}.
*/
@NonNull
Observable<R> query(@NonNull Q query);
<Result> Observable<Result> query(@NonNull Query<Result> query);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,27 @@ public class HandlesUtil {
* Register a data source annotated with the {@link Handles} annotation with an instance of {@link DelegatingRepository}. All the methods
* on the data source which are annotated must have a return type matching the return type of the repository or <code>void</code>
*/
public static <Q extends Query, R> void registerHandlersFromAnnotations(DelegatingRepository<Q, R> repo, Object annotatedDataSource) {
public static void registerHandlersFromAnnotations(DelegatingRepository repo, Object annotatedDataSource) {
for (Class<?> clazz : annotatedDataSource.getClass().getInterfaces()) {
processClass(repo, clazz, annotatedDataSource);
}
}

private static <Q extends Query, R> void processClass(DelegatingRepository<Q, R> repo, Class<?> clazz, Object annotatedDataSource) {
private static void processClass(DelegatingRepository<?> repo, Class<?> clazz, Object annotatedDataSource) {
for (final Method method : clazz.getDeclaredMethods()) {
final Handles handles = method.getAnnotation(Handles.class);
if (handles == null) {
continue;
}

//noinspection unchecked
final Class<Q> queryType = (Class<Q>) handles.value();
final DelegatingRepository.QueryHandler<Q, R> handler;
if (method.getReturnType() == Void.TYPE) {
handler = createHandlerWithEmptyReturn(annotatedDataSource, method);
}
else {
handler = createHandlerWithReturn(annotatedDataSource, method);
}
final Class queryType = (Class) handles.value();
final DelegatingRepository.QueryHandler handler = createHandlerWithReturn(annotatedDataSource, method);
//noinspection unchecked
repo.registerHandler(queryType, handler);
}
}

private static <Q extends Query, R> DelegatingRepository.QueryHandler<Q, R> createHandlerWithEmptyReturn(Object target, Method handlingMethod) {
return query -> {
try {
handlingMethod.invoke(target, query);
return Observable.empty();
}
catch (Exception e) {
throw new RuntimeException(e);
}
};
}

private static <Q extends Query, R> DelegatingRepository.QueryHandler<Q, R> createHandlerWithReturn(Object target, Method handlingMethod) {
return query -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.badoo.barf.mvp;

import android.support.annotation.NonNull;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
* A base class for presenters to provider some utility functionality. When using this presenter it's important that the lifecycle methods
* are called at the relevant times.
* A base class for presenters to provider some utility functionality to manage rx subscriptions. When using this presenter it's important
* that the lifecycle methods are called at the relevant times.
*/
public abstract class BasePresenter<V extends View, F extends Presenter.FlowListener> implements Presenter<V, F> {
public abstract class BaseRxPresenter implements MvpPresenter {

private final CompositeSubscription mTrackedSubscriptions = new CompositeSubscription();
private V mView;
private F mFlowListener;

/**
* Add a subscription to be tracked such that when this presenter is destroyed the subscription will be unsubscribed from.
Expand All @@ -23,30 +19,6 @@ public Subscription trackSubscription(Subscription subscription) {
return subscription;
}

@Override
public void attachView(@NonNull V view) {
mView = view;
}

@Override
public void attachFlowListener(@NonNull F flowListener) {
mFlowListener = flowListener;
}

/**
* Get the flow listener if one is attached.
*/
protected F getFlowListener() {
return mFlowListener;
}

/**
* Get the attached view.
*/
protected V getView() {
return mView;
}

@Override
public void onCreate() {
}
Expand Down
31 changes: 0 additions & 31 deletions Barf/src/main/java/com/badoo/barf/mvp/BaseView.java

This file was deleted.

9 changes: 9 additions & 0 deletions Barf/src/main/java/com/badoo/barf/mvp/FlowListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.badoo.barf.mvp;

/**
* An marker interface that is used in addition to the View for allowing the presenter to control navigation related functionality of the app.
* This was introduced to solve issues that arose from separating the View implementation from the containing Fragment/Activity as well
* as from the decision to split functionality into several View/Presenter pairs for each screen (based on areas or responsibility).
*/
public interface FlowListener {
}
32 changes: 32 additions & 0 deletions Barf/src/main/java/com/badoo/barf/mvp/MvpPresenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.badoo.barf.mvp;

import android.app.Activity;
import android.app.Fragment;

/**
* Interface definition of a MVP Presenter.
*/
public interface MvpPresenter {

/**
* Lifecycle callback that is invoked when the presenter is created
*/
void onCreate();

/**
* Lifecycle callback that is invoked from onStart of the Fragment/Activity managing the presenter
*/
void onStart();

/**
* Lifecycle callback that is invoked from onStop of the Fragment/Activity managing the presenter
*/
void onStop();

/**
* Should be called by either {@link Activity#onDestroy()}, {@link Fragment#onDestroy()} or {@link Fragment#onDestroyView()}
*/
void destroy();


}
7 changes: 7 additions & 0 deletions Barf/src/main/java/com/badoo/barf/mvp/MvpView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.badoo.barf.mvp;

/**
* Interface definition of a MVP View
*/
public interface MvpView {
}
Loading

0 comments on commit d318980

Please sign in to comment.