Skip to content

Commit

Permalink
Merge pull request #5 from DogusTeknoloji/feature/height_changes_fix
Browse files Browse the repository at this point in the history
Feature/height changes fix
  • Loading branch information
BerkkanB authored Feb 23, 2022
2 parents 409b2bd + 46ffae4 commit 93bb5a7
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 134 deletions.
58 changes: 37 additions & 21 deletions app/src/main/java/com/dt/calendarwork/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -34,38 +36,52 @@ class MainActivity : ComponentActivity() {
@Composable
fun MainScreen() {
val calendar = Calendar.getInstance()
calendar.set(Calendar.YEAR, 2021)
calendar.set(Calendar.MONTH, 5)
calendar.set(Calendar.YEAR, 2022)
calendar.set(Calendar.MONTH, 1)
calendar.set(Calendar.DAY_OF_MONTH, 1)

val calendarMax = Calendar.getInstance()
calendarMax.set(Calendar.YEAR, 2022)
calendarMax.set(Calendar.YEAR, 2023)
calendarMax.set(Calendar.MONTH, 0)
calendar.set(Calendar.DAY_OF_MONTH, 1)
calendarMax.set(Calendar.DAY_OF_MONTH, 1)

val initialCalendar = Calendar.getInstance()
initialCalendar.set(Calendar.YEAR, 2022)
initialCalendar.set(Calendar.MONTH, 3)
initialCalendar.set(Calendar.DAY_OF_MONTH, 1)

val (open, setOpen) = remember {
mutableStateOf(true)
}

Box(Modifier
.fillMaxSize()
.background(color = Color.Gray), contentAlignment = Alignment.Center) {
Box(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Gray),
contentAlignment = Alignment.Center
) {
if (open) {
ComposeCalendar(
minDate = calendar.time,
maxDate = calendarMax.time,
locale = Locale("en"),
title = "Select Date",
monthViewType = MonthViewType.ONLY_NUMBER_ONE_COLUMN,
listener = object : SelectDateListener {
override fun onDateSelected(date: Date) {
Log.i("DENEME", date.toString())
}
Box(
Modifier
.fillMaxWidth(0.8f)
.fillMaxHeight(0.7f)) {
ComposeCalendar(
minDate = calendar.time,
maxDate = calendarMax.time,
initialDate = initialCalendar.time,
locale = Locale("en"),
title = "Select Date",
monthViewType = MonthViewType.ONLY_NUMBER_ONE_COLUMN,
listener = object : SelectDateListener {
override fun onDateSelected(date: Date) {
Log.i("DENEME", date.toString())
}

override fun onCanceled() {
setOpen(false)
}
})
override fun onCanceled() {
setOpen(false)
}
})
}
}
}
}
23 changes: 16 additions & 7 deletions library/src/main/java/com/dt/composedatepicker/CalendarBottom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ package com.dt.composedatepicker
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

@Composable
fun CalendarBottom(onPositiveClick: () -> Unit, onCancelClick: () -> Unit,themeColor:Color,negativeButtonTitle:String,positiveButtonTitle:String) {
Row(modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 40.dp)
.padding(vertical = 20.dp),
horizontalArrangement = Arrangement.End) {
fun CalendarBottom(
onPositiveClick: () -> Unit,
onCancelClick: () -> Unit,
themeColor: Color,
negativeButtonTitle: String,
positiveButtonTitle: String
) {
Row(
modifier = Modifier
.fillMaxSize()
.padding(end = 20.dp),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically
) {
Text(text = negativeButtonTitle,
color = themeColor,
modifier = Modifier
Expand Down
122 changes: 78 additions & 44 deletions library/src/main/java/com/dt/composedatepicker/CalendarMonthView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package com.dt.composedatepicker
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
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.foundation.shape.RoundedCornerShape
Expand All @@ -13,39 +19,41 @@ 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 java.util.*

@Composable
fun CalendarMonthView(
monthList:List<MonthData>,
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,
minYear: Int,
maxYear: Int,
selectedYear: Int,
setShowMonths: (Boolean) -> Unit,
showOnlyMonth: Boolean,
themeColor: Color,
monthViewType: MonthViewType?
) {

val NUMBER_OF_ROW_ITEMS = 3
var numberOfElement = 0

LazyColumn(modifier = Modifier
.padding(horizontal = 20.dp)
.padding(vertical = 20.dp)
.onGloballyPositioned { setHeight(it.size.height) }) {
LazyColumn(
modifier = Modifier
.fillMaxHeight(0.85f)
.fillMaxWidth()
.padding(10.dp)
) {
items(items = monthList.chunked(NUMBER_OF_ROW_ITEMS)) { rowItems ->
Row(modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
for ((index, item) in rowItems.withIndex()) {
MonthItem(month = item,
MonthItem(
month = item,
index = index,
numberOfElement = numberOfElement,
rowSize = NUMBER_OF_ROW_ITEMS,
Expand Down Expand Up @@ -83,44 +91,70 @@ 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){
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)})"
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))
.fillMaxWidth(1f / (rowSize - index + 1f))
.aspectRatio(1f)
.clickable(indication = null,
interactionSource = remember { MutableInteractionSource() },
enabled = enabled) {
setMonth(month)
if (!showOnlyMonth) {
setShowMonths(false)
}
},
contentAlignment = Alignment.Center) {
Text(text = monthText,
Box(
modifier = Modifier
.background(
color = if (month.name == selectedMonth) themeColor else Color.Transparent,
shape = RoundedCornerShape(100)
)
.fillMaxWidth(1f / (rowSize - index + 1f))
.aspectRatio(1f)
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() },
enabled = enabled
) {
setMonth(month)
if (!showOnlyMonth) {
setShowMonths(false)
}
},
contentAlignment = Alignment.Center
) {
Text(
text = monthText,
color = if (enabled && month.name == selectedMonth) Color.White
else if (enabled) Color.Black
else Color.Gray)
else Color.Gray
)
}
}
private fun checkDate(minYear: Int,maxYear: Int,selectedYear: Int,minMonth: Int,maxMonth: Int,numberOfElement: Int):Boolean{

private fun checkDate(
minYear: Int,
maxYear: Int,
selectedYear: Int,
minMonth: Int,
maxMonth: Int,
numberOfElement: Int
): Boolean {

if (minYear == maxYear) return numberOfElement in minMonth..maxMonth
if (selectedYear==minYear){
return numberOfElement>=minMonth
}
else if (selectedYear==maxYear) {
if (numberOfElement>maxMonth) return false
if (selectedYear == minYear) {
return numberOfElement >= minMonth
} else if (selectedYear == maxYear) {
if (numberOfElement > maxMonth) return false
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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

Expand All @@ -29,18 +28,17 @@ fun CalendarMonthViewOneColumn(
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) {
LazyColumn(
modifier = Modifier
.fillMaxHeight(0.85f)
.fillMaxWidth()
.padding(10.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
items(items = monthList) { item ->
MonthItemOneColumn(
month = item,
Expand Down Expand Up @@ -75,28 +73,34 @@ fun MonthItemOneColumn(
showOnlyMonth: Boolean,
themeColor: Color,
) {
val enabled = checkDate(minYear = minYear,
val enabled = checkDate(
minYear = minYear,
maxYear = maxYear,
selectedYear = selectedYear,
maxMonth = maxMonth,
minMonth = minMonth,
numberOfElement = index)
numberOfElement = index
)

Box(modifier = Modifier
.padding(vertical = 6.dp)
.clickable(indication = null,
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() },
enabled = enabled) {
enabled = enabled
) {
setMonth(month)
if (!showOnlyMonth) {
setShowMonths(false)
}
}) {
Text(text = index.plus(1).toString(),
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)
else Color.Gray
)
}
}

Expand Down
Loading

0 comments on commit 93bb5a7

Please sign in to comment.