Search in sources :

Example 6 with AppData

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);
        }
    });
}
Also used : Context(android.content.Context) Entity(com.facebook.crypto.Entity) R(com.jakdor.labday.R) MapPath(com.jakdor.labday.common.model.maps.MapPath) CryptoConfig(com.facebook.crypto.CryptoConfig) Singleton(javax.inject.Singleton) KeyChain(com.facebook.crypto.keychain.KeyChain) Inject(javax.inject.Inject) Crypto(com.facebook.crypto.Crypto) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) AccessToken(com.jakdor.labday.common.model.AccessToken) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) MapService(com.jakdor.labday.common.network.MapService) Observable(io.reactivex.Observable) RxStatus(com.jakdor.labday.rx.RxStatus) AndroidConceal(com.facebook.android.crypto.keychain.AndroidConceal) AppData(com.jakdor.labday.common.model.AppData) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) LastUpdate(com.jakdor.labday.common.model.LastUpdate) FileInputStream(java.io.FileInputStream) File(java.io.File) Timber(timber.log.Timber) SharedPreferences(android.content.SharedPreferences) LastUpdate(com.jakdor.labday.common.model.LastUpdate)

Example 7 with AppData

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);
}
Also used : AppData(com.jakdor.labday.common.model.AppData) RxResponse(com.jakdor.labday.rx.RxResponse)

Example 8 with AppData

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();
}
Also used : AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 9 with AppData

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();
}
Also used : AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 10 with AppData

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();
}
Also used : AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) Test(org.junit.Test)

Aggregations

AppData (com.jakdor.labday.common.model.AppData)15 Test (org.junit.Test)12 Gson (com.google.gson.Gson)10 RxResponse (com.jakdor.labday.rx.RxResponse)9 LabService (com.jakdor.labday.common.network.LabService)7 Context (android.content.Context)6 SharedPreferences (android.content.SharedPreferences)6 R (com.jakdor.labday.R)6 LocalDbHandler (com.jakdor.labday.common.localdb.LocalDbHandler)6 LastUpdate (com.jakdor.labday.common.model.LastUpdate)6 RetrofitBuilder (com.jakdor.labday.common.network.RetrofitBuilder)6 RxSchedulersFacade (com.jakdor.labday.rx.RxSchedulersFacade)6 RxStatus (com.jakdor.labday.rx.RxStatus)6 FileOutputStream (java.io.FileOutputStream)6 Charset (java.nio.charset.Charset)6 Instrumentation (android.app.Instrumentation)5 ApplicationProvider (androidx.test.core.app.ApplicationProvider)5 InstrumentationRegistry (androidx.test.platform.app.InstrumentationRegistry)5 SoLoader (com.facebook.soloader.SoLoader)5 TestApp (com.jakdor.labday.TestApp)5