Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store locally an history of scans #120
TODO : export in csv & add the possibility to clear history
- Loading branch information
Showing
27 changed files
with
537 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alter table HISTORYPRODUCT add COLUMN lastSeen DATE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/src/main/java/openfoodfacts/github/scrachx/openfood/models/HistoryItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/main/java/openfoodfacts/github/scrachx/openfood/models/HistoryProduct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
app/src/main/java/openfoodfacts/github/scrachx/openfood/views/HistoryScanActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.