bytewelder / spork Goto Github PK
View Code? Open in Web Editor NEWAnnotation processing and dependency injection for Java/Android
Home Page: http://spork.bytewelder.com
License: Apache License 2.0
Annotation processing and dependency injection for Java/Android
Home Page: http://spork.bytewelder.com
License: Apache License 2.0
Must re-inject Views when they have changed for all @BindView targets.
Temporary work-around: call Spork.bind() again when necessary.
It would be nice to use the Java EE annotations:
@Inject
instead of @BindComponent
@Singleton
instead of @ComponentScope
https://mvnrepository.com/artifact/javax.inject/javax.inject
BindClick failed for AppCompatButton at method 'startMultiplayerWithRandomPlayer': onClick failed because the method arguments must be either empty or accept a a single View type
Fix a a
part of the text.
Inject different system services.
For example:
@BindSystemService
PowerManager mPowerManager;
BindFailedBuilder
with new class ErrorMessageBuilder
Hi! I'm having a problem trying to bind a RecyclerAdapter
public class OrdersRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
switch (viewType) {
case HEADER:
v = inflater.inflate(R.layout.order_header_item_row, parent, false);
return new HeaderHolder(v);
case ORDER:
v = inflater.inflate(R.layout.orders_adapter_item, parent, false);
return new OrderHolder(parent.getContext(), v);
case TASK:
v = inflater.inflate(R.layout.tasks_orders, parent, false);
return new TasksHolder(parent.getContext(), v);
case IMAGE:
v = inflater.inflate(R.layout.progress_image_item_row, parent, false);
return new ImageHolder(v);
default:
v = inflater.inflate(R.layout.order_header_item_row, parent, false);
return new HeaderHolder(v);
}
}
public class HeaderHolder extends RecyclerView.ViewHolder {
@BindView(R.id.order_pending_or_accepted)
TextView orderPendingOrAccepted;
public HeaderHolder(View itemView) {
super(itemView);
Spork.bind(this); <------- HERE IS THE ERROR
}
public void bind(OrderHomeWrapper object) {
String title = (String) object.getObject();
orderPendingOrAccepted.setText(title);
}
}
public class ImageHolder extends RecyclerView.ViewHolder {
@BindView(R.id.order_pending_or_accepted)
TextView orderPendingOrAccepted;
public ImageHolder(View itemView) {
super(itemView);
Spork.bind(this);
}
public void bind(OrderHomeWrapper object) {
String title = (String) object.getObject();
orderPendingOrAccepted.setText(title);
}
}
}
and so on with the other view holders
im getting the error: io.github.sporklibrary.exceptions.BindException: BindView failed for HeaderHolder: incompatible class to find views
Is there a way to have multiple view holders for the same recycler adapter?
Thanks!
Dependency injection for an an empty or default constructor shouldn't require a module to do so since it's a simple injection. it could be use full in a DAO Service architecture when a service is only a collection of DAO required by dependency injection but a services is just an ordinary Object with no special treatment required, thus a services would be injected in presenter or view model directly by ObjectGraph
Support for Fragment
argument binding.
For example:
@BindFragmentArgument
String something;
@ BindFragmentArgument("something")
String mSomething;
Hi! Is recommended use spork injection in android app (not spork android, spork injection product) or it decreases too much android performance???
When BindClick
fails because of an unresolved id
, the error says BindView failed for [...]
.
This is because BindView.class
is hardcoded into ViewResolver.getView()
.
The exception should provide the correct error message.
Suggestion: make ViewResolver.getView()
throw a custom error (maybe NotFoundException
or ViewNotFoundException
)
For types:
More types could be added, but these seem to be the most important ones.
Create a test that combines layout and view binding, proguard and more.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import io.github.sporklibrary.Spork;
import io.github.sporklibrary.annotations.BindLayout;
import io.github.sporklibrary.annotations.BindView;
@BindLayout(R.layout.activity_test)
public class TestActivity extends AppCompatActivity {
@BindView(R.id.textTest)
TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spork.bind(this);
txtView.setText("Hello Test");
}
}
Below is XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/main_bg"
android:orientation="vertical"
tools:context="com.devdigital.glowmotion.view.activities.FillOutBasicDataActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<include
android:id="@+id/scanner_toolbar"
layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<TextView
android:id="@+id/textTest"
android:layout_width="@dimen/DP200"
android:layout_height="@dimen/DP200"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/DP5"
/>
</LinearLayout>
FATAL EXCEPTION: main
Process: com.devdigital.glowmotion.debug, PID: 19629
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.devdigital.glowmotion.debug/com.devdigital.glowmotion.TestActivity}: io.github.sporklibrary.exceptions.BindException: BindView failed for TestActivity: View not found
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: io.github.sporklibrary.exceptions.BindException: BindView failed for TestActivity: View not found
This suggestion was made by Punksmurf. I'm merely writing the gist of our chat down in this ticket.
Create separate "Spork Android Support" library for all appcompat/support-v4 functionality.
This will get rid of the reflection in the code.
In effect, we should start with making Views.getRootView() more abstract. For example with a ViewProvider
/RootViewProvider
:
public interface RootViewProvider {
boolean canProvideFor(Object object);
@Nullable View getRootView(Object object);
}
There can be a DefaultRootViewResolver
and a SupportRootViewResolver
. The same idea goes for ContextResolver
.
These resolvers will be managed in a list where more resolves can be added to.
Hi! first of all it CONGRATS on developing this great library! I'm porting my project to use this library ASAP. One thing though, I'm getting errors when trying to use it on my RecyclerView adapters or ViewHolders.
By now I've tried:
On Adapter, onCreateViewHolder method:
View view = inflater.inflate(R.layout.news_item_row, parent, false);
Spork.bind(this); //didn't work
Spork.bind(view); //didn't work
return new NewsRecyclerViewHolder(context, view);
Also using Spork.bind(this) and Spork.bind(view) in the constructors of the ViewHolder itself but unfortunately nothing works.
public NewsRecyclerViewHolder(Context context, View itemView) {
super(itemView);
Spork.bind(this); //didn't work
Spork.bind(itemView); //didn't work either
}
How can we use Spork on the adapters for RecyclerViews?
Is there a workaround?
thanks!
PS: maybe you could upload an example project to see actual code. For example for using a Fragment I didn't knew that I had to call bind inside the fragment's OnViewCreated method. I figured it out searching on the tests folders.
super.onViewCreated(view, savedInstanceState);
Spork.bind(this);
Support for extra Intent
extras binding for Activity
.
For example:
@BindExtra
String something;
@BindExtra("something")
String mSomething;
I'm trying to use Spork on PagerAdapter (to display views on a ViewPager) but I'm having problems doing so. For now, I'm creating a custom view, use Spork, and then adding it to the container.
@BindLayout(R.layout.related_item_row)
public class RelatedView extends FrameLayout {
@BindView(R.id.textView_title)
TextView textViewTitle;
@BindView(R.id.imageView_play)
View imageViewPlay;
public RelatedView(Context context) {
super(context);
Spork.bind(this);
}
public void init(final RelatedFeed feed) {
textViewTitle.setText(feed.getTitle());
imageViewPlay.setVisibility(!feed.getDataType().equals("news") ? View.VISIBLE : View.GONE);
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
So in the PagerAdapter I just have this:
public class RelatedPagerAdapter extends PagerAdapter {
private Activity context;
private ArrayList<RelatedFeed> items;
public RelatedPagerAdapter(Activity context, ArrayList<RelatedFeed> items) {
Spork.bind(this);
this.context = context;
this.items = items;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final RelatedFeed feed = items.get(position);
RelatedView view = new RelatedView(context);
view.init(feed);
container.addView(view);
return view;
}
@Override
public int getCount() {
return items.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
}
It's working great for now. It would be great if Spork works the same way than in other views.
Another thing, how would be the approach if we didn't use PagerAdapter but instead a FragmentPagerAdapter (or FragmentStatePagerAdapter)?
For example:
@BindFragment(tag = "something")
Fragment mFragment;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.