use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.
the class TranslationManagerPresenterTest method getRemoteTranslationListObservableIssue.
@Test
public void getRemoteTranslationListObservableIssue() throws Exception {
MockResponse mockResponse = new MockResponse();
mockResponse.setResponseCode(500);
this.mockWebServer.enqueue(mockResponse);
TestObserver<TranslationList> testObserver = new TestObserver<>();
this.translationManager.getRemoteTranslationListObservable().subscribe(testObserver);
testObserver.awaitTerminalEvent();
testObserver.assertNoValues();
testObserver.assertError(IOException.class);
}
use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.
the class TranslationManagerPresenter method getCachedTranslationListObservable.
Observable<TranslationList> getCachedTranslationListObservable(final boolean forceDownload) {
return Observable.defer(() -> {
boolean isCacheStale = System.currentTimeMillis() - quranSettings.getLastUpdatedTranslationDate() > Constants.MIN_TRANSLATION_REFRESH_TIME;
if (forceDownload || isCacheStale) {
return Observable.empty();
}
try {
File cachedFile = getCachedFile();
if (cachedFile.exists()) {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
return Observable.just(jsonAdapter.fromJson(Okio.buffer(Okio.source(cachedFile))));
}
} catch (Exception e) {
Crashlytics.logException(e);
}
return Observable.empty();
});
}
use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.
the class TranslationManagerPresenter method writeTranslationList.
void writeTranslationList(TranslationList list) {
File cacheFile = getCachedFile();
try {
File directory = cacheFile.getParentFile();
boolean directoryExists = directory.mkdirs() || directory.isDirectory();
if (directoryExists) {
if (cacheFile.exists()) {
cacheFile.delete();
}
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<TranslationList> jsonAdapter = moshi.adapter(TranslationList.class);
BufferedSink sink = Okio.buffer(Okio.sink(cacheFile));
jsonAdapter.toJson(sink, list);
sink.close();
quranSettings.setLastUpdatedTranslationDate(System.currentTimeMillis());
}
} catch (Exception e) {
cacheFile.delete();
Crashlytics.logException(e);
}
}
use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.
the class TranslationManagerPresenterTest method getRemoteTranslationListObservable.
@Test
public void getRemoteTranslationListObservable() throws Exception {
MockResponse mockResponse = new MockResponse();
File file = new File(CLI_ROOT_DIRECTORY, "translations.json");
Buffer buffer = new Buffer();
buffer.writeAll(Okio.source(file));
mockResponse.setBody(buffer);
this.mockWebServer.enqueue(mockResponse);
TestObserver<TranslationList> testObserver = new TestObserver<>();
this.translationManager.getRemoteTranslationListObservable().subscribe(testObserver);
testObserver.awaitTerminalEvent();
testObserver.assertValueCount(1);
testObserver.assertNoErrors();
TranslationList list = testObserver.values().get(0);
assertThat(list.getTranslations()).hasSize(50);
}
use of com.quran.labs.androidquran.dao.translation.TranslationList in project quran_android by quran.
the class TranslationManagerPresenterTest method setup.
@Before
public void setup() {
Context mockAppContext = mock(Context.class);
QuranSettings mockSettings = mock(QuranSettings.class);
OkHttpClient mockOkHttp = new OkHttpClient.Builder().build();
mockWebServer = new MockWebServer();
translationManager = new TranslationManagerPresenter(mockAppContext, mockOkHttp, mockSettings, null, mock(QuranFileUtils.class)) {
@Override
void writeTranslationList(TranslationList list) {
// no op
}
};
translationManager.host = mockWebServer.url("").toString();
}
Aggregations