From ba0f9c73a11ab0fde29acf3dc62f5f136b36a2f0 Mon Sep 17 00:00:00 2001 From: kirill Date: Thu, 17 Jan 2019 16:17:14 +0200 Subject: [PATCH] Implemented max drag and date in drag textView on day selection --- .../vishnu/implementation/MoveManagerImpl.kt | 43 ++++++++------ .../com/yalantis/vishnu/view/MonthPage.kt | 58 ++++++++++++++----- vishnu/src/main/res/values/strings.xml | 8 +++ 3 files changed, 78 insertions(+), 31 deletions(-) create mode 100644 vishnu/src/main/res/values/strings.xml diff --git a/vishnu/src/main/java/com/yalantis/vishnu/implementation/MoveManagerImpl.kt b/vishnu/src/main/java/com/yalantis/vishnu/implementation/MoveManagerImpl.kt index 36baa18..b3dabf3 100644 --- a/vishnu/src/main/java/com/yalantis/vishnu/implementation/MoveManagerImpl.kt +++ b/vishnu/src/main/java/com/yalantis/vishnu/implementation/MoveManagerImpl.kt @@ -118,27 +118,36 @@ class MoveManagerImpl(private val viewProvider: ViewProvider) : MoveManager { private fun calculateOffsets(touchY: Float) { val newHeight = (totalHeight * (touchY / totalHeight)) + dragHeight - if (touchY > topLimit) { + when { + touchY > topLimit && touchY < bottomLimit -> { + performMovement(newHeight, touchY) + } + touchY > bottomLimit -> { + performMovement(bottomLimit.toFloat() + dragHeight, bottomLimit.toFloat()) + } + touchY <= topLimit -> { + viewProvider.moveWeek(selectedWeek, topLimit.toFloat()) + viewProvider.setViewHeight(minHeight + dragHeight) + viewProvider.setDragTop(minHeight.toFloat()) + controlAboveSelected(touchY, overScroll = true) + controlBelowSelected(touchY, overScroll = true) + } + } + } - viewProvider.setViewHeight(newHeight.toInt()) - viewProvider.setDragTop(touchY) + private fun performMovement(newHeight: Float, touchY: Float) { + viewProvider.setViewHeight(newHeight.toInt()) + viewProvider.setDragTop(touchY) - for (week in 0 until weekCount) { - if (week < selectedWeek) { - moveWeek(week, viewProvider.getWeekBottom(week), touchY - weekHeight, defaultPositions[week]) - } else { - moveWeek(week, viewProvider.getWeekBottom(week), touchY, defaultPositions[week]) - } + for (week in 0 until weekCount) { + if (week < selectedWeek) { + moveWeek(week, viewProvider.getWeekBottom(week), touchY - weekHeight, defaultPositions[week]) + } else { + moveWeek(week, viewProvider.getWeekBottom(week), touchY, defaultPositions[week]) } - controlAboveSelected(touchY) - controlBelowSelected(touchY) - } else { - viewProvider.moveWeek(selectedWeek, topLimit.toFloat()) - viewProvider.setViewHeight(minHeight + dragHeight) - viewProvider.setDragTop(minHeight.toFloat()) - controlAboveSelected(touchY, overScroll = true) - controlBelowSelected(touchY, overScroll = true) } + controlAboveSelected(touchY) + controlBelowSelected(touchY) } /** diff --git a/vishnu/src/main/java/com/yalantis/vishnu/view/MonthPage.kt b/vishnu/src/main/java/com/yalantis/vishnu/view/MonthPage.kt index 7e69e7f..1ae1475 100644 --- a/vishnu/src/main/java/com/yalantis/vishnu/view/MonthPage.kt +++ b/vishnu/src/main/java/com/yalantis/vishnu/view/MonthPage.kt @@ -17,6 +17,7 @@ import com.yalantis.vishnu.implementation.DateManagerImpl import com.yalantis.vishnu.implementation.MoveManagerImpl import com.yalantis.vishnu.interfaces.* import com.yalantis.vishnu.model.* +import java.text.SimpleDateFormat import java.util.* import kotlin.collections.ArrayList @@ -129,20 +130,20 @@ class MonthPage(context: Context, stylable: VishnuStylable) : LinearLayout(conte } private fun obtainStylable(stylable: VishnuStylable) { - dragText = stylable.dragText - dragColor = stylable.dragColor - dragHeight = stylable.dragHeight - dayTypeface = stylable.dayTypeface - dragTextSize = stylable.dragTextSize - dragTextColor = stylable.dragTextColor - monthTypeface = stylable.monthTypeface - weekDayTypeface = stylable.weekDayTypeface - kalendarBackground = stylable.pageBackground - selectedDayDrawable = stylable.selectedDayDrawable - monthSwitchBackground = stylable.monthSwitchBackground - weekDayNamesBackground = stylable.weekDayNamesBackground - selectedWeekBackground = stylable.selectedWeekBackground - unselectedWeekBackground = stylable.unselectedWeekBackground + dragText = stylable.dragText + dragColor = stylable.dragColor + dragHeight = stylable.dragHeight + dayTypeface = stylable.dayTypeface + dragTextSize = stylable.dragTextSize + dragTextColor = stylable.dragTextColor + monthTypeface = stylable.monthTypeface + weekDayTypeface = stylable.weekDayTypeface + kalendarBackground = stylable.pageBackground + selectedDayDrawable = stylable.selectedDayDrawable + monthSwitchBackground = stylable.monthSwitchBackground + weekDayNamesBackground = stylable.weekDayNamesBackground + selectedWeekBackground = stylable.selectedWeekBackground + unselectedWeekBackground = stylable.unselectedWeekBackground } @@ -246,6 +247,7 @@ class MonthPage(context: Context, stylable: VishnuStylable) : LinearLayout(conte if (moveManager.isInAction.not() && moveManager.isCollapsed.not()) { changeDayColors(day) selectWeek(day) + applyDragText(dragTextFor(day)) } else { actionQueue.add(KAction(ACTION_SELECT_DAY)) moveManager.expand() @@ -254,6 +256,33 @@ class MonthPage(context: Context, stylable: VishnuStylable) : LinearLayout(conte } } + private fun applyDragText(dragText: String) { + (getChildAt(childCount - 1) as TextView).text = dragText + } + + private fun dragTextFor(day: Day): String { + val calendar = Calendar.getInstance() + calendar.add(Calendar.DAY_OF_YEAR, -1) + val yesterday = formatDate(calendar.time) + calendar.add(Calendar.DAY_OF_YEAR, 1) + val today = formatDate(calendar.time) + calendar.add(Calendar.DAY_OF_YEAR, 1) + val tomorrow = formatDate(calendar.time) + + calendar.time = day.date + + val selectedDay = formatDate(calendar.time) + + return when (selectedDay) { + today -> resources.getString(R.string.today) + yesterday -> resources.getString(R.string.yesterday) + tomorrow -> resources.getString(R.string.tomorrow) + else -> selectedDay + } + } + + private fun formatDate(date: Date) = SimpleDateFormat("dd MMM yyyy", Locale.getDefault()).format(date) + /** * Method creates day view which will be later displayed */ @@ -431,6 +460,7 @@ class MonthPage(context: Context, stylable: VishnuStylable) : LinearLayout(conte val weekHeight = getWeekHeight() collapsedHeight = switchHeight + weekHeight * 2 + dragHeight totalHeight = switchHeight + (weekHeight * (childCount - 2)) + dragHeight + var weekBottom = switchHeight + weekHeight for (i in 0 until childCount - 1) { weekBottom += weekHeight diff --git a/vishnu/src/main/res/values/strings.xml b/vishnu/src/main/res/values/strings.xml new file mode 100644 index 0000000..62f2995 --- /dev/null +++ b/vishnu/src/main/res/values/strings.xml @@ -0,0 +1,8 @@ + + + + Yesterday + Today + Tomorrow + + \ No newline at end of file