Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.
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
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:roundIcon="@drawable/logo"
android:roundIcon="@drawable/logo_circle"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
Expand All @@ -30,7 +30,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ScannerActivity" />
<activity android:name=".ScannerActivity"/>
<activity android:name=".InfoActivity"/>
</application>

</manifest>
134 changes: 66 additions & 68 deletions app/src/main/java/com/app/qrchecker/FirestoreConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
public abstract class FirestoreConnector {
//TODO Some code style improvements
private static FirebaseFirestore db;
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/mm/YYYY-HH:mm:ss");
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/YYYY-HH:mm:ss");

private static CollectionReference userCollection;
private static CollectionReference logCollection;
private static CollectionReference satLunchCollection;
private static CollectionReference satDinnerCollection;
private static CollectionReference sunLunchCollection;

private static String env = "prod";
private static String year = "2022";
private static String database = "hackeps-" + year;
private static String guest = "HackEPS_Guest_.";
private static final String env = "prod";
private static final String year = "2022";
private static final String database = "hackeps-" + year;
private static final String guest = "HackEPS_Guest_[0-9]*";

private static void initFirebase() {
if (db == null)
Expand All @@ -62,6 +62,10 @@ private static void initFirebase() {
public static boolean isGuest(String uid) {
return uid.matches(guest);
}
private static String getGuestName(String uid){
String[] n=uid.split("_");
return n[n.length-1];
}

public static void addGuest(String uid, ScannerActivity c) {
Map<String, Object> docData = new HashMap<>();
Expand All @@ -71,7 +75,7 @@ public static void addGuest(String uid, ScannerActivity c) {
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
c.log(true, "Error writing to document as guest");
c.log(true, "Error writing to document as guest", "");
}
});
}
Expand All @@ -83,25 +87,24 @@ public static void registerUser(String uid, ScannerActivity c) {
public Void apply(Transaction transaction) throws FirebaseFirestoreException {
DocumentSnapshot snapshot = transaction.get(userCollection.document(uid));
Map<String, Object> data = new HashMap<>();
Map<String, Object> dataUser = new HashMap<>();
data.put("time_in", dtf.format(LocalDateTime.now()));
if (isGuest(uid)) {
addGuest(uid, c);
dataUser.put("food", "Guest User");
dataUser.put("registered",true);
if (isGuest(uid) && !snapshot.exists()) {
transaction.set(userCollection.document(uid), dataUser, SetOptions.merge());
transaction.set(logCollection.document(uid), data, SetOptions.merge());
c.log(false, "Guest User registered");
} else if (snapshot.exists() && (!snapshot.contains("registered") || !snapshot.get("registered", boolean.class))) {
c.log(false, "QR Acceptat","Usuari anonim afegit correctament");
} else if (snapshot.exists() && (!snapshot.contains("registered")
|| !snapshot.get("registered", boolean.class))) {
transaction.set(logCollection.document(uid), data, SetOptions.merge());
transaction.update(userCollection.document(uid), "registered", true);
c.log(false, "User registered");
} else c.log(true, "User not existent or already registered");
c.log(false, "QR Acceptat", "Usuari registrat correctament");
} else c.log(true, "QR Denegat","User no existent o ja registrat");
// Success
return null;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
c.log(true, "Unexpected error occurred.");
}
});
}).addOnFailureListener(e -> c.log(true, "Unexpected error occurred.", ""));

}

Expand Down Expand Up @@ -175,40 +178,40 @@ public void onComplete(@NonNull Task<QuerySnapshot> task) {
});
}

public static void accessUser(String uid, ScannerActivity c) {
//TODO finish this function
LocalDateTime now = LocalDateTime.now();
Map<String, Object> data = new HashMap<>();
data.put("time_in", dtf.format(now));
logCollection.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
logCollection.document(uid)
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
c.log(false, "User access correctly.");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
c.log(false, "Error occurred");
}
});
} else {
c.log(true, "User is not registered");
}
} else {
c.log(true, "Error occurred");
}
}
});
}
// public static void accessUser(String uid, ScannerActivity c) {
// //TODO unused for now
// LocalDateTime now = LocalDateTime.now();
// Map<String, Object> data = new HashMap<>();
// data.put("time_in", dtf.format(now));
// logCollection.document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
// @Override
// public void onComplete(@NonNull Task<DocumentSnapshot> task) {
// if (task.isSuccessful()) {
// DocumentSnapshot document = task.getResult();
// if (document.exists()) {
// logCollection.document(uid)
// .set(data)
// .addOnSuccessListener(new OnSuccessListener<Void>() {
// @Override
// public void onSuccess(Void aVoid) {
// c.log(false, "User access correctly.", "");
// }
// })
// .addOnFailureListener(new OnFailureListener() {
// @Override
// public void onFailure(@NonNull Exception e) {
// c.log(false, "Error occurred", "");
// }
// });
// } else {
// c.log(true, "User is not registered", "");
// }
// } else {
// c.log(true, "Error occurred", "");
// }
// }
// });
// }


public static void eatUser(String uid, EatOptions eat, ScannerActivity c) {
Expand All @@ -226,34 +229,29 @@ else if (eat == EatOptions.dinner_sat)
else if (eat == EatOptions.lunch_sun)
collection = sunLunchCollection;

DocumentSnapshot snapshot = transaction.get(logCollection.document(uid));
DocumentSnapshot userSnapshot = transaction.get(userCollection.document(uid));

if (snapshot.exists()) {
if (userSnapshot.exists()
&& userSnapshot.contains("registered")
&& userSnapshot.get("registered",boolean.class)) {
DocumentSnapshot snapshotEat = transaction.get(collection.document(uid));

if (snapshotEat.exists()) {
// El usuario ya ha comido
c.log(true, "User action already registered");
c.log(true, "QR Denegat", "L'usuari ja ha consumit aquest apat");
} else {
Map<String, Object> data = new HashMap<>();
data.put("eatTime", dtf.format(LocalDateTime.now()));
transaction.set(collection.document(uid), data, SetOptions.merge());
c.log(false, "User action registered");
c.log(false, "QR Acceptat", "Restricció Alimentaria:\n"+userSnapshot.get("food", String.class));
}
} else c.log(true, "User not registered");
// Success
} else c.log(true, "QR Denegat", "Usuari no registrat o inexistent");
return null;
}
}).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
//throw new RuntimeException("");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//Log.w(TAG, "Transaction failure.", e);
}
}).addOnSuccessListener(aVoid -> {
//throw new RuntimeException("");
}).addOnFailureListener(e -> {
//Log.w(TAG, "Transaction failure.", e);
});
}
}
55 changes: 55 additions & 0 deletions app/src/main/java/com/app/qrchecker/InfoActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.app.qrchecker;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class InfoActivity extends AppCompatActivity{
String info;
String title;
TextView info_text;
TextView title_text;
Button back_button;
RelativeLayout layout;
boolean error;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_info);
info = getIntent().getExtras().getString("info");
title = getIntent().getExtras().getString("title");
error = getIntent().getExtras().getBoolean("error");
setError();
initStrings();
}
private void setError(){
if(!error) {
findViewById(R.id.layout).setBackgroundResource(R.color.green);
findViewById(R.id.image).setBackgroundResource(R.drawable.ok);
// ((Button)findViewById(R.id.back)).setTextColor(R.color.green);
}else{
findViewById(R.id.layout).setBackgroundResource(R.color.red);
findViewById(R.id.image).setBackgroundResource(R.drawable.nook);
// ((Button)findViewById(R.id.back)).setTextColor(R.color.red);
}
}
private void initStrings(){
findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
info_text = findViewById(R.id.info);
info_text.setText(info);
title_text = findViewById(R.id.title);
title_text.setText(title);
}
}
12 changes: 3 additions & 9 deletions app/src/main/java/com/app/qrchecker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,10 @@ protected void onCreate(Bundle savedInstanceState) {

private void setButtons() {
MainActivity m = this;
Button access = findViewById(R.id.acces);
Button register = findViewById(R.id.registre);
Button menjar = findViewById(R.id.menjar);
ImageView refresh = findViewById(R.id.refresh);
access.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoScan(ScanOptions.ACCESS);
}
});

register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand Down Expand Up @@ -87,13 +81,13 @@ private void vibrate(){
}
public void setUsers(int size) {
TextView access = findViewById(R.id.users);
access.setText("users -> " + size);
access.setText("Inscrits -> " + size);
vibrate();
}

public void setRegUsers(int size) {
TextView access = findViewById(R.id.regUsers);
access.setText("registered users -> " + size);
access.setText("Registrats -> " + size);
vibrate();
}
public void setSatLunch(int size) {
Expand Down
Loading