android-calendar-card icon indicating copy to clipboard operation
android-calendar-card copied to clipboard

Is it possible to show from Sunday to Monday?

Open huytower opened this issue 8 years ago • 1 comments

Thank @kenumir so much for this best Calendar library.

I custom your calendar and show correctly from Monday to Sunday with respective day.

Now I want show from Sunday to Monday.

  • I'm able to show the Title from Sunday to Monday.
  • But the Day not show respectively with.

I try to set locale or setFirstDayOfWeek but looks not work as I thought.

in CalendarCard.java

 private void updateCells(boolean isRefresh, ArrayList<CardGridItem> o) {
               Calendar cal;
               Integer counter = 0;

        if (mCalendarDateDisplay != null)	cal = (Calendar) mCalendarDateDisplay.clone();
        else	cal = Calendar.getInstance();

        /*
         * Set specified specifiedMonth to refresh if have
         */
        if (isRefresh)  cal.set(Calendar.MONTH, getSpecifiedMonth());
        cal.set(Calendar.DAY_OF_MONTH, 1);

        int daySpacing = getDaySpacing(cal.get(Calendar.DAY_OF_WEEK));

        /*
         DAY IN PREVIOUS MONTH
          */
        if (daySpacing > 0) {
            Calendar mCalPrevMonth = (Calendar)cal.clone();

            mCalPrevMonth.add(Calendar.MONTH, -1);
            mCalPrevMonth.set(Calendar.DAY_OF_MONTH, mCalPrevMonth.getActualMaximum(Calendar.DAY_OF_MONTH) - daySpacing + 1);

            for(int i=0; i<daySpacing; i++) {
                CheckableLayout cell = cells.get(counter);
                cell.setTag(
                        new CardGridItem(mCalPrevMonth.get(Calendar.DAY_OF_MONTH), mCalPrevMonth.get(Calendar.MONTH), mCalPrevMonth.get(Calendar.YEAR))
                                .setEnabled(isValidMonth(mCalPrevMonth))
                                .setDiffMonth(true)
                                .setSaturday(isSaturday(mCalPrevMonth))
                                .setSunday(isSunday(mCalPrevMonth))
                                .setTransferToSpecifiedMonth(true));

                /*
                Need check previous month is out of range or not
                - If Yes, should disable cell
                 - If no, allow select previous and transfer to previous month
                 */
                cell.setEnabled(isValidMonth(mCalPrevMonth));

                (mOnItemRender == null ? mOnItemRenderDefault : mOnItemRender).onRender(cell, (CardGridItem)cell.getTag());
                counter++;
                mCalPrevMonth.add(Calendar.DAY_OF_MONTH, 1);
            }
        }

        /*
        DAY IN CURRENT MONTH
         */
        // Reset first day & last day in Card array for checking & update new items from API
        int firstDay = cal.get(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

        int lastDay = cal.get(Calendar.DAY_OF_MONTH) + 1;

        Calendar mCalCurrentMonth = Calendar.getInstance();
        int currentDay = mCalCurrentMonth.get(Calendar.MONTH) == cal.get(Calendar.MONTH) ?
                mCalCurrentMonth.get(Calendar.DAY_OF_MONTH) :
                cal.get(Calendar.DAY_OF_MONTH);

        for (int i = firstDay; i < lastDay; i++) {

            cal.set(Calendar.DAY_OF_MONTH, i - 1);

            Calendar date = (Calendar) cal.clone();
            date.add(Calendar.DAY_OF_MONTH, 1);

            CheckableLayout cell = cells.get(counter);

            /*
            Change to logic disable from first day to current day to show respective color
            */
            CardGridItem item = new CardGridItem(i, date.get(Calendar.MONTH), date.get(Calendar.YEAR))
                    .setEnabled(isValidDay(date))
                    .setDate(date)
                    .setSaturday(isSaturday(date))
                    .setSunday(isSunday(date));

            cell.setTag(item);

            /*
            Change to logic disable from first day to current day
            - Return true if current month not equal to specified Month
            - Return true if current month equal to specified Month && i > current day of current Month
            - Otherwise return false
             */
            boolean isDiffMonth = mCalCurrentMonth.get(Calendar.MONTH) != getSpecifiedMonth();
            boolean isSameMonth = mCalCurrentMonth.get(Calendar.MONTH) == getSpecifiedMonth();
            boolean isSameYear = mCalCurrentMonth.get(Calendar.YEAR) == getSpecifiedYear();
            cell.setEnabled(isSameYear
                    && (isDiffMonth || (isSameMonth && i > currentDay)));
            cell.setVisibility(View.VISIBLE);

            (mOnItemRender == null ? mOnItemRenderDefault : mOnItemRender).onRender(cell, (CardGridItem) cell.getTag());
            counter++;
        }

        /*
        DAY IN NEXT MONTH
         */
        if (mCalendarDateDisplay != null)	cal = (Calendar) mCalendarDateDisplay.clone();
        else	cal = Calendar.getInstance();

        Calendar mCalNextMonth = (Calendar)cal.clone();

        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

        daySpacing = getDaySpacingEnd(cal.get(Calendar.DAY_OF_WEEK));

        if (daySpacing > 0) {

            mCalNextMonth.add(Calendar.MONTH, 1);
            mCalNextMonth.set(Calendar.DAY_OF_MONTH, 1);

            for(int i=0; i<daySpacing; i++) {
                CheckableLayout cell = cells.get(counter);

                cell.setTag(new CardGridItem(i + 1, mCalNextMonth.get(Calendar.MONTH), mCalNextMonth.get(Calendar.YEAR))
                        .setEnabled(true)
                        .setDiffMonth(true)
                        .setSaturday(isSaturday(mCalNextMonth))
                        .setSunday(isSunday(mCalNextMonth))
                        .setTransferToSpecifiedMonth(true));
                cell.setEnabled(true);
                cell.setVisibility(View.VISIBLE);

                (mOnItemRender == null ? mOnItemRenderDefault : mOnItemRender).onRender(cell, (CardGridItem)cell.getTag());

                counter++;
                mCalNextMonth.add(Calendar.DAY_OF_MONTH, 1);
            }
        }

        if (counter < cells.size()) {
            for(int i=counter; i<cells.size(); i++) {
                cells.get(i).setVisibility(View.GONE);
            }
        }
    }`

Therefore I post this enhancement,
Please help me detail how to do this?

Thank you,

huytower avatar Apr 04 '17 06:04 huytower

I did not foresee such a situation, this will require more code intervention. The library was supposed to be simple. There are more things missing than this :(

It is necessary to rebuild calculations - loop generating data.

Now I do not have time to do it. Maybe for some time.

kenumir avatar Apr 04 '17 06:04 kenumir