print successfully integrated!
This commit is contained in:
@@ -19,6 +19,8 @@ import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -42,6 +44,10 @@ import com.example.quiz.utils.Result;
|
||||
import com.example.quiz.viewModel.PariViewModel;
|
||||
import com.example.quiz.viewModel.SharedViewModel;
|
||||
import com.google.android.material.appbar.MaterialToolbar;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -65,6 +71,7 @@ public class BetValidation extends Fragment {
|
||||
SharedViewModel shared;
|
||||
|
||||
|
||||
private boolean order;
|
||||
|
||||
|
||||
private TypeOfBet typeOfBet;
|
||||
@@ -73,6 +80,8 @@ public class BetValidation extends Fragment {
|
||||
|
||||
private Course course;
|
||||
|
||||
private int coeff;
|
||||
|
||||
private int mise;
|
||||
|
||||
private ActivityResultLauncher<Intent> enableBluetoothLauncher;
|
||||
@@ -83,9 +92,6 @@ public class BetValidation extends Fragment {
|
||||
PariViewModel pariViewModel;
|
||||
|
||||
|
||||
boolean order = false;
|
||||
|
||||
|
||||
|
||||
public BetValidation() {
|
||||
// Required empty public constructor
|
||||
@@ -107,7 +113,34 @@ public class BetValidation extends Fragment {
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
binding = FragmentBetValidationBinding.inflate(inflater, container, false);
|
||||
binding.combination.setText(getString(R.string.combination,""));
|
||||
binding.coeff.setText(String.valueOf(1));
|
||||
coeff = Integer.parseInt(binding.coeff.getText().toString());
|
||||
binding.coeff.addTextChangedListener(new TextWatcher(){
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||
if(charSequence.toString().isEmpty()){
|
||||
binding.coeff.setError("Le coefficient est obligatoire");
|
||||
return;
|
||||
}
|
||||
if(Integer.parseInt(charSequence.toString())<1){
|
||||
binding.coeff.setError("Le coefficient doit être supérieur ou égal à 1 ");
|
||||
return;
|
||||
}
|
||||
coeff = Integer.parseInt(charSequence.toString());
|
||||
calculateMise(selectedHorses.getValue().size());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable editable) {
|
||||
|
||||
}
|
||||
});
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
@@ -154,7 +187,7 @@ public class BetValidation extends Fragment {
|
||||
String combinationText = selectedHorses.getValue().stream()
|
||||
.map(h -> h)
|
||||
.collect(Collectors.joining("-"));
|
||||
binding.combination.setText(getString(R.string.combination, combinationText));
|
||||
binding.combination.setText( combinationText);
|
||||
});
|
||||
|
||||
|
||||
@@ -166,6 +199,9 @@ public class BetValidation extends Fragment {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
|
||||
pariViewModel = new ViewModelProvider(this).get(PariViewModel.class);
|
||||
typeOfBet = shared.typeOfBet.getValue();
|
||||
reunion = shared.selectedReunion.getValue();
|
||||
course = shared.selectedCourse.getValue();
|
||||
AppCompatActivity activity = (AppCompatActivity) getActivity();
|
||||
if(activity != null){
|
||||
MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
|
||||
@@ -195,18 +231,23 @@ public class BetValidation extends Fragment {
|
||||
}
|
||||
});
|
||||
|
||||
binding.order.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
order = isChecked;
|
||||
calculateMise(selectedHorses.getValue().size(), shared.typeOfBet.getValue().getLabel());
|
||||
});
|
||||
|
||||
selectedHorses.observe(getViewLifecycleOwner(), new Observer<List<String>>() {
|
||||
@Override
|
||||
public void onChanged(List<String> horses) {
|
||||
calculateMise(horses.size(), shared.typeOfBet.getValue().getLabel());
|
||||
calculateMise(horses.size());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
if(shared.typeOfBet.getValue().getNumberOfHorse() > 2 && horses.size() >= shared.typeOfBet.getValue().getNumberOfHorse()){
|
||||
binding.order.setVisibility(View.VISIBLE);
|
||||
}else{
|
||||
binding.order.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
});
|
||||
binding.order.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
order = isChecked;
|
||||
calculateMise(selectedHorses.getValue().size());
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
});
|
||||
|
||||
binding.betValidateBtn.setOnClickListener(v->{
|
||||
if(binding.paymentType.getSelectedItem().toString().equals("Orange Money") && binding.phoneNumber.getText().toString().isEmpty()){
|
||||
@@ -217,12 +258,9 @@ public class BetValidation extends Fragment {
|
||||
if(shared.typeOfBet == null || shared.selectedReunion.getValue() == null || shared.selectedCourse == null){
|
||||
return;
|
||||
}
|
||||
typeOfBet = shared.typeOfBet.getValue();
|
||||
reunion = shared.selectedReunion.getValue();
|
||||
course = shared.selectedCourse.getValue();
|
||||
|
||||
|
||||
int required = typeOfBet.getNumberOfHorse();
|
||||
|
||||
if(selectedHorses.getValue().size() < required){
|
||||
Toast.makeText(getContext(), "Veuillez sélectionner au moins"+required+" chevaux", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
@@ -238,12 +276,12 @@ public class BetValidation extends Fragment {
|
||||
true,
|
||||
true,
|
||||
new PariCourseDto(shared.selectedCourse.getValue().getId()),
|
||||
selectedHorses.getValue(),
|
||||
order?selectedHorses.getValue():new ArrayList<String>(),
|
||||
selectedHorses.getValue(),
|
||||
order?new ArrayList<String>(): selectedHorses.getValue(),
|
||||
order,
|
||||
"MULTI_4"
|
||||
);
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(), new Observer<Result<Pari>>() {
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(),new Observer<Result<Pari>>() {
|
||||
@Override
|
||||
public void onChanged(Result<Pari> pariResult) {
|
||||
switch (pariResult.status){
|
||||
@@ -254,7 +292,11 @@ public class BetValidation extends Fragment {
|
||||
Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case SUCCESS:
|
||||
printPari();
|
||||
try {
|
||||
printPari(pariResult.data);
|
||||
} catch (WriterException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -266,32 +308,37 @@ public class BetValidation extends Fragment {
|
||||
});
|
||||
|
||||
binding.deleteBtn.setOnClickListener(v->{
|
||||
selectedHorses.setValue(List.of());
|
||||
binding.combination.setText(getString(R.string.combination,""));
|
||||
setupNumberGrid(binding.gridNumbers);
|
||||
_initializeToZero();
|
||||
});
|
||||
}
|
||||
|
||||
public void printPari(){
|
||||
public void printPari(Pari pari) throws WriterException {
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pmu_logo);
|
||||
Bitmap barcode = generateBarcodeBitmap(pari.getNumeroTicket(), 384, 100);
|
||||
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");
|
||||
tspl.append("Bamako").append("\n");
|
||||
tspl.append(shared.selectedCourse.getValue().getNom()).append("\n");
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.parse(shared.selectedCourse.getValue().getDateDepartCourse());
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
|
||||
String formattedDate = dateTime.format(formatter);
|
||||
tspl.append(formattedDate).append("\n");
|
||||
tspl.append("Course ").append(String.valueOf(shared.selectedCourse.getValue().getId())).append("\n");
|
||||
tspl.append(shared.selectedCourse.getValue().getType()).append("\n");
|
||||
tspl.append("--------------------------------\n");
|
||||
boolean isElargie = selectedHorses.getValue().size()>shared.typeOfBet.getValue().getNumberOfHorse();
|
||||
tspl.append(isElargie?pari.getTypePari()+"/Elargie":pari.getTypePari()).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");
|
||||
tspl.append(combinationText).append("\n");
|
||||
tspl.append("COEF: "+String.valueOf(coeff)).append(".0").append("\n");
|
||||
tspl.append("--------------------------------\n");
|
||||
tspl.append("MONTANT: ").append(mise).append(" XOF\n");
|
||||
tspl.append("-------------------------------\n");
|
||||
tspl.append("AGENT: ").append(" 3332\n");
|
||||
tspl.append("DATE: ").append(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss").format(LocalDateTime.now())).append("\n");
|
||||
if (BluetoothUtils.needsBluetoothPermissions()) {
|
||||
if (!BluetoothUtils.hasBluetoothPermission(requireContext())) {
|
||||
// Demande la permission si non accordée
|
||||
@@ -302,37 +349,43 @@ public class BetValidation extends Fragment {
|
||||
|
||||
// 2️⃣ Permission OK, on peut afficher la liste
|
||||
try {
|
||||
Printama.with(getContext()).pintTextBuilder(tspl);
|
||||
Printama.with(getContext()).printTextBuilder(tspl, bitmap, pari.getNumeroTicket(), barcode);
|
||||
_initializeToZero();
|
||||
} catch (SecurityException e) {
|
||||
Toast.makeText(requireContext(),
|
||||
"Permission Bluetooth non accordée", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
|
||||
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){
|
||||
void _initializeToZero(){
|
||||
mise = 0;
|
||||
binding.mise.setText(String.valueOf(mise+" CFA"));
|
||||
selectedHorses.setValue(List.of());
|
||||
binding.combination.setText("");
|
||||
binding.order.setChecked(false);
|
||||
order = false;
|
||||
binding.order.setVisibility(View.GONE);
|
||||
binding.coeff.setText("1");
|
||||
coeff = Integer.parseInt(binding.coeff.getText().toString());
|
||||
setupNumberGrid(binding.gridNumbers);
|
||||
}
|
||||
|
||||
public void calculateMise(int nombreChevauxSelectionnes){
|
||||
if(typeOfBet.getNumberOfHorse() == 1){
|
||||
if(nombreChevauxSelectionnes==1){
|
||||
mise = 300;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 2){
|
||||
mise = 300 + (nombreChevauxSelectionnes - 3)*300;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.toString().toLowerCase().contains("tierce")){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
if(typeOfBet.getNumberOfHorse() == 2){
|
||||
if(nombreChevauxSelectionnes == 2){
|
||||
mise = 400;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 3){
|
||||
mise = 400 + (nombreChevauxSelectionnes - 3)*400;
|
||||
if(nombreChevauxSelectionnes > 2){
|
||||
mise = 400 + (nombreChevauxSelectionnes - 2)*300;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.toString().toLowerCase().contains("quarte")){
|
||||
if(typeOfBet.getNumberOfHorse() == 3){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
mise = 500;
|
||||
}
|
||||
@@ -340,16 +393,47 @@ public class BetValidation extends Fragment {
|
||||
mise = 500 + (nombreChevauxSelectionnes - 3)*500;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.toString().toLowerCase().contains("quinte")){
|
||||
if(nombreChevauxSelectionnes == 3){
|
||||
mise = 1000;
|
||||
if(typeOfBet.getNumberOfHorse() == 4){
|
||||
if(nombreChevauxSelectionnes == 4){
|
||||
mise = 600;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 3){
|
||||
mise = 1000 + (nombreChevauxSelectionnes - 3)*1000;
|
||||
if(nombreChevauxSelectionnes > 4){
|
||||
mise = 600 + (nombreChevauxSelectionnes - 4)*600;
|
||||
}
|
||||
}
|
||||
if(typeOfBet.getNumberOfHorse() == 5){
|
||||
if(nombreChevauxSelectionnes == 5){
|
||||
mise = 1200;
|
||||
}
|
||||
if(nombreChevauxSelectionnes > 5){
|
||||
mise = 1200 + (nombreChevauxSelectionnes - 5)*1200;
|
||||
}
|
||||
}
|
||||
mise = order?mise * coeff * _calculateFactorial(shared.typeOfBet.getValue().getNumberOfHorse()):mise * coeff;
|
||||
}
|
||||
|
||||
Integer _calculateFactorial(int n){
|
||||
int f = 1;
|
||||
if(n == 0){
|
||||
return f;
|
||||
}
|
||||
for(int i = 1; i <=n; i++){
|
||||
f *= i;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
public Bitmap generateBarcodeBitmap(String contents, int width, int height) throws WriterException {
|
||||
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, width, height);
|
||||
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
bmp.setPixel(x, y, bitMatrix.get(x, y) ? android.graphics.Color.BLACK : android.graphics.Color.WHITE);
|
||||
}
|
||||
}
|
||||
return bmp;
|
||||
}
|
||||
public static String generate12Digits() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
|
||||
Reference in New Issue
Block a user