diff --git a/app/build.gradle b/app/build.gradle index afd4213..78e765a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,6 +30,7 @@ dependencies { implementation project(':collapsiblecalendarview2') implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2' + implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1' testImplementation 'junit:junit:4.12' diff --git a/app/src/main/java/com/shrikanthravi/collapsiblecalendarview/MainActivity.java b/app/src/main/java/com/shrikanthravi/collapsiblecalendarview/MainActivity.java index 1c16cd5..b630aaa 100644 --- a/app/src/main/java/com/shrikanthravi/collapsiblecalendarview/MainActivity.java +++ b/app/src/main/java/com/shrikanthravi/collapsiblecalendarview/MainActivity.java @@ -3,11 +3,13 @@ import android.graphics.Color; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; +import android.util.Log; import android.view.View; import com.shrikanthravi.collapsiblecalendarview.widget.CollapsibleCalendar; -import java.util.Calendar; +import org.threeten.bp.LocalDate; + import java.util.GregorianCalendar; @@ -21,12 +23,12 @@ protected void onCreate(Bundle savedInstanceState) { getWindow().setStatusBarColor(getResources().getColor(R.color.google_red)); CollapsibleCalendar collapsibleCalendar = findViewById(R.id.collapsibleCalendarView); - Calendar today=new GregorianCalendar(); - collapsibleCalendar.addEventTag(today.get(Calendar.YEAR),today.get(Calendar.MONTH),today.get(Calendar.DAY_OF_MONTH)); - today.add(Calendar.DATE,1); - collapsibleCalendar.addEventTag(today.get(Calendar.YEAR),today.get(Calendar.MONTH),today.get(Calendar.DAY_OF_MONTH),Color.BLUE); + LocalDate today = LocalDate.now(); + collapsibleCalendar.addEventTag(today); + LocalDate tomorrow = today.plusDays(1); + collapsibleCalendar.addEventTag(tomorrow, Color.BLUE); - System.out.println("Testing date "+collapsibleCalendar.getSelectedDay().getDay()+"/"+collapsibleCalendar.getSelectedDay().getMonth()+"/"+collapsibleCalendar.getSelectedDay().getYear()); + Log.d("Testing date ", collapsibleCalendar.getSelectedDay().toString()); collapsibleCalendar.setCalendarListener(new CollapsibleCalendar.CalendarListener() { @Override public void onDaySelect() { diff --git a/collapsiblecalendarview2/build.gradle b/collapsiblecalendarview2/build.gradle index e1af48b..f3284bc 100644 --- a/collapsiblecalendarview2/build.gradle +++ b/collapsiblecalendarview2/build.gradle @@ -34,6 +34,7 @@ android { dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1' testImplementation 'junit:junit:4.12' diff --git a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/CalendarAdapter.java b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/CalendarAdapter.java index 45303fa..b742eed 100644 --- a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/CalendarAdapter.java +++ b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/CalendarAdapter.java @@ -1,7 +1,6 @@ package com.shrikanthravi.collapsiblecalendarview.data; import android.content.Context; -import android.graphics.Color; import android.graphics.PorterDuff; import android.view.LayoutInflater; import android.view.View; @@ -11,27 +10,31 @@ import com.shrikanthravi.collapsiblecalendarview.R; import com.shrikanthravi.collapsiblecalendarview.widget.UICalendar; +import org.threeten.bp.DayOfWeek; +import org.threeten.bp.LocalDate; + import java.util.ArrayList; -import java.util.Calendar; import java.util.List; +import static org.threeten.bp.Month.DECEMBER; +import static org.threeten.bp.Month.JANUARY; + /** * Created by shrikanthravi on 06/03/18. */ public class CalendarAdapter { - private int mFirstDayOfWeek = 0; - private Calendar mCal; + private DayOfWeek mFirstDayOfWeek = DayOfWeek.MONDAY; + private LocalDate mCal; private LayoutInflater mInflater; - private int mEventDotSize= UICalendar.EVENT_DOT_BIG; + private int mEventDotSize = UICalendar.EVENT_DOT_BIG; - List mItemList = new ArrayList<>(); - List mViewList = new ArrayList<>(); - List mEventList = new ArrayList<>(); + private List mItemList = new ArrayList<>(); + private List mViewList = new ArrayList<>(); + private List mEventList = new ArrayList<>(); - public CalendarAdapter(Context context, Calendar cal) { - this.mCal = (Calendar) cal.clone(); - this.mCal.set(Calendar.DAY_OF_MONTH, 1); + public CalendarAdapter(Context context) { + this.mCal = LocalDate.now().withDayOfMonth(1); mInflater = LayoutInflater.from(context); refresh(); @@ -42,7 +45,7 @@ public int getCount() { return mItemList.size(); } - public Day getItem(int position) { + public LocalDate getItem(int position) { return mItemList.get(position); } @@ -50,14 +53,27 @@ public View getView(final int position) { return mViewList.get(position); } - public void setFirstDayOfWeek(int firstDayOfWeek) { + public void nextMonth() { + mCal = mCal.plusMonths(1); + } + + public void previousMonth() { + mCal = mCal.minusMonths(1); + } + + public void setDate(LocalDate date) { + mCal = date; + } + + public void setFirstDayOfWeek(DayOfWeek firstDayOfWeek) { mFirstDayOfWeek = firstDayOfWeek; } + public void setEventDotSize(int eventDotSize) { mEventDotSize = eventDotSize; } - public Calendar getCalendar() { + public LocalDate getCalendar() { return mCal; } @@ -71,42 +87,41 @@ public void refresh() { mViewList.clear(); // set calendar - int year = mCal.get(Calendar.YEAR); - int month = mCal.get(Calendar.MONTH); + int year = mCal.getYear(); + int month = mCal.getMonthValue(); - mCal.set(year, month, 1); + mCal = LocalDate.of(year, month, 1); - int lastDayOfMonth = mCal.getActualMaximum(Calendar.DAY_OF_MONTH); - int firstDayOfWeek = mCal.get(Calendar.DAY_OF_WEEK) - 1; + int lastDayOfMonth = mCal.lengthOfMonth(); + DayOfWeek firstDayOfWeek = mCal.getDayOfWeek(); // generate day list - int offset = 0 - (firstDayOfWeek - mFirstDayOfWeek) + 1; - int length = (int) Math.ceil((float) (lastDayOfMonth - offset + 1) / 7) * 7; + int offset = 0 - (firstDayOfWeek.getValue() - mFirstDayOfWeek.getValue()); + if (offset > 0) offset += -7; + int length = (int) Math.ceil((float) (lastDayOfMonth - offset) / 7) * 7; for (int i = offset; i < length + offset; i++) { int numYear; int numMonth; int numDay; - Calendar tempCal = Calendar.getInstance(); if (i <= 0) { // prev month - if (month == 0) { + if (month == JANUARY.getValue()) { numYear = year - 1; - numMonth = 11; + numMonth = DECEMBER.getValue(); } else { numYear = year; numMonth = month - 1; } - tempCal.set(numYear, numMonth, 1); - numDay = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH) + i; + LocalDate tempCal = LocalDate.of(numYear, numMonth, 1); + numDay = tempCal.lengthOfMonth() + i; } else if (i > lastDayOfMonth) { // next month - if (month == 11) { + if (month == DECEMBER.getValue()) { numYear = year + 1; - numMonth = 0; + numMonth = JANUARY.getValue(); } else { numYear = year; - numMonth = month + 1; + numMonth = month; } - tempCal.set(numYear, numMonth, 1); numDay = i - lastDayOfMonth; } else { numYear = year; @@ -114,28 +129,27 @@ public void refresh() { numDay = i; } - Day day = new Day(numYear, numMonth, numDay); + LocalDate day = LocalDate.of(numYear, numMonth, numDay); View view; - if(mEventDotSize==UICalendar.EVENT_DOT_SMALL) - view = mInflater.inflate(R.layout.day_layout_small, null); - else + if (mEventDotSize == UICalendar.EVENT_DOT_SMALL) { + view = mInflater.inflate(R.layout.day_layout_small, null); + } else { view = mInflater.inflate(R.layout.day_layout, null); + } - TextView txtDay = (TextView) view.findViewById(R.id.txt_day); - ImageView imgEventTag = (ImageView) view.findViewById(R.id.img_event_tag); + TextView txtDay = view.findViewById(R.id.txt_day); + ImageView imgEventTag = view.findViewById(R.id.img_event_tag); - txtDay.setText(String.valueOf(day.getDay())); - if (day.getMonth() != mCal.get(Calendar.MONTH)) { + txtDay.setText(String.valueOf(day.getDayOfMonth())); + if (day.getMonth() != mCal.getMonth()) { txtDay.setAlpha(0.3f); } for (int j = 0; j < mEventList.size(); j++) { Event event = mEventList.get(j); - if (day.getYear() == event.getYear() - && day.getMonth() == event.getMonth() - && day.getDay() == event.getDay()) { + if (day.equals(event.getDate())) { imgEventTag.setVisibility(View.VISIBLE); - imgEventTag.setColorFilter(event.getColor(),PorterDuff.Mode.SRC_ATOP); + imgEventTag.setColorFilter(event.getColor(), PorterDuff.Mode.SRC_ATOP); } } diff --git a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Day.java b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Day.java deleted file mode 100644 index 0c2b1a5..0000000 --- a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Day.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.shrikanthravi.collapsiblecalendarview.data; - -import android.content.Intent; -import android.os.Parcel; -import android.os.Parcelable; - -/** - * Created by shrikanthravi on 06/03/18. - */ - -public class Day implements Parcelable{ - - private int mYear; - private int mMonth; - private int mDay; - - public Day(int year, int month, int day){ - this.mYear = year; - this.mMonth = month; - this.mDay = day; - } - - public int getMonth(){ - return mMonth; - } - - public int getYear(){ - return mYear; - } - - public int getDay(){ - return mDay; - } - - - - public Day(Parcel in){ - int[] data = new int[3]; - in.readIntArray(data); - this.mYear = data[0]; - this.mMonth = data[1]; - this.mYear = data[2]; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeIntArray(new int[] {this.mYear, - this.mMonth, - this.mDay}); - } - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public Day createFromParcel(Parcel in) { - return new Day(in); - } - - public Day[] newArray(int size) { - return new Day[size]; - } - }; - - -} diff --git a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Event.java b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Event.java index 4ace985..51970bf 100644 --- a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Event.java +++ b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/data/Event.java @@ -1,38 +1,27 @@ package com.shrikanthravi.collapsiblecalendarview.data; +import org.threeten.bp.LocalDate; + /** * Created by shrikanthravi on 06/03/18. */ public class Event { - private int mYear; - private int mMonth; - private int mDay; - private int mColor; - - public Event(int year, int month, int day){ - this.mYear = year; - this.mMonth = month; - this.mDay = day; - } + private final LocalDate date; - public Event(int year, int month, int day, int color){ - this.mYear = year; - this.mMonth = month; - this.mDay = day; - this.mColor=color; - } + private int mColor; - public int getMonth(){ - return mMonth; + public Event(LocalDate date) { + this.date = date; } - public int getYear(){ - return mYear; + public Event(LocalDate date, int color) { + this.date = date; + this.mColor = color; } - public int getDay(){ - return mDay; + public LocalDate getDate() { + return date; } public int getColor() { diff --git a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/CollapsibleCalendar.java b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/CollapsibleCalendar.java index 0c154c7..3df0a47 100644 --- a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/CollapsibleCalendar.java +++ b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/CollapsibleCalendar.java @@ -9,8 +9,6 @@ import android.graphics.Color; import android.os.Handler; import android.util.AttributeSet; -import android.view.GestureDetector; -import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -20,27 +18,27 @@ import android.widget.TableRow; import android.widget.TextView; - import com.shrikanthravi.collapsiblecalendarview.R; import com.shrikanthravi.collapsiblecalendarview.data.CalendarAdapter; -import com.shrikanthravi.collapsiblecalendarview.data.Day; import com.shrikanthravi.collapsiblecalendarview.data.Event; import com.shrikanthravi.collapsiblecalendarview.listener.OnSwipeTouchListener; import com.shrikanthravi.collapsiblecalendarview.view.ExpandIconView; -import java.text.SimpleDateFormat; -import java.util.Calendar; +import org.threeten.bp.LocalDate; +import org.threeten.bp.format.DateTimeFormatter; + +import androidx.annotation.NonNull; public class CollapsibleCalendar extends UICalendar { - private CalendarAdapter mAdapter; + private CalendarAdapter mAdapter; private CalendarListener mListener; - private boolean expanded=false; + private boolean expanded = false; private int mInitHeight = 0; - private Handler mHandler = new Handler(); + private Handler mHandler = new Handler(); private boolean mIsWaitingForUpdate = false; private int mCurrentWeekIndex; @@ -62,13 +60,10 @@ protected void init(Context context) { super.init(context); - int size=getEventDotSize(); - Calendar cal = Calendar.getInstance(); - CalendarAdapter adapter = new CalendarAdapter(context, cal); - adapter.setEventDotSize(getEventDotSize()); - setAdapter(adapter); - - + int size = getEventDotSize(); + CalendarAdapter adapter = new CalendarAdapter(context); + adapter.setEventDotSize(getEventDotSize()); + setAdapter(adapter); // bind events @@ -101,16 +96,15 @@ public void onClick(View v) { } }); - expandIconView.setState(ExpandIconView.MORE,true); + expandIconView.setState(ExpandIconView.MORE, true); expandIconView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - if(expanded){ + if (expanded) { collapse(400); - } - else{ + } else { expand(400); } } @@ -124,7 +118,6 @@ public void run() { }); - } private OnSwipeTouchListener getSwipeTouchListener() { @@ -133,20 +126,23 @@ public void onSwipeTop() { collapse(400); } + public void onSwipeLeft() { - if (getState() == STATE_COLLAPSED) + if (getState() == STATE_COLLAPSED) { nextWeek(); - else if (getState() == STATE_EXPANDED) + } else if (getState() == STATE_EXPANDED) { nextMonth(); + } } + public void onSwipeRight() { if (getState() == STATE_COLLAPSED) { prevWeek(); - } - else if (getState() == STATE_EXPANDED) { + } else if (getState() == STATE_EXPANDED) { prevMonth(); } } + public void onSwipeBottom() { expand(400); } @@ -187,9 +183,9 @@ protected void redraw() { // redraw all views of day if (mAdapter != null) { for (int i = 0; i < mAdapter.getCount(); i++) { - Day day = mAdapter.getItem(i); + LocalDate day = mAdapter.getItem(i); View view = mAdapter.getView(i); - TextView txtDay = (TextView) view.findViewById(R.id.txt_day); + TextView txtDay = view.findViewById(R.id.txt_day); txtDay.setBackgroundColor(Color.TRANSPARENT); txtDay.setTextColor(getTextColor()); @@ -215,9 +211,9 @@ protected void reload() { mAdapter.refresh(); // reset UI - SimpleDateFormat dateFormat = new SimpleDateFormat("MMM yyyy"); - dateFormat.setTimeZone(mAdapter.getCalendar().getTimeZone()); - mTxtTitle.setText(dateFormat.format(mAdapter.getCalendar().getTime())); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MMMM YYYY"); + String formattedDate = dateFormat.format(mAdapter.getCalendar()); + mTxtTitle.setText(formattedDate); mTableHead.removeAllViews(); mTableBody.removeAllViews(); @@ -240,7 +236,7 @@ protected void reload() { for (int i = 0; i < 7; i++) { View view = mInflater.inflate(R.layout.layout_day_of_week, null); TextView txtDayOfWeek = (TextView) view.findViewById(R.id.txt_day_of_week); - txtDayOfWeek.setText(dayOfWeekIds[(i + getFirstDayOfWeek()) % 7]); + txtDayOfWeek.setText(dayOfWeekIds[(i + getFirstDayOfWeek().getValue()) % 7]); view.setLayoutParams(new TableRow.LayoutParams( 0, ViewGroup.LayoutParams.WRAP_CONTENT, @@ -296,17 +292,18 @@ private int getSuitableRowIndex() { } } - public void onItemClicked(View view, Day day) { + public void onItemClicked(View view, LocalDate day) { select(day); - Calendar cal = mAdapter.getCalendar(); + LocalDate cal = mAdapter.getCalendar(); int newYear = day.getYear(); - int newMonth = day.getMonth(); - int oldYear = cal.get(Calendar.YEAR); - int oldMonth = cal.get(Calendar.MONTH); + int newMonth = day.getMonthValue() - 1; + int oldYear = cal.getYear(); + int oldMonth = cal.getMonthValue(); if (newMonth != oldMonth) { - cal.set(day.getYear(), day.getMonth(), 1); + LocalDate d = day.withDayOfMonth(1); + mAdapter.setDate(d); if (newYear > oldYear || newMonth > oldMonth) { mCurrentWeekIndex = 0; @@ -336,25 +333,20 @@ public void setAdapter(CalendarAdapter adapter) { mCurrentWeekIndex = getSuitableRowIndex(); } - public void addEventTag(int numYear, int numMonth, int numDay) { - mAdapter.addEvent(new Event(numYear, numMonth, numDay,getEventColor())); + public void addEventTag(LocalDate date) { + mAdapter.addEvent(new Event(date, getEventColor())); reload(); } - public void addEventTag(int numYear, int numMonth, int numDay,int color) { - mAdapter.addEvent(new Event(numYear, numMonth, numDay,color)); + public void addEventTag(LocalDate date, int color) { + mAdapter.addEvent(new Event(date, color)); reload(); } public void prevMonth() { - Calendar cal = mAdapter.getCalendar(); - if (cal.get(Calendar.MONTH) == cal.getActualMinimum(Calendar.MONTH)) { - cal.set((cal.get(Calendar.YEAR) - 1), cal.getActualMaximum(Calendar.MONTH), 1); - } else { - cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); - } + mAdapter.previousMonth(); reload(); if (mListener != null) { mListener.onMonthChange(); @@ -362,12 +354,7 @@ public void prevMonth() { } public void nextMonth() { - Calendar cal = mAdapter.getCalendar(); - if (cal.get(Calendar.MONTH) == cal.getActualMaximum(Calendar.MONTH)) { - cal.set((cal.get(Calendar.YEAR) + 1), cal.getActualMinimum(Calendar.MONTH), 1); - } else { - cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); - } + mAdapter.nextMonth(); reload(); if (mListener != null) { mListener.onMonthChange(); @@ -394,51 +381,25 @@ public void nextWeek() { } } - public int getYear() { - return mAdapter.getCalendar().get(Calendar.YEAR); - } - - public int getMonth() { - return mAdapter.getCalendar().get(Calendar.MONTH); - } - - public Day getSelectedDay() { - if (getSelectedItem()==null){ - Calendar cal = Calendar.getInstance(); - int day = cal.get(Calendar.DAY_OF_MONTH); - int month = cal.get(Calendar.MONTH); - int year = cal.get(Calendar.YEAR); - return new Day( - year, - month+1, - day); + public LocalDate getSelectedDay() { + if (getSelectedItem() == null) { + return LocalDate.now(); } - return new Day( - getSelectedItem().getYear(), - getSelectedItem().getMonth(), - getSelectedItem().getDay()); + return getSelectedItem(); } - public boolean isSelectedDay(Day day) { - return day != null - && getSelectedItem() != null - && day.getYear() == getSelectedItem().getYear() - && day.getMonth() == getSelectedItem().getMonth() - && day.getDay() == getSelectedItem().getDay(); + public boolean isSelectedDay(@NonNull LocalDate day) { + return day.equals(getSelectedItem()); } - public boolean isToady(Day day) { - Calendar todayCal = Calendar.getInstance(); - return day != null - && day.getYear() == todayCal.get(Calendar.YEAR) - && day.getMonth() == todayCal.get(Calendar.MONTH) - && day.getDay() == todayCal.get(Calendar.DAY_OF_MONTH); + public boolean isToady(@NonNull LocalDate day) { + return LocalDate.now().equals(day); } public int getSelectedItemPosition() { int position = -1; for (int i = 0; i < mAdapter.getCount(); i++) { - Day day = mAdapter.getItem(i); + LocalDate day = mAdapter.getItem(i); if (isSelectedDay(day)) { position = i; @@ -451,7 +412,7 @@ public int getSelectedItemPosition() { public int getTodayItemPosition() { int position = -1; for (int i = 0; i < mAdapter.getCount(); i++) { - Day day = mAdapter.getItem(i); + LocalDate day = mAdapter.getItem(i); if (isToady(day)) { position = i; @@ -508,7 +469,7 @@ protected void applyTransformation(float interpolatedTime, Transformation t) { startAnimation(anim); } - expandIconView.setState(ExpandIconView.MORE,true); + expandIconView.setState(ExpandIconView.MORE, true); } private void collapseTo(int index) { @@ -575,22 +536,22 @@ protected void applyTransformation(float interpolatedTime, Transformation t) { startAnimation(anim); } - expandIconView.setState(ExpandIconView.LESS,true); + expandIconView.setState(ExpandIconView.LESS, true); } @Override public void setState(int state) { super.setState(state); - if(state == STATE_COLLAPSED) { + if (state == STATE_COLLAPSED) { expanded = false; } - if(state == STATE_EXPANDED) { + if (state == STATE_EXPANDED) { expanded = true; } } - public void select(Day day) { - setSelectedItem(new Day(day.getYear(), day.getMonth(), day.getDay())); + public void select(LocalDate day) { + setSelectedItem(day); redraw(); @@ -632,6 +593,5 @@ public interface CalendarListener { } - } diff --git a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/UICalendar.java b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/UICalendar.java index a4ab78e..fd5f736 100644 --- a/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/UICalendar.java +++ b/collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/UICalendar.java @@ -1,16 +1,13 @@ package com.shrikanthravi.collapsiblecalendarview.widget; import android.content.Context; -import android.content.res.ColorStateList; import android.content.res.TypedArray; import android.graphics.Color; import android.graphics.PorterDuff; -import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; -import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; @@ -18,23 +15,15 @@ import android.widget.TextView; import com.shrikanthravi.collapsiblecalendarview.R; -import com.shrikanthravi.collapsiblecalendarview.data.Day; -import com.shrikanthravi.collapsiblecalendarview.listener.OnSwipeTouchListener; import com.shrikanthravi.collapsiblecalendarview.view.ExpandIconView; import com.shrikanthravi.collapsiblecalendarview.view.LockScrollView; +import org.threeten.bp.DayOfWeek; +import org.threeten.bp.LocalDate; -public abstract class UICalendar extends LinearLayout { +public abstract class UICalendar extends LinearLayout { - // Day of Week - public static final int SUNDAY = 0; - public static final int MONDAY = 1; - public static final int TUESDAY = 2; - public static final int WEDNESDAY = 3; - public static final int THURSDAY = 4; - public static final int FRIDAY = 5; - public static final int SATURDAY = 6; // State public static final int STATE_EXPANDED = 0; public static final int STATE_COLLAPSED = 1; @@ -60,9 +49,9 @@ public abstract class UICalendar extends LinearLayout { protected ExpandIconView expandIconView; // Attributes - private boolean mShowWeek = true; - private int mFirstDayOfWeek = SUNDAY; - private int mState = STATE_COLLAPSED; + private boolean mShowWeek = true; + private DayOfWeek mFirstDayOfWeek = DayOfWeek.SUNDAY; + private int mState = STATE_COLLAPSED; private int mTextColor = Color.BLACK; private int mPrimaryColor = Color.WHITE; @@ -79,7 +68,7 @@ public abstract class UICalendar extends LinearLayout { private Drawable mButtonRightDrawable = getResources().getDrawable(R.drawable.right_icon); - private Day mSelectedItem = null; + private LocalDate mSelectedItem = null; private int mButtonLeftDrawableTintColor=Color.BLACK; private int mButtonRightDrawableTintColor=Color.BLACK; @@ -131,17 +120,14 @@ protected void init(Context context) { mBtnPrevWeek = rootView.findViewById(R.id.btn_prev_week); mBtnNextWeek = rootView.findViewById(R.id.btn_next_week); expandIconView = rootView.findViewById(R.id.expandIcon); - - - - } protected void setAttributes(TypedArray attrs) { // set attributes by the values from XML //setStyle(attrs.getInt(R.styleable.UICalendar_style, mStyle)); setShowWeek(attrs.getBoolean(R.styleable.UICalendar_showWeek, mShowWeek)); - setFirstDayOfWeek(attrs.getInt(R.styleable.UICalendar_firstDayOfWeek, mFirstDayOfWeek)); + int dayOfWeekValue = attrs.getInt(R.styleable.UICalendar_firstDayOfWeek, mFirstDayOfWeek.getValue()); + setFirstDayOfWeek(DayOfWeek.of(dayOfWeekValue)); setState(attrs.getInt(R.styleable.UICalendar_state, mState)); setTextColor(attrs.getColor(R.styleable.UICalendar_textColor, mTextColor)); @@ -189,7 +175,7 @@ protected void setAttributes(TypedArray attrs) { setButtonLeftDrawableTintColor(attrs.getColor(R.styleable.UICalendar_buttonLeft_drawableTintColor,mButtonLeftDrawableTintColor)); setButtonRightDrawableTintColor(attrs.getColor(R.styleable.UICalendar_buttonRight_drawableTintColor,mButtonRightDrawableTintColor)); setExpandIconColor(attrs.getColor(R.styleable.UICalendar_expandIconColor,mExpandIconColor)); - Day selectedItem = null; + mSelectedItem = null; } public void setButtonLeftDrawableTintColor(int color){ @@ -226,11 +212,11 @@ public void setShowWeek(boolean showWeek) { } } - public int getFirstDayOfWeek() { + public DayOfWeek getFirstDayOfWeek() { return mFirstDayOfWeek; } - public void setFirstDayOfWeek(int firstDayOfWeek) { + public void setFirstDayOfWeek(DayOfWeek firstDayOfWeek) { this.mFirstDayOfWeek = firstDayOfWeek; reload(); } @@ -330,32 +316,24 @@ public void setSelectedItemBackgroundDrawable(Drawable selectedItemBackground) { redraw(); } - public Drawable getButtonLeftDrawable() { - return mButtonLeftDrawable; - } - public void setButtonLeftDrawable(Drawable buttonLeftDrawable) { this.mButtonLeftDrawable = buttonLeftDrawable; mBtnPrevMonth.setImageDrawable(buttonLeftDrawable); mBtnPrevWeek.setImageDrawable(buttonLeftDrawable); } - public Drawable getButtonRightDrawable() { - return mButtonRightDrawable; - } - public void setButtonRightDrawable(Drawable buttonRightDrawable) { this.mButtonRightDrawable = buttonRightDrawable; mBtnNextMonth.setImageDrawable(buttonRightDrawable); mBtnNextWeek.setImageDrawable(buttonRightDrawable); } - public Day getSelectedItem() { + public LocalDate getSelectedItem() { return mSelectedItem; } - public void setSelectedItem(Day selectedItem) { - this.mSelectedItem = selectedItem; + public void setSelectedItem(LocalDate selectedDate) { + this.mSelectedItem = selectedDate; }