com.ncatz.yeray.calendarview.CalendarDecorator.SelectedDayDecorator.java Source code

Java tutorial

Introduction

Here is the source code for com.ncatz.yeray.calendarview.CalendarDecorator.SelectedDayDecorator.java

Source

/*
   Copyright (c) 2017 Yeray Ruiz Jurez, https://github.com/yeray697
    
   Permission is hereby granted, free of charge, to any person obtaining
   a copy of this software and associated documentation files (the
   "Software"), to deal in the Software without restriction, including
   without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense, and/or sell copies of the Software, and to
   permit persons to whom the Software is furnished to do so, subject to
   the following conditions:
    
   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.
    
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.ncatz.yeray.calendarview.CalendarDecorator;

import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.ncatz.yeray.calendarview.R;

import java.util.Date;

/**
 * Decorate a day by making the text big and bold and circled if it is the selected day
 * @author yeray697
 */
public class SelectedDayDecorator implements DayViewDecorator {

    private CalendarDay date;
    private final Drawable drawable;

    public SelectedDayDecorator(Activity context) {
        date = CalendarDay.today();
        drawable = ContextCompat.getDrawable(context, R.drawable.circle_selected_day);
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return date != null && day.equals(date);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.addSpan(new StyleSpan(Typeface.BOLD));
        view.addSpan(new RelativeSizeSpan(1.4f));
        view.setSelectionDrawable(drawable);
    }

    /**
     * Set selected date
     */
    public void setDate(Date date) {
        this.date = CalendarDay.from(date);
    }
}