Skip to content

Commit

Permalink
Merge pull request #2 from DogusTeknoloji/feature/add_month_number_view
Browse files Browse the repository at this point in the history
Feature/add month number view
  • Loading branch information
BerkkanB authored Jan 10, 2022
2 parents bb287dd + 2f7dd54 commit d16c4dd
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/dt/calendarwork/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.dt.calendarwork.ui.theme.CalendarWorkTheme
import com.dt.composedatepicker.ComposeCalendar
import com.dt.composedatepicker.MonthViewType
import com.dt.composedatepicker.SelectDateListener
import java.util.*

Expand Down Expand Up @@ -51,6 +52,7 @@ fun MainScreen() {
maxDate = calendarMax.time,
locale = Locale("en"),
title = "Select Date",
monthViewType = null,
listener = object : SelectDateListener {
override fun onDateSelected(date: Date) {
Log.i("DENEME", date.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fun CalendarMonthView(
setShowMonths:(Boolean)->Unit,
setHeight:(Int)->Unit,
showOnlyMonth:Boolean,
themeColor:Color
themeColor:Color,
monthViewType: MonthViewType?
) {

val NUMBER_OF_ROW_ITEMS = 3
Expand All @@ -55,9 +56,10 @@ fun CalendarMonthView(
minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
setShowMonths = setShowMonths,
setShowMonths = setShowMonths,
showOnlyMonth = showOnlyMonth,
themeColor = themeColor
themeColor = themeColor,
monthViewType = monthViewType
)
numberOfElement += 1
}
Expand All @@ -81,9 +83,16 @@ fun MonthItem(
selectedYear: Int,
setShowMonths: (Boolean) -> Unit,
showOnlyMonth: Boolean,
themeColor:Color
themeColor:Color,
monthViewType: MonthViewType?
) {
val enabled = checkDate(minYear = minYear,maxYear = maxYear,selectedYear = selectedYear,maxMonth = maxMonth,minMonth = minMonth,numberOfElement = numberOfElement)
val monthText:String = when(monthViewType){
MonthViewType.ONLY_MONTH -> month.name.uppercase()
MonthViewType.ONLY_NUMBER -> numberOfElement.plus(1).toString()
MonthViewType.BOTH_NUMBER_AND_MONTH -> month.name.uppercase() + " " + "(${numberOfElement.plus(1)})"
else -> month.name.uppercase()
}
Box(modifier = Modifier
.background(color = if (month.name == selectedMonth) themeColor else Color.Transparent,
shape = RoundedCornerShape(100))
Expand All @@ -98,7 +107,7 @@ fun MonthItem(
}
},
contentAlignment = Alignment.Center) {
Text(text = month.name.uppercase(),
Text(text = monthText,
color = if (enabled && month.name == selectedMonth) Color.White
else if (enabled) Color.Black
else Color.Gray)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.dt.composedatepicker

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun CalendarMonthViewOneColumn(
monthList: List<MonthData>,
selectedMonth: MonthData,
setMonth: (MonthData) -> Unit,
minMonth: Int,
maxMonth: Int,
minYear: Int,
maxYear: Int,
selectedYear: Int,
setShowMonths: (Boolean) -> Unit,
setHeight: (Int) -> Unit,
showOnlyMonth: Boolean,
themeColor: Color,
) {

LazyColumn(modifier = Modifier
.padding(horizontal = 20.dp)
.padding(vertical = 20.dp)
.fillMaxHeight(0.7f)
.fillMaxWidth()
.onGloballyPositioned { setHeight(it.size.height) },
horizontalAlignment = Alignment.CenterHorizontally) {
items(items = monthList) { item ->
MonthItemOneColumn(
month = item,
index = item.index,
selectedMonth = selectedMonth.name,
setMonth = setMonth,
minMonth = minMonth,
maxMonth = maxMonth,
minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
setShowMonths = setShowMonths,
showOnlyMonth = showOnlyMonth,
themeColor = themeColor,
)
}
}
}

@Composable
fun MonthItemOneColumn(
month: MonthData,
selectedMonth: String,
setMonth: (MonthData) -> Unit,
index: Int,
minMonth: Int,
maxMonth: Int,
minYear: Int,
maxYear: Int,
selectedYear: Int,
setShowMonths: (Boolean) -> Unit,
showOnlyMonth: Boolean,
themeColor: Color,
) {
val enabled = checkDate(minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
maxMonth = maxMonth,
minMonth = minMonth,
numberOfElement = index)

Box(modifier = Modifier
.padding(vertical = 6.dp)
.clickable(indication = null,
interactionSource = remember { MutableInteractionSource() },
enabled = enabled) {
setMonth(month)
if (!showOnlyMonth) {
setShowMonths(false)
}
}) {
Text(text = index.plus(1).toString(),
fontSize = if (month.name == selectedMonth) 35.sp else 30.sp,
color = if (enabled && month.name == selectedMonth) themeColor
else if (enabled) Color.Black
else Color.Gray)
}
}

private fun checkDate(
minYear: Int,
maxYear: Int,
selectedYear: Int,
minMonth: Int,
maxMonth: Int,
numberOfElement: Int,
): Boolean {
if (minMonth == 0) return true
if (minYear == maxYear) return numberOfElement in minMonth..maxMonth
if (selectedYear == minYear) {
return numberOfElement >= minMonth
} else if (selectedYear == maxYear) {
if (numberOfElement > maxMonth) return false
}
return true
}
53 changes: 40 additions & 13 deletions library/src/main/java/com/dt/composedatepicker/ComposeCalendar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fun ComposeCalendar(
showOnlyYear: Boolean = false,
themeColor:Color = Color(0xFF614FF0),
negativeButtonTitle:String = "CANCEL",
positiveButtonTitle:String = "OK"
positiveButtonTitle:String = "OK",
monthViewType: MonthViewType? = MonthViewType.ONLY_MONTH
) {
if (showOnlyMonth && showOnlyYear) {
throw IllegalStateException("'showOnlyMonth' and 'showOnlyYear' states cannot be true at the same time")
Expand Down Expand Up @@ -115,18 +116,38 @@ fun ComposeCalendar(
themeColor=themeColor)
Crossfade(targetState = showMonths) {
when (it) {
true -> CalendarMonthView(selectedMonth = selectedMonth,
setMonth = setMonth,
minMonth = minMonth,
maxMonth = maxMonth,
setShowMonths = setShowMonths,
minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
monthList = monthList,
setHeight = setHeight,
showOnlyMonth = showOnlyMonth,
themeColor=themeColor)
true -> {
if (monthViewType == MonthViewType.ONLY_NUMBER_ONE_COLUMN){
CalendarMonthViewOneColumn(selectedMonth = selectedMonth,
setMonth = setMonth,
minMonth = minMonth,
maxMonth = maxMonth,
setShowMonths = setShowMonths,
minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
monthList = monthList,
setHeight = setHeight,
showOnlyMonth = showOnlyMonth,
themeColor=themeColor
)
}
else{
CalendarMonthView(selectedMonth = selectedMonth,
setMonth = setMonth,
minMonth = minMonth,
maxMonth = maxMonth,
setShowMonths = setShowMonths,
minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
monthList = monthList,
setHeight = setHeight,
showOnlyMonth = showOnlyMonth,
themeColor=themeColor,
monthViewType = monthViewType)
}
}
false -> CalendarYearView(selectedYear = selectedYear,
setYear = setYear,
minYear = minYear,
Expand All @@ -144,3 +165,9 @@ fun ComposeCalendar(
}
}
}
enum class MonthViewType {
ONLY_MONTH,
ONLY_NUMBER,
ONLY_NUMBER_ONE_COLUMN,
BOTH_NUMBER_AND_MONTH
}

0 comments on commit d16c4dd

Please sign in to comment.