Skip to content

Initial checkin of the ViewPump lib #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Changelog

#0.1.0
- Initial Release
16 changes: 16 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
GROUP=io.github.inflationx
VERSION_NAME=0.1.0
VERSION_CODE=1

POM_PACKAGING=aar
POM_URL=https://github.com/InflationX/ViewPump
POM_DESCRIPTION=View inflation with an pre/post-inflation interceptors
POM_SCM_URL=https://github.com/InflationX/ViewPump
POM_SCM_CONNECTION=scm:[email protected]:InflationX/ViewPump.git
POM_SCM_DEV_CONNECTION=scm:[email protected]:InflationX/ViewPump.git
POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=InflationX
POM_DEVELOPER_NAME=InflationX
POM_DEVELOPER_EMAIL=
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':viewpump'
31 changes: 31 additions & 0 deletions viewpump/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"

defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode project.ext.versionCodeInt
versionName version
consumerProguardFiles 'consumer-proguard-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'consumer-proguard-rules.pro'
}
}
}

dependencies {
provided 'com.android.support:appcompat-v7:24.0.0'

testCompile 'org.assertj:assertj-core:1.7.1'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.0.42-beta'
}

apply from: rootProject.file('gradle/deploy.gradle')
2 changes: 2 additions & 0 deletions viewpump/consumer-proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-keep class io.github.inflationx.viewpump.* { *; }
-keep class io.github.inflationx.viewpump.*$* { *; }
9 changes: 9 additions & 0 deletions viewpump/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SubProject Library Gradle Properties
# See parent properties for global properties

GROUP=io.github.inflationx
POM_NAME=ViewPump
POM_ARTIFACT_ID=viewpump
POM_PACKAGING=aar

VERSION_NAME=0.1.0
4 changes: 4 additions & 0 deletions viewpump/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.inflationx.viewpump">
<application />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.inflationx.viewpump;

import android.view.View;

class FallbackViewCreationInterceptor implements Interceptor {

@Override
public InflateResult intercept(Chain chain) {
InflateRequest request = chain.request();
FallbackViewCreator viewCreator = request.fallbackViewCreator();
View fallbackView = viewCreator.onCreateView(request.parent(), request.name(), request.context(), request.attrs());

return InflateResult.builder()
.view(fallbackView)
.name(fallbackView != null ? fallbackView.getClass().getName() : request.name())
.context(request.context())
.attrs(request.attrs())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.inflationx.viewpump;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public interface FallbackViewCreator {
@Nullable
View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @Nullable AttributeSet attrs);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package io.github.inflationx.viewpump;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class InflateRequest {
private final String name;
private final Context context;
private final AttributeSet attrs;
private final View parent;
private final FallbackViewCreator fallbackViewCreator;

private InflateRequest(Builder builder) {
name = builder.name;
context = builder.context;
attrs = builder.attrs;
parent = builder.parent;
fallbackViewCreator = builder.fallbackViewCreator;
}

@NonNull
public String name() {
return name;
}

@NonNull
public Context context() {
return context;
}

@Nullable
public AttributeSet attrs() {
return attrs;
}

@Nullable
public View parent() {
return parent;
}

@NonNull
public FallbackViewCreator fallbackViewCreator() {
return fallbackViewCreator;
}

@NonNull
public static Builder builder() {
return new Builder();
}

@NonNull
public Builder toBuilder() {
return new Builder(this);
}

@NonNull
@Override
public String toString() {
return "InflateRequest{" +
"name='" + name + '\'' +
", context=" + context +
", attrs=" + attrs +
", parent=" + parent +
", fallbackViewCreator=" + fallbackViewCreator +
'}';
}

public static final class Builder {
private String name;
private Context context;
private AttributeSet attrs;
private View parent;
private FallbackViewCreator fallbackViewCreator;

private Builder() { }

private Builder(InflateRequest request) {
this.name = request.name;
this.context = request.context;
this.attrs = request.attrs;
this.parent = request.parent;
this.fallbackViewCreator = request.fallbackViewCreator;
}

public Builder name(@NonNull String name) {
this.name = name;
return this;
}

public Builder context(@NonNull Context context) {
this.context = context;
return this;
}

public Builder attrs(@Nullable AttributeSet attrs) {
this.attrs = attrs;
return this;
}

public Builder parent(@Nullable View parent) {
this.parent = parent;
return this;
}

public Builder fallbackViewCreator(@NonNull FallbackViewCreator fallbackViewCreator) {
this.fallbackViewCreator = fallbackViewCreator;
return this;
}

public InflateRequest build() {
if (name == null) {
throw new IllegalStateException("name == null");
}
if (context == null) {
throw new IllegalStateException("context == null");
}
if (fallbackViewCreator == null) {
throw new IllegalStateException("fallbackViewCreator == null");
}
return new InflateRequest(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package io.github.inflationx.viewpump;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class InflateResult {
private final View view;
private final String name;
private final Context context;
private final AttributeSet attrs;

private InflateResult(Builder builder) {
view = builder.view;
name = builder.name;
context = builder.context;
attrs = builder.attrs;
}

@Nullable
public View view() {
return view;
}

@NonNull
public String name() {
return name;
}

@NonNull
public Context context() {
return context;
}

@Nullable
public AttributeSet attrs() {
return attrs;
}

@NonNull
public static Builder builder() {
return new Builder();
}

@NonNull
public Builder toBuilder() {
return new Builder(this);
}

@NonNull
@Override
public String toString() {
return "InflateResult{" +
"view=" + view +
", name=" + name +
", context=" + context +
", attrs=" + attrs +
'}';
}

public static final class Builder {
private View view;
private String name;
private Context context;
private AttributeSet attrs;

private Builder() { }

private Builder(InflateResult result) {
this.view = result.view;
this.name = result.name;
this.context = result.context;
this.attrs = result.attrs;
}

public Builder view(@Nullable View view) {
this.view = view;
return this;
}

public Builder name(@NonNull String name) {
this.name = name;
return this;
}

public Builder context(@NonNull Context context) {
this.context = context;
return this;
}

public Builder attrs(@Nullable AttributeSet attrs) {
this.attrs = attrs;
return this;
}

public InflateResult build() {
if (name == null) {
throw new IllegalStateException("name == null");
}
if (context == null) {
throw new IllegalStateException("context == null");
}
if (view != null && !name.equals(view.getClass().getName())) {
throw new IllegalStateException("name (" + name + ") "
+ "must be the view's fully qualified name (" + view.getClass().getName() + ")");
}
return new InflateResult(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.inflationx.viewpump;

/**
* Observes, modifies, and potentially short-circuits inflation requests going out and the
* corresponding views that are inflated or returned. Typically interceptors change the name
* of the view to be inflated, return a programmatically instantiated view, or perform actions
* on a view after it is inflated based on its Context or AttributeSet.
*/
public interface Interceptor {
InflateResult intercept(Chain chain);

interface Chain {
InflateRequest request();

InflateResult proceed(InflateRequest request);
}
}
Loading