Skip to content

Commit 7f3dbda

Browse files
authored
fix: scale down AM/PM in clock fragment (#218)
* fix: scale down AM/PM in clock fragment * fix: add a tiny bottom margin in stopwatch landscape layout
1 parent 870f5de commit 7f3dbda

5 files changed

Lines changed: 84 additions & 12 deletions

File tree

app/src/main/kotlin/org/fossify/clock/fragments/ClockFragment.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.view.LayoutInflater
77
import android.view.View
88
import android.view.ViewGroup
99
import androidx.fragment.app.Fragment
10-
import org.fossify.clock.R
1110
import org.fossify.clock.activities.SimpleActivity
1211
import org.fossify.clock.adapters.TimeZonesAdapter
1312
import org.fossify.clock.databinding.FragmentClockBinding
@@ -93,13 +92,6 @@ class ClockFragment : Fragment() {
9392
val hours = (passedSeconds / 3600) % 24
9493
val minutes = (passedSeconds / 60) % 60
9594
val seconds = passedSeconds % 60
96-
97-
val safeContext = context ?: return
98-
if (!safeContext.config.use24HourFormat) {
99-
binding.clockTime.textSize =
100-
resources.getDimension(R.dimen.clock_text_size_smaller) / resources.displayMetrics.density
101-
}
102-
10395
if (seconds == 0) {
10496
if (hours == 0 && minutes == 0) {
10597
updateDate()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.fossify.clock.views
2+
3+
import android.content.Context
4+
import android.text.SpannableString
5+
import android.text.Spanned
6+
import android.text.style.RelativeSizeSpan
7+
import android.util.AttributeSet
8+
import android.widget.TextClock
9+
import androidx.annotation.AttrRes
10+
import org.fossify.clock.extensions.config
11+
import java.text.DateFormatSymbols
12+
13+
private const val AM_PM_SCALE = 0.4f
14+
15+
class MyTextClock @JvmOverloads constructor(
16+
context: Context,
17+
attrs: AttributeSet? = null,
18+
@AttrRes defStyleAttr: Int = android.R.attr.textViewStyle,
19+
) : TextClock(context, attrs, defStyleAttr) {
20+
21+
private val amPmStrings by lazy {
22+
DateFormatSymbols.getInstance(
23+
resources.configuration.locales[0]
24+
).amPmStrings
25+
}
26+
27+
private var reenter = false
28+
29+
override fun setText(text: CharSequence?, type: BufferType?) {
30+
if (reenter) {
31+
super.setText(text, type)
32+
return
33+
}
34+
35+
if (context.config.use24HourFormat || text.isNullOrEmpty()) {
36+
super.setText(text, type)
37+
return
38+
}
39+
40+
val full = text.toString()
41+
var index = -1
42+
var amPmString: String? = null
43+
for (s in amPmStrings) {
44+
if (s.isNotEmpty()) {
45+
val i = full.indexOf(s, ignoreCase = true)
46+
if (i != -1) {
47+
index = i
48+
amPmString = s
49+
break
50+
}
51+
}
52+
}
53+
54+
if (index != -1 && amPmString != null) {
55+
val spannable = SpannableString(text)
56+
spannable.setSpan(
57+
RelativeSizeSpan(AM_PM_SCALE),
58+
index - 1, // including the space before AM/PM
59+
index + amPmString.length,
60+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
61+
)
62+
reenter = true
63+
64+
try {
65+
super.setText(spannable, type ?: BufferType.SPANNABLE)
66+
} finally {
67+
reenter = false
68+
}
69+
} else {
70+
super.setText(text, type)
71+
}
72+
}
73+
}

app/src/main/res/layout-land/fragment_stopwatch.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
android:background="?attr/selectableItemBackground"
1919
android:fontFeatureSettings="tnum"
2020
android:gravity="center_horizontal"
21+
android:layout_marginBottom="@dimen/medium_margin"
2122
android:includeFontPadding="false"
2223
android:maxLines="1"
2324
android:padding="@dimen/small_margin"

app/src/main/res/layout/fragment_clock.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent">
1313

14-
<TextClock
14+
<org.fossify.clock.views.MyTextClock
1515
android:id="@+id/clock_time"
1616
android:layout_width="match_parent"
1717
android:layout_height="wrap_content"
18+
android:layout_marginHorizontal="@dimen/big_margin"
1819
android:layout_marginTop="@dimen/activity_margin"
20+
android:layout_marginBottom="@dimen/small_margin"
21+
android:autoSizeMaxTextSize="@dimen/clock_text_size"
22+
android:autoSizeMinTextSize="@dimen/extra_big_text_size"
23+
android:autoSizeStepGranularity="2sp"
24+
android:autoSizeTextType="uniform"
1925
android:fontFeatureSettings="tnum"
2026
android:gravity="center_horizontal"
27+
android:maxLines="1"
2128
android:textSize="@dimen/clock_text_size"
2229
tools:text="00:00:00" />
2330

@@ -39,7 +46,7 @@
3946
android:layout_below="@+id/clock_date"
4047
android:layout_centerHorizontal="true"
4148
android:layout_marginTop="@dimen/medium_margin"
42-
android:drawableLeft="@drawable/ic_clock_small"
49+
android:drawableStart="@drawable/ic_clock_small"
4350
android:drawablePadding="@dimen/small_margin"
4451
android:gravity="center_horizontal"
4552
android:textSize="@dimen/big_text_size"

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
<dimen name="textview_drawable_size">24dp</dimen>
1414
<dimen name="drag_handle_size">40dp</dimen>
1515

16-
<dimen name="clock_text_size">70sp</dimen>
17-
<dimen name="clock_text_size_smaller">60sp</dimen>
16+
<dimen name="clock_text_size">80sp</dimen>
1817
<dimen name="alarm_text_size">60sp</dimen>
1918
<dimen name="timer_text_size">@dimen/alarm_text_size</dimen>
2019
<dimen name="stopwatch_text_size">80sp</dimen>

0 commit comments

Comments
 (0)