Skip to content

Commit c501f11

Browse files
authored
fix: rework current time indicator (#1294)
Refs: #99
1 parent d985d5e commit c501f11

9 files changed

Lines changed: 80 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Changed
1313
- Updated holiday data
14+
- Updated current time indicator in weekly view
1415

1516
### Fixed
1617
- Fixed CalDAV sync not consistently working on Android 11+ ([#656])

app/src/main/kotlin/org/fossify/calendar/fragments/WeekFragment.kt

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import android.view.MotionEvent
1616
import android.view.ScaleGestureDetector
1717
import android.view.View
1818
import android.view.ViewGroup
19-
import android.widget.ImageView
2019
import android.widget.RelativeLayout
2120
import androidx.collection.LongSparseArray
2221
import androidx.fragment.app.Fragment
@@ -61,6 +60,7 @@ import org.fossify.calendar.interfaces.WeekFragmentListener
6160
import org.fossify.calendar.interfaces.WeeklyCalendar
6261
import org.fossify.calendar.models.Event
6362
import org.fossify.calendar.models.EventWeeklyView
63+
import org.fossify.calendar.views.CurrentTimeIndicatorView
6464
import org.fossify.calendar.views.MyScrollView
6565
import org.fossify.commons.dialogs.RadioGroupDialog
6666
import org.fossify.commons.extensions.adjustAlpha
@@ -72,6 +72,7 @@ import org.fossify.commons.extensions.getContrastColor
7272
import org.fossify.commons.extensions.getProperPrimaryColor
7373
import org.fossify.commons.extensions.getProperTextColor
7474
import org.fossify.commons.extensions.hideKeyboard
75+
import org.fossify.commons.extensions.isRTLLayout
7576
import org.fossify.commons.extensions.onGlobalLayout
7677
import org.fossify.commons.extensions.realScreenSize
7778
import org.fossify.commons.extensions.removeBit
@@ -123,7 +124,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
123124
private var wasScaled = false
124125
private var isPrintVersion = false
125126
private var selectedGrid: View? = null
126-
private var currentTimeView: ImageView? = null
127+
private var currentTimeView: CurrentTimeIndicatorView? = null
127128
private var fadeOutHandler = Handler()
128129
private var allDayHolders = ArrayList<RelativeLayout>()
129130
private var allDayRows = ArrayList<HashSet<Int>>()
@@ -863,43 +864,45 @@ class WeekFragment : Fragment(), WeeklyCalendar {
863864
}
864865

865866
private fun addCurrentTimeIndicator() {
866-
if (todayColumnIndex != -1) {
867-
val calendar = Calendar.getInstance()
868-
val minutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
869-
if (todayColumnIndex >= dayColumns.size) {
870-
currentTimeView?.alpha = 0f
871-
return
872-
}
873-
874-
if (currentTimeView != null) {
875-
binding.weekEventsHolder.removeView(currentTimeView)
876-
}
867+
if (todayColumnIndex == -1) {
868+
return
869+
}
877870

878-
if (isPrintVersion) {
879-
return
880-
}
871+
val calendar = Calendar.getInstance()
872+
val minutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
873+
if (todayColumnIndex >= dayColumns.size) {
874+
currentTimeView?.alpha = 0f
875+
return
876+
}
881877

882-
val weeklyViewDays = config.weeklyViewDays
883-
currentTimeView = WeekNowMarkerBinding.inflate(layoutInflater).root.apply {
884-
applyColorFilter(primaryColor)
885-
binding.weekEventsHolder.addView(this)
886-
val extraWidth =
887-
res.getDimension(org.fossify.commons.R.dimen.activity_margin).toInt()
888-
val markerHeight = res.getDimension(R.dimen.weekly_view_now_height).toInt()
889-
val minuteHeight = rowHeight / 60
890-
(layoutParams as RelativeLayout.LayoutParams).apply {
891-
width = (binding.root.width / weeklyViewDays) + extraWidth
892-
height = markerHeight
893-
}
878+
if (currentTimeView != null) {
879+
binding.weekEventsHolder.removeView(currentTimeView)
880+
}
894881

895-
x = if (weeklyViewDays == 1) {
896-
0f
897-
} else {
898-
(binding.root.width / weeklyViewDays * todayColumnIndex).toFloat() - extraWidth / 2f
899-
}
882+
if (isPrintVersion) {
883+
return
884+
}
900885

901-
y = minutes * minuteHeight - markerHeight / 2
886+
currentTimeView = WeekNowMarkerBinding.inflate(layoutInflater).root.apply {
887+
binding.weekEventsHolder.addView(this)
888+
val todayColumn = dayColumns[todayColumnIndex]
889+
val dotSize = res.getDimension(R.dimen.weekly_view_now_dot_size)
890+
val dotRadius = dotSize / 2f
891+
val isRtl = requireContext().isRTLLayout
892+
val leftOverflow = if (isRtl) dotRadius else dotSize
893+
val rightOverflow = if (isRtl) dotSize else dotRadius
894+
val markerLeft = (todayColumn.left - leftOverflow).coerceAtLeast(0f)
895+
val markerRight = (todayColumn.right + rightOverflow).coerceAtMost(binding.weekEventsHolder.width.toFloat())
896+
val markerHeight = res.getDimensionPixelSize(R.dimen.weekly_view_now_height)
897+
val minuteHeight = rowHeight / 60
898+
(layoutParams as RelativeLayout.LayoutParams).apply {
899+
addRule(RelativeLayout.ALIGN_PARENT_LEFT)
900+
width = (markerRight - markerLeft).roundToInt()
901+
height = markerHeight
902902
}
903+
904+
x = markerLeft
905+
y = minutes * minuteHeight - markerHeight / 2
903906
}
904907
}
905908

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.fossify.calendar.views
2+
3+
import android.content.Context
4+
import android.graphics.Canvas
5+
import android.graphics.Paint
6+
import android.util.AttributeSet
7+
import android.view.View
8+
import org.fossify.calendar.R
9+
import org.fossify.commons.extensions.getProperTextColor
10+
11+
class CurrentTimeIndicatorView(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) {
12+
companion object {
13+
private const val LINE_WIDTH_DP = 1.5f
14+
private const val DOT_OVERLAP_DP = 1f
15+
}
16+
17+
private val density = resources.displayMetrics.density
18+
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
19+
color = context.getProperTextColor()
20+
strokeCap = Paint.Cap.ROUND
21+
strokeWidth = LINE_WIDTH_DP * density
22+
}
23+
private val dotRadius = resources.getDimension(R.dimen.weekly_view_now_dot_size) / 2f
24+
private val dotOverlap = DOT_OVERLAP_DP * density
25+
26+
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
27+
28+
override fun onDraw(canvas: Canvas) {
29+
super.onDraw(canvas)
30+
val centerY = height / 2f
31+
val isRtl = layoutDirection == LAYOUT_DIRECTION_RTL
32+
val dotCenterX = if (isRtl) width - dotRadius - dotOverlap else dotRadius + dotOverlap
33+
val lineEndX = if (isRtl) 0f else width.toFloat()
34+
35+
canvas.drawLine(dotCenterX, centerY, lineEndX, centerY, paint)
36+
canvas.drawCircle(dotCenterX, centerY, dotRadius, paint)
37+
}
38+
}
-437 Bytes
Binary file not shown.
-473 Bytes
Binary file not shown.
-682 Bytes
Binary file not shown.
-666 Bytes
Binary file not shown.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
2+
<org.fossify.calendar.views.CurrentTimeIndicatorView xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:scaleType="fitXY"
6-
android:src="@drawable/weekly_now" />
5+
android:importantForAccessibility="no" />

app/src/main/res/values/dimens.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
<dimen name="weekly_view_row_height">60dp</dimen>
1313
<dimen name="weekly_view_minimal_event_height">10dp</dimen>
14-
<dimen name="weekly_view_now_height">10dp</dimen>
14+
<dimen name="weekly_view_now_height">12dp</dimen>
15+
<dimen name="weekly_view_now_dot_size">10dp</dimen>
1516
<dimen name="weekly_view_min_day_label">50dp</dimen>
1617

1718
<dimen name="min_widget_width">250dp</dimen>

0 commit comments

Comments
 (0)