Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting the TimeZone of the WeekView #272

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
Expand Down Expand Up @@ -138,6 +139,7 @@ private enum Direction {
private boolean mShowDistinctPastFutureColor = false;
private boolean mHorizontalFlingEnabled = true;
private boolean mVerticalFlingEnabled = true;
private TimeZone mTimeZone = null;

// Listeners.
private EventClickListener mEventClickListener;
Expand Down Expand Up @@ -627,7 +629,7 @@ else if (mNewHourHeight > mMaxHourHeight)
float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom + mCurrentOrigin.y;

if (sameDay){
Calendar now = Calendar.getInstance();
Calendar now = getInstance();
float beforeNow = (now.get(Calendar.HOUR_OF_DAY) + now.get(Calendar.MINUTE)/60.0f) * mHourHeight;
canvas.drawRect(start, startY, startPixel + mWidthPerDay, startY+beforeNow, pastPaint);
canvas.drawRect(start, startY+beforeNow, startPixel + mWidthPerDay, getHeight(), futurePaint);
Expand Down Expand Up @@ -666,7 +668,7 @@ else if (day.before(today)) {
// Draw the line at the current time.
if (mShowNowLine && sameDay){
float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom + mCurrentOrigin.y;
Calendar now = Calendar.getInstance();
Calendar now = getInstance();
float beforeNow = (now.get(Calendar.HOUR_OF_DAY) + now.get(Calendar.MINUTE)/60.0f) * mHourHeight;
canvas.drawLine(start, startY + beforeNow, startPixel + mWidthPerDay, startY + beforeNow, mNowLinePaint);
}
Expand Down Expand Up @@ -700,6 +702,13 @@ else if (day.before(today)) {

}

private Calendar getInstance() {
if(mTimeZone != null)
return Calendar.getInstance(mTimeZone);
else
return Calendar.getInstance();
}

/**
* Get the time and date where the user clicked on.
* @param x The x position of the touch event.
Expand Down Expand Up @@ -1252,7 +1261,7 @@ public String interpretDate(Calendar date) {

@Override
public String interpretTime(int hour) {
Calendar calendar = Calendar.getInstance();
Calendar calendar = getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, 0);

Expand Down Expand Up @@ -1515,6 +1524,24 @@ public void setDayNameLength(int length) {
this.mDayNameLength = length;
}


/**
* Get the timeZone this calendar will display in
* @return
*/
public TimeZone getTimeZone() {
return mTimeZone;
}

/**
* Set the timezone that this calendar will dispaly in
* @param timeZone
*/
public void setTimeZone(TimeZone timeZone) {
this.mTimeZone = timeZone;
invalidate();
}

public int getOverlappingEventGap() {
return mOverlappingEventGap;
}
Expand Down Expand Up @@ -1812,7 +1839,7 @@ private boolean forceFinishScroll() {
* Show today on the week view.
*/
public void goToToday() {
Calendar today = Calendar.getInstance();
Calendar today = getInstance();
goToDate(today);
}

Expand All @@ -1836,7 +1863,7 @@ public void goToDate(Calendar date) {

mRefreshEvents = true;

Calendar today = Calendar.getInstance();
Calendar today = getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
Expand Down Expand Up @@ -1964,7 +1991,7 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
* @return the calendar instance
*/
private Calendar today(){
Calendar today = Calendar.getInstance();
Calendar today = getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
Expand Down
12 changes: 10 additions & 2 deletions library/src/main/java/com/alamkanak/weekview/WeekViewEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alamkanak.weekview;

import java.util.Calendar;
import java.util.TimeZone;

/**
* Created by Raquib-ul-Alam Kanak on 7/21/2014.
Expand Down Expand Up @@ -34,16 +35,23 @@ public WeekViewEvent(){
* @param endMinute Minute when the event ends.
*/
public WeekViewEvent(long id, String name, int startYear, int startMonth, int startDay, int startHour, int startMinute, int endYear, int endMonth, int endDay, int endHour, int endMinute) {
this(id, name, startYear, startMonth, startDay, startHour, startMinute, null, endYear, endMonth, endDay, endHour, endMinute, null);
}


public WeekViewEvent(long id, String name,
int startYear, int startMonth, int startDay, int startHour, int startMinute, TimeZone startTimezone,
int endYear, int endMonth, int endDay, int endHour, int endMinute, TimeZone endTimezone) {
this.mId = id;

this.mStartTime = Calendar.getInstance();
this.mStartTime = startTimezone == null? Calendar.getInstance() : Calendar.getInstance(startTimezone);
this.mStartTime.set(Calendar.YEAR, startYear);
this.mStartTime.set(Calendar.MONTH, startMonth-1);
this.mStartTime.set(Calendar.DAY_OF_MONTH, startDay);
this.mStartTime.set(Calendar.HOUR_OF_DAY, startHour);
this.mStartTime.set(Calendar.MINUTE, startMinute);

this.mEndTime = Calendar.getInstance();
this.mEndTime = endTimezone == null ? Calendar.getInstance() : Calendar.getInstance(endTimezone);
this.mEndTime.set(Calendar.YEAR, endYear);
this.mEndTime.set(Calendar.MONTH, endMonth-1);
this.mEndTime.set(Calendar.DAY_OF_MONTH, endDay);
Expand Down