Skip to content

Commit

Permalink
Fixes Issue openfoodfacts#2069: optimized scanning and saving to data…
Browse files Browse the repository at this point in the history
…base operations.
  • Loading branch information
prashantkh19 committed Jan 12, 2019
1 parent b9c37ae commit cd551cf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher"
android:theme="@style/AppTheme.NoActionBar">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
Expand All @@ -49,7 +50,6 @@
public class SplashActivity extends BaseActivity implements ISplashPresenter.View {

private static final String TAG = "SplashActivity";
private static final int BUFFER_SIZE = 4096;
@BindView(R.id.tagline)
TextView tagline;
@BindView(R.id.text_loading)
Expand Down Expand Up @@ -97,6 +97,8 @@ public void onCreate(Bundle savedInstanceState) {
.onPositive((dialog, which) -> doDownload())
.onNegative((dialog, which) -> presenter.refreshData())
.show();
} else {
presenter.refreshData();
}
}

Expand Down Expand Up @@ -328,6 +330,7 @@ protected Boolean doInBackground(Void... voids) {
//parsing csv file
try (CSVReader reader = new CSVReader(new FileReader(file.getAbsolutePath()), '\t')) {
List<String[]> records = reader.readAll();
List<OfflineProduct> list = new ArrayList<>();
Iterator<String[]> iterator = records.iterator();
// To skip header in the csv file
iterator.hasNext();
Expand All @@ -336,8 +339,12 @@ protected Boolean doInBackground(Void... voids) {
while (iterator.hasNext()) {
String[] record = iterator.next();
OfflineProduct offlineProduct = new OfflineProduct(record[1], record[3], record[0], record[2], record[4]);
//saving to db
mOfflineProductDao.insertOrReplace(offlineProduct);
list.add(offlineProduct);
if (count % 15000 == 0) {
// saving to db
mOfflineProductDao.insertOrReplaceInTx(list);
list.clear();
}
count++;
Log.d(TAG, "completed " + count + " / " + size);
long finalCount = count;
Expand All @@ -349,6 +356,8 @@ public void run() {
});
Log.d(TAG, "progress " + ((float) count / (float) size) * 100 + "% ");
}
Log.d(TAG, "done progress " + ((float) count / (float) size) * 100 + "% ");
mOfflineProductDao.insertOrReplaceInTx(list);
} catch (IOException e) {
e.printStackTrace();
return false;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@
<string name="download_offline_product">Do you want to download data to search product offline?</string>
<string name="txtDownload">Download</string>
<string name="txtProcessing">Processing Data...</string>
<string name="txtSaving">Saving to database (Progress</string>
<string name="txtSaving">Saving to database (Progress </string>
<string name="txtConnectionFailed">Connection failed!</string>
<string name="retry">Retry</string>
<!-- Used for diets AddOn -->
Expand Down

0 comments on commit cd551cf

Please sign in to comment.