Skip to content

Commit 15455e3

Browse files
author
Shrikanth Ravi
authored
Merge pull request #49 from karangoel16/internationalization_support
Internationalization support
2 parents a12ec20 + 8bf86c1 commit 15455e3

File tree

6 files changed

+92
-25
lines changed

6 files changed

+92
-25
lines changed

collapsiblecalendarview2/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies {
4646
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
4747
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4848
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
49+
implementation 'androidx.cardview:cardview:1.0.0'
4950
}
5051
repositories {
5152
mavenCentral()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.shrikanthravi.collapsiblecalendarview.view
2+
3+
class BounceAnimator(amplitude: Double, frequency: Double) : android.view.animation.Interpolator {
4+
private var mAmplitude = 1.0
5+
private var mFrequency = 10.0
6+
7+
init {
8+
mAmplitude = amplitude
9+
mFrequency = frequency
10+
}
11+
12+
override fun getInterpolation(time: Float): Float {
13+
return (-1.0 * Math.pow(Math.E, -time / mAmplitude) *
14+
Math.cos(mFrequency * time) + 1).toFloat()
15+
}
16+
}

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/view/LockScrollView.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class LockScrollView : ScrollView {
3030
}
3131
}
3232

33+
@SuppressLint("ClickableViewAccessibility")
3334
override fun onTouchEvent(ev: MotionEvent): Boolean {
3435
super.onTouchEvent(ev)
3536
return true

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/CollapsibleCalendar.kt

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.util.AttributeSet
1212
import android.view.View
1313
import android.view.ViewGroup
1414
import android.view.animation.Animation
15+
import android.view.animation.AnimationUtils
1516
import android.view.animation.Transformation
1617
import android.widget.LinearLayout
1718
import android.widget.TableLayout
@@ -21,17 +22,20 @@ import com.shrikanthravi.collapsiblecalendarview.R
2122
import com.shrikanthravi.collapsiblecalendarview.data.CalendarAdapter
2223
import com.shrikanthravi.collapsiblecalendarview.data.Day
2324
import com.shrikanthravi.collapsiblecalendarview.data.Event
25+
import com.shrikanthravi.collapsiblecalendarview.view.BounceAnimator
2426
import com.shrikanthravi.collapsiblecalendarview.view.ExpandIconView
27+
import java.text.DateFormatSymbols
2528
import java.text.SimpleDateFormat
2629
import java.util.*
2730

31+
2832
class CollapsibleCalendar : UICalendar, View.OnClickListener {
2933
override fun changeToToday() {
3034
val calendar = Calendar.getInstance()
31-
var calenderAdapter = CalendarAdapter(context, calendar);
35+
val calenderAdapter = CalendarAdapter(context, calendar);
3236
calenderAdapter.mEventList = mAdapter!!.mEventList
3337
calenderAdapter.setFirstDayOfWeek(firstDayOfWeek)
34-
var today = GregorianCalendar()
38+
val today = GregorianCalendar()
3539
this.selectedItem = null
3640
this.selectedItemPosition = -1
3741
this.selectedDay = Day(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH))
@@ -252,26 +256,29 @@ class CollapsibleCalendar : UICalendar, View.OnClickListener {
252256
override fun reload() {
253257
mAdapter?.let { mAdapter ->
254258
mAdapter.refresh()
255-
259+
val calendar = Calendar.getInstance()
260+
val tempDatePattern: String
261+
if (calendar.get(Calendar.YEAR) != mAdapter.calendar.get(Calendar.YEAR)) {
262+
tempDatePattern = "MMMM YYYY"
263+
} else {
264+
tempDatePattern = datePattern
265+
}
256266
// reset UI
257-
val dateFormat = SimpleDateFormat(datePattern)
267+
val dateFormat = SimpleDateFormat(tempDatePattern, getCurrentLocale(context))
258268
dateFormat.timeZone = mAdapter.calendar.timeZone
259269
mTxtTitle.text = dateFormat.format(mAdapter.calendar.time)
260270
mTableHead.removeAllViews()
261271
mTableBody.removeAllViews()
262272

263273
var rowCurrent: TableRow
264-
265-
// set day of week
266-
val dayOfWeekIds = intArrayOf(R.string.sunday, R.string.monday, R.string.tuesday, R.string.wednesday, R.string.thursday, R.string.friday, R.string.saturday)
267274
rowCurrent = TableRow(context)
268275
rowCurrent.layoutParams = TableLayout.LayoutParams(
269276
ViewGroup.LayoutParams.MATCH_PARENT,
270277
ViewGroup.LayoutParams.WRAP_CONTENT)
271278
for (i in 0..6) {
272279
val view = mInflater.inflate(R.layout.layout_day_of_week, null)
273280
val txtDayOfWeek = view.findViewById<View>(R.id.txt_day_of_week) as TextView
274-
txtDayOfWeek.setText(dayOfWeekIds[(i + firstDayOfWeek) % 7])
281+
txtDayOfWeek.setText(DateFormatSymbols().getShortWeekdays()[(i + firstDayOfWeek) % 7 + 1])
275282
view.layoutParams = TableRow.LayoutParams(
276283
0,
277284
ViewGroup.LayoutParams.WRAP_CONTENT,
@@ -298,6 +305,7 @@ class CollapsibleCalendar : UICalendar, View.OnClickListener {
298305
params.let { params ->
299306
if (params != null && (mAdapter.getItem(i).diff < params.prevDays || mAdapter.getItem(i).diff > params.nextDaysBlocked)) {
300307
view.isClickable = false
308+
view.alpha = 0.3f
301309
} else {
302310
view.setOnClickListener { v -> onItemClicked(v, mAdapter.getItem(i)) }
303311
}
@@ -365,27 +373,46 @@ class CollapsibleCalendar : UICalendar, View.OnClickListener {
365373

366374
fun prevMonth() {
367375
val cal = mAdapter!!.calendar
368-
if (cal.get(Calendar.MONTH) == cal.getActualMinimum(Calendar.MONTH)) {
369-
cal.set(cal.get(Calendar.YEAR) - 1, cal.getActualMaximum(Calendar.MONTH), 1)
370-
} else {
371-
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1)
372-
}
373-
reload()
374-
if (mListener != null) {
375-
mListener!!.onMonthChange()
376+
params.let {
377+
if (it != null && (Calendar.getInstance().get(Calendar.YEAR) * 12 + Calendar.getInstance().get(Calendar.MONTH) + it.prevDays / 30) > (cal.get(Calendar.YEAR) * 12 + cal.get(Calendar.MONTH))) {
378+
val myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce)
379+
val interpolator = BounceAnimator(0.1, 10.0)
380+
myAnim.setInterpolator(interpolator)
381+
mLayoutRoot.startAnimation(myAnim)
382+
return
383+
}
384+
if (cal.get(Calendar.MONTH) == cal.getActualMinimum(Calendar.MONTH)) {
385+
cal.set(cal.get(Calendar.YEAR) - 1, cal.getActualMaximum(Calendar.MONTH), 1)
386+
} else {
387+
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1)
388+
}
389+
reload()
390+
if (mListener != null) {
391+
mListener!!.onMonthChange()
392+
}
376393
}
394+
377395
}
378396

379397
fun nextMonth() {
380398
val cal = mAdapter!!.calendar
381-
if (cal.get(Calendar.MONTH) == cal.getActualMaximum(Calendar.MONTH)) {
382-
cal.set(cal.get(Calendar.YEAR) + 1, cal.getActualMinimum(Calendar.MONTH), 1)
383-
} else {
384-
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1)
385-
}
386-
reload()
387-
if (mListener != null) {
388-
mListener!!.onMonthChange()
399+
params.let {
400+
if (it != null && (Calendar.getInstance().get(Calendar.YEAR) * 12 + Calendar.getInstance().get(Calendar.MONTH) + it.nextDaysBlocked / 30) < (cal.get(Calendar.YEAR) * 12 + cal.get(Calendar.MONTH))) {
401+
val myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce)
402+
val interpolator = BounceAnimator(0.1, 10.0)
403+
myAnim.setInterpolator(interpolator)
404+
this.startAnimation(myAnim)
405+
return
406+
}
407+
if (cal.get(Calendar.MONTH) == cal.getActualMaximum(Calendar.MONTH)) {
408+
cal.set(cal.get(Calendar.YEAR) + 1, cal.getActualMinimum(Calendar.MONTH), 1)
409+
} else {
410+
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1)
411+
}
412+
reload()
413+
if (mListener != null) {
414+
mListener!!.onMonthChange()
415+
}
389416
}
390417
}
391418

@@ -559,7 +586,7 @@ class CollapsibleCalendar : UICalendar, View.OnClickListener {
559586
mScrollViewBody.requestLayout()
560587

561588
if (interpolatedTime == 1f) {
562-
state = UICalendar.Companion.STATE_EXPANDED
589+
state = STATE_EXPANDED
563590

564591
mBtnPrevMonth.isClickable = true
565592
mBtnNextMonth.isClickable = true

collapsiblecalendarview2/src/main/java/com/shrikanthravi/collapsiblecalendarview/widget/UICalendar.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import com.shrikanthravi.collapsiblecalendarview.view.ExpandIconView
1616
import com.shrikanthravi.collapsiblecalendarview.view.LockScrollView
1717
import com.shrikanthravi.collapsiblecalendarview.view.OnSwipeTouchListener
1818
import java.util.*
19+
import android.os.Build
20+
21+
1922

2023
@SuppressLint("ClickableViewAccessibility")
2124
abstract class UICalendar constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : ScrollView(context, attrs, defStyleAttr) {
@@ -179,6 +182,15 @@ abstract class UICalendar constructor(context: Context, attrs: AttributeSet? = n
179182
}
180183
}
181184

185+
fun getCurrentLocale(context: Context): Locale {
186+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
187+
context.resources.configuration.locales.get(0)
188+
} else {
189+
190+
context.resources.configuration.locale
191+
}
192+
}
193+
182194
init {
183195
mInflater = LayoutInflater.from(context)
184196

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:fillAfter="true"
4+
android:interpolator="@android:anim/bounce_interpolator">
5+
6+
<translate
7+
android:duration="300"
8+
android:fromXDelta="100%p"
9+
android:toXDelta="0%p" />
10+
</set>

0 commit comments

Comments
 (0)