158 lines
5.6 KiB
Java
158 lines
5.6 KiB
Java
package com.example.quiz;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.app.DatePickerDialog;
|
|
import android.os.Bundle;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.fragment.app.Fragment;
|
|
import androidx.fragment.app.FragmentManager;
|
|
import androidx.lifecycle.LifecycleOwner;
|
|
import androidx.lifecycle.Observer;
|
|
import androidx.lifecycle.ViewModelProvider;
|
|
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.Toast;
|
|
|
|
import com.example.quiz.databinding.FragmentSoldBinding;
|
|
import com.example.quiz.utils.LoaderDialog;
|
|
import com.example.quiz.utils.MessageDialog;
|
|
import com.example.quiz.utils.Result;
|
|
import com.example.quiz.utils.SharedPrefsHelper;
|
|
import com.example.quiz.viewModel.LogsViewModel;
|
|
import com.example.quiz.viewModel.PariViewModel;
|
|
import com.google.android.material.appbar.MaterialToolbar;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import dagger.hilt.android.AndroidEntryPoint;
|
|
|
|
/**
|
|
* A simple {@link Fragment} subclass.
|
|
* Use the {@link Sold#newInstance} factory method to
|
|
* create an instance of this fragment.
|
|
*/
|
|
@AndroidEntryPoint
|
|
public class Sold extends Fragment {
|
|
|
|
FragmentSoldBinding binding;
|
|
|
|
LoaderDialog dialog;
|
|
|
|
PariViewModel pariViewModel;
|
|
SharedPrefsHelper prefsHelper;
|
|
LogsViewModel logsViewModel;
|
|
|
|
public Sold() {
|
|
// Required empty public constructor
|
|
}
|
|
|
|
// TODO: Rename and change types and number of parameters
|
|
public static Sold newInstance() {
|
|
Sold fragment = new Sold();
|
|
Bundle args = new Bundle();
|
|
fragment.setArguments(args);
|
|
return fragment;
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
prefsHelper = SharedPrefsHelper.getInstance(getContext());
|
|
AppCompatActivity activity = (AppCompatActivity) getActivity();
|
|
if(activity != null){
|
|
MaterialToolbar toolbar = activity.findViewById(R.id.toolbar);
|
|
activity.setSupportActionBar(toolbar);
|
|
if(activity.getSupportActionBar() != null){
|
|
activity.getSupportActionBar().setTitle("Soldes");
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
// Inflate the layout for this fragment
|
|
binding = FragmentSoldBinding.inflate(inflater, container, false);
|
|
dialog = new LoaderDialog(getContext());
|
|
logsViewModel = new ViewModelProvider(this).get(LogsViewModel.class);
|
|
return binding.getRoot();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
super.onViewCreated(view, savedInstanceState);
|
|
pariViewModel = new ViewModelProvider(this).get(PariViewModel.class);
|
|
binding.btnByCourse.setOnClickListener(v -> {
|
|
FragmentManager fragmentManager = getParentFragmentManager();
|
|
SoldByCourse soldByCourse = SoldByCourse.newInstance();
|
|
fragmentManager.beginTransaction()
|
|
.replace(R.id.nav_host_fragment_content_main, soldByCourse)
|
|
.addToBackStack(null)
|
|
.commit();
|
|
});
|
|
|
|
binding.btnByDay.setOnClickListener(v->{
|
|
_showCalendar();
|
|
});
|
|
}
|
|
|
|
|
|
String _reformatDateForDate(int num){
|
|
if(num<10){
|
|
return "0"+num;
|
|
}
|
|
return String.valueOf(num);
|
|
}
|
|
void _showCalendar(){
|
|
Calendar calendar = Calendar.getInstance();
|
|
DatePickerDialog datePickerDialog = new DatePickerDialog(
|
|
getContext(),
|
|
(view, year, month, dayOfMonth) -> {
|
|
String date = year + "-" + _reformatDateForDate(month + 1) + "-" + _reformatDateForDate(dayOfMonth);
|
|
pariViewModel.getSoldeByDay(prefsHelper.get("id"), date).observe(getViewLifecycleOwner(), new Observer<Result<Double>>() {
|
|
@Override
|
|
public void onChanged(Result<Double> doubleResult) {
|
|
switch (doubleResult.status){
|
|
case LOADING:{
|
|
dialog.show("Solde du jour");
|
|
break;
|
|
}
|
|
case ERROR:{
|
|
dialog.dismiss();
|
|
MessageDialog.showError(getContext(), doubleResult.message);
|
|
break;
|
|
}
|
|
case SUCCESS:{
|
|
dialog.dismiss();
|
|
_showSold(doubleResult.data);
|
|
logsViewModel.insertLog(prefsHelper.get("id"), "SOLDE JOUR", "Solde du "+date, System.currentTimeMillis());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
},
|
|
calendar.get(Calendar.YEAR),
|
|
calendar.get(Calendar.MONTH),
|
|
calendar.get(Calendar.DAY_OF_MONTH)
|
|
);
|
|
|
|
datePickerDialog.show();
|
|
}
|
|
|
|
void _showSold(double solde){
|
|
new AlertDialog.Builder(getContext())
|
|
.setTitle("Solde")
|
|
.setMessage("Solde la course "+solde)
|
|
.setPositiveButton("Ok", (dialog, which)->{
|
|
dialog.dismiss();
|
|
}).show();
|
|
}
|
|
} |