type of game step done!

This commit is contained in:
OnlyPapy98
2025-10-31 16:17:15 +01:00
parent bd3d8a3d3f
commit 159837bd6f
30 changed files with 789 additions and 172 deletions

View File

@@ -12,19 +12,27 @@ import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.quiz.data.model.Horse;
import com.example.quiz.data.model.Reunion;
import com.example.quiz.data.model.TypeOfBet;
import com.example.quiz.databinding.FragmentBetValidationBinding;
import com.example.quiz.utils.HPRTPrinterUtil;
import com.example.quiz.viewModel.BetViewModel;
import com.example.quiz.viewModel.SharedViewModel;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import dagger.hilt.android.AndroidEntryPoint;
@@ -47,9 +55,9 @@ public class BetValidation extends Fragment {
private HPRTPrinterUtil printer;
private Integer id;
private TypeOfBet typeOfBet;
private String typeOfBet;
private Reunion reunion;
private List<Horse> selectedHorses = new ArrayList<Horse>();
@@ -83,7 +91,7 @@ public class BetValidation extends Fragment {
}
private void setupNumberGrid(GridLayout grid) {
binding.gridNumbers.removeAllViews();
int columns = 4;
grid.setColumnCount(columns);
@@ -133,28 +141,92 @@ public class BetValidation extends Fragment {
super.onViewCreated(view, savedInstanceState);
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
viewModel = new ViewModelProvider(this).get(BetViewModel.class);
shared.betId.observe(getViewLifecycleOwner(), id ->{
this.id = id;
viewModel.getBetNameById(id - 1);
viewModel.betName.observe(getViewLifecycleOwner(), name ->{
binding.horseName.setText(name);
});
viewModel.loadHorses(id - 1);
viewModel.horses.observe(getViewLifecycleOwner(), h->{
this.totalHorses = h;
setupNumberGrid(binding.gridNumbers);
});
viewModel.getBetNameById(shared.betId.getValue() - 1);
viewModel.betName.observe(getViewLifecycleOwner(), name ->{
binding.horseName.setText(name);
});
viewModel.loadHorses(shared.betId.getValue() - 1);
viewModel.horses.observe(getViewLifecycleOwner(), h->{
this.totalHorses = h;
setupNumberGrid(binding.gridNumbers);
});
shared.typeOfBet.observe(getViewLifecycleOwner(), type ->{
this.typeOfBet = type;
});
binding.btnValidate.setOnClickListener(v->{
//Toast.makeText(getContext(), "L'id de bet: "+this.id+" et le type est: "+this.typeOfBet, Toast.LENGTH_SHORT).show();
printer = new HPRTPrinterUtil(getContext());
boolean ok = printer.connectBluetooth("02:03:00:00:00:00");
if (ok) {
printer.printText("Bonjour");
viewModel.loadReunionById(shared.betId.getValue() - 1);
reunion = viewModel.reunion.getValue();
binding.paymentType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected = binding.paymentType.getSelectedItem().toString();
if (selected.equals("Orange Money")) {
binding.phoneNumber.setVisibility(View.VISIBLE);
} else {
binding.phoneNumber.setVisibility(View.GONE); // ou GONE
binding.phoneNumber.setText(""); // vider le champ si non utilisé
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
binding.phoneNumber.setVisibility(View.INVISIBLE);
}
});
binding.betValidateBtn.setOnClickListener(v->{
if(binding.paymentType.getSelectedItem().toString().equals("Orange Money") && binding.phoneNumber.getText().toString().isEmpty()){
Toast.makeText(getContext(), "Veuillez entrer un numéro de téléphone", Toast.LENGTH_SHORT).show();
return;
}
Integer required = Integer.parseInt(typeOfBet.getNumberOfHorse());
if(required == null){
Toast.makeText(getContext(), "Erreur de type de pari", Toast.LENGTH_SHORT).show();
return;
}
if(selectedHorses.size() != required){
Toast.makeText(getContext(), "Veuillez sélectionner "+required+" chevaux", Toast.LENGTH_SHORT).show();
return;
}
printer = new HPRTPrinterUtil(getContext());
boolean ok = printer.autoConnectBluetoothByName();
if(ok){
StringBuilder tspl = new StringBuilder();
tspl.append("PARIS HIPPIQUE (PMU MALI)\n");
tspl.append(typeOfBet.getName()+"\n");
tspl.append("Tel: 555-1234\n");
tspl.append("----------------------------\n");
tspl.append(reunion.getName()+"/"+reunion.getAddress());
tspl.append("----------------------------\n");
String combinationText = selectedHorses.stream()
.map(h -> String.valueOf(h.getNumber()))
.collect(Collectors.joining("-"));
tspl.append("COMBINAISON : "+combinationText);
tspl.append("\n----------------------------\n");
int mise = 300;
int total = mise * selectedHorses.size();
tspl.append("TOTAL MISE: ").append(total).append(" Fcfa\n");
tspl.append("----------------------------\n");
tspl.append("Bonne chance !\n\n\n");
printer.printText(tspl);
}
});
binding.backBtn.setOnClickListener(v->{
getActivity().onBackPressed();
});
binding.deleteBtn.setOnClickListener(v->{
selectedHorses.clear();
binding.combination.setText(getString(R.string.combination,""));
setupNumberGrid(binding.gridNumbers);
});
}

View File

@@ -6,19 +6,30 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.quiz.data.adapter.TypeOfBetAdapter;
import com.example.quiz.databinding.FragmentListOfTypeOfBetsBinding;
import com.example.quiz.viewModel.BetViewModel;
import com.example.quiz.viewModel.SharedViewModel;
import java.util.ArrayList;
import dagger.hilt.android.AndroidEntryPoint;
/**
@@ -34,6 +45,10 @@ public class ListOfTypeOfBets extends Fragment {
private SharedViewModel shared;
private BetViewModel viewModel;
private TypeOfBetAdapter adapter;
public ListOfTypeOfBets() {
// Required empty public constructor
}
@@ -58,20 +73,33 @@ public class ListOfTypeOfBets extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(
getContext(),
R.anim.layout_fad_in
);
binding.typeOfBetRecyclerView.setLayoutAnimation(controller);
binding.typeOfBetRecyclerView.scheduleLayoutAnimation();
binding.typeOfBetRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
// ViewModels
viewModel = new ViewModelProvider(this).get(BetViewModel.class);
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
RadioGroup types = binding.optionsGroup;
// ⚡ Initialiser ladapter UNE SEULE FOIS
adapter = new TypeOfBetAdapter(new ArrayList<>());
adapter.setOnItemClickListener(type -> shared.setTypeOfBet(type));
binding.typeOfBetRecyclerView.setAdapter(adapter);
// Charger les données
viewModel.loadTypes(shared.betId.getValue()-1);
// Observer les résultats
viewModel.types.observe(getViewLifecycleOwner(), types -> {
if (types != null) {
adapter.setTypes(types);
}
});
Button btnValidate = binding.btnValidate;
btnValidate.setOnClickListener(v->{
int selectedId = types.getCheckedRadioButtonId();
if (selectedId == -1) {
Toast.makeText(requireContext(), "Veuillez sélectionner une option", Toast.LENGTH_SHORT).show();
return;
}
RadioButton selectedButton = view.findViewById(selectedId);
String selectedType = selectedButton.getText().toString();
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
shared.setTypeOfBet(selectedType);
FragmentManager fragmentManager = getParentFragmentManager();
BetValidation betValidation = BetValidation.newInstance();
fragmentManager.beginTransaction()

View File

@@ -2,6 +2,8 @@ package com.example.quiz.data;
import com.example.quiz.data.model.Bet;
import com.example.quiz.data.model.Horse;
import com.example.quiz.data.model.Reunion;
import com.example.quiz.data.model.TypeOfBet;
import java.time.LocalDate;
import java.util.Arrays;
@@ -13,6 +15,16 @@ public class BetsBank {
new Bet(
1,
"Course de chevaux",
new Reunion(1,
"Réunion ",
LocalDate.of(2025,10,1),
"Hippodrome Verssaille"
),
Arrays.asList(
new TypeOfBet(1, "Couplet", "2"),
new TypeOfBet(2, "Tierce", "3"),
new TypeOfBet(3, "Place", "1")
),
LocalDate.of(2025,10,1),
Arrays.asList(
new Horse(1),
@@ -21,20 +33,45 @@ public class BetsBank {
new Horse(4)
)
),
new Bet(
2,
"Course de chevaux 2",
new Reunion(2,
"Réunion 2",
LocalDate.of(2025,10,1),
"Hippodrome Bordeaux"
),
Arrays.asList(
new TypeOfBet(1, "Quarte", "4"),
new TypeOfBet(2, "Quinte", "5"),
new TypeOfBet(3, "Placé", "1")
),
LocalDate.of(2025,10,1),
Arrays.asList(
new Horse(4),
new Horse(5),
new Horse(7),
new Horse(6)
new Horse(6),
new Horse(8),
new Horse(9),
new Horse(10),
new Horse(11)
)
),
new Bet(
3,
"Course de chevaux 3",
new Reunion(2,
"Réunion 2",
LocalDate.of(2025,10,1),
"Hippodrome Bordeaux"
),
Arrays.asList(
new TypeOfBet(1, "Quarte", "4"),
new TypeOfBet(2, "Quinte", "5"),
new TypeOfBet(3, "Placé", "1")
),
LocalDate.of(2025,10,1),
Arrays.asList(
new Horse(10),
@@ -46,6 +83,16 @@ public class BetsBank {
new Bet(
4,
"Course de chevaux 4",
new Reunion(2,
"Réunion 2",
LocalDate.of(2025,10,1),
"Hippodrome Bordeaux"
),
Arrays.asList(
new TypeOfBet(1, "Couple", "2"),
new TypeOfBet(2, "Tierce", "3"),
new TypeOfBet(3, "Quarte", "4")
),
LocalDate.of(2025,10,1),
Arrays.asList(
new Horse(5),

View File

@@ -0,0 +1,80 @@
package com.example.quiz.data.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.quiz.R;
import com.example.quiz.data.model.TypeOfBet;
import java.util.List;
public class TypeOfBetAdapter extends RecyclerView.Adapter<TypeOfBetAdapter.TypeOfBetViewHolder> {
private List<TypeOfBet> types;
private onItemClickListener listener;
private int selectedPosition = -1;
public interface onItemClickListener{
void onItemClick(TypeOfBet type);
}
public void setOnItemClickListener(TypeOfBetAdapter.onItemClickListener listener){
this.listener = listener;
}
public TypeOfBetAdapter(List<TypeOfBet> types){
this.types = types;
}
public void setTypes(List<TypeOfBet> types){
this.types = types;
notifyDataSetChanged();
}
@NonNull
@Override
public TypeOfBetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_of_bet_item, parent, false);
return new TypeOfBetViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TypeOfBetViewHolder holder, int position) {
TypeOfBet type = types.get(position);
holder.type_of_bet_name.setText(type.getName());
if(selectedPosition != position){
holder.itemView.setBackgroundResource(R.drawable.item_gradient_bg);
}else{
holder.itemView.setBackgroundResource(R.drawable.item_gradient_bg_selected);
}
holder.itemView.setOnClickListener(v->{
if(listener != null){
listener.onItemClick(type);
}
int previousPosition = selectedPosition;
selectedPosition = holder.getAdapterPosition();
notifyItemChanged(previousPosition);
notifyItemChanged(selectedPosition);
});
}
@Override
public int getItemCount() {
return types.size();
}
public class TypeOfBetViewHolder extends RecyclerView.ViewHolder {
TextView type_of_bet_name;
public TypeOfBetViewHolder(@NonNull View itemView) {
super(itemView);
type_of_bet_name = itemView.findViewById(R.id.type_of_bet_name);
}
}
}

View File

@@ -8,8 +8,28 @@ public class Bet {
private String name;
private LocalDate date;
private Reunion reunion;
List<TypeOfBet> types;
private List<Horse> horses;
public Reunion getReunion() {
return reunion;
}
public void setReunion(Reunion reunion) {
this.reunion = reunion;
}
public List<TypeOfBet> getTypes() {
return types;
}
public void setTypes(List<TypeOfBet> types) {
this.types = types;
}
public List<Horse> getHorses() {
return horses;
}
@@ -43,9 +63,11 @@ public class Bet {
}
public Bet(int id, String name, LocalDate date, List<Horse> horses) {
public Bet(int id, String name, Reunion reunion, List<TypeOfBet> types, LocalDate date, List<Horse> horses) {
this.id = id;
this.name = name;
this.reunion = reunion;
this.types = types;
this.date = date;
this.horses = horses;
}

View File

@@ -0,0 +1,50 @@
package com.example.quiz.data.model;
import java.time.LocalDate;
public class Reunion {
private int id;
private String name;
private LocalDate date;
private String address;
public Reunion(int id, String name,LocalDate date, String address) {
this.id = id;
this.name = name;
this.date = date;
this.address = address;
}
public void setId(int id) {
this.id = id;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

View File

@@ -0,0 +1,37 @@
package com.example.quiz.data.model;
public class TypeOfBet {
private int id;
private String name;
private String numberOfHorse;
public TypeOfBet(int id, String name, String numberOfHorse) {
this.id = id;
this.name = name;
this.numberOfHorse = numberOfHorse;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumberOfHorse() {
return numberOfHorse;
}
public void setNumberOfHorse(String numberOfHorse) {
this.numberOfHorse = numberOfHorse;
}
}

View File

@@ -3,6 +3,8 @@ package com.example.quiz.data.repository;
import com.example.quiz.data.BetsBank;
import com.example.quiz.data.model.Bet;
import com.example.quiz.data.model.Horse;
import com.example.quiz.data.model.Reunion;
import com.example.quiz.data.model.TypeOfBet;
import java.util.List;
@@ -43,4 +45,11 @@ public class BetRepository {
return getBetById(id).getName();
}
public List<TypeOfBet> getTypeOfBetByBetId(int id){
return getBetById(id).getTypes();
}
public Reunion getReunionByHorseId(int id){
return getBetById(id).getReunion();
}
}

View File

@@ -1,9 +1,17 @@
package com.example.quiz.utils;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;
import java.io.InputStream;
import java.util.Set;
import tspl.HPRTPrinterHelper;
public class HPRTPrinterUtil {
@@ -11,15 +19,55 @@ public class HPRTPrinterUtil {
private static final String TAG = "HPRTPrinterUtil";
private Context context;
BluetoothAdapter mBluetoothAdapter;
public HPRTPrinterUtil(Context context) {
this.context = context;
}
/**
* Connecte une imprimante Bluetooth
* @param btAddress adresse MAC Bluetooth de l'imprimante
* @return true si connecté, false sinon
*/
@SuppressLint("MissingPermission")
public boolean autoConnectBluetoothByName() {
try {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
Toast.makeText(context, "Bluetooth désactivé ou non disponible", Toast.LENGTH_LONG).show();
return false;
}
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices != null) {
for (BluetoothDevice device : pairedDevices) {
if (device.getName() != null && device.getName().contains("MP4P Printer")) {
String btAddress = device.getAddress();
// Connexion via ton SDK HPRT
int result = HPRTPrinterHelper.PortOpen("Bluetooth," + btAddress);
if (result == 0) {
Toast.makeText(context, "Imprimante connectée : " + device.getName(), Toast.LENGTH_SHORT).show();
Log.d(TAG, "Connexion réussie : " + device.getName() + " - " + btAddress);
return true;
} else {
Toast.makeText(context, "Erreur connexion imprimante: " + result, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Erreur connexion imprimante: " + result);
return false;
}
}
}
}
Toast.makeText(context, "Imprimante non trouvée. Veuillez appairer d'abord.", Toast.LENGTH_LONG).show();
return false;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Erreur auto-connexion : " + e.getMessage(), Toast.LENGTH_LONG).show();
return false;
}
}
public boolean connectBluetooth(String btAddress) {
try {
HPRTPrinterHelper.PortClose(); // ferme toute connexion existante
@@ -39,7 +87,7 @@ public class HPRTPrinterUtil {
}
}
public void printText(String text) {
public void printText(StringBuilder text) {
try {
if (!HPRTPrinterHelper.IsOpened()) {
Toast.makeText(context, "Imprimante non connectée", Toast.LENGTH_SHORT).show();

View File

@@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel;
import com.example.quiz.data.model.Bet;
import com.example.quiz.data.model.Horse;
import com.example.quiz.data.model.Reunion;
import com.example.quiz.data.model.TypeOfBet;
import com.example.quiz.data.repository.BetRepository;
import java.util.List;
@@ -24,6 +26,10 @@ public class BetViewModel extends ViewModel {
public MutableLiveData<List<Horse>> horses = new MutableLiveData<>();
public MutableLiveData<List<TypeOfBet>> types = new MutableLiveData<>();
public MutableLiveData<Reunion> reunion = new MutableLiveData<Reunion>();
@Inject
@@ -43,4 +49,13 @@ public class BetViewModel extends ViewModel {
betName.setValue(betRepository.getBetNameById(id));
}
public void loadTypes(int id){
types.setValue(betRepository.getBetById(id).getTypes());
}
public void loadReunionById(int id){
reunion.setValue(betRepository.getReunionByHorseId(id));
}
}

View File

@@ -3,6 +3,8 @@ package com.example.quiz.viewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.quiz.data.model.TypeOfBet;
import javax.inject.Inject;
import dagger.hilt.android.lifecycle.HiltViewModel;
@@ -11,12 +13,12 @@ import dagger.hilt.android.lifecycle.HiltViewModel;
public class SharedViewModel extends ViewModel {
public MutableLiveData<Integer> betId = new MutableLiveData<Integer>();
public MutableLiveData<String> typeOfBet = new MutableLiveData<String>();
public MutableLiveData<TypeOfBet> typeOfBet = new MutableLiveData<TypeOfBet>();
public void setBetId(int id){
betId.setValue(id);
}
public void setTypeOfBet(String type){
public void setTypeOfBet(TypeOfBet type){
typeOfBet.setValue(type);
}
}