Skip to content

Commit

Permalink
Add export to csv in history scan
Browse files Browse the repository at this point in the history
Add button to remove all history scan
Fixe #120
  • Loading branch information
itchix committed Oct 9, 2016
1 parent ab8ab71 commit 6b64107
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 22 deletions.
Binary file modified app/app-release.apk
Binary file not shown.
6 changes: 4 additions & 2 deletions app/build.gradle
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "openfoodfacts.github.scrachx.openfood"
minSdkVersion 16
targetSdkVersion 24
versionCode 24
versionName "0.2.6"
versionCode 25
versionName "0.2.7"
}

dexOptions {
Expand Down Expand Up @@ -69,6 +69,8 @@ dependencies {

compile 'com.loopj.android:android-async-http:1.4.9'

compile 'com.opencsv:opencsv:3.8'

compile 'com.fasterxml.jackson.core:jackson-core:2.8.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.3'
Expand Down
@@ -1,15 +1,21 @@
package openfoodfacts.github.scrachx.openfood.views;


import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.ShareActionProvider;
Expand All @@ -19,19 +25,27 @@
import android.view.MenuItem;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.opencsv.CSVWriter;
import com.orm.query.Select;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.HistoryItem;
import openfoodfacts.github.scrachx.openfood.models.HistoryProduct;
import openfoodfacts.github.scrachx.openfood.utils.Utils;
import openfoodfacts.github.scrachx.openfood.views.adapters.HistoryListAdapter;

public class HistoryScanActivity extends BaseActivity {
Expand Down Expand Up @@ -64,29 +78,76 @@ public boolean onOptionsItemSelected(MenuItem item) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_edit_product:
// TODO : export option
case R.id.action_remove_all_history:
new MaterialDialog.Builder(this)
.title(R.string.title_clear_history_dialog)
.content(R.string.text_clear_history_dialog)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
HistoryProduct.deleteAll(HistoryProduct.class);
productItems.clear();
recyclerHistoryScanView.getAdapter().notifyDataSetChanged();
}
})
.positiveText(R.string.txtYes)
.negativeText(R.string.txtNo)
.show();
return true;
case R.id.action_export_all_history:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new MaterialDialog.Builder(this)
.title(R.string.action_about)
.content(R.string.permision_write_external_storage)
.neutralText(R.string.txtOk)
.show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Utils.MY_PERMISSIONS_REQUEST_STORAGE);
}
} else {
exportCSV();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

public void exportCSV() {
Toast.makeText(this, R.string.txt_exporting_history, Toast.LENGTH_LONG).show();
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
Log.d("dir", baseDir);
String fileName = "exportHistoryOFF"+new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+".csv";
String filePath = baseDir + File.separator + fileName;
File f = new File(filePath );
CSVWriter writer;
FileWriter fileWriter;
try {
if(f.exists() && !f.isDirectory()) {
fileWriter = new FileWriter(filePath , true);
writer = new CSVWriter(fileWriter);
} else {
writer = new CSVWriter(new FileWriter(filePath));
}
String[] headers = {"Barcode", "Name", "Brands"};
writer.writeNext(headers);
List<HistoryProduct> listHistoryProducts = HistoryProduct.listAll(HistoryProduct.class);
for (HistoryProduct hp : listHistoryProducts) {
String[] line = {hp.getBarcode(), hp.getTitle(), hp.getBrands()};
writer.writeNext(line);
}
writer.close();
Toast.makeText(this, R.string.txt_history_exported, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
/*getMenuInflater().inflate(R.menu.menu_product, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
String url = " " + Utils.getUriProductByCurrentLanguage() + mState.getProduct().getCode();
if (mState.getProduct().getUrl() != null) {
url = " " + mState.getProduct().getUrl();
}
shareIntent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.msg_share) + url);
shareIntent.setType("text/plain");
setShareIntent(shareIntent);*/

getMenuInflater().inflate(R.menu.menu_history, menu);
return true;
}

Expand Down Expand Up @@ -141,4 +202,34 @@ protected void onPostExecute(Context ctx) {
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case Utils.MY_PERMISSIONS_REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
exportCSV();
} else {
new MaterialDialog.Builder(this)
.title(R.string.permission_title)
.content(R.string.permission_denied)
.negativeText(R.string.txtNo)
.positiveText(R.string.txtYes)
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
})
.show();
}
break;
}
}
}

}
Expand Up @@ -7,7 +7,6 @@
import android.net.NetworkInfo;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
Expand Down Expand Up @@ -39,7 +38,6 @@ public HistoryScanHolder(final View itemView) {
@Override
public void onClick(View view) {
String url = " " + Utils.getUriProductByCurrentLanguage() + txtBarcode.getText();

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = itemView.getResources().getString(R.string.msg_share) + url;
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions app/src/main/res/menu/menu_history.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/action_remove_all_history"
app:showAsAction="ifRoom"
android:title="@string/clear_history"
android:icon="@drawable/ic_delete_forever_white_48dp" />

<item android:id="@+id/action_export_all_history"
app:showAsAction="ifRoom"
android:title="@string/export_csv_history"
android:icon="@drawable/ic_file_download_white_48dp" />

</menu>
3 changes: 1 addition & 2 deletions app/src/main/res/menu/menu_product.xml
@@ -1,8 +1,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/menu_item_share"
<item android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="@string/share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -188,5 +188,12 @@
<!-- Open beauty app -->
<string name="openBeautyApp" translatable="false">org.openbeautyfacts.scanner</string>
<string name="scan_history_drawer">History</string>
<string name="clear_history">Clear history</string>
<string name="title_clear_history_dialog">Clear</string>
<string name="text_clear_history_dialog">Do you want to clear all scan history?</string>
<string name="export_csv_history">Export CSV</string>
<string name="txt_exporting_history">Exporting history...</string>
<string name="txt_history_exported">History exported</string>
<string name="permision_write_external_storage">Write external storage permission is needed to export history.</string>

</resources>

0 comments on commit 6b64107

Please sign in to comment.