Skip to content

Commit c2b14c2

Browse files
committed
week mode crash fixed
1 parent e8915e8 commit c2b14c2

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ As the CalendarView projects in GitHub seems to be not so much **customizable**,
1515
- both month mode or week mode are introduced
1616

1717
# logs
18+
## 2015.4.5
19+
- fix Utils.getWeekDiff. Why Java Calendar seems so powerless when it comes to calculate difference!Maybe u can take a coffee time to look at [joda-time-android](https://github.com/dlew/joda-time-android) if u don't want to be f***ed up.LOl
20+
1821
## 2015.4.1
1922
- happy Fool's Day!Now the CalendarView can make u money per-millis! if u believe me...
2023
- out-of-month day click event
@@ -29,8 +32,11 @@ As the CalendarView projects in GitHub seems to be not so much **customizable**,
2932
- select day by code
3033

3134
# Issues
35+
## 2016.4.5
36+
- switch to week mode,sometimes it cannot navigate to the selected Calendar and unclick seems to be blocked until slide out the 3 limit page of PagerAdapter;
37+
3238
## 2016.4.1
33-
- week mode crash?what f!
39+
- ~~week mode crash?what f!~~ *2016.4.5* deprecate Utils.ifSameWeek,return fake result in CalendarView. Calendar.setFirstDayOfWeek() seems weird
3440

3541
## 2016.3.31
3642
- ~~out-of-month day click event~~ *2016.4.1*

app/src/main/java/com/xyz/calendarview/TestActivity.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.callmexyz.calendarview.CalendarView;
1010
import com.callmexyz.calendarview.DayView;
1111
import com.callmexyz.calendarview.dayclicklistener.CircleColor;
12+
import com.callmexyz.calendarview.styles.MonthViewStyle;
1213

1314
import java.text.SimpleDateFormat;
1415
import java.util.Calendar;
@@ -25,6 +26,7 @@ public class TestActivity extends Activity {
2526
protected void onCreate(Bundle savedInstanceState) {
2627
super.onCreate(savedInstanceState);
2728
setContentView(R.layout.activity_test);
29+
2830
calendarView = (CalendarView) findViewById(R.id.calendar_view);
2931
calendarView.setDayClickListener(new MyDayClickListener(getResources().getColor(android.R.color.holo_blue_light)));
3032

@@ -50,11 +52,12 @@ public void onClick(View v) {
5052
calendarView.navToFormer();
5153
}
5254
});
55+
56+
5357
/*
5458
example for selectDay using selectDay(Calendar) when initiating
5559
or u can just call selectDayAtInit(Calendar);
56-
*/
57-
/* Thread t = new Thread(){
60+
*//* Thread t = new Thread(){
5861
@Override
5962
public void run() {
6063
super.run();
@@ -76,26 +79,26 @@ public void run() {
7679
};
7780
t.start();*/
7881

79-
calendarView.setFirstDayOfWeek(Calendar.FRIDAY);
82+
calendarView.setFirstDayOfWeek(Calendar.FRIDAY);
8083
calendarView.selectDayAtInit(Calendar.getInstance());
8184
// u may need to set the time title manually since initiating won't call onMonthSelected
8285
Date date = new Date(System.currentTimeMillis());
8386
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
8487
tv.setText(sdf.format(date));
88+
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
89+
@Override
90+
public void onClick(View v) {
91+
// calendarView.startCollapse();
92+
MonthViewStyle w = calendarView.getMonthViewStyle();
93+
if (w.getMonthType() == MonthViewStyle.MonthType.MONTH_VIEW)
94+
w.setMonthType(MonthViewStyle.MonthType.WEEK_VIEW);
95+
else w.setMonthType(MonthViewStyle.MonthType.MONTH_VIEW);
96+
calendarView.setMonthViewStyle(w);
97+
}
8598

86-
87-
findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
88-
@Override
89-
public void onClick(View v) {
90-
calendarView.startCollapse();
91-
}
92-
});
93-
99+
});
94100
}
95101

96-
97-
98-
99102
class MyDayClickListener extends CircleColor {
100103
public MyDayClickListener(@ColorInt int color) {
101104
super(color);

library/src/main/java/com/callmexyz/calendarview/CalendarView.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class CalendarView extends ViewGroup {
3333
private MonthSelectedListener mMonthSelectedListener;
3434
private DayClickListener mDayClickListener;
3535
// date for the current showing month's start
36+
// TODO: 2016/4/5 should use week start when in week mode
3637
private Calendar mCurrentMonthStart;
3738
private int mCurrentPosition;
3839
//styles
@@ -111,11 +112,9 @@ private void initViews() {
111112
mMonthViewPager = new MonthViewPager(getContext());
112113

113114
mMonthPagerAdapter = new MonthPagerAdapter(this);
114-
115115
mMonthViewPager.setAdapter(mMonthPagerAdapter);
116116
mCurrentPosition = mMonthPagerAdapter.getPosition(Calendar.getInstance());
117117
mMonthViewPager.setCurrentItem(mCurrentPosition);
118-
119118
mMonthViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
120119
@Override
121120
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
@@ -336,9 +335,15 @@ public MonthViewStyle getMonthViewStyle() {
336335
return mMonthViewStyle;
337336
}
338337

339-
// TODO: 2016/4/1 refresh ui
340-
public void setmMonthViewStyle(MonthViewStyle mMonthViewStyle) {
338+
// TODO: 2016/4/5 animation
339+
public void setMonthViewStyle(MonthViewStyle mMonthViewStyle) {
341340
this.mMonthViewStyle = mMonthViewStyle;
341+
refreshUI();
342+
if (null != mSelectedCalendar) {
343+
navToPage(mSelectedCalendar);
344+
return;
345+
}
346+
if (null != mCurrentMonthStart) navToPage(mCurrentMonthStart);
342347
}
343348

344349
public Calendar getSelectedCalendar() {

library/src/main/java/com/callmexyz/calendarview/MonthPagerAdapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public int getCount() {
5959
return getCount(mRangeStart, mRangeEnd);
6060
}
6161

62-
// TODO: 2016/4/1 quiete complicated here to get the exact range of differen MonthStyle especially for WeekView
62+
// TODO: 2016/4/1 quite complicated here to get the exact range of difference MonthStyle especially for WeekView
6363
private int getCount(Calendar start, Calendar end) {
6464
if (end.before(start)) {
6565
Log.w(TAG, "range end is before start");
@@ -107,7 +107,7 @@ public int getPosition(Calendar c) {
107107
if (i < 0 || i > getCount() - 1) {
108108
Log.w(TAG, "the given calendar is out of valid range");
109109
}
110-
return i < 0 ? 0 : (i > getCount() ? getCount() : i);
110+
return i < 0 ? 0 : (i > getCount()-1 ? getCount()-1 : i);
111111
}
112112

113113
/**
@@ -116,10 +116,10 @@ public int getPosition(Calendar c) {
116116
*/
117117
public DayView getDayView(Calendar c) {
118118
for (int i = 0; i < mViewList.size(); i++) {
119-
if ((MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType()
119+
if ((MonthViewStyle.MonthType.MONTH_VIEW == mCalendarView.getMonthViewStyle().getMonthType()
120120
&& Utils.ifSameMonth(mViewList.get(i).getFirstDay(), c))
121121
|| (MonthViewStyle.MonthType.WEEK_VIEW == mCalendarView.getMonthViewStyle().getMonthType()
122-
&& Utils.ifSameWeek(mViewList.get(i).getFirstDay(), c, mCalendarView.getFirstDayOfWeek()))) {
122+
&& Utils.ifSameWeekAsWeekStart(mViewList.get(i).getMonthStartDay(), c))) {
123123
return mViewList.get(i).getChildAt(c);
124124
}
125125
}

library/src/main/java/com/callmexyz/calendarview/Utils.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ public static boolean ifSameDay(Calendar c1, Calendar c2) {
7373

7474
}
7575

76+
public static boolean ifSameWeekAsWeekStart(Calendar weekStart, Calendar cal) {
77+
if (ifSameDay(weekStart, cal)) return true;
78+
if (weekStart.after(cal)) return false;
79+
return getDayDifference(weekStart, cal) < 7;
80+
}
81+
// TODO: 2016/4/5
82+
83+
/**
84+
* @param cc1
85+
* @param cc2
86+
* @param firstDayOfWeek
87+
* @return
88+
* @deprecated function good when call in normal use ,but problematic when in {@link MonthPagerAdapter}.Why?
89+
*/
7690
public static boolean ifSameWeek(Calendar cc1, Calendar cc2, int firstDayOfWeek) {
7791
if (null == cc1 || null == cc2) return false;
7892
Calendar c1 = (Calendar) cc1.clone();
@@ -92,12 +106,11 @@ public static int getWeekDiff(Calendar s, Calendar b, int firstDayOfWeek) {
92106
if (b.before(s)) return getWeekDiff(s, b, firstDayOfWeek);
93107
Calendar small = (Calendar) s.clone();
94108
Calendar big = (Calendar) b.clone();
95-
s.setFirstDayOfWeek(firstDayOfWeek);
96-
b.setFirstDayOfWeek(firstDayOfWeek);
97-
int weekEnd = (s.get(Calendar.DAY_OF_WEEK)-1)%7;
98-
if(0==weekEnd) weekEnd=Calendar.SATURDAY;
99-
s.set(Calendar.DAY_OF_WEEK,weekEnd);
100-
return (int) (getDayDifference(small, big) / 7)+1;
109+
small.setFirstDayOfWeek(firstDayOfWeek);
110+
int weekEnd = (firstDayOfWeek -1 ) % 7;
111+
if (0 == weekEnd) weekEnd = Calendar.SATURDAY;
112+
small.set(Calendar.DAY_OF_WEEK, weekEnd);
113+
return (int) (getDayDifference(small, big) / 7) + 1;
101114
}
102115

103116
public static long getDayDifference(Calendar small, Calendar big) {

0 commit comments

Comments
 (0)