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] added feature to set the text color of holiday #27

Open
wants to merge 13 commits into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Thu Sep 22 13:29:06 JST 2016
#Mon Apr 03 15:11:18 JST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
2 changes: 1 addition & 1 deletion javasample/build.gradle
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "jp.co.recruit.android.lightcalendarview.javasample"
Expand Down
Expand Up @@ -43,6 +43,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
calendarView.setMonthFrom(calFrom.getTime());
calendarView.setMonthTo(calTo.getTime());
calendarView.setMonthCurrent(calNow.getTime());
// 前月・翌月を表示する
calendarView.setDisplayOutside(true);
// 当日の強調表示固定
calendarView.setFixToday(true);

calendarView.setOnMonthSelected(new Function2<Date, MonthView, Unit>() {
@Override
public Unit invoke(Date date, final MonthView monthView) {
Expand All @@ -53,11 +58,16 @@ public Unit invoke(Date date, final MonthView monthView) {
public void run() {
Calendar cal = Calendar.getInstance();
List<Date> dates = new ArrayList<Date>();
List<Date> holidays = new ArrayList<Date>();
for (int i = 0; i < 31; i++) {
if (i % 2 == 0) {
cal.set(monthView.getMonth().getYear() + 1900, monthView.getMonth().getMonth(), i);
dates.add(cal.getTime());
}
if (i < 7) {
cal.set(monthView.getMonth().getYear() + 1900, monthView.getMonth().getMonth(), i);
holidays.add(cal.getTime());
}
}
HashMap<Date, List<Accent>> map = new HashMap<>();
for (Date date : dates) {
Expand All @@ -68,6 +78,8 @@ public void run() {
map.put(date, accents);
}
monthView.setAccents(map);
// 祝日を設定
monthView.setHolidays(holidays);
}
}, 1000);

Expand Down
6 changes: 4 additions & 2 deletions javasample/src/main/res/layout/activity_main.xml
Expand Up @@ -23,11 +23,13 @@
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:lcv_weekDayTextSize="18sp"
app:lcv_dayTextSize="18sp"
app:lcv_weekDayTextSize="12sp"
app:lcv_dayTextSize="12sp"
app:lcv_textColor="@color/calendar_day_text"
app:lcv_selectionColor="@color/calendar_selection"
app:lcv_accentColor="@color/calendar_accent"
app:lcv_outsideTextColor="#ccc"
app:lcv_holidayTextColor="#CD0303"
app:lcv_firstDayOfWeek="@integer/lcv_monday"/>

</LinearLayout>
Expand Down
2 changes: 1 addition & 1 deletion kotlinsample/build.gradle
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.0'

defaultConfig {
applicationId "jp.co.recruit.android.lightcalendarview.kotlinsample"
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
buildToolsVersion '25.0.0'

defaultConfig {
minSdkVersion 15
Expand Down
Expand Up @@ -35,6 +35,10 @@ class CalendarSettings(private val context: Context) : ObservableSettings() {
val dayView = DayView(observer)
var timeZone: TimeZone = TimeZone.getDefault()
var locale: Locale = Locale.getDefault()
var displayOutside: Boolean = false

// 当日を常に強調表示
var fixToday: Boolean = false

// settings for DayLayout and WeekDayLayout: first day of the week
var firstDayOfWeek: WeekDay = WeekDay.SUNDAY
Expand Down Expand Up @@ -118,6 +122,8 @@ class CalendarSettings(private val context: Context) : ObservableSettings() {
todayTextPaint.textSize(value)
selectedTextPaint.textSize(value)
selectedTodayTextPaint.textSize(value)
holidayTextPaint.textSize(value)
outsideTextPaint.textSize(value)
}

private val textFilterColorMap: MutableMap<WeekDay, Int?> = WeekDay.values().map {
Expand Down Expand Up @@ -169,6 +175,8 @@ class CalendarSettings(private val context: Context) : ObservableSettings() {
// ------------ Text ---------------------------------------------------------------------------------------
internal fun defaultTextPaint(weekDay: WeekDay): Paint = defaultTextPaints[weekDay] ?: throw IllegalStateException("cannot find default Paint with weekDay - $weekDay")

internal var outsideTextPaint: Paint = initializeOutsideTextPaint()
internal var holidayTextPaint: Paint = initializeHolidayTextPaint()
internal var defaultTextPaints: Map<WeekDay, Paint> = initializedDefaultTextPaints()
internal var todayTextPaint: Paint = initializedTodayTextPaint()
internal var selectedTextPaint: Paint = initializeSelectedTextPaint()
Expand All @@ -178,6 +186,8 @@ class CalendarSettings(private val context: Context) : ObservableSettings() {
it to baseTextPaint.copy().colorFilter(textFilterColorMap[it]?.let { color -> PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP) } ?: throw IllegalStateException("Day color map for $it not found."))
}.toMap()

private fun initializeOutsideTextPaint() = baseTextPaint.copy().color(context.getStyledColor(android.R.attr.textColorPrimary, context.getColorCompat(R.color.light_calendar_view__day_today_text_color)))
private fun initializeHolidayTextPaint() = baseTextPaint.copy().color(context.getStyledColor(android.R.attr.textColorPrimary, context.getColorCompat(R.color.light_calendar_view__day_today_text_color)))
private fun initializedTodayTextPaint() = baseTextPaint.copy().color(context.getStyledColor(android.R.attr.textColorPrimary, context.getColorCompat(R.color.light_calendar_view__day_today_text_color)))
private fun initializeSelectedTextPaint() = baseTextPaint.copy().typeface(Typeface.DEFAULT_BOLD).color(context.getStyledColor(android.R.attr.textColorPrimaryInverse, context.getColorCompat(R.color.light_calendar_view__day_selected_text_color)))
private fun initializedSelectedTodayTextPaint() = baseTextPaint.copy().typeface(Typeface.DEFAULT_BOLD).color(context.getStyledColor(android.R.attr.textColorPrimaryInverse, context.getColorCompat(R.color.light_calendar_view__day_selected_today_text_color)))
Expand Down Expand Up @@ -206,6 +216,13 @@ class CalendarSettings(private val context: Context) : ObservableSettings() {
selectedTextPaint.color(colorStateList, State.SELECTED)
selectedTodayTextPaint.color(colorStateList, State.SELECTED_TODAY)
}
internal fun setOutsideTextColorStateList(color:Int) {
outsideTextPaint.color = color
}

internal fun setHolidayTextColorStateList(color: Int) {
holidayTextPaint.color = color
}

internal fun setTextFilterColor(weekDay: WeekDay, color: Int?) {
textFilterColorMap[weekDay] = color
Expand Down
Expand Up @@ -18,6 +18,7 @@ package jp.co.recruit_mp.android.lightcalendarview

import android.content.Context
import android.support.v4.view.ViewCompat
import android.text.format.DateUtils
import java.util.*

/**
Expand Down Expand Up @@ -106,7 +107,11 @@ class DayLayout(context: Context, settings: CalendarSettings, var month: Date) :
addView(instantiateDayView(cal.clone() as Calendar))
}
else -> {
addView(EmptyView(context, settings))
if (settings.displayOutside) {
addView(instantiateDayView(cal.clone() as Calendar).setOutside())
} else {
addView(EmptyView(context, settings))
}
}
}
cal.add(Calendar.DAY_OF_YEAR, 1)
Expand Down Expand Up @@ -135,8 +140,11 @@ class DayLayout(context: Context, settings: CalendarSettings, var month: Date) :

private fun setSelectedDay(view: DayView?) {
selectedDayView?.apply {
isSelected = false
updateState()
if (!settings.fixToday || !DateUtils.isToday(selectedDayView?.date!!.time)) {
// 今日の場合は常に丸を表示させる
isSelected = false
updateState()
}
}
selectedDayView = view?.apply {
isSelected = true
Expand Down
Expand Up @@ -54,6 +54,8 @@ class DayView(context: Context, settings: CalendarSettings, cal: Calendar) : Cel
private var accentsCenterY: Float = 0f

private var drawCircle: Boolean = false
private var isOutside: Boolean = false
private var isHoliday: Boolean = false

private var radius: Float = 0f
private var currentRadius: Float = 0f
Expand Down Expand Up @@ -110,6 +112,15 @@ class DayView(context: Context, settings: CalendarSettings, cal: Calendar) : Cel
animateAccents()
}

fun setOutside():DayView {
this.isOutside = true
return this
}
// 祝日に設定
fun setHoliday() {
this.isHoliday = true
updatePaint();
}
// 各アクセントの位置を設定する
private fun layoutAccents() {
// アクセント左端の位置を計算
Expand Down Expand Up @@ -184,7 +195,6 @@ class DayView(context: Context, settings: CalendarSettings, cal: Calendar) : Cel
/** 状態を更新する. {@link DayLayout} から呼ばれることを想定している. */
internal fun updateState() {
drawCircle = isSelected || fadeInAnim.isRunning || fadeOutAnim.isRunning

updatePaint()
}

Expand All @@ -209,6 +219,15 @@ class DayView(context: Context, settings: CalendarSettings, cal: Calendar) : Cel
textPaint = settings.dayView.todayTextPaint
accentPaint = settings.dayView.todayAccentPaint
}
isOutside -> {
textPaint = settings.dayView.outsideTextPaint
accentPaint = settings.dayView.defaultAccentPaint
}
// 祝日の設定
isHoliday -> {
textPaint = settings.dayView.holidayTextPaint
accentPaint = settings.dayView.defaultAccentPaint
}
else -> {
textPaint = settings.dayView.defaultTextPaint(weekDay)
accentPaint = settings.dayView.defaultAccentPaint
Expand Down
Expand Up @@ -79,6 +79,8 @@ class LightCalendarView(context: Context, attrs: AttributeSet? = null, defStyleA
R.styleable.LightCalendarView_lcv_selectionColor -> setSelectionColor(a.getColorStateList(attr))
R.styleable.LightCalendarView_lcv_accentColor -> setAccentColor(a.getColorStateList(attr))
R.styleable.LightCalendarView_lcv_firstDayOfWeek -> setFirstDayOfWeek(a.getInt(attr, 0))
R.styleable.LightCalendarView_lcv_outsideTextColor -> setOutsideTextColor(a.getColor(attr, 0))
R.styleable.LightCalendarView_lcv_holidayTextColor -> setHolidayTextColor(a.getColor(attr, 0))
}
}
a.recycle()
Expand Down Expand Up @@ -177,6 +179,23 @@ class LightCalendarView(context: Context, attrs: AttributeSet? = null, defStyleA
}.notifySettingsChanged()
}

/**
* 前月・翌月の文字色を設定する
*/
fun setOutsideTextColor(color: Int) {
settings.dayView.apply {
setOutsideTextColorStateList(color)
}.notifySettingsChanged()
}
/**
* 祝日の文字色を設定する
*/
fun setHolidayTextColor(color: Int) {
settings.dayView.apply {
setHolidayTextColorStateList(color)
}.notifySettingsChanged()
}

/**
* 日付ビューの選択時の背景色を設定する
*/
Expand Down Expand Up @@ -227,6 +246,13 @@ class LightCalendarView(context: Context, attrs: AttributeSet? = null, defStyleA
firstDayOfWeek = WeekDay.fromOrdinal(n)
}

var displayOutside: Boolean
get() = settings.displayOutside
set(value) {
settings.apply {
displayOutside = value
}.notifySettingsChanged()
}
/**
* Sets the timezone to use in LightCalendarView.
* Set null to use TimeZone.getDefault()
Expand All @@ -239,6 +265,13 @@ class LightCalendarView(context: Context, attrs: AttributeSet? = null, defStyleA
}.notifySettingsChanged()
}

var fixToday: Boolean
get() = settings.fixToday
set(value) {
settings.apply {
fixToday = value
}.notifySettingsChanged()
}
/**
* Sets the locale to use in LightCalendarView.
* Set null to use Locale.getDefault()
Expand Down
Expand Up @@ -59,6 +59,13 @@ class MonthView(context: Context, settings: CalendarSettings, var month: Date) :
dayLayout.invalidateDayViews()
}

// 祝日追加
fun setHolidays(map: Collection<Date>) {
map.forEach { it ->
val date = it
dayLayout.getDayView(date)?.setHoliday();
}
}

override fun toString(): String = "MonthView($month)"
}
4 changes: 3 additions & 1 deletion library/src/main/res/values/attrs.xml
Expand Up @@ -7,5 +7,7 @@
<attr name="lcv_selectionColor" format="reference|color" />
<attr name="lcv_accentColor" format="reference|color" />
<attr name="lcv_firstDayOfWeek" format="integer" />
<attr name="lcv_outsideTextColor" format="reference|color" />
<attr name="lcv_holidayTextColor" format="reference|color" />
</declare-styleable>
</resources>
</resources>