František Gažo
to the Rescue...
MVP knižnica s Dependency Injection pre Android
Model
View
Presenter
Dependency Injection
Application Scope
Screen Scope
Activity Scope
Annotation Processing
ByteCode Weaving
buildscript {
dependencies {
repositories {
mavenCentral()
}
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
classpath 'eu.inloop.knight:knight-plugin:0.0.4'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'knight'
@KnightApp
public class App extends Application {
}
@KnightApp
public class App extends Application {
@Inject SomeUtil mUtil;
@Override
protected void onCreate() {
super.onCreate();
// Dostupne Injecty
}
}
@KnightActivity
public class MyActivity extends Activity {
@Inject SomeUtil mUtil;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Dostupne Injecty
}
}
@KnightActivity
public class MyActivity extends Activity {
}
public class CustomView extends View {
@Inject SomeUtil mUtil;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// Dostupne Injecty
}
}
public class MyFragment extends Fragment {
@Inject SomeUtil mUtil;
@Override
protected void onAttach() {
super.onAttach();
// Dostupne Injecty
}
}
@KnightActivity({
MyFragment.class,
CustomView.class
})
public class MyActivity extends Activity {
//...
}
public class SomeService extends IntentService {
@Inject SomeUtil mUtil;
public SomeService() {
super("SomeName");
// Dostupne Injecty
}
// ...
}
@KnightApp(
SomeService.class
)
public class App extends Application {
//...
}
public class SomeUtil {
@AppProvided
@Singleton
public SomeUtil(Application app) {
//...
}
// ...
}
public class UiUtil {
@ActivityProvided({
ExampleActivity.class
})
@Singleton
public UiUtil(Activity activity) {
//...
}
// ...
}
@Module
public class MyModule {
@Provides
public SomeObject providesObject(/* args */) {
return new SomeObject(/* args */);
}
}
@AppProvided
@Module
public class MyModule {
@Provides
public SomeObject providesObject(/* args */) {
return new SomeObject(/* args */);
}
}
@AppProvided
@Module
public class MyModule {
@Provides
@AppScope
public SomeObject providesObject(/* args */) {
return new SomeObject(/* args */);
}
}
public class ContactPresenter extends BasePresenter<IContactView> {
@ScreenProvided(ContactActivity.class)
@Singleton
public ContactPresenter(...) {...}
@Override
public void onCreate(@Nullable Bundle savedState) {...}
@Override
public void onSave(@NonNull Bundle outState) {...}
@Override
public void onRemove() {...}
@Override
public void onBindView(@NonNull V view) {...}
@Override
public void onReleaseView() {...}
}
public interface IContactView
extends IView {
void show(Contact c);
void show(String msg);
}
public class ContactView extends View implements IContactView {
@Inject ContactPresenter mPresenter;
public ContactView(Context c, ...) {
super(c, ...);
}
@Override
public void show(String msg) {...}
@Override
public void show(Contact c) {...}
}
public class ContactView extends View implements IContactView {
@Inject ContactPresenter mPresenter;
public ContactView(Context c, ...) {
super(c, ...);
// nastav view
mPresenter.bindView(this);
}
@Override
public void show(String msg) {...}
@Override
public void show(Contact c) {...}
}
public class ContactView extends View implements IContactView {
@Inject ContactPresenter mPresenter;
public ContactView(Context c, ...) {
super(c, ...);
// nastav view
mPresenter.bindView(this);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// uvolni view
mPresenter.releaseView();
}
@Override
public void show(String msg) {...}
@Override
public void show(Contact c) {...}
}
@KnightActivity
public class ContactActivity extends Activity {
@Extra
Contact mContact;
//...
}
public class ContactListActivity extends Activity {
public void showContact(Contact contact) {
I.startContactActivity(this, contact);
}
// ...
}
trieda I pre navigáciu medzi Activitami
2 metódy pre každú activity:
Intent forXActivity(Context c[, T extra, ...])
void startXActivity(Context c[, T extra, ...])