How can I reset the calendarchangeobserver in singlerowcalendar library?

2020-03-18 android android-recyclerview android-calendar

I'm using a library called singlerowcalendar in my app. My app contains a toggle button that will change the calendar mode form showing days of month to showing months of year.

days of month view

months of year view

The thing is I'm including the calendar setup code in the toggle change listener, and as a consequence the app keeps piling up calendar selection change listeners, and now I get the calendar change callback to fire multiple times. Is there a way to keep only one calendar change listener and removing the others.

here is the code of the toggle button where I call setCalendar();

toggle.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
        @Override
        public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
            if (isChecked && checkedId == R.id.btn_day) {
                selectedTimeFrame = "day";
                tvChartTitleDay.setVisibility(View.VISIBLE);
                tvChartTitleMonth.setVisibility(View.GONE);
                Log.d("pongo2", "day selecteeeeed");
            }
            if (isChecked && checkedId == R.id.btn_month) {
                selectedTimeFrame = "month";
                tvChartTitleDay.setVisibility(View.GONE);
                tvChartTitleMonth.setVisibility(View.VISIBLE);
                Log.d("pongo2", "month selecteeeeed");
            }
            setCalendar();
        }
    });

Here is the calendar setup code.

private void setCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    ArrayList<Date> list = new ArrayList<>();

    if(selectedTimeFrame.equals("day")) {
        calendar.add(Calendar.DAY_OF_MONTH , -700);
        for (int i = 0; i < 30; i++) {
            list.add(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH , -1);
        }
        tvMonth.setVisibility(View.VISIBLE);
        tvMonth.setText(DateUtils.INSTANCE.getMonth3LettersName(calendar.getTime()));
        tvYear.setText(DateUtils.INSTANCE.getYear(calendar.getTime()));
    } else {
        calendar.add(Calendar.MONTH , -24);
        for (int i = 0; i < 30; i++) {
            list.add(calendar.getTime());
            calendar.add(Calendar.MONTH , -1);
        }
        tvMonth.setVisibility(View.GONE);
        tvYear.setText(DateUtils.INSTANCE.getYear(calendar.getTime()));
    }

    myCalendar.setDates(list);
    myCalendar.init();
    myCalendar.select(5);
}

calendar change observer is here:

 private void initCalendar() {
    myCalendarChangesObserver = new CalendarChangesObserver() {
        @Override
        public void whenWeekMonthYearChanged(String s, String s1, String s2, String s3, Date date) {

        }

        @Override
        public void whenSelectionChanged(boolean b, int i, Date date) {
            if (b) {
                tvMonth.setText(DateUtils.INSTANCE.getMonth3LettersName(date));
                tvYear.setText(DateUtils.INSTANCE.getYear(date));
                Log.d("pongo", date.toString());
                loadNewData();
            }
        }

        @Override
        public void whenCalendarScrolled(int i, int i1) {

        }

        @Override
        public void whenSelectionRestored() {
        }

        @Override
        public void whenSelectionRefreshed() {
        }
    };

    myCalendar.setCalendarChangesObserver(myCalendarChangesObserver);
}

Answers

Related