-
Notifications
You must be signed in to change notification settings - Fork 0
Csv export #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Csv export #131
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| package com.trueproof.trueproof.activities; | ||
|
|
||
| import android.content.Intent; | ||
| import android.net.Uri; | ||
| import android.os.Build; | ||
| import android.os.Bundle; | ||
| import android.os.Environment; | ||
| import android.util.Log; | ||
| import android.view.Menu; | ||
| import android.view.MenuItem; | ||
|
|
@@ -33,6 +35,10 @@ | |
| import com.trueproof.trueproof.utils.UserSettings; | ||
| import com.trueproof.trueproof.viewmodels.BatchDetailViewModel; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
|
|
@@ -85,6 +91,7 @@ protected void onCreate(Bundle savedInstanceState) { | |
| observeLiveData(); | ||
| setUpMeasurementList(); | ||
| setUpSpinner(); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -175,6 +182,7 @@ private void onClickUpdateButton() { | |
| .status(status) | ||
| .build(); | ||
| viewModel.updateBatch(newBatch); | ||
|
|
||
| } | ||
|
|
||
| private void populateTextFields(Batch batch) { | ||
|
|
@@ -190,6 +198,40 @@ private void populateTextFields(Batch batch) { | |
| } | ||
| } | ||
| } | ||
| public void sendAsEmail (Batch batch){ | ||
| String batchHeader = "\"Type\",\"Number\", \"Identifier\", \"Created At\", \"Updated At\""; | ||
| String batchInfo = "\"" + batch.getType() + "\",\"" + batch.getBatchNumber() +"\",\"" + | ||
| batch.getBatchIdentifier()+ "\""; | ||
| // +"\",\"" + batch.getCreatedAt().toString()+"\",\"" + batch.getUpdatedAt().toString() | ||
| String combinedString = batchHeader + "\n" + batchInfo; | ||
| File file = null; | ||
| File root = Environment.getExternalStorageDirectory(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's see if we can use internal storage rather than external storage so we don't need to request permissions. |
||
| if (root.canWrite()){ | ||
| File directory = new File (root.getAbsolutePath() + "/BatchData"); | ||
| directory.mkdirs(); | ||
| file = new File (directory, "Data.csv"); | ||
| FileOutputStream out = null; | ||
| try{ | ||
| out = new FileOutputStream(file); | ||
| } catch (FileNotFoundException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| try{ | ||
| out.write(combinedString.getBytes()); | ||
| out.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| Uri uri; | ||
| uri = Uri.fromFile(file); | ||
| Intent send = new Intent(Intent.ACTION_SEND); | ||
| send.putExtra(Intent.EXTRA_SUBJECT, batch.getBatchIdentifier()); | ||
| send.putExtra(Intent.EXTRA_STREAM, uri); | ||
| send.setType("text/html"); | ||
| startActivity(send); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean onCreateOptionsMenu(Menu menu) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.trueproof.trueproof.utils; | ||
|
|
||
| import android.content.Intent; | ||
| import android.net.Uri; | ||
| import android.os.Environment; | ||
|
|
||
| import com.amplifyframework.datastore.generated.model.Batch; | ||
| import com.amplifyframework.datastore.generated.model.Measurement; | ||
| import com.opencsv.CSVWriter; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class BatchToCsv { | ||
|
|
||
| String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath(); | ||
| CSVWriter csvWriter; | ||
|
|
||
| public BatchToCsv() throws IOException { | ||
| } | ||
|
|
||
| public void convertToCsv (Batch batch) throws IOException { | ||
| String path = baseDir + File.separator + "BatchData.csv"; | ||
| File f = new File(path); | ||
| if (f.exists()&& f.isDirectory()){ | ||
| csvWriter= new CSVWriter(new FileWriter(path, true)); | ||
| } else{ | ||
| csvWriter= new CSVWriter(new FileWriter(path)); | ||
| } | ||
|
|
||
| // still need to use intent to save file or email | ||
| List<Measurement> batchMeasurements = batch.getMeasurements(); | ||
| csvWriter.writeNext(new String[] {"Type", "Number", "Identifier", "Created At", "Updated At"}); | ||
| csvWriter.writeNext(new String [] {batch.getType(), String.valueOf(batch.getBatchNumber()), batch.getBatchIdentifier(), batch.getCreatedAt().toString(), batch.getUpdatedAt().toString()}); | ||
| csvWriter.writeNext(new String [] {""}); | ||
| csvWriter.writeNext(new String [] {"True Proof", "Temperature", "Temp. Correction", "Hydrometer", "Hydro Correction", "Created At"}); | ||
| for (Measurement m : batchMeasurements | ||
| ) { | ||
| csvWriter.writeNext(new String [] {String.valueOf(m.getTrueProof()), String.valueOf(m.getTemperature()), | ||
| String.valueOf(m.getTemperatureCorrection()), String.valueOf(m.getHydrometer()), String.valueOf(m.getHydrometerCorrection()) | ||
| , m.getCreatedAt().toString()}); | ||
| } | ||
| csvWriter.close(); | ||
| } | ||
|
Comment on lines
+27
to
+49
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally we want this method to be side-effect free and return something, rather than returning void and doing side effects. My thoughts are to either make this return a String with the contents of the CSV file. Or, if we want to return a file, create the file without Android's API and return a URI. |
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe we actually need this permission to create a file and attach it to an email. I believe we can accomplish our task without this.