Skip to content

Commit

Permalink
Store locally an history of scans #120
Browse files Browse the repository at this point in the history
TODO : export in csv & add the possibility to clear history
  • Loading branch information
itchix committed Oct 9, 2016
1 parent 2fdeeff commit ab8ab71
Show file tree
Hide file tree
Showing 27 changed files with 537 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Expand Up @@ -62,6 +62,7 @@ dependencies {
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'

compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -54,13 +54,22 @@
<activity
android:name=".views.SaveProductOfflineActivity"
android:screenOrientation="portrait"/>
<activity
android:name=".views.HistoryScanActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateUnchanged|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".views.MainActivity"/>
</activity>


<meta-data
android:name="DATABASE"
android:value="offacts.db"/>
<meta-data
android:name="VERSION"
android:value="7"/>
android:value="10"/>
<meta-data
android:name="QUERY_LOG"
android:value="true"/>
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions app/src/main/assets/sugar_upgrades/9.sql
@@ -0,0 +1 @@
alter table HISTORYPRODUCT add COLUMN lastSeen DATE;
Expand Up @@ -9,7 +9,6 @@
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down
Expand Up @@ -14,6 +14,7 @@
import net.steamcrafted.loadtoast.LoadToast;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import cz.msebera.android.httpclient.Header;
Expand Down Expand Up @@ -80,6 +81,15 @@ public void onNegative(MaterialDialog dialog) {
.show();
}else{
lt.success();
List<HistoryProduct> resHp = HistoryProduct.find(HistoryProduct.class, "barcode = ?", barcode);
HistoryProduct hp = null;
if(resHp.size() == 1) {
hp = resHp.get(0);
hp.setLastSeen(new Date());
} else {
hp = new HistoryProduct(s.getProduct().getProductName(), s.getProduct().getBrands(), s.getProduct().getImageFrontUrl(), barcode);
}
hp.save();
Intent intent = new Intent(activity, ProductActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("state", s);
Expand Down
@@ -0,0 +1,52 @@
package openfoodfacts.github.scrachx.openfood.models;

import android.graphics.Bitmap;

public class HistoryItem {

private String title;
private String brands;
private Bitmap url;
private String barcode;

public HistoryItem(){}

public HistoryItem(String title, String brands, Bitmap url, String barcode) {
this.title = title;
this.brands = brands;
this.url = url;
this.barcode = barcode;
}

public String getBarcode() {
return barcode;
}

public void setBarcode(String barcode) {
this.barcode = barcode;
}

public String getTitle(){
return this.title;
}

public void setTitle(String title){
this.title = title;
}

public Bitmap getUrl() {
return url;
}

public void setUrl(Bitmap url) {
this.url = url;
}

public String getBrands() {
return brands;
}

public void setBrands(String brands) {
this.brands = brands;
}
}
@@ -0,0 +1,71 @@
package openfoodfacts.github.scrachx.openfood.models;

import com.orm.SugarRecord;
import com.orm.dsl.Unique;
import java.util.Date;

public class HistoryProduct extends SugarRecord {

private String title;
private String brands;
private String url;
private Date lastSeen;
@Unique
private String barcode;

public HistoryProduct() {
this.title = "";
this.brands = "";
this.url = "";
this.barcode = "";
this.lastSeen = new Date();
}

public HistoryProduct(String title, String brands, String url, String barcode) {
this.title = title;
this.brands = brands;
this.url = url;
this.barcode = barcode;
this.lastSeen = new Date();
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getBrands() {
return brands;
}

public void setBrands(String brands) {
this.brands = brands;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getBarcode() {
return barcode;
}

public void setBarcode(String barcode) {
this.barcode = barcode;
}

public Date getLastSeen() {
return lastSeen;
}

public void setLastSeen(Date lastSeen) {
this.lastSeen = lastSeen;
}
}
@@ -0,0 +1,144 @@
package openfoodfacts.github.scrachx.openfood.views;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.orm.query.Select;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
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.views.adapters.HistoryListAdapter;

public class HistoryScanActivity extends BaseActivity {

private ShareActionProvider mShareActionProvider;
private List<HistoryItem> productItems;

@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.listHistoryScan)
RecyclerView recyclerHistoryScanView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history_scan);

toolbar.setTitleTextColor(getResources().getColor(R.color.white));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

productItems = new ArrayList<>();
new HistoryScanActivity.FillAdapter().execute(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_edit_product:
// TODO : export option
default:
return super.onOptionsItemSelected(item);
}
}


@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);*/

return true;
}

// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}

public class FillAdapter extends AsyncTask<Context, Void, Context> {

@Override
protected void onPreExecute() {
List<HistoryProduct> listHistoryProducts = HistoryProduct.listAll(HistoryProduct.class);
if (listHistoryProducts.size() == 0) {
Toast.makeText(getApplicationContext(), R.string.txtNoData, Toast.LENGTH_LONG).show();
cancel(true);
} else {
Toast.makeText(getApplicationContext(), R.string.txtLoading, Toast.LENGTH_LONG).show();
}
}

@Override
protected Context doInBackground(Context... ctx) {
List<HistoryProduct> listHistoryProducts = Select.from(HistoryProduct.class).orderBy("LAST_SEEN DESC").list();
for (int i = 0; i < listHistoryProducts.size(); i++) {
HistoryProduct hp = listHistoryProducts.get(i);
Bitmap imgUrl = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_no), 200, 200, true);
try {
URL url = new URL(hp.getUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
imgUrl = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(input), 200, 200, true);
} catch (IOException e) {
e.printStackTrace();
}

productItems.add(new HistoryItem(hp.getTitle(), hp.getBrands(), imgUrl, hp.getBarcode()));
}

return ctx[0];
}

@Override
protected void onPostExecute(Context ctx) {
HistoryListAdapter adapter = new HistoryListAdapter(productItems, getApplication());
recyclerHistoryScanView.setAdapter(adapter);
recyclerHistoryScanView.setLayoutManager(new LinearLayoutManager(ctx));
}
}

}
@@ -1,6 +1,7 @@
package openfoodfacts.github.scrachx.openfood.views;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -105,13 +106,14 @@ protected void onCreate(Bundle savedInstanceState) {
new SecondaryDrawerItem().withName(R.string.search_by_barcode_drawer).withIcon(FontAwesome.Icon.faw_barcode).withIdentifier(2),
new SecondaryDrawerItem().withName(R.string.search_by_name_drawer).withIcon(FontAwesome.Icon.faw_search).withIdentifier(3),
new SecondaryDrawerItem().withName(R.string.scan_search).withIcon(FontAwesome.Icon.faw_camera).withIdentifier(4),
new SecondaryDrawerItem().withName(R.string.scan_history_drawer).withIcon(FontAwesome.Icon.faw_clock_o).withIdentifier(5),
new SectionDrawerItem().withName(R.string.user_drawer),
new SecondaryDrawerItem().withName(R.string.sign_in_drawer).withIcon(FontAwesome.Icon.faw_sign_in).withIdentifier(5),
new SecondaryDrawerItem().withName(R.string.alert_drawer).withIcon(FontAwesome.Icon.faw_info).withIdentifier(6),
new SecondaryDrawerItem().withName(R.string.sign_in_drawer).withIcon(FontAwesome.Icon.faw_sign_in).withIdentifier(6),
new SecondaryDrawerItem().withName(R.string.alert_drawer).withIcon(FontAwesome.Icon.faw_info).withIdentifier(7),
new DividerDrawerItem(),
new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(FontAwesome.Icon.faw_anchor).withIdentifier(7),
new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(FontAwesome.Icon.faw_anchor).withIdentifier(8),
new DividerDrawerItem(),
new PrimaryDrawerItem().withName(R.string.open_beauty_drawer).withIcon(FontAwesome.Icon.faw_shopping_bag).withIdentifier(8)
new PrimaryDrawerItem().withName(R.string.open_beauty_drawer).withIcon(FontAwesome.Icon.faw_shopping_bag).withIdentifier(9)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
Expand Down Expand Up @@ -144,23 +146,26 @@ public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
startActivity(intent);
}
} else if (drawerItem.getIdentifier() == 5) {
Intent intent = new Intent(MainActivity.this, HistoryScanActivity.class);
startActivity(intent);
} else if (drawerItem.getIdentifier() == 6) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivityForResult(intent, LOGIN_REQUEST);
} else if (drawerItem.getIdentifier() == 6) {
} else if (drawerItem.getIdentifier() == 7) {
fragment = new AlertUserFragment();
getSupportActionBar().setTitle(R.string.alert_drawer);
} else if (drawerItem.getIdentifier() == 7) {
} else if (drawerItem.getIdentifier() == 8) {
fragment = new OfflineEditFragment();
getSupportActionBar().setTitle(getResources().getString(R.string.offline_edit_drawer));
} else if (drawerItem.getIdentifier() == 8) {
} else if (drawerItem.getIdentifier() == 9) {
boolean openBeautyInstalled = Utils.isApplicationInstalled(MainActivity.this, getString(R.string.openBeautyApp));
if(openBeautyInstalled) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(getString(R.string.openBeautyApp));
startActivity(LaunchIntent);
} else {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getString(R.string.openBeautyApp))));
} catch (android.content.ActivityNotFoundException anfe) {
} catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + getString(R.string.openBeautyApp))));
}
}
Expand Down

0 comments on commit ab8ab71

Please sign in to comment.