Skip to content

Commit

Permalink
Support year switching
Browse files Browse the repository at this point in the history
  • Loading branch information
kukadiajayesh committed Apr 17, 2023
1 parent 024fdba commit 441a72b
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.mkb.ethiopian.lib.models.DayAndDates
import com.mkb.ethiopian.lib.models.OnSelectListener
import java.util.Calendar
import java.util.Date
import java.util.concurrent.TimeUnit


class CustomCalendarView : LinearLayout {
Expand All @@ -23,8 +24,10 @@ class CustomCalendarView : LinearLayout {
removeTime(Calendar.getInstance())
}

private var mPreviousYearBtn: ImageView? = null
private var mPreviousBtn: ImageView? = null
private var mNextBtn: ImageView? = null
private var mNextYearBtn: ImageView? = null
private var mCurrentMonthEC: TextView? = null
private var mCurrentMonthGC: TextView? = null
private var mCalendarGridView: GridView? = null
Expand Down Expand Up @@ -163,6 +166,9 @@ class CustomCalendarView : LinearLayout {

mPreviousBtn = mainView.findViewById(R.id.previous_month_button)
mNextBtn = mainView.findViewById(R.id.next_month_button)
mPreviousYearBtn = mainView.findViewById(R.id.previous_year_button)
mNextYearBtn = mainView.findViewById(R.id.next_year_button)

mCurrentMonthEC = mainView.findViewById(R.id.month_display_EC)
mCurrentMonthGC = mainView.findViewById(R.id.moth_display_GC)
setCalendarHeaderLabel()
Expand All @@ -181,6 +187,15 @@ class CustomCalendarView : LinearLayout {
if (mCurrentEthiopianMonth > 1) mCurrentEthiopianMonth - 1 else 13
changeMonth()
}

mNextYearBtn?.setOnClickListener {
mCurrentEthiopianYear += 1
changeMonth()
}
mPreviousYearBtn?.setOnClickListener {
mCurrentEthiopianYear -= 1
changeMonth()
}
}

private fun changeMonth() {
Expand All @@ -198,13 +213,21 @@ class CustomCalendarView : LinearLayout {
timeInMillis = minDate!!
}
val minValues: IntArray = EthiopicCalendar(minCalendar).gregorianToEthiopic()
val prevDay = minValues[2]
val prevMonth = minValues[1]
val prevYear = minValues[0]

val enablePrevMonthArrow = if (prevMonth < mCurrentEthiopianMonth &&
prevYear <= mCurrentEthiopianYear
) true else prevYear < mCurrentEthiopianYear
mPreviousBtn?.enableView(enablePrevMonthArrow)

val ecal = EthiopicCalendar(mCurrentEthiopianYear, mCurrentEthiopianMonth, 1)
val greValues: IntArray = ecal.ethiopicToGregorian()

mPreviousYearBtn?.enableView(hasAYearDiff(
minDate!!, getTimeInMills(greValues[2], greValues[1], greValues[0])
))
}

private fun validateMaxDate() {
Expand All @@ -213,13 +236,20 @@ class CustomCalendarView : LinearLayout {
timeInMillis = maxDate!!
}
val maxValues: IntArray = EthiopicCalendar(maxCalendar).gregorianToEthiopic()
val nextDay = maxValues[2]
val nextMonth = maxValues[1]
val nextYear = maxValues[0]

val enableNextMonthArrow = if (nextMonth > mCurrentEthiopianMonth &&
nextYear >= mCurrentEthiopianYear
) true else nextYear > mCurrentEthiopianYear
mNextBtn?.enableView(enableNextMonthArrow)

val ecal = EthiopicCalendar(mCurrentEthiopianYear, mCurrentEthiopianMonth, 1)
val greValues: IntArray = ecal.ethiopicToGregorian()
mNextYearBtn?.enableView(hasAYearDiff(
getTimeInMills(greValues[2], greValues[1], greValues[0]), maxDate!!
))
}

private fun setCalendarHeaderLabel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import java.util.Calendar
import java.util.concurrent.TimeUnit

fun Context.getColorFromContext(@ColorInt resId: Int): Int {
val typedValue = TypedValue()
Expand Down Expand Up @@ -66,3 +67,21 @@ fun Calendar.isLessThanOrEqual(date: Long?): Boolean {
if (date == null) return false
return timeInMillis < date || isEqualDate(date)
}

fun hasAYearDiff(dateFrom: Long, dateTo: Long): Boolean {
return getDiffInDays(dateFrom, dateTo) >= 365
}

fun getTimeInMills(day: Int, month: Int, year: Int): Long {
return getCalendar(day, month, year).timeInMillis
}

fun getCalendar(day: Int, month: Int, year: Int): Calendar {
return Calendar.getInstance().apply {
set(year, month, day)
}
}

fun getDiffInDays(dateFrom: Long, dateTo: Long): Long {
return TimeUnit.MILLISECONDS.toDays(dateTo - dateFrom) + 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M17.59,18l1.41,-1.41l-4.58,-4.59l4.58,-4.59l-1.41,-1.41l-6,6z"/>
<path android:fillColor="@android:color/white" android:pathData="M11,18l1.41,-1.41l-4.58,-4.59l4.58,-4.59l-1.41,-1.41l-6,6z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6.41,6l-1.41,1.41l4.58,4.59l-4.58,4.59l1.41,1.41l6,-6z"/>
<path android:fillColor="@android:color/white" android:pathData="M13,6l-1.41,1.41l4.58,4.59l-4.58,4.59l1.41,1.41l6,-6z"/>
</vector>
25 changes: 23 additions & 2 deletions ethiopian-calendar-lib/src/main/res/layout/calendar_view.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">

<LinearLayout
Expand All @@ -17,12 +19,22 @@
android:id="@+id/previous_month_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="?selectableItemBackgroundBorderless"
android:baselineAligned="false"
android:padding="5dp"
android:src="@drawable/ic_navigate_before_black_24dp" />

<ImageView
android:id="@+id/previous_year_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?selectableItemBackgroundBorderless"
android:baselineAligned="false"
android:padding="5dp"
android:src="@drawable/double_arrow_left_24"
tools:tint="@color/colorWhite"
app:tint="@color/colorWhite" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -52,11 +64,20 @@
android:textStyle="bold" />
</LinearLayout>

<ImageView
android:id="@+id/next_year_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="?selectableItemBackgroundBorderless"
android:padding="5dp"
tools:tint="@color/colorWhite"
android:src="@drawable/double_arrow_right_24"
app:tint="@color/colorWhite" />

<ImageView
android:id="@+id/next_month_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:background="?selectableItemBackgroundBorderless"
android:padding="5dp"
android:src="@drawable/ic_navigate_next_black_24dp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FirstFragment : Fragment() {
it.set(2006, 9, 2); it.timeInMillis
},
minDate = Calendar.getInstance().let {
it.set(2006, 8, 2); it.timeInMillis
it.set(2004, 8, 2); it.timeInMillis
},
maxDate = Calendar.getInstance().let {
it.set(2006, 9, 13); it.timeInMillis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MainActivity : AppCompatActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)

val locale = Locale.forLanguageTag("am-ET")
val locale = Locale.forLanguageTag("om-ET")
Lingver.getInstance().setLocale(this, locale)

binding = ActivityMainBinding.inflate(layoutInflater)
Expand Down

0 comments on commit 441a72b

Please sign in to comment.