Skip to content

Commit

Permalink
More localization friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Oct 8, 2018
1 parent cc49136 commit 5b6f6bb
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 273 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
## v1.4.2
- Fixed French translations.
- Fixed bug where days of the week of weekly recurrence were not checked.
- Target API changed to 28.

## v1.4.1
- Fixed library dependencies not being added to the project

Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
compileSdkVersion 28
defaultConfig {
applicationId "com.maltaisn.recurpickerdemo"
minSdkVersion 19
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -23,7 +23,7 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:28.0.0'

implementation project(":recurpicker")
}
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Thu May 10 07:21:04 EDT 2018
#Sun Oct 07 15:34:59 EDT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
10 changes: 5 additions & 5 deletions recurpicker/build.gradle
Expand Up @@ -14,8 +14,8 @@ ext {
siteUrl = 'https://github.com/maltaisn/recurpickerlib'
gitUrl = 'https://github.com/maltaisn/recurpickerlib.git'

libraryVersionCode = 13
libraryVersion = '1.4.1'
libraryVersionCode = 14
libraryVersion = '1.4.2'

developerId = 'maltaisn'

Expand All @@ -25,11 +25,11 @@ ext {
}

android {
compileSdkVersion 27
compileSdkVersion 28

defaultConfig {
minSdkVersion 19
targetSdkVersion 27
targetSdkVersion 28
versionCode libraryVersionCode
versionName libraryVersion
}
Expand All @@ -52,7 +52,7 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:appcompat-v7:28.0.0'
}

apply from: 'maven-install.gradle'
Expand Down
Expand Up @@ -22,17 +22,20 @@
package com.maltaisn.recurpicker;

import android.content.Context;
import android.content.res.Resources;
import android.support.v4.os.ConfigurationCompat;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class RecurrenceFormat {

private static final String TAG = RecurrenceFormat.class.getSimpleName();

private Context context;
private Resources res;
private DateFormat dateFormat;

private Calendar calendar;
Expand All @@ -43,7 +46,7 @@ public class RecurrenceFormat {
* @param dateFormat date format for the end date, can be null but must be set before formatting
*/
public RecurrenceFormat(Context context, DateFormat dateFormat) {
this.context = context;
res = context.getResources();
this.dateFormat = dateFormat;

calendar = Calendar.getInstance();
Expand All @@ -68,80 +71,89 @@ public DateFormat getDateFormat() {
*/
public String format(Recurrence r) {
// Generate first part of the text -> ex: Repeats every 2 days
String[] recurFormats = context.getResources().getStringArray(R.array.rp_recur_formats);
String recurFormat = "";
StringBuilder recurSb = new StringBuilder();
int freq = r.getFrequency();
switch (r.getPeriod()) {
case Recurrence.NONE:
recurFormat = recurFormats[0]; // Does not repeat
// Does not repeat
recurSb.append(res.getString(R.string.rp_format_none));
break;

case Recurrence.DAILY:
recurFormat = MessageFormat.format(recurFormats[1], r.getFrequency());
// Repeat on every [freq] day(s)
recurSb.append(res.getQuantityString(R.plurals.rp_format_day, freq, freq));
break;

case Recurrence.WEEKLY:
StringBuilder dayList = null;
// Repeat on every [freq] week(s)
recurSb.append(res.getQuantityString(R.plurals.rp_format_week, freq, freq));
if (!r.isDefault()) {
// Make a list of days of week
dayList = new StringBuilder();
StringBuilder weekOptionStr = new StringBuilder();
if (r.getDaySetting() == Recurrence.EVERY_DAY_OF_WEEK) {
dayList.append(context.getString(R.string.rp_days_of_week_list_all));
// on every day of the week
weekOptionStr.append(res.getString(R.string.rp_format_weekly_all));
} else {
String[] daysAbbr = context.getResources().getStringArray(R.array.rp_days_of_week_abbr);
String listSep = context.getString(R.string.rp_days_of_week_list_sep);
for (int day = 1; day <= 7; day++) {
// on [Sun, Mon, Wed, ...]
String[] daysAbbr = res.getStringArray(R.array.rp_days_of_week_abbr);
for (int day = Calendar.SUNDAY; day <= Calendar.SATURDAY; day++) {
if (r.isRepeatedOnDaysOfWeek(1 << day)) {
dayList.append(daysAbbr[day - 1]);
dayList.append(listSep);
weekOptionStr.append(daysAbbr[day - 1]);
weekOptionStr.append(", ");
}
}
dayList.delete(dayList.length() - listSep.length(), dayList.length()); // Remove extra separator
weekOptionStr.delete(weekOptionStr.length() - 2, weekOptionStr.length()); // Remove extra separator
}
recurSb.append(' ');
recurSb.append(res.getString(R.string.rp_format_weekly_option, weekOptionStr.toString()));
}
recurFormat = MessageFormat.format(recurFormats[2], r.getFrequency(), dayList == null ? 0 : 1, dayList);
break;

case Recurrence.MONTHLY:
String when = "";
int daySetting = r.getDaySetting();
if (!r.isDefault() && daySetting == Recurrence.SAME_DAY_OF_MONTH)
when = context.getString(R.string.rp_repeat_monthly_same_day);
else if (daySetting == Recurrence.SAME_DAY_OF_WEEK) when = getSameDayOfSameWeekString(r.getStartDate());
else if (daySetting == Recurrence.LAST_DAY_OF_MONTH)
when = context.getString(R.string.rp_repeat_monthly_last_day);
// Repeat on every [freq] month(s)
recurSb.append(res.getQuantityString(R.plurals.rp_format_month, freq, freq));

recurFormat = MessageFormat.format(recurFormats[3], r.getFrequency(), when.isEmpty() ? 0 : 1, when);
int daySetting = r.getDaySetting();
if (!r.isDefault()) {
recurSb.append(" (");
String monthOptionStr = null;
switch (daySetting) {
case Recurrence.SAME_DAY_OF_MONTH:
// on the same day of each month
recurSb.append(res.getString(R.string.rp_format_monthly_same_day));
break;
case Recurrence.SAME_DAY_OF_WEEK:
// on every [nth] [Sunday]
recurSb.append(getSameDayOfSameWeekString(r.getStartDate()));
break;
case Recurrence.LAST_DAY_OF_MONTH:
// on the last day of each month
recurSb.append(res.getString(R.string.rp_format_monthly_last_day));
break;
}
recurSb.append(")");
}
break;

case Recurrence.YEARLY:
recurFormat = MessageFormat.format(recurFormats[4], r.getFrequency());
recurSb.append(res.getQuantityString(R.plurals.rp_format_year, freq, freq));
break;
}

// Generate second part of the text (how recurrence ends) -> ex: until 31-12-2017
String[] endFormats = context.getResources().getStringArray(R.array.rp_end_formats);
String endFormat = "";
switch (r.getEndType()) {
case Recurrence.END_NEVER:
endFormat = endFormats[0];
break;
case Recurrence.END_BY_DATE:
endFormat = MessageFormat.format(endFormats[1],
dateFormat.format(new Date(r.getEndDate())));
break;
case Recurrence.END_BY_COUNT:
endFormat = MessageFormat.format(endFormats[2], r.getEndCount());
break;
int endType = r.getEndType();
if (endType != Recurrence.END_NEVER) {
recurSb.append("; ");
if (endType == Recurrence.END_BY_DATE) {
// until [date]
recurSb.append(res.getString(R.string.rp_format_end_date,
dateFormat.format(new Date(r.getEndDate()))));
} else {
// for [endCount] event(s)
int endCount = r.getEndCount();
recurSb.append(res.getQuantityString(R.plurals.rp_format_end_count, endCount, endCount));
}
}

String result;
if (endFormat.isEmpty()) {
result = recurFormat;
} else {
result = MessageFormat.format(context.getString(R.string.rp_merge_format), recurFormat, endFormat);
}

return result;
return recurSb.toString();
}

/**
Expand All @@ -152,11 +164,11 @@ else if (daySetting == Recurrence.LAST_DAY_OF_MONTH)
String getSameDayOfSameWeekString(long date) {
calendar.setTimeInMillis(date);

String[] daysOfWeek = context.getResources().getStringArray(R.array.rp_days_of_week);
String[] ordinalNbs = context.getResources().getStringArray(R.array.rp_ordinal_numbers);
String weekOrd = ordinalNbs[calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 1];
String dayOfWeek = daysOfWeek[calendar.get(Calendar.DAY_OF_WEEK) - 1];
return MessageFormat.format(context.getString(R.string.rp_repeat_monthly_same_week), weekOrd, dayOfWeek);
Locale locale = ConfigurationCompat.getLocales(res.getConfiguration()).get(0);
String[] daysStr = res.getStringArray(R.array.rp_format_monthly_same_week);
String[] numbersStr = res.getStringArray(R.array.rp_format_monthly_ordinal);
String weekNbStr = numbersStr[calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 1];
return String.format(locale, daysStr[calendar.get(Calendar.DAY_OF_WEEK) - 1], weekNbStr);
}

}
Expand Up @@ -28,15 +28,11 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.SwitchCompat;
import android.text.Editable;
import android.text.InputFilter;
Expand All @@ -58,12 +54,10 @@
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.Calendar;

@SuppressWarnings({"SameParameterValue", "UnusedReturnValue"})
Expand Down Expand Up @@ -206,6 +200,7 @@ private void initLayout() {

final LinearLayout freqLayout = findViewById(R.id.freq_layout);
freqEdit = findViewById(R.id.edit_freq);
final TextView freqLabel = findViewById(R.id.text_freq_label);
final TextView freqEventLabel = findViewById(R.id.text_freq_event);

final LinearLayout weekButtonLayout1 = findViewById(R.id.week_button_row1);
Expand Down Expand Up @@ -419,7 +414,7 @@ public void afterTextChanged(Editable editable) {
}

// Change between "event" and "events"
endValueLabel.setText(MessageFormat.format(getContext().getString(R.string.rp_event), endCount));
endValueLabel.setText(getResources().getQuantityString(R.plurals.rp_end_count_event, endCount));
}
});

Expand Down Expand Up @@ -459,9 +454,24 @@ public void afterTextChanged(Editable editable) {
}

// Change between week and weeks (or other time unit)
String unitFormat = getResources().getStringArray(R.array.rp_recur_units)
[recurPeriodSpin.getSelectedItemPosition()];
freqEventLabel.setText(MessageFormat.format(unitFormat, freq));
int formatId = 0;
switch (recurPeriodSpin.getSelectedItemPosition()) {
case 0:
formatId = R.plurals.rp_frequency_day;
break;
case 1:
formatId = R.plurals.rp_frequency_week;
break;
case 2:
formatId = R.plurals.rp_frequency_month;
break;
case 3:
formatId = R.plurals.rp_frequency_year;
break;
}
String[] parts = getResources().getQuantityString(formatId, freq).split("%d", 2);
freqLabel.setText(parts[0].trim());
freqEventLabel.setText(parts[1].trim());
}
});

Expand Down Expand Up @@ -582,7 +592,6 @@ public void updateMode() {

/**
* Change the editing mode of the recurrence picker and updates the current views to match the recurrence
*
* @param creatorShown if not shown, the list of default options is shown
*/
public void changeMode(boolean creatorShown) {
Expand All @@ -606,7 +615,8 @@ public void changeMode(boolean creatorShown) {
// Select days of week matching recurrence's settings
if (recurrence.getPeriod() == Recurrence.WEEKLY) {
for (int i = 0; i < 7; i++) {
weekButtons[i].setChecked(recurrence.isRepeatedOnDaysOfWeek(i + 1));
int day = 1 << (i + 1);
weekButtons[i].setChecked(recurrence.isRepeatedOnDaysOfWeek(day));
}
} else {
// If weekly is not the current period, set the default for when it will be selected
Expand Down Expand Up @@ -776,7 +786,6 @@ public void onDismiss(DialogInterface dialog) {
* Call this method to notice the view that it is in a dialog
* When restoring, the view needs to know when to show the date dialog (if it was opened)
* to prevent it from being displayed being its parent dialog.
*
* @param isInDialog whether in a dialog or not
*/
public void setIsInDialog(boolean isInDialog) {
Expand All @@ -794,7 +803,6 @@ public void showDateDialogIfNeeded() {
/**
* Set a listener to call when picker goes from option list to creator
* Can be used to hide the view while it rearranges its views
*
* @param listener the listener
*/
public RecurrencePickerSettings setOnCreatorShownListener(@Nullable OnCreatorShownListener listener) {
Expand All @@ -804,7 +812,6 @@ public RecurrencePickerSettings setOnCreatorShownListener(@Nullable OnCreatorSho

/**
* Set a listener to call when picker produces a recurrence
*
* @param listener the listener
*/
public RecurrencePickerSettings setOnRecurrenceSelectedListener(@Nullable OnRecurrenceSelectedListener listener) {
Expand All @@ -814,7 +821,6 @@ public RecurrencePickerSettings setOnRecurrenceSelectedListener(@Nullable OnRecu

/**
* Set the cancel lsitener to call when the picker is cancelled
*
* @param listener the listener
* @return the picker
*/
Expand Down

0 comments on commit 5b6f6bb

Please sign in to comment.