use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.
the class ProjectRepository method getUpdate.
/**
* Checks if update is necessary, then gets AppData from API or local db
* @return {Single<RxResponse<AppData>>} appData wrapped with {@link RxResponse}
*/
public Observable<RxResponse<AppData>> getUpdate(String apiUrl, Context context) {
if (accessToken == null) {
if (!loadAccessToken(context)) {
repositoryState = repositoryStates.ERROR;
return Observable.just(RxResponse.loginError(new Throwable("no access token")));
}
} else if (accessToken.equals("-1")) {
repositoryState = repositoryStates.ERROR;
return Observable.just(RxResponse.loginError(new Throwable("bad access token")));
}
networkManager.configAuth(apiUrl, accessToken);
networkManager.configAuth(apiUrl);
return networkManager.getLastUpdate().subscribeOn(rxSchedulersFacade.io()).observeOn(rxSchedulersFacade.ui()).onErrorResumeNext(Observable.just(new LastUpdate("-1"))).onExceptionResumeNext(Observable.just(new LastUpdate("-1"))).flatMap(s -> isLocalDataCurrent(apiUpdateId = s.getUpdatedAt(), context) ? // load from local db
localDbHandler.getAppDataFromDb() : // get appData from api
apiRequest(networkManager.getAppData())).onErrorResumeNext(// last effort data retrieval
localDbHandler.getAppDataFromDb()).flatMap(appDataRxResponse -> {
// last effort data retrieval from local db
if (appDataRxResponse.status == RxStatus.ERROR) {
return localDbHandler.getAppDataFromDb();
} else {
return Observable.just(appDataRxResponse);
}
}).doOnNext(appDataRxResponse -> {
if (appDataRxResponse.status == RxStatus.SUCCESS) {
saveApiLastUpdateId(apiUpdateId, context);
} else if (appDataRxResponse.status == RxStatus.SUCCESS_DB) {
this.data = appDataRxResponse;
this.repositoryState = repositoryStates.READY;
}
}).onErrorReturn(throwable -> {
if (apiUpdateCurrent) {
this.repositoryState = repositoryStates.NO_DB;
return RxResponse.noDb(throwable);
} else {
this.repositoryState = repositoryStates.ERROR;
return RxResponse.error(throwable);
}
});
}
use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.
the class LocalDbHandler method getAppDataFromDb.
/**
* Creates {@link RxResponse<AppData>} from local db zipped responses
* @return RxResponse<AppData>
*/
public Observable<RxResponse<AppData>> getAppDataFromDb() {
return Observable.zip(EventDao.getAllEvents(db), MapOtherDao.getMapOthers(db), PathDao.getAllPaths(db), TimetableDao.getAllTimetables(db), SpeakerDao.getAllSpeakers(db), ((events, mapOthers, paths, timetables, speakers) -> {
AppData appData = new AppData(events, mapOthers, paths, timetables, speakers);
Timber.i("Retrieved AppData from local db");
return RxResponse.successDb(appData);
})).onErrorReturn(RxResponse::error);
}
use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.
the class LocalDbIntegrationTests method integrationTestSaveAndLoad.
/**
* Save and load {@link com.jakdor.labday.common.model.AppData} to local db
*/
@Test
public void integrationTestSaveAndLoad() throws Exception {
Gson gson = new Gson();
AppData appData = gson.fromJson(readAssetFile(InstrumentationRegistry.getInstrumentation().getContext(), "api/app_data.json"), AppData.class);
localDbHandler.pushAppDataToDb(appData);
TestObserver<RxResponse<AppData>> testObserver = localDbHandler.getAppDataFromDb().test();
testObserver.assertSubscribed();
testObserver.awaitCount(1);
testObserver.assertNoErrors();
testObserver.assertValue(appDataRxResponse -> {
Assert.assertEquals(RxStatus.SUCCESS_DB, appDataRxResponse.status);
Assert.assertNotNull(appDataRxResponse.data);
Assert.assertNull(appDataRxResponse.error);
Assert.assertEquals(appData, appDataRxResponse.data);
Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
Assert.assertNotNull(appDataRxResponse.data.getEvents());
Assert.assertNotNull(appDataRxResponse.data.getMapOthers());
Assert.assertNotNull(appDataRxResponse.data.getPaths());
Assert.assertNotNull(appDataRxResponse.data.getSpeakers());
Assert.assertNotNull(appDataRxResponse.data.getTimetables());
return true;
});
testObserver.onComplete();
}
use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.
the class NetworkManagerIntegrationTest method getAppDataTest.
/**
* {@link NetworkManager} integration test: get AppData from local REST API mock server,
* compere parsed response to AppData parsed directly from .json file
*/
@Test
public void getAppDataTest() throws Exception {
networkManager.configAuth(dummyApiUrl, dummyToken);
Gson gson = new Gson();
AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
TestObserver<AppData> testObserver = networkManager.getAppData().test();
testObserver.awaitCount(1);
testObserver.assertSubscribed();
testObserver.assertNoErrors();
testObserver.assertValue(appData1 -> {
Assert.assertEquals(appData, appData1);
Assert.assertEquals(appData.hashCode(), appData1.hashCode());
return true;
});
testObserver.onComplete();
}
use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.
the class NetworkManagerIntegrationTest method getAppDataTokenTest.
/**
* {@link NetworkManager} integration test: same as getAppDataTest() but with token Authorization
*/
@Test
public void getAppDataTokenTest() throws Exception {
networkManager.configAuth(dummyApiUrl, dummyToken);
Gson gson = new Gson();
AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
TestObserver<AppData> testObserver = networkManager.getAppData().test();
testObserver.awaitCount(1);
testObserver.assertSubscribed();
testObserver.assertNoErrors();
testObserver.assertValue(appData1 -> {
Assert.assertEquals(appData, appData1);
Assert.assertEquals(appData.hashCode(), appData1.hashCode());
return true;
});
testObserver.onComplete();
}
Aggregations