Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor changes for #200 to allow ViewBinder.with(LayoutInflater).
- Loading branch information
1 parent
b656581
commit ed2346a
Showing
3 changed files
with
113 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 26 additions & 26 deletions
52
framework/src/main/java/org/robobinding/NonBindingViewInflater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
164
framework/src/main/java/org/robobinding/binder/ViewBinderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |