Skip to content

Commit

Permalink
Merge pull request #29 from PiotrNakonowski/main
Browse files Browse the repository at this point in the history
branch update
  • Loading branch information
AdamPlewa authored Jun 1, 2024
2 parents 073c6a4 + 0afc318 commit d1f84b7
Show file tree
Hide file tree
Showing 17 changed files with 2,174 additions and 290 deletions.
15 changes: 14 additions & 1 deletion MobileApp/.idea/deploymentTargetDropDown.xml

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

2 changes: 1 addition & 1 deletion MobileApp/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId "com.example.whizzapp"
minSdk 24
minSdk 26
targetSdk 34
versionCode 1
versionName "1.0"
Expand Down
6 changes: 6 additions & 0 deletions MobileApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<activity android:name=".LoadingScreen"
android:exported="false" />

<activity android:name=".ToDoList"
android:exported="false" />

<activity android:name=".AddTask"
android:exported="false" />

<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
Expand Down
104 changes: 71 additions & 33 deletions MobileApp/app/src/main/java/com/example/whizzapp/AddEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AddEvent extends AppCompatActivity {
private MaterialCardView inputFrameEventTitle;
private EditText eventTitle;
private FirebaseAuth mAuth;
private MaterialCardView inputFrameEventDescription;
private EditText eventDescription;
private ImageView backIcon;
private MaterialCardView addEventButton;
private MaterialCardView addPhotoButton;
private MaterialCardView inputFramePhoto;
private TextView addPhotoText;
ImageView backIcon;
private MaterialCardView inputFrameMaxAttendanceNumberInput;
private EditText maxAttendanceNumberInput;
private FirebaseAuth mAuth;
private Uri uri;
private String fileName;

Expand All @@ -51,27 +56,27 @@ public class AddEvent extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_event);
backIcon = findViewById(R.id.back_icon);
backIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Events.class);
startActivity(intent);
finish();
}
});



inputFrameEventTitle = findViewById(R.id.inputframeEventTitle);
inputFrameEventDescription = findViewById(R.id.inputframeEventDescription);
backIcon = findViewById(R.id.back_icon);
addEventButton = findViewById(R.id.sendButton);
addPhotoButton = findViewById(R.id.addPhotoButton);
addPhotoText = findViewById(R.id.addPhotoText);
eventTitle = findViewById(R.id.eventTitle);
eventDescription = findViewById(R.id.eventDescription);
inputFramePhoto = findViewById(R.id.inputframePhoto);
inputFrameMaxAttendanceNumberInput = findViewById(R.id.maxAttendanceNumber);
maxAttendanceNumberInput = findViewById(R.id.maxAttendanceNumberText);
mAuth = FirebaseAuth.getInstance();

/* backIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ToDoList.class);
startActivity(intent);
finish();
}
});*/

addPhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -129,7 +134,16 @@ protected void sendImages(Uri uri, String fileName, OnImageUploadListener listen
private void addEvent() {
String title = eventTitle.getText().toString().trim();
String description = eventDescription.getText().toString().trim();
long maxAttendanceNumber = Long.parseLong(maxAttendanceNumberInput.getText().toString().trim());
long maxAttendanceNumber;
try{
maxAttendanceNumber = Long.parseLong(maxAttendanceNumberInput.getText().toString().trim());
}catch (NumberFormatException e){
maxAttendanceNumberInput.setError("Zły format liczby!");
maxAttendanceNumberInput.requestFocus();
inputFrameMaxAttendanceNumberInput.setStrokeColor(getResources().getColor(R.color.error));
return;
}

long attendance = 0;

try {
Expand All @@ -156,6 +170,7 @@ private void addEvent() {
}


long finalMaxAttendanceNumber = maxAttendanceNumber;
sendImages(uri, fileName, new OnImageUploadListener() {
@Override
public void onImageUploadSuccess(String downloadUrl) {
Expand All @@ -164,29 +179,52 @@ public void onImageUploadSuccess(String downloadUrl) {

Log.d("Firebase", "Image upload successful");

Map<String, Object> eventData = new HashMap<>();
eventData.put("Title", title);
eventData.put("Description", description);
eventData.put("PhotoUrl", downloadUrl);
eventData.put("Attendance", attendance);
eventData.put("MaxAttendance", maxAttendanceNumber);
eventCollectionRef.add(eventData).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(AddEvent.this, "Nowe wydarzenie zostało utworzone!", Toast.LENGTH_SHORT).show();
Log.d("Firestore", "Dane zostały pomyślnie dodane do bazy danych");
Intent intent = new Intent(getApplicationContext(), Events.class);
startActivity(intent);
finish();
mAuth = FirebaseAuth.getInstance();

String userId = mAuth.getCurrentUser().getUid();
DocumentReference docRef = db.collection("users").document(userId);

docRef.get().addOnCompleteListener(documentTask -> {
if (documentTask.isSuccessful() && documentTask.getResult() != null) {
DocumentSnapshot document = documentTask.getResult();
if (document.exists()) {
Map<String, Object> eventData = new HashMap<>();

String name = document.getString("name");
String surname = document.getString("surname");

eventData.put("Name",name);
eventData.put("Surname",surname);
eventData.put("Title", title);
eventData.put("Description", description);
eventData.put("PhotoUrl", downloadUrl);
eventData.put("Attendance", attendance);
eventData.put("MaxAttendance", finalMaxAttendanceNumber);
eventData.put("CreatedAt", FieldValue.serverTimestamp());

eventCollectionRef.add(eventData).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(AddEvent.this, "Nowe wydarzenie zostało utworzone!", Toast.LENGTH_SHORT).show();
Log.d("Firestore", "Dane zostały pomyślnie dodane do bazy danych");
Intent intent = new Intent(getApplicationContext(), Events.class);
startActivity(intent);
finish();
}
}).addOnFailureListener(e -> {
Log.e("Firestore", "Błąd podczas dodawania danych do bazy danych: " + e.getMessage());
Toast.makeText(AddEvent.this, "Wystąpił błąd podczad dodawania wydarzenia!", Toast.LENGTH_SHORT).show();
});
}
}
}).addOnFailureListener(e -> {
Log.e("Firestore", "Błąd podczas dodawania danych do bazy danych: " + e.getMessage());
Toast.makeText(AddEvent.this, "Wystąpił błąd podczad dodawania wydarzenia!", Toast.LENGTH_SHORT).show();
});


}

@Override
public void onImageUploadFailure(String errorMessage) {

Log.e("Firebase", "Image upload failed: " + errorMessage);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -122,32 +127,44 @@ public void onActivityResult(Uri result) {
}

private void sendImages() {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
FirebaseAuth mAuth = FirebaseAuth.getInstance();
String userID = mAuth.getCurrentUser().getUid();

File userDir = new File(getFilesDir(), userID);
if (!userDir.exists()) {
userDir.mkdirs();
}

for (Map.Entry<String, Uri> entry : scheduleImages.entrySet()) {
String day = entry.getKey();
Uri uri = entry.getValue();

if (uri != null) {
StorageReference imageRef = storageRef.child("images/" + userID + "/" + day +".jpg");

UploadTask uploadTask = imageRef.putFile(uri);

uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
Toast.makeText(AddSchoolSchedule.this, "Zdjęcia zostały wysłane do bazy danych.", Toast.LENGTH_SHORT).show();
Log.d("FirebaseStorage", "Upload successful for day: " + day);
} else {
Toast.makeText(AddSchoolSchedule.this, "Błąd podczas wysyłania zdjęć do bazy danych.", Toast.LENGTH_SHORT).show();
Log.e("FirebaseStorage", "Upload failed for day: " + day, task.getException());
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
if (inputStream != null) {
File file = new File(userDir, day + ".jpg");
OutputStream outputStream = new FileOutputStream(file);

byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

inputStream.close();
outputStream.close();

Toast.makeText(AddSchoolSchedule.this, "Zdjęcie zostało zapisane.", Toast.LENGTH_SHORT).show();
Log.d("LocalStorage", "Save successful for day: " + day + ", path: " + file.getAbsolutePath());
} else {
Log.e("LocalStorage", "InputStream is null for URI: " + uri);
}
});
}
catch (IOException e) {
Toast.makeText(AddSchoolSchedule.this, "Błąd podczas zapisywania zdjęć lokalnie.", Toast.LENGTH_SHORT).show();
Log.e("LocalStorage", "Save failed for day: " + day, e);
}
}
}

Expand Down
Loading

0 comments on commit d1f84b7

Please sign in to comment.