Created
October 2, 2013 06:10
-
-
Save simekadam/6789727 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Timeslot implements Parcelable { | |
public static final int MONDAY = 1, | |
TUESDAY = 2, | |
WEDNESDAY = 3, | |
THURSDAY = 4, | |
FRIDAY = 5, | |
SATURDAY = 6, | |
SUNDAY = 7; | |
public int dayOfWeek, startHour, startMinute, endHour, endMinute; | |
public BranchOffice branchOffice; | |
public Timeslot(int dayOfWeek, int startHour, int startMinute, int endHour, int endMinute, BranchOffice branchOffice) { | |
this.dayOfWeek = dayOfWeek; | |
this.startHour = startHour; | |
this.startMinute = startMinute; | |
this.endHour = endHour; | |
this.endMinute = endMinute; | |
this.branchOffice = branchOffice; | |
} | |
public Timeslot() { | |
} | |
public Timeslot(Parcel p){ | |
Bundle data = p.readBundle(Timeslot.class.getClassLoader()); | |
data.setClassLoader(Timeslot.class.getClassLoader()); | |
this.dayOfWeek = data.getInt("dayOfWeek"); | |
this.startHour = data.getInt("startHour"); | |
this.startMinute = data.getInt("startMinute"); | |
this.endHour = data.getInt("endHour"); | |
this.endMinute = data.getInt("endMinute"); | |
this.branchOffice = BranchOffice.get(data.getInt("branchOffice")); | |
} | |
public Timeslot(int firstHour, int duration) { | |
startHour = 7; | |
startMinute = 30 + (firstHour - 1) * 45 + ((firstHour % 2 != 0) ? 15 : 0) * (firstHour / 2) + ((firstHour % 2 == 0) ? (firstHour / 4) * 15 : 0); | |
while (startMinute >= 60) { | |
startHour++; | |
startMinute -= 60; | |
} | |
endHour = startHour; | |
endMinute = startMinute + duration * 45; | |
while (endMinute >= 60) { | |
endHour++; | |
endMinute -= 60; | |
} | |
} | |
public Timeslot(int dayOfWeek, int startHour, int startMinute, int endHour, int endMinute) { | |
this.dayOfWeek = dayOfWeek; | |
this.startHour = startHour; | |
this.startMinute = startMinute; | |
this.endHour = endHour; | |
this.endMinute = endMinute; | |
} | |
public static String getNameOfDay(int day) { | |
switch (day) { | |
case 1: | |
return App.getInstance().getString(R.string.monday); | |
case 2: | |
return App.getInstance().getString(R.string.tuesday); | |
case 3: | |
return App.getInstance().getString(R.string.wednesday); | |
case 4: | |
return App.getInstance().getString(R.string.thursday); | |
case 5: | |
return App.getInstance().getString(R.string.friday); | |
} | |
return null; | |
} | |
public static Timeslot getNow() { | |
// if (true) | |
// return new Timeslot(4, 13, 01, 13, 01); | |
int hour = Calendar.getInstance(Locale.UK).get(Calendar.HOUR_OF_DAY); | |
int minute = Calendar.getInstance(Locale.UK).get(Calendar.MINUTE); | |
return new Timeslot(Calendar.getInstance(Locale.UK).get(Calendar.DAY_OF_WEEK) - 1, hour, minute, hour, minute, null); | |
} | |
public boolean isOpenNow() { | |
Timeslot now = getNow(); | |
// příklad | |
// now = new Timeslot(2, 14, 0, 14, 0); | |
return dayOfWeek == now.dayOfWeek && | |
((startHour == now.startHour && startMinute <= now.startMinute) || | |
(startHour < now.startHour)) && | |
((endHour == now.endHour && endMinute > now.endMinute) || | |
(endHour > now.endHour)); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || !(o instanceof Timeslot)) return false; | |
Timeslot timeslot = (Timeslot) o; | |
if (dayOfWeek != timeslot.dayOfWeek) return false; | |
if (endHour != timeslot.endHour) return false; | |
if (endMinute != timeslot.endMinute) return false; | |
if (startHour != timeslot.startHour) return false; | |
if (startMinute != timeslot.startMinute) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = dayOfWeek; | |
result = 31 * result + startHour; | |
result = 31 * result + startMinute; | |
result = 31 * result + endHour; | |
result = 31 * result + endMinute; | |
return result; | |
} | |
@Override | |
public int describeContents() { | |
return 0; | |
} | |
@Override | |
public void writeToParcel(Parcel dest, int flags) { | |
Bundle data = new Bundle(Timeslot.class.getClassLoader()); | |
data.putInt("dayOfWeek", dayOfWeek); | |
data.putInt("startHour", startHour); | |
data.putInt("startMinute", startMinute); | |
data.putInt("endHour", endHour); | |
data.putInt("endMinute", endMinute); | |
data.putInt("branchOffice", this.branchOffice != null ? this.branchOffice.ID : BranchOffice.UNKNOWN.ID); | |
dest.writeBundle(data); | |
} | |
public static final Creator<Timeslot> CREATOR = new Creator<Timeslot>() { | |
@Override | |
public Timeslot createFromParcel(Parcel parcel) { | |
return new Timeslot(parcel); | |
} | |
@Override | |
public Timeslot[] newArray(int size) { | |
return new Timeslot[size]; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank for help