Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #78 from lyft/view-binder-factory
Browse files Browse the repository at this point in the history
add a view binder factory
  • Loading branch information
eveliotc committed Jul 28, 2016
2 parents d75df51 + 83f3922 commit bd57040
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
11 changes: 10 additions & 1 deletion scoop-basics/src/main/java/com/example/scoop/basics/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.scoop.basics.scoop.ButterKnifeViewBinder;
import com.facebook.stetho.Stetho;
import com.lyft.scoop.Scoop;
import com.lyft.scoop.ViewBinder;
import com.lyft.scoop.ViewBinderFactory;
import dagger.ObjectGraph;
import timber.log.Timber;

Expand All @@ -20,12 +22,19 @@ public void onCreate() {

Timber.d("onCreate");

Scoop.setViewBinder(new ButterKnifeViewBinder());
Scoop.setViewBinderFactory(VIEW_BINDER_FACTORY);

applicationGraph = ObjectGraph.create(new AppModule(this));
}

public ObjectGraph getApplicationGraph() {
return applicationGraph;
}

private static final ViewBinderFactory VIEW_BINDER_FACTORY = new ViewBinderFactory() {
@Override
public ViewBinder create(Object object) {
return new ButterKnifeViewBinder();
}
};
}
9 changes: 9 additions & 0 deletions scoop/src/main/java/com/lyft/scoop/NoOpViewBinderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lyft.scoop;

class NoOpViewBinderFactory implements ViewBinderFactory {

@Override
public ViewBinder create(Object object) {
return new NoOpViewBinder();
}
}
9 changes: 4 additions & 5 deletions scoop/src/main/java/com/lyft/scoop/Scoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public final class Scoop {

static ViewBinder viewBinder = new NoOpViewBinder();
static ViewBinderFactory viewBinderFactory = new NoOpViewBinderFactory();

private String name;
private Scoop parent;
private Map<String, Object> services;
private HashMap<String, Scoop> children = new LinkedHashMap<>();
private Map<String, Scoop> children = new LinkedHashMap<>();
private boolean destroyed;

private Scoop(String name, Scoop parent, Map<String, Object> services) {
Expand Down Expand Up @@ -108,8 +107,8 @@ public Scoop build() {
}
}

public static void setViewBinder(ViewBinder binder) {
viewBinder = binder;
public static void setViewBinderFactory(ViewBinderFactory factory) {
viewBinderFactory = factory;
}

public static Scoop fromView(View view) {
Expand Down
6 changes: 6 additions & 0 deletions scoop/src/main/java/com/lyft/scoop/ViewBinderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.lyft.scoop;

public interface ViewBinderFactory {

ViewBinder create(Object object);
}
9 changes: 6 additions & 3 deletions scoop/src/main/java/com/lyft/scoop/ViewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public abstract class ViewController {
private boolean attached;
private Scoop scoop;
private View view;
private ViewBinder viewBinder;

final void attach(View view) {
this.view = view;
Scoop.viewBinder.bind(this, view);
this.viewBinder = scoop.viewBinderFactory.create(this);
this.viewBinder.bind(this, view);
view.setTag(VIEW_CONTROLLER_TAG, this);
onAttach();
this.attached = true;
Expand All @@ -30,10 +32,11 @@ protected final boolean attached() {

final void detach(View view) {
this.isDetaching = true;
if(this.attached) {
if (this.attached) {
onDetach();
view.setTag(VIEW_CONTROLLER_TAG, null);
Scoop.viewBinder.unbind(this);
this.viewBinder.unbind(this);
this.viewBinder = null;
this.view = null;
this.attached = false;
this.isDetaching = false;
Expand Down

0 comments on commit bd57040

Please sign in to comment.