Skip to content

Commit

Permalink
Merge pull request #2361 from EyeSeeTea/feature-optimize_the_download…
Browse files Browse the repository at this point in the history
…_of_languages_time_in_splash_activity

Download current language terms first and then the rest in async way
  • Loading branch information
ifoche authored Jan 30, 2019
2 parents 2db4d67 + 0552de0 commit c2c4257
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import android.support.annotation.NonNull;
import android.util.Log;

import com.raizlabs.android.dbflow.sql.language.SQLite;

Expand All @@ -13,8 +14,6 @@
import org.eyeseetea.malariacare.data.sync.importer.poeditor.models.Term;
import org.eyeseetea.malariacare.data.sync.importer.strategies.ILanguagesClient;
import org.eyeseetea.malariacare.domain.boundary.IConnectivityManager;
import org.eyeseetea.malariacare.domain.boundary.repositories.ISettingsRepository;
import org.eyeseetea.malariacare.domain.entity.Settings;

import java.util.ArrayList;
import java.util.Date;
Expand All @@ -23,7 +22,7 @@
import java.util.Map;

public class LanguageDownloader {

private static String TAG = "LanguageDownloader";

private ILanguagesClient client;
private IConnectivityManager connectivity;
Expand All @@ -35,26 +34,41 @@ public LanguageDownloader(ILanguagesClient client, IConnectivityManager connecti
}


public void start() throws Exception {
public void start(String languageCodeFilter) throws Exception {
if (connectivity.isDeviceOnline()) {


Log.d(TAG, "Starting download languages and terms by filter:" + languageCodeFilter);

Date downloadedDate = new Date();

Log.d(TAG, "Retrieving languages from server");

List<Language> languagesFromServer = client.getLanguages();

List<Language> languagesToDownload = getLanguagesToDownload(languagesFromServer, downloadedDate);
Log.d(TAG, "getLanguagesToDownload");
List<Language> languagesToDownload = getLanguagesToDownload(languageCodeFilter,
languagesFromServer, downloadedDate);

updateLocalTranslations(languagesToDownload, client);
Log.d(TAG, "Finished download languagesand terms");
}
}

private void updateLocalTranslations(List<Language> toUpdate, ILanguagesClient client)
throws Exception {

for (Language language : toUpdate) {
Log.d(TAG, "Download terms of " + language.name);
List<Term> terms = client.getTranslationBy(language.code);

Log.d(TAG, "Delete previous terms of " + language.name);
deletePreviousTranslations(language.code);
for (Term term : terms) {
save(term, language.code);
}

Log.d(TAG, "Saving terms of " + language.name);
save(language);
}
}
Expand All @@ -77,7 +91,8 @@ private void deletePreviousTranslations(String languageCode) {
}

@NonNull
private List<Language> getLanguagesToDownload(List<Language> languagesFromServer, Date downloadDateTime) {
private List<Language> getLanguagesToDownload(String languageCodeFilter,
List<Language> languagesFromServer, Date downloadDateTime) {

List<Language> languagesToDownload = new ArrayList<>();
List<TranslationLanguageDB> languagesFromDB = SQLite.select().
Expand All @@ -87,11 +102,18 @@ private List<Language> getLanguagesToDownload(List<Language> languagesFromServer
languagesFromDB);

for (Language languageServer : languagesFromServer) {
if(languageServer.updated == null){
languageServer.updated = downloadDateTime;
}
if (requiresToBeDownloaded(languageServer, mapLanguagesByCodesInDB)) {
languagesToDownload.add(languageServer);
if (languageCodeFilter != null && !languageCodeFilter.isEmpty()) {
if (languageCodeFilter.equals(languageServer.code)) {
languagesToDownload.add(languageServer);
break;
}
} else {
if (languageServer.updated == null) {
languageServer.updated = downloadDateTime;
}
if (requiresToBeDownloaded(languageServer, mapLanguagesByCodesInDB)) {
languagesToDownload.add(languageServer);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public DownloadLanguageTranslationUseCase(CredentialsReader credentialsReader,
this.credentialsReader = credentialsReader;
}

public void downloadAsync(IAsyncExecutor mAsyncExecutor) throws Exception {
public void downloadAsync(IAsyncExecutor mAsyncExecutor) {
mAsyncExecutor.run(this);
}

public void download() throws Exception {
public void download(String languageCode) throws Exception {

LanguageDownloader downloader = LanguageFactory.getLanguageDownloader(getClient(),
connectivity);
downloader.start();
downloader.start(languageCode);

}

Expand All @@ -40,7 +40,7 @@ private ILanguagesClient getClient() throws Exception {
@Override
public void run() {
try {
download();
download("");
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@
import org.eyeseetea.malariacare.data.database.datasources.AuthDataSource;
import org.eyeseetea.malariacare.data.database.datasources.SurveyLocalDataSource;
import org.eyeseetea.malariacare.data.database.datasources.ValueLocalDataSource;
import org.eyeseetea.malariacare.data.authentication.CredentialsReader;
import org.eyeseetea.malariacare.data.database.utils.PreferencesState;
import org.eyeseetea.malariacare.domain.boundary.IConnectivityManager;
import org.eyeseetea.malariacare.domain.boundary.executors.IAsyncExecutor;
import org.eyeseetea.malariacare.domain.boundary.executors.IMainExecutor;
import org.eyeseetea.malariacare.domain.boundary.repositories.IAuthRepository;
import org.eyeseetea.malariacare.domain.boundary.repositories.ISurveyRepository;
import org.eyeseetea.malariacare.domain.boundary.repositories.IValueRepository;
import org.eyeseetea.malariacare.domain.entity.Survey;
import org.eyeseetea.malariacare.domain.boundary.IConnectivityManager;
import org.eyeseetea.malariacare.domain.usecase.ClearAuthUseCase;
import org.eyeseetea.malariacare.domain.usecase.DownloadLanguageTranslationUseCase;
import org.eyeseetea.malariacare.domain.usecase.SaveSurveyFromIntentUseCase;
import org.eyeseetea.malariacare.domain.usecase.ClearAuthUseCase;
import org.eyeseetea.malariacare.network.factory.NetworkManagerFactory;
import org.eyeseetea.malariacare.presentation.executors.AsyncExecutor;
import org.eyeseetea.malariacare.presentation.executors.UIThreadExecutor;
import org.eyeseetea.malariacare.domain.boundary.IConnectivityManager;
import org.eyeseetea.malariacare.domain.usecase.DownloadLanguageTranslationUseCase;
import org.eyeseetea.malariacare.network.factory.NetworkManagerFactory;

public class SplashActivityStrategy extends ASplashActivityStrategy {
public static final String INTENT_JSON_EXTRA_KEY = "ConnectVoucher";
Expand Down Expand Up @@ -140,10 +136,13 @@ public void downloadLanguagesFromServer() {
CredentialsReader credentialsReader = CredentialsReader.getInstance();
IConnectivityManager connectivity = NetworkManagerFactory.getConnectivityManager(
activity);
DownloadLanguageTranslationUseCase downloader =
DownloadLanguageTranslationUseCase useCase =
new DownloadLanguageTranslationUseCase(credentialsReader, connectivity);

downloader.download();
String currentLanguage = PreferencesState.getInstance().getCurrentLocale();

useCase.download(currentLanguage);
useCase.downloadAsync(new AsyncExecutor());
}
} catch (Exception e) {
Log.e(TAG, "Unable to download Languages From Server" + e.getMessage());
Expand Down

0 comments on commit c2c4257

Please sign in to comment.