Skip to content

Commit

Permalink
Solve year bug
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Aug 13, 2017
1 parent 1481216 commit 73d5848
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -235,7 +236,7 @@ public void onClick(View view) {
yearSimpleDate = new SimpleDateFormat("yyyy", Locale.getDefault());

// Init headers
yearHeaderValues.setText(String.valueOf(dateTimeCalendar.get(Calendar.YEAR)));
yearHeaderValues.setText(yearSimpleDate.format(dateTimeCalendar.getTime()));
monthAndDayHeaderValues.setText(dayAndMonthSimpleDate.format(dateTimeCalendar.getTime()));

// Construct TimePicker
Expand Down Expand Up @@ -270,7 +271,7 @@ public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull Calend
dateTimeCalendar.set(Calendar.MONTH, calendarDay.getMonth());
dateTimeCalendar.set(Calendar.DAY_OF_MONTH, calendarDay.getDay());
listPickerYearView.assignCurrentYear(calendarDay.getYear());
yearHeaderValues.setText(String.valueOf(calendarDay.getYear()));
yearHeaderValues.setText(yearSimpleDate.format(dateTimeCalendar.getTime()));
monthAndDayHeaderValues.setText(dayAndMonthSimpleDate.format(currentDate));
}
});
Expand All @@ -285,7 +286,7 @@ public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull Calend
@Override
public void onYearSelected(View view, int yearPicker) {
dateTimeCalendar.set(Calendar.YEAR, yearPicker);
yearHeaderValues.setText(String.valueOf(yearPicker));
yearHeaderValues.setText(yearSimpleDate.format(dateTimeCalendar.getTime()));

// Unfortunately, we have lags here and thread isn't a solution :/
materialCalendarView.setCurrentDate(dateTimeCalendar.getTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.widget.TextView;

import com.kunzisoft.switchdatetime.R;

/**
* TextView with circular colored background
* @author JJamet
*/
public class TextCircularIndicatorView extends TextView {
public class TextCircularIndicatorView extends AppCompatTextView {

private int mCircleColor = Color.BLUE;
private Paint mCirclePaint = new Paint();
Expand All @@ -35,12 +33,6 @@ public TextCircularIndicatorView(Context context, AttributeSet attrs, int defSty
init(attrs);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public TextCircularIndicatorView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}

/**
* Initialize constructor
* @param attrs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

import com.kunzisoft.switchdatetime.R;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
* Adapter for manage elements of ListPickerYearView
Expand All @@ -22,6 +26,9 @@ class YearPickerAdapter extends RecyclerView.Adapter<YearPickerAdapter.TextIndic

public static final int UNDEFINED = -1;

private SimpleDateFormat yearFormat;
private Calendar calendar;

private List<Integer> listYears;
private Integer selectedYear;
private int positionSelectedYear;
Expand All @@ -34,6 +41,8 @@ class YearPickerAdapter extends RecyclerView.Adapter<YearPickerAdapter.TextIndic
YearPickerAdapter() {
this.listYears = new ArrayList<>();
this.selectedYear = UNDEFINED;
this.yearFormat = new SimpleDateFormat("yyyy", Locale.getDefault());
this.calendar = Calendar.getInstance();
}

@Override
Expand Down Expand Up @@ -62,7 +71,9 @@ public TextIndicatorViewHolder onCreateViewHolder(ViewGroup parent, int viewType
@Override
public void onBindViewHolder(TextIndicatorViewHolder holder, int position) {
Integer currentYear = listYears.get(position);
holder.textView.setText(String.valueOf(currentYear));

calendar.set(Calendar.YEAR, currentYear);
holder.textView.setText(yearFormat.format(calendar.getTime()));

if(onClickYearListener != null)
holder.container.setOnClickListener(new BufferYearClickListener(currentYear, position));
Expand Down
44 changes: 14 additions & 30 deletions switchdatetime/src/main/res/layout-ldrtl-v17/time_header_label.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,9 @@
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:minWidth="@dimen/time_label_min_width"
android:layout_gravity="center">
<View
android:id="@+id/center_view"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="#00000000"
android:layout_centerInParent="true"
android:visibility="invisible"
android:importantForAccessibility="no" />

<TextView
android:id="@+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_placeholder"
android:layout_toStartOf="@+id/separator"
android:layout_centerVertical="true"
android:visibility="invisible"
style="@style/Theme.SwitchDateTime.TimeLabelText"
android:importantForAccessibility="no" />

<TextView
android:id="@+id/separator"
Expand All @@ -46,10 +27,17 @@
android:text="@string/time_separator"
android:paddingLeft="@dimen/separator_padding"
android:paddingRight="@dimen/separator_padding"
android:layout_alignEnd="@+id/center_view"
android:layout_centerInParent="true"
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<TextView
android:id="@+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_placeholder"
android:layout_toStartOf="@id/separator"
android:layout_centerVertical="true"
style="@style/Theme.SwitchDateTime.TimeLabelText"
android:importantForAccessibility="no" />
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<TextView
android:id="@+id/minutes"
Expand All @@ -58,16 +46,13 @@
android:text="@string/time_placeholder"
android:layout_toEndOf="@+id/separator"
android:layout_centerVertical="true"
android:visibility="invisible"
style="@style/Theme.SwitchDateTime.TimeLabelText"
android:importantForAccessibility="no" />
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<com.kunzisoft.switchdatetime.time.widget.AccessibleTextView
android:id="@+id/ampm_hitspace"
android:layout_width="@dimen/ampm_label_size"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/ampm_label"
android:layout_alignEnd="@+id/ampm_label" />
<TextView
Expand All @@ -79,6 +64,5 @@
android:paddingRight="@dimen/ampm_left_padding"
android:layout_toEndOf="@+id/minutes"
android:layout_alignBaseline="@+id/separator"
style="@style/Theme.SwitchDateTime.TimeLabelAmPm"
android:importantForAccessibility="no" />
style="@style/Theme.SwitchDateTime.TimeLabelAmPm" />
</RelativeLayout>
46 changes: 16 additions & 30 deletions switchdatetime/src/main/res/layout/time_header_label.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,10 @@
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:minWidth="@dimen/time_label_min_width"
android:layout_gravity="center">
<View
android:id="@+id/center_view"
android:layout_width="1dp"
android:layout_height="1dp"
android:background="#00000000"
android:layout_centerInParent="true"
android:visibility="invisible"
android:importantForAccessibility="no" />
<TextView
android:id="@+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_placeholder"
android:layout_toLeftOf="@+id/separator"
android:layout_toStartOf="@+id/separator"
android:layout_centerVertical="true"
style="@style/Theme.SwitchDateTime.TimeLabelText"
android:importantForAccessibility="no" />
<!--TODO Accessible elements-->

<TextView
android:id="@+id/separator"
Expand All @@ -45,11 +28,18 @@
android:text="@string/time_separator"
android:paddingLeft="@dimen/separator_padding"
android:paddingRight="@dimen/separator_padding"
android:layout_alignRight="@+id/center_view"
android:layout_alignEnd="@+id/center_view"
android:layout_centerInParent="true"
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<TextView
android:id="@+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_placeholder"
android:layout_toLeftOf="@id/separator"
android:layout_toStartOf="@id/separator"
android:layout_centerVertical="true"
style="@style/Theme.SwitchDateTime.TimeLabelText"
android:importantForAccessibility="no" />
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<TextView
android:id="@+id/minutes"
Expand All @@ -59,16 +49,13 @@
android:layout_toRightOf="@+id/separator"
android:layout_toEndOf="@+id/separator"
android:layout_centerVertical="true"
android:visibility="invisible"
style="@style/Theme.SwitchDateTime.TimeLabelText"
android:importantForAccessibility="no" />
style="@style/Theme.SwitchDateTime.TimeLabelText" />

<com.kunzisoft.switchdatetime.time.widget.AccessibleTextView
android:id="@+id/ampm_hitspace"
android:layout_width="@dimen/ampm_label_size"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/ampm_label"
android:layout_alignStart="@+id/ampm_label"
android:layout_alignRight="@+id/ampm_label"
Expand All @@ -83,6 +70,5 @@
android:layout_toRightOf="@+id/minutes"
android:layout_toEndOf="@+id/minutes"
android:layout_alignBaseline="@+id/separator"
style="@style/Theme.SwitchDateTime.TimeLabelAmPm"
android:importantForAccessibility="no" />
style="@style/Theme.SwitchDateTime.TimeLabelAmPm" />
</RelativeLayout>

0 comments on commit 73d5848

Please sign in to comment.