Skip to content
Extraits de code Groupes Projets

Remy

Fusionnées Rémy Mathieu a demandé de fusionner remy vers main
10 fichiers
+ 565
10
Comparer les modifications
  • Côte à côte
  • En ligne
Fichiers
10
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
Chargement en cours