mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Merge branch 'sciamano-day-of-week' into dev
This commit is contained in:
@@ -25,6 +25,7 @@ import android.text.format.DateFormat;
|
||||
import org.isoron.uhabits.R;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
@@ -155,14 +156,26 @@ public class DateHelper
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Throughout the code, it is assumed that the weekdays are numbered from 0 (Saturday) to 6
|
||||
* (Friday). In the Java Calendar they are numbered from 1 (Sunday) to 7 (Saturday). This
|
||||
* function converts from Java to our internal representation.
|
||||
*
|
||||
* @return weekday number in the internal interpretation
|
||||
*/
|
||||
public static int javaWeekdayToLoopWeekday(int number)
|
||||
{
|
||||
return number % 7;
|
||||
}
|
||||
|
||||
public static String[] getDayNames(int format)
|
||||
{
|
||||
String[] wdays = new String[7];
|
||||
|
||||
GregorianCalendar day = new GregorianCalendar();
|
||||
day.set(GregorianCalendar.DAY_OF_WEEK, 0);
|
||||
Calendar day = new GregorianCalendar();
|
||||
day.set(GregorianCalendar.DAY_OF_WEEK, Calendar.SATURDAY);
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
for (int i = 0; i < wdays.length; i++)
|
||||
{
|
||||
wdays[i] = day.getDisplayName(GregorianCalendar.DAY_OF_WEEK, format,
|
||||
Locale.getDefault());
|
||||
@@ -172,6 +185,43 @@ public class DateHelper
|
||||
return wdays;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array with weekday names starting according to locale settings,
|
||||
* e.g. [Mo,Di,Mi,Do,Fr,Sa,So] in Europe
|
||||
*/
|
||||
public static String[] getLocaleDayNames(int format)
|
||||
{
|
||||
String[] days = new String[7];
|
||||
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.set(GregorianCalendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
|
||||
for (int i = 0; i < days.length; i++)
|
||||
{
|
||||
days[i] = calendar.getDisplayName(GregorianCalendar.DAY_OF_WEEK, format,
|
||||
Locale.getDefault());
|
||||
calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
|
||||
}
|
||||
|
||||
return days;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array with week days numbers starting according to locale settings,
|
||||
* e.g. [2,3,4,5,6,7,1] in Europe
|
||||
*/
|
||||
public static Integer[] getLocaleWeekdayList()
|
||||
{
|
||||
Integer[] dayNumbers = new Integer[7];
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.set(GregorianCalendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
|
||||
for (int i = 0; i < dayNumbers.length; i++)
|
||||
{
|
||||
dayNumbers[i] = calendar.get(GregorianCalendar.DAY_OF_WEEK);
|
||||
calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
|
||||
}
|
||||
return dayNumbers;
|
||||
}
|
||||
|
||||
public static String formatWeekdayList(Context context, boolean weekday[])
|
||||
{
|
||||
String shortDayNames[] = getShortDayNames();
|
||||
|
||||
@@ -62,7 +62,6 @@ public class HabitFrequencyView extends ScrollableDataView implements HabitDataV
|
||||
private boolean isBackgroundTransparent;
|
||||
|
||||
private HashMap<Long, Integer[]> frequency;
|
||||
private String wdays[];
|
||||
|
||||
public HabitFrequencyView(Context context)
|
||||
{
|
||||
@@ -89,8 +88,6 @@ public class HabitFrequencyView extends ScrollableDataView implements HabitDataV
|
||||
createPaints();
|
||||
createColors();
|
||||
|
||||
wdays = DateHelper.getShortDayNames();
|
||||
|
||||
dfMonth = DateHelper.getDateFormat("MMM");
|
||||
dfYear = DateHelper.getDateFormat("yyyy");
|
||||
|
||||
@@ -247,11 +244,13 @@ public class HabitFrequencyView extends ScrollableDataView implements HabitDataV
|
||||
float rowHeight = rect.height() / 8.0f;
|
||||
prevRect.set(rect);
|
||||
|
||||
for (int i = 0; i < 7; i++)
|
||||
Integer[] localeWeekdayList = DateHelper.getLocaleWeekdayList();
|
||||
for (int j = 0; j < localeWeekdayList.length; j++)
|
||||
{
|
||||
rect.set(0, 0, baseSize, baseSize);
|
||||
rect.offset(prevRect.left, prevRect.top + baseSize * i);
|
||||
rect.offset(prevRect.left, prevRect.top + baseSize * j);
|
||||
|
||||
int i = DateHelper.javaWeekdayToLoopWeekday(localeWeekdayList[j]);
|
||||
if(values != null)
|
||||
drawMarker(canvas, rect, values[i]);
|
||||
|
||||
@@ -289,9 +288,8 @@ public class HabitFrequencyView extends ScrollableDataView implements HabitDataV
|
||||
pText.setColor(textColor);
|
||||
pGrid.setColor(dimmedTextColor);
|
||||
|
||||
for (int i = 0; i < nRows; i++)
|
||||
{
|
||||
canvas.drawText(wdays[i], rGrid.right - columnWidth,
|
||||
for (String day : DateHelper.getLocaleDayNames(Calendar.SHORT)) {
|
||||
canvas.drawText(day, rGrid.right - columnWidth,
|
||||
rGrid.top + rowHeight / 2 + 0.25f * em, pText);
|
||||
|
||||
pGrid.setStrokeWidth(1f);
|
||||
|
||||
@@ -57,13 +57,13 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
||||
private float columnHeight;
|
||||
private int nColumns;
|
||||
|
||||
private String wdays[];
|
||||
private SimpleDateFormat dfMonth;
|
||||
private SimpleDateFormat dfYear;
|
||||
|
||||
private Calendar baseDate;
|
||||
private int nDays;
|
||||
private int todayWeekday;
|
||||
/** 0-based-position of today in the column */
|
||||
private int todayPositionInColumn;
|
||||
private int colors[];
|
||||
private RectF baseLocation;
|
||||
private int primaryColor;
|
||||
@@ -98,7 +98,6 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
||||
isEditable = false;
|
||||
checkmarks = new int[0];
|
||||
primaryColor = ColorHelper.palette[7];
|
||||
wdays = DateHelper.getShortDayNames();
|
||||
dfMonth = DateHelper.getDateFormat("MMM");
|
||||
dfYear = DateHelper.getDateFormat("yyyy");
|
||||
|
||||
@@ -111,10 +110,11 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
||||
baseDate.add(Calendar.DAY_OF_YEAR, -(getDataOffset() - 1) * 7);
|
||||
|
||||
nDays = (nColumns - 1) * 7;
|
||||
todayWeekday = DateHelper.getStartOfTodayCalendar().get(Calendar.DAY_OF_WEEK) % 7;
|
||||
int realWeekday = DateHelper.getStartOfTodayCalendar().get(Calendar.DAY_OF_WEEK);
|
||||
todayPositionInColumn = (7 + realWeekday - baseDate.getFirstDayOfWeek()) % 7;
|
||||
|
||||
baseDate.add(Calendar.DAY_OF_YEAR, -nDays);
|
||||
baseDate.add(Calendar.DAY_OF_YEAR, -todayWeekday);
|
||||
baseDate.add(Calendar.DAY_OF_YEAR, -todayPositionInColumn);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -155,7 +155,7 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
||||
{
|
||||
float width = 0;
|
||||
|
||||
for(String w : wdays)
|
||||
for(String w : DateHelper.getLocaleDayNames(Calendar.SHORT))
|
||||
width = Math.max(width, pSquareFg.measureText(w));
|
||||
|
||||
return width;
|
||||
@@ -274,9 +274,10 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
||||
|
||||
for (int j = 0; j < 7; j++)
|
||||
{
|
||||
if (!(column == nColumns - 2 && getDataOffset() == 0 && j > todayWeekday))
|
||||
if (!(column == nColumns - 2 && getDataOffset() == 0 && j > todayPositionInColumn))
|
||||
{
|
||||
int checkmarkOffset = getDataOffset() * 7 + nDays - 7 * (column + 1) + todayWeekday - j;
|
||||
int checkmarkOffset = getDataOffset() * 7 + nDays - 7 * (column + 1) +
|
||||
todayPositionInColumn - j;
|
||||
drawSquare(canvas, location, date, checkmarkOffset);
|
||||
}
|
||||
|
||||
@@ -298,10 +299,10 @@ public class HabitHistoryView extends ScrollableDataView implements HabitDataVie
|
||||
|
||||
private void drawAxis(Canvas canvas, RectF location)
|
||||
{
|
||||
for (int i = 0; i < 7; i++)
|
||||
for (String day : DateHelper.getLocaleDayNames(Calendar.SHORT))
|
||||
{
|
||||
location.offset(0, columnWidth);
|
||||
canvas.drawText(wdays[i], location.left + headerTextOffset,
|
||||
canvas.drawText(day, location.left + headerTextOffset,
|
||||
location.bottom - headerTextOffset, pTextHeader);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user