238 lines
8.2 KiB
Java
238 lines
8.2 KiB
Java
package com.example.quiz;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import android.util.Log;
|
|
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;
|
|
|
|
/**
|
|
* A simple {@link Fragment} subclass.
|
|
* Use the {@link BetValidation#newInstance} factory method to
|
|
* create an instance of this fragment.
|
|
*/
|
|
|
|
@AndroidEntryPoint
|
|
public class BetValidation extends Fragment {
|
|
|
|
FragmentBetValidationBinding binding;
|
|
|
|
SharedViewModel shared;
|
|
|
|
BetViewModel viewModel;
|
|
|
|
|
|
private HPRTPrinterUtil printer;
|
|
|
|
private TypeOfBet typeOfBet;
|
|
|
|
private Reunion reunion;
|
|
|
|
private List<Horse> selectedHorses = new ArrayList<Horse>();
|
|
|
|
private List<Horse> totalHorses;
|
|
|
|
|
|
|
|
|
|
public BetValidation() {
|
|
// Required empty public constructor
|
|
}
|
|
|
|
public static BetValidation newInstance() {
|
|
BetValidation fragment = new BetValidation();
|
|
Bundle args = new Bundle();
|
|
fragment.setArguments(args);
|
|
return fragment;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
binding = FragmentBetValidationBinding.inflate(inflater, container, false);
|
|
binding.combination.setText(getString(R.string.combination,""));
|
|
return binding.getRoot();
|
|
}
|
|
|
|
private void setupNumberGrid(GridLayout grid) {
|
|
binding.gridNumbers.removeAllViews();
|
|
int columns = 4;
|
|
|
|
grid.setColumnCount(columns);
|
|
|
|
if(totalHorses != null){
|
|
totalHorses.stream()
|
|
.map(this::createNumberItem)
|
|
.forEach(grid::addView);
|
|
}
|
|
}
|
|
|
|
private TextView createNumberItem(Horse horse) {
|
|
TextView textView = new TextView(requireContext());
|
|
textView.setText(String.valueOf(horse.getNumber()));
|
|
textView.setTextColor(getResources().getColor(R.color.white));
|
|
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
|
|
params.setMargins(10, 10, 10, 10);
|
|
textView.setLayoutParams(params);
|
|
textView.setTextSize(21);
|
|
textView.setWidth(130);
|
|
textView.setHeight(130);
|
|
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 -> String.valueOf(h.getNumber()))
|
|
.collect(Collectors.joining("-"));
|
|
binding.combination.setText(getString(R.string.combination, combinationText));
|
|
});
|
|
|
|
return textView;
|
|
}
|
|
|
|
@Override
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
shared = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
|
|
viewModel = new ViewModelProvider(this).get(BetViewModel.class);
|
|
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;
|
|
});
|
|
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);
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onDestroyView() {
|
|
super.onDestroyView();
|
|
binding = null;
|
|
}
|
|
} |