create pari
This commit is contained in:
@@ -5,6 +5,10 @@ import android.os.Bundle;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import android.util.Log;
|
||||
@@ -18,10 +22,17 @@ import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.quiz.data.model.Course;
|
||||
import com.example.quiz.data.model.Pari;
|
||||
import com.example.quiz.data.model.Reunion;
|
||||
import com.example.quiz.data.model.TypeOfBet;
|
||||
import com.example.quiz.data.model.enums.PariStatut;
|
||||
import com.example.quiz.databinding.FragmentBetValidationBinding;
|
||||
import com.example.quiz.utils.HPRTPrinterUtil;
|
||||
import com.example.quiz.utils.Result;
|
||||
import com.example.quiz.viewModel.PariViewModel;
|
||||
import com.example.quiz.viewModel.SharedViewModel;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -50,9 +61,14 @@ public class BetValidation extends Fragment {
|
||||
|
||||
private Course course;
|
||||
|
||||
private List<String> selectedHorses = new ArrayList<String>();
|
||||
private int mise;
|
||||
|
||||
private final MutableLiveData<List<String>> selectedHorses = new MutableLiveData<>(List.of());
|
||||
|
||||
PariViewModel pariViewModel;
|
||||
|
||||
|
||||
boolean order = false;
|
||||
|
||||
|
||||
|
||||
@@ -107,22 +123,25 @@ public class BetValidation extends Fragment {
|
||||
textView.setGravity(Gravity.CENTER);
|
||||
textView.setBackgroundResource(R.drawable.number_background);
|
||||
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
||||
|
||||
textView.setOnClickListener(v -> {
|
||||
if (selectedHorses.contains(horse)) {
|
||||
selectedHorses.remove(horse);
|
||||
v.setSelected(false);
|
||||
v.setBackgroundResource(R.drawable.number_background);
|
||||
} else {
|
||||
selectedHorses.add(horse);
|
||||
v.setSelected(true);
|
||||
v.setBackgroundResource(R.drawable.number_selected_background);
|
||||
}
|
||||
String combinationText = selectedHorses.stream()
|
||||
.map(h -> h)
|
||||
.collect(Collectors.joining("-"));
|
||||
binding.combination.setText(getString(R.string.combination, combinationText));
|
||||
});
|
||||
final List<String> horses = new ArrayList<>(selectedHorses.getValue());
|
||||
if (horses.contains(horse)) {
|
||||
horses.remove(horse);
|
||||
selectedHorses.setValue(horses);
|
||||
v.setSelected(false);
|
||||
v.setBackgroundResource(R.drawable.number_background);
|
||||
} else {
|
||||
horses.add(horse);
|
||||
selectedHorses.setValue(horses);
|
||||
v.setSelected(true);
|
||||
v.setBackgroundResource(R.drawable.number_selected_background);
|
||||
}
|
||||
String combinationText = selectedHorses.getValue().stream()
|
||||
.map(h -> h)
|
||||
.collect(Collectors.joining("-"));
|
||||
binding.combination.setText(getString(R.string.combination, combinationText));
|
||||
});
|
||||
|
||||
|
||||
return textView;
|
||||
}
|
||||
@@ -131,6 +150,7 @@ public class BetValidation extends Fragment {
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
|
||||
pariViewModel = new ViewModelProvider(this).get(PariViewModel.class);
|
||||
/*viewModel = new ViewModelProvider(this).get(BetViewModel.class);
|
||||
viewModel.getBetNameById(shared.betId.getValue() - 1);
|
||||
viewModel.betName.observe(getViewLifecycleOwner(), name ->{
|
||||
@@ -161,6 +181,18 @@ public class BetValidation extends Fragment {
|
||||
}
|
||||
});
|
||||
|
||||
binding.order.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
order = isChecked;
|
||||
calculateMise(selectedHorses.getValue().size(), shared.typeOfBet.getValue());
|
||||
});
|
||||
|
||||
selectedHorses.observe(getViewLifecycleOwner(), new Observer<List<String>>() {
|
||||
@Override
|
||||
public void onChanged(List<String> horses) {
|
||||
calculateMise(horses.size(), shared.typeOfBet.getValue());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
}
|
||||
});
|
||||
|
||||
binding.betValidateBtn.setOnClickListener(v->{
|
||||
if(binding.paymentType.getSelectedItem().toString().equals("Orange Money") && binding.phoneNumber.getText().toString().isEmpty()){
|
||||
@@ -193,40 +225,52 @@ public class BetValidation extends Fragment {
|
||||
Toast.makeText(getContext(), "Erreur de type de pari", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if(selectedHorses.size() < required){
|
||||
if(selectedHorses.getValue().size() < required){
|
||||
Toast.makeText(getContext(), "Veuillez sélectionner au moins"+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).append("\n");
|
||||
tspl.append("Tel: 555-1234\n");
|
||||
tspl.append("----------------------------\n");
|
||||
tspl.append(reunion.getNom()).append("/").append(course.getLieu());
|
||||
tspl.append("----------------------------\n");
|
||||
String combinationText = selectedHorses.stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining("-"));
|
||||
|
||||
tspl.append("COMBINAISON : ").append(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);
|
||||
}
|
||||
selectedHorses = List.of();
|
||||
binding.combination.setText(getString(R.string.combination,""));
|
||||
setupNumberGrid(binding.gridNumbers);
|
||||
Pari pari = new Pari(
|
||||
generate12Digits(),
|
||||
shared.typeOfBet.getValue(),
|
||||
"QUARTE_PLUS",
|
||||
mise,
|
||||
"07/11/2025 05:08:00",
|
||||
shared.selectedCourse.getValue().getId(),
|
||||
"123456789",
|
||||
"John Doe",
|
||||
1,
|
||||
5,
|
||||
6,
|
||||
4,
|
||||
11,
|
||||
5,
|
||||
10,
|
||||
order?selectedHorses.getValue().stream().map(Integer::parseInt).collect(Collectors.toList()):List.of(),
|
||||
selectedHorses.getValue().stream().map(Integer::parseInt).collect(Collectors.toList()),
|
||||
order?selectedHorses.getValue().stream().map(Integer::parseInt).collect(Collectors.toList()):List.of(),
|
||||
order,
|
||||
shared.typeOfBet.getValue(),
|
||||
PariStatut.PERDANT,
|
||||
false,
|
||||
false
|
||||
);
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(), new Observer<Result<Pari>>() {
|
||||
@Override
|
||||
public void onChanged(Result<Pari> pariResult) {
|
||||
switch (pariResult.status){
|
||||
case ERROR:
|
||||
Toast.makeText(getContext(), pariResult.message, Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case LOADING:
|
||||
Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case SUCCESS:
|
||||
printPari();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
binding.backBtn.setOnClickListener(v->{
|
||||
@@ -234,12 +278,87 @@ public class BetValidation extends Fragment {
|
||||
});
|
||||
|
||||
binding.deleteBtn.setOnClickListener(v->{
|
||||
selectedHorses.clear();
|
||||
selectedHorses.setValue(List.of());
|
||||
binding.combination.setText(getString(R.string.combination,""));
|
||||
setupNumberGrid(binding.gridNumbers);
|
||||
});
|
||||
}
|
||||
|
||||
public void printPari(){
|
||||
printer = new HPRTPrinterUtil(getContext());
|
||||
boolean ok = printer.autoConnectBluetoothByName();
|
||||
if(ok){
|
||||
StringBuilder tspl = new StringBuilder();
|
||||
tspl.append("PARIS HIPPIQUE (PMU MALI)\n");
|
||||
|
||||
tspl.append(typeOfBet).append("\n");
|
||||
tspl.append("Tel: 555-1234\n");
|
||||
tspl.append("----------------------------\n");
|
||||
tspl.append(reunion.getNom()).append("/").append(course.getLieu());
|
||||
tspl.append("----------------------------\n");
|
||||
String combinationText = selectedHorses.getValue().stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining("-"));
|
||||
|
||||
tspl.append("COMBINAISON : ").append(combinationText);
|
||||
|
||||
tspl.append("\n----------------------------\n");
|
||||
tspl.append("TOTAL MISE: ").append(mise).append(" Fcfa\n");
|
||||
tspl.append("----------------------------\n");
|
||||
tspl.append("Bonne chance !\n\n\n");
|
||||
|
||||
printer.printText(tspl);
|
||||
}
|
||||
selectedHorses.setValue(List.of());
|
||||
binding.combination.setText(getString(R.string.combination,""));
|
||||
setupNumberGrid(binding.gridNumbers);
|
||||
}
|
||||
|
||||
public void calculateMise(int nombreChevauxSelectionnes, String typeOfBet){
|
||||
if(typeOfBet.toString().toLowerCase().contains("couple")){
|
||||
if(nombreChevauxSelectionnes == 2){
|
||||
mise = 300;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 2){
|
||||
mise = 300 + (nombreChevauxSelectionnes - 3)*300;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.toString().toLowerCase().contains("tierce")){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
mise = 400;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 3){
|
||||
mise = 400 + (nombreChevauxSelectionnes - 3)*400;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.toString().toLowerCase().contains("quarte")){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
mise = 500;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 3){
|
||||
mise = 500 + (nombreChevauxSelectionnes - 3)*500;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.toString().toLowerCase().contains("quinte")){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
mise = 1000;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 3){
|
||||
mise = 1000 + (nombreChevauxSelectionnes - 3)*1000;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String generate12Digits() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int digit = (int) (Math.random() * 10); // 0 à 9
|
||||
sb.append(digit);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
|
||||
Reference in New Issue
Block a user