Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.opencsv:opencsv:5.4'

// Utility methods
implementation 'com.google.code.gson:gson:2.8.6'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trueproof.trueproof">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- requests write to storage permission for csv export-->
Comment on lines +5 to +6
Copy link
Collaborator

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.

<application
android:name=".TrueProofApplication"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class TrueProofApplication extends Application {
@Override
public void onCreate() {
super.onCreate();

try {
Amplify.addPlugin(new AWSApiPlugin());
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Expand Down
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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -85,6 +91,7 @@ protected void onCreate(Bundle savedInstanceState) {
observeLiveData();
setUpMeasurementList();
setUpSpinner();

}

@Override
Expand Down Expand Up @@ -175,6 +182,7 @@ private void onClickUpdateButton() {
.status(status)
.build();
viewModel.updateBatch(newBatch);

}

private void populateTextFields(Batch batch) {
Expand All @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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) {
Expand Down
51 changes: 51 additions & 0 deletions app/src/main/java/com/trueproof/trueproof/utils/BatchToCsv.java
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.


}