Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.2 kB
3
Indexable
Never
package com.example.managerstaff.fragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.managerstaff.R;
import com.example.managerstaff.adapter.CalendarAdapter;
import com.example.managerstaff.api.ApiService;
import com.example.managerstaff.databinding.FragmentCalendarBinding;
import com.example.managerstaff.databinding.FragmentHomeBinding;
import com.example.managerstaff.models.Calendar;
import com.example.managerstaff.models.User;
import com.example.managerstaff.models.responses.ListCalendarResponse;
import com.example.managerstaff.models.responses.UserResponse;
import com.example.managerstaff.supports.Support;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CalendarFragment extends Fragment {

    FragmentCalendarBinding binding;
    private int IdUser;
    private User user;
    private CalendarAdapter adapter;
    private List<Calendar> listCalendars;
    private String day;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentCalendarBinding.inflate(inflater, container, false);
        user=new User();
        IdUser=getArguments().getInt("id_user");
        listCalendars=new ArrayList<>();
        day=Support.geDayNow();
        binding.txtDayCalendar.setText(Support.defineTime(Support.changeReverDateTime(day,false)));
        adapter=new CalendarAdapter(requireActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
        binding.rcvListCalendar.setLayoutManager(linearLayoutManager);
        clickCallApiGetUserDetail();
        binding.datePicker1.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                month++;
                day = year+"-"+month+"-"+dayOfMonth;
                binding.txtDayCalendar.setText("Ngày "+((dayOfMonth<10)?"0":"")+dayOfMonth+" Tháng "+((month<10)?"0":"")+month);
                clickCallApiGetListCalendar(user.getPart().getIdPart());
            }
        });

        return binding.getRoot();
    }

    private void clickCallApiGetUserDetail() {
        ApiService.apiService.getUserDetail(IdUser).enqueue(new Callback<UserResponse>() {
            @Override
            public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                UserResponse userResponse = response.body();
                if (userResponse != null) {
                    if(userResponse.getCode()==200){
                        user=userResponse.getUser();
                        clickCallApiGetListCalendar(user.getPart().getIdPart());
                    }else{
                        Toast.makeText(getContext(), getString(R.string.system_error), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getContext(), getString(R.string.system_error), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserResponse> call, Throwable t) {
                Toast.makeText(getContext(), getString(R.string.system_error), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void clickCallApiGetListCalendar(int idPart) {
        ApiService.apiService.getCalendar(idPart,day).enqueue(new Callback<ListCalendarResponse>() {
            @Override
            public void onResponse(Call<ListCalendarResponse> call, Response<ListCalendarResponse> response) {
                ListCalendarResponse listCalendarResponse = response.body();
                if (listCalendarResponse != null) {
                    if(listCalendarResponse.getCode()==200){
                       listCalendars=listCalendarResponse.getListCalendars();
                       adapter.setData(listCalendars);
                       binding.rcvListCalendar.setAdapter(adapter);
                    }else{
                        Toast.makeText(getContext(), getString(R.string.system_error), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getContext(), getString(R.string.system_error), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ListCalendarResponse> call, Throwable t) {
                Toast.makeText(getContext(), getString(R.string.system_error), Toast.LENGTH_SHORT).show();
            }
        });
    }


}