我想询问MVP和匕首的工作方式。因此,在android开发中,我们知道Activity和fragment是一个视图,我们通过实现观察者(在我的情况下,使用rxjava + retrofit)来制作演示者。在这种情况下,我使用匕首注入Rest服务(网络组件 - >改装)。
我仍然混淆将Rest服务注入我的演示者,因为我发现的所有示例都是注入活动。
这是我的代码。
视图:
section .data
counter: dd '0' ;holds the num of chars read
section .bss
Buff resb 1 ;hold the value of one char
fd_in resb 4
fd_out resb 4
section .data
global _start
_start:
...
...
...
;increment the counter by 1 each time a char is wrote to file
Add_counter:
mov eax, [counter]
sub eax, '0'
inc eax
add eax, '0'
mov [counter], eax
...
...
...
;print out number of characters wrote
Show_num_char:
mov eax, 4
mov ebx, 1
mov ecx, counter
mov edx, 4
int 80h
....
...
..
作为主持人:
public class PageFragment extends Fragment implements ScheduleViewInterface{
private ScheduleCursorAdapter scheduleAdapter;
@Inject RestApi restApiInject;
private SchedulePresenter mPresenterJson;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.getApiComponent(getActivity()).inject(this);
mPresenterJson.fetchSchedule();
}
@Override
public Observable<ScheduleList> getSchedule() {
return restApiInject.getScheduleListByUrl("url here");
}
这是我的dagger2代码
public class SchedulePresenter implements Observer<ScheduleList> {
private ScheduleViewInterface mInterface;
public SchedulePresenter(ScheduleViewInterface viewInterface){
mInterface = viewInterface;
}
@Override
public void onCompleted() {
mInterface.jsonCompleted();
}
@Override
public void onError(Throwable e) {
mInterface.jsonError(e.getMessage());
}
@Override
public void onNext(ScheduleList scheduleList) {
mInterface.jsonScheduleList(scheduleList);
}
public void fetchSchedules(){
unSubscribeAll();
subscribe(mInterface.getSchedule(), SchedulePresenter.this);
}
答案 0 :(得分:0)
最佳做法是创建一个Interactor类来处理它,然后在演示者中注入该交互器。看下面我项目中的代码来理解。演示者代码是:
public class PopularMoviesPresenterImpl implements PopularMoviesPresenter {
@Inject
public PopularMoviesInteractor interactor;
private PopularMoviesView view;
private Subscription subscription = Subscriptions.empty();
@Inject
public PopularMoviesPresenterImpl() {
}
@Override
public void setView(PopularMoviesView view) {
this.view = view;
}
@Override
public void destroy() {
if (subscription != null && !subscription.isUnsubscribed())
subscription.unsubscribe();
interactor.onDestroy();
view = null;
interactor = null;
}
@Override
public void loadPopularMoviesData(boolean isConnected, int page, int limit) {
if (null != view) {
view.showProgress();
}
subscription = interactor.loadPopularMovies(page, limit).subscribe(movies ->
{
if (null != view) {
view.hideProgress();
view.setPopularMoviesValue(movies);
if (!isConnected)
view.showOfflineMessage();
}
},
throwable -> {
if (null != view) {
view.hideProgress();
}
if (isConnected) {
if (null != view) {
view.showRetryMessage();
}
} else {
if (null != view) {
view.showOfflineMessage();
}
}
});
}}
交互者是:
public class PopularMoviesInteractorImpl implements PopularMoviesInteractor {
private TraktApi api;
private SchedulerProvider scheduler;
private ReplaySubject<Movie[]> moviesDataSubject;
private Subscription moviesSubscription;
@Inject
public PopularMoviesInteractorImpl(TraktApi api, SchedulerProvider scheduler) {
this.api = api;
this.scheduler = scheduler;
}
@Override
public Observable<Movie[]> loadPopularMovies(int page, int limit) {
if (moviesSubscription == null || moviesSubscription.isUnsubscribed()) {
moviesDataSubject = ReplaySubject.create();
moviesSubscription = api.getPopularMovies(page, limit, Constants.API_EXTENDED_FULL_IMAGES)
.subscribeOn(scheduler.backgroundThread())
.observeOn(scheduler.mainThread())
.subscribe(moviesDataSubject);
}
return moviesDataSubject.asObservable();
}
@Override
public void onDestroy() {
if (moviesSubscription != null && !moviesSubscription.isUnsubscribed())
moviesSubscription.unsubscribe();
}}
交互界面:
public interface PopularMoviesInteractor {
Observable<Movie[]> loadPopularMovies(int page, int limit);
void onDestroy();}
并在你的模块中写:
@Provides
public PopularMoviesInteractor provideInteractor(PopularMoviesInteractorImpl interactor) {
return interactor;}
通过构造注入提供了交互器。(你可以通过简单的注入来完成)
这是一个简洁的mvp示例: http://github.com/mmirhoseini/fyber_mobile_offers
答案 1 :(得分:0)
我看到这个问题已经回答了。但是如果你想要一个关于如何在MVP架构中使用依赖注入的明确示例,我建议你检查一下boilerplate,因为它完全基于使用Dagger2的DI而且它遵循MVP架构。
锅炉板当前处理的依赖关系如下:
依赖注入的强大功能非常方便,特别是对于测试,因为您可以轻松地将测试环境中的依赖项切换为虚拟依赖项。