Java tutorial
/* The MIT License (MIT) Copyright (c) [2016] [Sundeepk] Modifications Copyright(C) 2016 Fred Grott(GrottWorkShop) 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.github.shareme.gwsmaterialuikit.library.ccv; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.support.v4.view.GestureDetectorCompat; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.widget.OverScroller; import com.github.shareme.gwsmaterialuikit.library.ccv.domain.CalendarDayEvent; import java.util.Date; import java.util.List; import java.util.Locale; public class CompactCalendarView extends View { private final AnimationHandler animationHandler; private CompactCalendarController compactCalendarController; private GestureDetectorCompat gestureDetector; private boolean shouldScroll = true; public interface CompactCalendarViewListener { public void onDayClick(Date dateClicked); public void onMonthScroll(Date firstDayOfNewMonth); } private final GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { @Override public void onLongPress(MotionEvent e) { } @Override public boolean onSingleTapConfirmed(MotionEvent e) { compactCalendarController.onSingleTapConfirmed(e); invalidate(); return super.onSingleTapConfirmed(e); } @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return true; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if (shouldScroll) { compactCalendarController.onScroll(e1, e2, distanceX, distanceY); invalidate(); } return true; } }; public CompactCalendarView(Context context) { this(context, null); } public CompactCalendarView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CompactCalendarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); compactCalendarController = new CompactCalendarController(new Paint(), new OverScroller(getContext()), new Rect(), attrs, getContext(), Color.argb(255, 233, 84, 81), Color.argb(255, 64, 64, 64), Color.argb(255, 219, 219, 219), VelocityTracker.obtain()); gestureDetector = new GestureDetectorCompat(getContext(), gestureListener); animationHandler = new AnimationHandler(compactCalendarController, this); } /* Use a custom locale for compact calendar. */ public void setLocale(Locale locale) { compactCalendarController.setLocale(locale); invalidate(); } /* Compact calendar will use the locale to determine the abbreviation to use as the day column names. The default is to use the default locale and to abbreviate the day names to one character. Setting this to true will displace the short weekday string provided by java. */ public void setUseThreeLetterAbbreviation(boolean useThreeLetterAbbreviation) { compactCalendarController.setUseWeekDayAbbreviation(useThreeLetterAbbreviation); invalidate(); } public void setCalendarBackgroundColor(final int calenderBackgroundColor) { compactCalendarController.setCalenderBackgroundColor(calenderBackgroundColor); invalidate(); } /* Will draw the indicator for events as a small dot under the day rather than a circle behind the day. */ public void drawSmallIndicatorForEvents(boolean shouldDrawDaysHeader) { compactCalendarController.showSmallIndicator(shouldDrawDaysHeader); } /* Sets the name for each day of the week. No attempt is made to adjust width or text size based on the length of each day name. Works best with 3-4 characters for each day. */ public void setDayColumnNames(String[] dayColumnNames) { compactCalendarController.setDayColumnNames(dayColumnNames); } public void setShouldShowMondayAsFirstDay(boolean shouldShowMondayAsFirstDay) { compactCalendarController.setShouldShowMondayAsFirstDay(shouldShowMondayAsFirstDay); invalidate(); } public void setCurrentSelectedDayBackgroundColor(int currentSelectedDayBackgroundColor) { compactCalendarController.setCurrentSelectedDayBackgroundColor(currentSelectedDayBackgroundColor); invalidate(); } public void setCurrentDayBackgroundColor(int currentDayBackgroundColor) { compactCalendarController.setCurrentDayBackgroundColor(currentDayBackgroundColor); invalidate(); } public int getHeightPerDay() { return compactCalendarController.getHeightPerDay(); } public void setListener(CompactCalendarViewListener listener) { compactCalendarController.setListener(listener); } public Date getFirstDayOfCurrentMonth() { return compactCalendarController.getFirstDayOfCurrentMonth(); } public void setCurrentDate(Date dateTimeMonth) { compactCalendarController.setCurrentDate(dateTimeMonth); invalidate(); } public int getWeekNumberForCurrentMonth() { return compactCalendarController.getWeekNumberForCurrentMonth(); } public void setShouldDrawDaysHeader(boolean shouldDrawDaysHeader) { compactCalendarController.setShouldDrawDaysHeader(shouldDrawDaysHeader); } /** * see {@link #addEvent(CalendarDayEvent, boolean)} when adding single events * or {@link #addEvents(List)} when adding multiple events * @param event */ @Deprecated public void addEvent(CalendarDayEvent event) { addEvent(event, false); } /** * Adds an event to be drawn as an indicator in the calendar. * If adding multiple events see {@link #addEvents(List)}} method. * @param event to be added to the calendar * @param shouldInvalidate true if the view should invalidate */ public void addEvent(CalendarDayEvent event, boolean shouldInvalidate) { compactCalendarController.addEvent(event); if (shouldInvalidate) { invalidate(); } } /** * Adds multiple events to the calendar and invalidates the view once all events are added. */ public void addEvents(List<CalendarDayEvent> events) { compactCalendarController.addEvents(events); invalidate(); } /** * see {@link #removeEvent(CalendarDayEvent, boolean)} when removing single events * or {@link #removeEvents(List)} (java.util.List)} when removing multiple events * @param event */ @Deprecated public void removeEvent(CalendarDayEvent event) { removeEvent(event, false); } /** * Removes an event from the calendar. * If removing multiple events see {@link #removeEvents(List)} * * @param event event to remove from the calendar * @param shouldInvalidate true if the view should invalidate */ public void removeEvent(CalendarDayEvent event, boolean shouldInvalidate) { compactCalendarController.removeEvent(event); if (shouldInvalidate) { invalidate(); } } /** * Adds multiple events to the calendar and invalidates the view once all events are added. */ public void removeEvents(List<CalendarDayEvent> events) { compactCalendarController.removeEvents(events); invalidate(); } /** * Clears all Events from the calendar. */ public void removeAllEvents() { compactCalendarController.removeAllEvents(); } private void checkTargetHeight() { if (compactCalendarController.getTargetHeight() <= 0) { throw new IllegalStateException( "Target height must be set in xml properties in order to expand/collapse CompactCalendar."); } } public void showCalendar() { checkTargetHeight(); animationHandler.openCalendar(); } public void hideCalendar() { checkTargetHeight(); animationHandler.closeCalendar(); } public void showCalendarWithAnimation() { checkTargetHeight(); animationHandler.openCalendarWithAnimation(); } public void hideCalendarWithAnimation() { checkTargetHeight(); animationHandler.closeCalendarWithAnimation(); } public void showNextMonth() { compactCalendarController.showNextMonth(); invalidate(); } public void showPreviousMonth() { compactCalendarController.showPreviousMonth(); invalidate(); } @Override protected void onMeasure(int parentWidth, int parentHeight) { super.onMeasure(parentWidth, parentHeight); int width = MeasureSpec.getSize(parentWidth); int height = MeasureSpec.getSize(parentHeight); if (width > 0 && height > 0) { compactCalendarController.onMeasure(width, height, getPaddingRight(), getPaddingLeft()); } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { compactCalendarController.onDraw(canvas); } @Override public void computeScroll() { super.computeScroll(); if (compactCalendarController.computeScroll()) { invalidate(); } } public void shouldScrollMonth(boolean shouldDisableScroll) { this.shouldScroll = shouldDisableScroll; } public boolean onTouchEvent(MotionEvent event) { compactCalendarController.onTouch(event); invalidate(); // always allow gestureDetector to detect onSingleTap and scroll events return gestureDetector.onTouchEvent(event); } }