diff --git a/app/build.gradle b/app/build.gradle index 6a60d984eb597c1f3ba2f9f9216e601f13e19bc6..60ec7141393217006a4b21a6fa2c47cb5ca9d92e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,4 +44,5 @@ dependencies { androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation platform('com.google.firebase:firebase-bom:31.2.3') implementation 'com.google.firebase:firebase-auth' + implementation 'com.google.code.gson:gson:2.8.8' } \ No newline at end of file diff --git a/app/src/main/java/be/lepl1509group13/workoutwarrior/MainActivity.java b/app/src/main/java/be/lepl1509group13/workoutwarrior/MainActivity.java index e84b222fb10c6efde701a83666010fd82fc65807..1e2a66e37d56265313d0fd4f9f68017074f5def4 100644 --- a/app/src/main/java/be/lepl1509group13/workoutwarrior/MainActivity.java +++ b/app/src/main/java/be/lepl1509group13/workoutwarrior/MainActivity.java @@ -1,33 +1,44 @@ package be.lepl1509group13.workoutwarrior; +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.ImageButton; +import android.widget.TextView; +import android.widget.Toast; + import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import android.annotation.SuppressLint; -import android.content.Intent; -import android.os.Bundle; -import android.view.View; -import android.widget.ImageButton; -import android.widget.TextView; -import android.widget.Toast; - import com.google.android.material.navigation.NavigationView; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; +import com.google.gson.Gson; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -41,6 +52,7 @@ public class MainActivity extends AppCompatActivity implements MainAdapter.OnIte private final FirebaseDatabase firebaseDb = FirebaseDatabase.getInstance(); private DatabaseReference db; + private FirebaseAuth mAuth; private Integer numberOfProgramsToDisplay = 0, totalNumberOfCustomPrograms = 0; private boolean needsRefresh = false; @@ -51,6 +63,9 @@ public class MainActivity extends AppCompatActivity implements MainAdapter.OnIte setContentView(R.layout.activity_main); getSupportActionBar().hide(); + mAuth = FirebaseAuth.getInstance(); + FirebaseUser currentUser = mAuth.getCurrentUser(); + db = firebaseDb.getReference("Programs"); getProgramData(); @@ -74,6 +89,9 @@ public class MainActivity extends AppCompatActivity implements MainAdapter.OnIte drawerLayout.closeDrawer(GravityCompat.START); return false; }); + + //check if any programs were shared by friends + getUserPseudo(currentUser); } @Override @@ -241,4 +259,176 @@ public class MainActivity extends AppCompatActivity implements MainAdapter.OnIte }); } + private void getSharedPrograms(String currentUser) { + db = firebaseDb.getReference("Shared_program"); + db.addValueEventListener(new ValueEventListener() { + @SuppressLint("SetTextI18n") + @Override + public void onDataChange(DataSnapshot dataSnapshot) { + DataSnapshot firstChild = dataSnapshot.child(currentUser); + for (DataSnapshot childData: firstChild.getChildren()) { + + for(DataSnapshot realData : firstChild.child(childData.getKey()).getChildren()){ + + //aled + String dataFromDB = realData.getValue(String.class); + + Gson gson = new Gson(); + HashMap<String, ArrayList<Exercise>> programByDay = gson.fromJson(dataFromDB, HashMap.class); + + androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(MainActivity.this); + LayoutInflater inflater = getLayoutInflater(); + View dialogView = inflater.inflate(R.layout.shared_workout_popup, null); + TextView title = dialogView.findViewById(R.id.share_popup_title); + title.setText(childData.getKey() + " a partagé avec vous son programme personnel."); + TextView description = dialogView.findViewById(R.id.share_popup_description); + description.setText("Voulez-vous l'accepter ?"); + builder.setView(dialogView); + + builder.setPositiveButton("J'accepte", (dialog, which) -> + addNewCustomWorkout(childData.getKey(), programByDay)); + builder.setNegativeButton("Je refuse", (dialog, which) -> + Log.d("MainActivity", "Cancel workout share with: " + childData.getKey())); + + AlertDialog dialog = builder.create(); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + Button positiveButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE); + positiveButton.setTextColor(getResources().getColor(R.color.green)); + Button negativeButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE); + negativeButton.setTextColor(getResources().getColor(R.color.black)); + } + }); + dialog.show(); + + } + } + } + + @Override + public void onCancelled(DatabaseError databaseError) { + Toast.makeText(MainActivity.this, "Une erreur est survenue !", Toast.LENGTH_SHORT).show(); + } + }); + } + + private void getUserPseudo(FirebaseUser currentUser) { + if(currentUser != null){ + db = firebaseDb.getReference("Account_pseudo"); + db.addValueEventListener(new ValueEventListener() { + @Override + public void onDataChange(@NonNull DataSnapshot dataSnapshot) { + for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { + String userKey = childSnapshot.getKey(); + String pseudo = childSnapshot.getValue(String.class); + if(Objects.equals(userKey, currentUser.getUid())){ + db = firebaseDb.getReference("Friends"); + getSharedPrograms(pseudo); + return; + } + } + } + + @Override + public void onCancelled(@NonNull DatabaseError error) { + Toast.makeText(MainActivity.this, "Une erreur est survenue !", Toast.LENGTH_SHORT).show(); + } + }); + } + } + + private void addNewCustomWorkout(String username, HashMap<String, ArrayList<Exercise>> programByDay) { + /* + Log.d("NOP", "user: " + username); + Log.d("NOP", "data: " + programByDay); + + Log.d("TAG", "addNewCustomWorkout: " + programByDay.get("lundi")); + + + String lundiString = null, lundiSubstring, mardiString = null, mardiSubstring, mercrediString = null, mercrediSubstring, jeudiString = null, jeudiSubstring, + vendrediString = null, vendrediSubstring, samediString = null, samediSubstring, dimancheString = null, dimancheSubstring; + + ArrayList<Exercise> lundi = programByDay.get("lundi"); + if(!lundi.isEmpty()){ + lundiSubstring = lundi.toString().substring(lundi.toString().indexOf("name=") + 5, lundi.toString().indexOf(", timer")); + lundiString = lundiSubstring.trim().replaceAll("^\"|\"$", ""); + } + + ArrayList<Exercise> mardi = programByDay.get("mardi"); + if(!mardi.isEmpty()){ + mardiSubstring = mardi.toString().substring(mardi.toString().indexOf("name=") + 5, mardi.toString().indexOf(", timer")); + mardiString = mardiSubstring.trim().replaceAll("^\"|\"$", ""); + } + + ArrayList<Exercise> mercredi = programByDay.get("mercredi"); + if(!mercredi.isEmpty()){ + mercrediSubstring = mercredi.toString().substring(mercredi.toString().indexOf("name=") + 5, mercredi.toString().indexOf(", timer")); + mercrediString = mercrediSubstring.trim().replaceAll("^\"|\"$", ""); + } + + ArrayList<Exercise> jeudi = programByDay.get("jeudi"); + if(!jeudi.isEmpty()){ + jeudiSubstring = jeudi.toString().substring(jeudi.toString().indexOf("name=") + 5, jeudi.toString().indexOf(", timer")); + jeudiString = jeudiSubstring.trim().replaceAll("^\"|\"$", ""); + } + + ArrayList<Exercise> vendredi = programByDay.get("vendredi"); + if(!vendredi.isEmpty()){ + vendrediSubstring = vendredi.toString().substring(vendredi.toString().indexOf("name=") + 5, vendredi.toString().indexOf(", timer")); + vendrediString = vendrediSubstring.trim().replaceAll("^\"|\"$", ""); + } + + ArrayList<Exercise> samedi = programByDay.get("samedi"); + if(!samedi.isEmpty()){ + samediSubstring = samedi.toString().substring(samedi.toString().indexOf("name=") + 5, samedi.toString().indexOf(", timer")); + samediString = samediSubstring.trim().replaceAll("^\"|\"$", ""); + } + + ArrayList<Exercise> dimanche = programByDay.get("dimanche"); + if(dimanche.isEmpty()){ + dimancheSubstring = dimanche.toString().substring(dimanche.toString().indexOf("name=") + 5, dimanche.toString().indexOf(", timer")); + dimancheString = dimancheSubstring.trim().replaceAll("^\"|\"$", ""); + } + + + //String inputString = "[{breakVal=60.0, description=Répétitions : 4x10, image_url=@drawable/developpe_couche_barre, name=Développé couché, timer=0.0, workingDays=[]}]"; + + System.out.println(lundiString); // Output: Développé couché + System.out.println(mardiString); // Output: Développé couché + System.out.println(mercrediString); // Output: Développé couché + System.out.println(jeudiString); // Output: Développé couché + System.out.println(vendrediString); // Output: Développé couché + System.out.println(samediString); // Output: Développé couché + System.out.println(dimancheString); // Output: Développé couché + + ArrayList<Exercise> newLundi = new ArrayList<>(); + newLundi.add(new Exercise(lundiString, "fff", 10, "ah oue", 10)); + + programByDay.clear(); + programByDay.put("lundi", newLundi); + + + try { + String programpath = username + ".ser"; + + HashMap<String, ArrayList<Exercise>> entry = programByDay; + ExerciseEntry exerciseEntry = new ExerciseEntry(entry); + FileOutputStream fileOutputStream = openFileOutput(programpath, Context.MODE_PRIVATE); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); + objectOutputStream.writeObject(exerciseEntry); + objectOutputStream.close(); + fileOutputStream.close(); + + //todo + //Tell the mainActivity to refresh, to add the new program + //setResult(1); + Toast.makeText(MainActivity.this, "Programme enregistré !", + Toast.LENGTH_SHORT).show(); + } catch (Exception e) { + e.printStackTrace(); + } + + */ + } } \ No newline at end of file diff --git a/app/src/main/res/layout/shared_workout_popup.xml b/app/src/main/res/layout/shared_workout_popup.xml new file mode 100644 index 0000000000000000000000000000000000000000..b1c72d5588f69cd141075b2332461ebc7b8607ac --- /dev/null +++ b/app/src/main/res/layout/shared_workout_popup.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@color/white"> + + <androidx.constraintlayout.widget.ConstraintLayout + android:id="@+id/share_popup" + android:layout_width="match_parent" + android:layout_height="wrap_content" + app:layout_constraintTop_toTopOf="parent" + android:layout_marginTop="20dp" + android:layout_marginBottom="60dp" + android:padding="15dp"> + + <TextView + android:id="@+id/share_popup_title" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:fontFamily="@font/poppins_semibold" + android:textSize="20sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + tools:text="test" /> + + <TextView + android:id="@+id/share_popup_description" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:fontFamily="@font/poppins" + android:layout_marginTop="20dp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/share_popup_title" + tools:text="test" + android:textSize="20sp" /> + + </androidx.constraintlayout.widget.ConstraintLayout> + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file