Usage of Blade in Android
- 2021-08-17 00:54:11
- OfStack
Start Activity and pass parameters
Extra
Code that normally starts Activity and passes parameters:
Intent intent = new Intent(context,LoginActivity.class);
intent.putExtra("phone","123456);
intent.putExtra("pwd","123456);
startActivity(intent);
How to start Activity with Blade
public class LoginActivity extends AppcompatActivity{
@Extra
String mText;
@Extra
MyData mData;
}
With the above code, one of the following two methods will be automatically generated
Intent forX(Context c, T1 extra1[, T2 extra2, ...])
void startX(Context c, T1 extra1[, T2 extra2, ...])
Then we can start Activity directly through I. startLoginActivity.
Create an Fragment instance
@Arg
Method used to generate newInstance for Fragment
Usually we create Fragment objects with the following boilerplate code
public class MyFragment extends Fragment{
public MyFragment newInstance(String data){
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putExtra("data",data);
fragment.setArguments(bundle);
return fragment;
}
...
}
Ways to use Blade
public class MyFragment extends Fragment {
@Arg
String mText;
@Arg
MyData mData;
}
Custom serialization
public class MyFragment extends Fragment {
@Arg(MyTypeBundler.class)
MyType mMyType;
}
public class MyTypeBundler implements Bundler<MyType> {
void save(@Nullable final MyType value, @NonNull final Bundle state) {
// save given value to the state
}
@Nullable
MyType restore(@NonNull final Bundle state) {
// restore and return value from state
}
}
@Parcel
When we create an entity class and need to implement Parcelable, we can write it as follows
@blade.Parcel
public class MyClass implements Parcelable {
String text;
int number;
boolean flag;
double[] doubleArray;
protected MyClass(Parcel in) {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
}
If a field wants to be ignored and does not need to be serialized, use @ blade. ParcelIgnore
Mvp
Mvp is used in conjunction with Dager.
Step 1: Add dager dependencies to your build. gradle
compile 'com.google.dagger:dagger:2.x'
apt 'com.google.dagger:dagger-compiler:2.x'
Step 2: Create an interface that inherits from IView
public interface IMyView extends blade.mvp.IView {
void show(String something);
}
Step 3: Create Prensenter and View interfaces to interact with each other
public class MyPresenter extends blade.mvp.BasePresenter<IMyView> {
public void onUserDidSomething() {
String s = // do something ...
if (getView() != null) {
getView().show(s);
}
}
//...
}
Step 4: Create an implementation of View, inject Presenter with @ Inject, now support Fragmnt, Activit, View
public class LoginActivity extends AppcompatActivity{
@Extra
String mText;
@Extra
MyData mData;
}
0
Step 5: Activity contains Fragment and View with Presenter, so the Activity needs to be annotated with @ Blade once to let Blade know.
State
To simplify state management, the @ State annotation generates a helper class with two static methods:
public class LoginActivity extends AppcompatActivity{
@Extra
String mText;
@Extra
MyData mData;
}
1
Classes that inherit from Fragment, Activity, or View automatically manage state. The functionality of custom serialization is shown above.
Blade allows us to write a lot less boilerplate code, which I haven't applied to the project yet, and will be used in the project later to make the project look clearer.
Blade Address: https://github.com/FrantisekGazo/Blade
Summarize