Skip to content

Commit

Permalink
Minor changes for #200 to allow ViewBinder.with(LayoutInflater).
Browse files Browse the repository at this point in the history
  • Loading branch information
weicheng113 committed Jul 24, 2015
1 parent b656581 commit ed2346a
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 109 deletions.
6 changes: 3 additions & 3 deletions framework/build.gradle
Expand Up @@ -244,16 +244,16 @@ uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

/*
repository(url: 'https://oss.sonatype.org/service/local/staging/deploy/maven2/') {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
snapshotRepository(url: 'https://oss.sonatype.org/content/repositories/snapshots/') {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
}*/

//repository(url: "file://$projectDir/mavenRepo")
repository(url: "file://$projectDir/mavenRepo")
pom.project {
name 'RoboBinding'
description 'A Presentation Model(MVVM) framework for the Android platform'
Expand Down
52 changes: 26 additions & 26 deletions framework/src/main/java/org/robobinding/NonBindingViewInflater.java
@@ -1,27 +1,27 @@
package org.robobinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
*
* @since 1.0
* @version $Revision: 1.0 $
* @author Cheng Wei
*/
public class NonBindingViewInflater {
private final LayoutInflater layoutInflater;

public NonBindingViewInflater(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}

public View inflateWithoutRoot(int layoutId) {
return layoutInflater.inflate(layoutId, null);
}

public View inflate(int layoutId, ViewGroup root, boolean attachToRoot) {
return layoutInflater.inflate(layoutId, root, attachToRoot);
}
package org.robobinding;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
*
* @since 1.0
* @version $Revision: 1.0 $
* @author Cheng Wei
*/
public class NonBindingViewInflater {
private final LayoutInflater layoutInflater;

public NonBindingViewInflater(LayoutInflater layoutInflater) {
this.layoutInflater = layoutInflater;
}

public View inflateWithoutRoot(int layoutId) {
return layoutInflater.inflate(layoutId, null);
}

public View inflate(int layoutId, ViewGroup root, boolean attachToRoot) {
return layoutInflater.inflate(layoutId, root, attachToRoot);
}
}
164 changes: 84 additions & 80 deletions framework/src/main/java/org/robobinding/binder/ViewBinderImpl.java
@@ -1,80 +1,84 @@
package org.robobinding.binder;

import static org.robobinding.util.Preconditions.checkValidResourceId;

import org.robobinding.ViewBinder;
import org.robobinding.presentationmodel.AbstractPresentationModelObject;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.common.base.Preconditions;

/**
*
* @since 1.0
* @version $Revision: 1.0 $
* @author Cheng Wei
*/
public class ViewBinderImpl implements ViewBinder {
private final BindingViewInflater bindingViewInflater;
private final ViewBindingLifecycle viewBindingLifecycle;
private final PresentationModelObjectLoader presentationModelObjectLoader;

public ViewBinderImpl(BindingViewInflater bindingViewInflater, ViewBindingLifecycle viewBindingLifecycle,
PresentationModelObjectLoader presentationModelObjectLoader) {
this.bindingViewInflater = bindingViewInflater;
this.viewBindingLifecycle = viewBindingLifecycle;
this.presentationModelObjectLoader = presentationModelObjectLoader;
}

@Override
public View inflateAndBind(int layoutId, Object presentationModel) {
checkLayoutId(layoutId);
checkPresentationModel(presentationModel);
AbstractPresentationModelObject presentationModelObject = presentationModelObjectLoader.load(presentationModel);

InflatedViewWithRoot inflatedView = bindingViewInflater.inflateView(layoutId);
viewBindingLifecycle.run(inflatedView, presentationModelObject);
return inflatedView.getRootView();
}

private void checkLayoutId(int layoutId) {
checkValidResourceId(layoutId, "invalid layoutId '" + layoutId + "'");
}

private void checkPresentationModel(Object presentationModel) {
Preconditions.checkNotNull(presentationModel, "presentationModel must not be null");
}

@Override
public View inflateAndBind(int layoutId, Object presentationModel, ViewGroup root) {
return inflateAndBind(layoutId, presentationModel, root, true);
}

private View inflateAndBind(int layoutId, Object presentationModel, ViewGroup root, boolean attachToRoot) {
checkLayoutId(layoutId);
checkPresentationModel(presentationModel);
checkRoot(root);
AbstractPresentationModelObject presentationModelObject = presentationModelObjectLoader.load(presentationModel);

InflatedViewWithRoot inflatedView = bindingViewInflater.inflateView(layoutId, root, attachToRoot);
viewBindingLifecycle.run(inflatedView, presentationModelObject);
return inflatedView.getRootView();
}

private void checkRoot(ViewGroup root) {
Preconditions.checkNotNull(root, "Root must not be null");
}

@Override
public View inflateAndBindWithoutAttachingToRoot(int layoutId, Object presentationModel, ViewGroup root) {
return inflateAndBind(layoutId, presentationModel, root, false);
}

public ViewBinder with(LayoutInflater layoutInflater) {
return new ViewBinderImpl(bindingViewInflater.with(layoutInflater),
viewBindingLifecycle, presentationModelObjectLoader);
}
}
package org.robobinding.binder;

import static org.robobinding.util.Preconditions.checkValidResourceId;

import org.robobinding.ViewBinder;
import org.robobinding.ViewCreationListenerInstaller;
import org.robobinding.presentationmodel.AbstractPresentationModelObject;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.common.base.Preconditions;

/**
*
* @since 1.0
* @version $Revision: 1.0 $
* @author Cheng Wei
*/
public class ViewBinderImpl implements ViewBinder {
private final BindingViewInflater bindingViewInflater;
private final ViewBindingLifecycle viewBindingLifecycle;
private final PresentationModelObjectLoader presentationModelObjectLoader;

public ViewBinderImpl(BindingViewInflater bindingViewInflater, ViewBindingLifecycle viewBindingLifecycle,
PresentationModelObjectLoader presentationModelObjectLoader) {
this.bindingViewInflater = bindingViewInflater;
this.viewBindingLifecycle = viewBindingLifecycle;
this.presentationModelObjectLoader = presentationModelObjectLoader;
}

@Override
public View inflateAndBind(int layoutId, Object presentationModel) {
checkLayoutId(layoutId);
checkPresentationModel(presentationModel);
AbstractPresentationModelObject presentationModelObject = presentationModelObjectLoader.load(presentationModel);

InflatedViewWithRoot inflatedView = bindingViewInflater.inflateView(layoutId);
viewBindingLifecycle.run(inflatedView, presentationModelObject);
return inflatedView.getRootView();
}

private void checkLayoutId(int layoutId) {
checkValidResourceId(layoutId, "invalid layoutId '" + layoutId + "'");
}

private void checkPresentationModel(Object presentationModel) {
Preconditions.checkNotNull(presentationModel, "presentationModel must not be null");
}

@Override
public View inflateAndBind(int layoutId, Object presentationModel, ViewGroup root) {
return inflateAndBind(layoutId, presentationModel, root, true);
}

private View inflateAndBind(int layoutId, Object presentationModel, ViewGroup root, boolean attachToRoot) {
checkLayoutId(layoutId);
checkPresentationModel(presentationModel);
checkRoot(root);
AbstractPresentationModelObject presentationModelObject = presentationModelObjectLoader.load(presentationModel);

InflatedViewWithRoot inflatedView = bindingViewInflater.inflateView(layoutId, root, attachToRoot);
viewBindingLifecycle.run(inflatedView, presentationModelObject);
return inflatedView.getRootView();
}

private void checkRoot(ViewGroup root) {
Preconditions.checkNotNull(root, "Root must not be null");
}

@Override
public View inflateAndBindWithoutAttachingToRoot(int layoutId, Object presentationModel, ViewGroup root) {
return inflateAndBind(layoutId, presentationModel, root, false);
}

public ViewBinder with(LayoutInflater layoutInflater) {
LayoutInflater newLayoutInflater = layoutInflater.cloneInContext(layoutInflater.getContext());
BindingViewInflater newBindingViewInflater = bindingViewInflater.with(newLayoutInflater);
new ViewCreationListenerInstaller(newLayoutInflater).install(newBindingViewInflater);

return new ViewBinderImpl(newBindingViewInflater, viewBindingLifecycle, presentationModelObjectLoader);
}
}

0 comments on commit ed2346a

Please sign in to comment.