Last active
April 11, 2019 20:24
-
-
Save simplexityx/f9513ccae0e48aa7dadc22dc0c87066d to your computer and use it in GitHub Desktop.
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
package com.example.edmonproject; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.util.Log; | |
import com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.OnFailureListener; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
import com.google.firebase.firestore.DocumentReference; | |
import com.google.firebase.firestore.DocumentSnapshot; | |
import com.google.firebase.firestore.FirebaseFirestore; | |
import com.google.firebase.firestore.QueryDocumentSnapshot; | |
import com.google.firebase.firestore.QuerySnapshot; | |
import java.io.BufferedReader; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Map; | |
import java.text.DateFormat; | |
import static android.support.constraint.Constraints.TAG; | |
import static android.support.constraint.Constraints.TAG; | |
public class Util { | |
private static FirebaseAuth mAuth; | |
private static FirebaseUser currentUser; | |
public static void WriteToFile(Context context, String filepath, String jsonfile){ | |
FileOutputStream fos = null; | |
try{ | |
fos = context.openFileOutput(filepath, Context.MODE_PRIVATE); | |
fos.write(jsonfile.getBytes()); | |
}catch(FileNotFoundException e){ | |
e.printStackTrace(); | |
}catch(IOException e){ | |
e.printStackTrace(); | |
} finally{ | |
if(fos!=null){ | |
try { | |
fos.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public static String ReadFromFile(Context context, String filepath){ | |
FileInputStream fis = null; | |
try { | |
fis = context.openFileInput(filepath); | |
InputStreamReader isr = new InputStreamReader(fis); | |
BufferedReader br = new BufferedReader(isr); | |
StringBuilder sb = new StringBuilder(); | |
String text; | |
while((text = br.readLine()) != null){ | |
sb.append(text).append("\n"); | |
} | |
return sb.toString(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static Date addDays(Date date, int days) | |
{ | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(date); | |
cal.add(Calendar.DATE, days); //minus number would decrement the days | |
return cal.getTime(); | |
} | |
public static void PostRecordingToFirebase(String recType, Map<String, Object> data) | |
{ | |
FirebaseFirestore db = FirebaseFirestore.getInstance(); | |
mAuth = FirebaseAuth.getInstance(); | |
currentUser = mAuth.getCurrentUser(); | |
db.collection("users") | |
.document(currentUser.getUid()) | |
.collection(recType) | |
.add(data) | |
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() { | |
@Override | |
public void onSuccess(DocumentReference documentReference) { | |
Log.e("FIREBASE", "DocumentSnapshot written with ID: " + documentReference.getId()); | |
} | |
}) | |
.addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
Log.e("FIREBASE", "Error adding document", e); | |
} | |
}); | |
} | |
public static void GetRecordFromFirebase(String collection) { | |
FirebaseFirestore db = FirebaseFirestore.getInstance(); | |
mAuth = FirebaseAuth.getInstance(); | |
currentUser = mAuth.getCurrentUser(); | |
DocumentReference docRef = db.collection("users").document(currentUser.getUid()); | |
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { | |
@Override | |
public void onComplete(@NonNull Task<DocumentSnapshot> task) { | |
if (task.isSuccessful()) { | |
DocumentSnapshot document = task.getResult(); | |
if (document.exists()) { | |
Log.e(TAG, "DocumentSnapshot data: " + document.getData()); | |
} else { | |
Log.e(TAG, "No such document"); | |
} | |
} else { | |
Log.e(TAG, "get failed with ", task.getException()); | |
} | |
} | |
}); | |
} | |
public static Date DateFromString(String d){ | |
DateFormat formatter = new SimpleDateFormat("hh:mm:ss::dd/MM/yyyy"); | |
Date date = new Date(); | |
try{ | |
date = formatter.parse(d); | |
}catch(ParseException p){ | |
Log.e("parse fucked up", "fucked up parse"); | |
p.printStackTrace(); | |
} | |
return date; | |
} | |
public static void GetRecordingFromFireBase(final String recType, final onDataListener listener){ | |
FirebaseFirestore db = FirebaseFirestore.getInstance(); | |
mAuth = FirebaseAuth.getInstance(); | |
currentUser = mAuth.getCurrentUser(); | |
db.collection("users").document(currentUser.getUid()).collection("heartRate") | |
.get() | |
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { | |
@Override | |
public void onComplete(@NonNull Task<QuerySnapshot> task) { | |
if (task.isSuccessful()) { | |
listener.onSuccess(task.getResult(), recType); | |
} else { | |
Log.e(TAG, "Error getting documents: ", task.getException()); | |
listener.onFailure(); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment