我在Android应用中使用带有匕首的MVVM。我已经将Activity与ViewModel附加在一起,我在其中调用了API,并且运行良好。
如果在后台并且应用程序收到通知,并且想从ViewModel调用API以实现可重用性,但我却有一个场景,但是匕首不允许我从ViewModel访问相同的API。
我该怎么办? 我可以使用其他类或存储库,并从通知或ViewModel中调用该实例。
谢谢
我的ViewModel代码:
public class HomeViewModel extends BaseViewModel {
private static final String TAG = "HomeViewModelTimber";
@Inject
MainApi mainApi;
@Inject
RequestManager requestManager;
@Inject
AlertRepository alertRepository;
@Inject
HomeViewModel(MainApi mainApi, SessionManager sessionManager, CompositeDisposable compositeDisposable,
SchedulerProvider schedulerProvider,
AlertRepository alertRepository) {
super(sessionManager,compositeDisposable,schedulerProvider);
this.mainApi = mainApi;
this.alertRepository = alertRepository;
}
void getAlertById(int alertId, IHome iHome){
mainApi.getAlertById(getSessionManager().getJwtToken(), alertId)
.subscribeOn(getSchedulerProvider().io())
.doOnSubscribe(disposable -> Timber.tag(TAG).i(" getAlertById %s", Constants.SUBSCRIBE))
.doOnComplete(() -> Timber.tag(TAG).i(" getAlertById %s", Constants.COMPLETE))
.doOnError(disposable -> Timber.tag(TAG).i(" getAlertById %s", Constants.ERROR))
.observeOn(getSchedulerProvider().ui())
.subscribe(getAlertById -> {
if (getAlertById != null){
if (getAlertById.getPayLoad() == null) {
return;
}
PayLoad payLoad = getAlertById.getPayLoad();
if (payLoad.getFolder() == null) {
return;
}
}});
我想从通知中访问“ getAlertById”