Skip to content

Commit 529eb00

Browse files
authored
fix: address various uncaught exceptions (#248)
* fix: address NPE in setupAlarms * fix: address IllegalStateException in TimerFragment * style: add missing empty line * fix: address IndexOutOfBoundsException in MyTextClock * docs: update changelog
1 parent 9b67957 commit 529eb00

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Fixed
10+
- Fixed crash on startup ([#247])
11+
912
## [1.3.0] - 2025-07-13
1013
### Changed
1114
- Updated translations
@@ -78,6 +81,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7881
[#158]: https://github.com/FossifyOrg/Clock/issues/158
7982
[#206]: https://github.com/FossifyOrg/Clock/issues/206
8083
[#207]: https://github.com/FossifyOrg/Clock/issues/207
84+
[#247]: https://github.com/FossifyOrg/Clock/issues/247
8185

8286
[Unreleased]: https://github.com/FossifyOrg/Clock/compare/1.3.0...HEAD
8387
[1.3.0]: https://github.com/FossifyOrg/Clock/compare/1.2.2...1.3.0

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
140140
private fun setupAlarms() {
141141
getSortedAlarms { sortedAlarms ->
142142
alarms = sortedAlarms
143+
val safeActivity = activity as? SimpleActivity ?: return@getSortedAlarms
143144
var currAdapter = binding.alarmsList.adapter as? AlarmsAdapter
144145
if (currAdapter == null) {
145146
currAdapter = AlarmsAdapter(
146-
activity = activity as SimpleActivity,
147+
activity = safeActivity,
147148
alarms = alarms,
148149
toggleAlarmInterface = this,
149150
recyclerView = binding.alarmsList
@@ -154,12 +155,9 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
154155
}
155156
} else {
156157
currAdapter.apply {
157-
val context = context
158158
updatePrimaryColor()
159-
if (context != null) {
160-
updateBackgroundColor(context.getProperBackgroundColor())
161-
updateTextColor(context.getProperTextColor())
162-
}
159+
updateBackgroundColor(safeActivity.getProperBackgroundColor())
160+
updateTextColor(safeActivity.getProperTextColor())
163161
updateItems(alarms)
164162
}
165163
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class TimerFragment : Fragment() {
112112

113113
private fun getSortedTimers(callback: (List<Timer>) -> Unit) {
114114
activity?.timerHelper?.getTimers { timers ->
115-
val sortedTimers = when (requireContext().config.timerSort) {
115+
val safeContext = context ?: return@getTimers
116+
val sortedTimers = when (safeContext.config.timerSort) {
116117
SORT_BY_TIMER_DURATION -> timers.sortedBy { it.seconds }
117118
SORT_BY_DATE_CREATED -> timers.sortedBy { it.id }
118119
SORT_BY_CUSTOM -> {

app/src/main/kotlin/org/fossify/clock/views/MyTextClock.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,36 @@ class MyTextClock @JvmOverloads constructor(
3838
}
3939

4040
val full = text.toString()
41-
var index = -1
41+
var amPmPosition = -1
4242
var amPmString: String? = null
4343
for (s in amPmStrings) {
4444
if (s.isNotEmpty()) {
4545
val i = full.indexOf(s, ignoreCase = true)
4646
if (i != -1) {
47-
index = i
47+
amPmPosition = i
4848
amPmString = s
4949
break
5050
}
5151
}
5252
}
5353

54-
if (index != -1 && amPmString != null) {
54+
if (amPmPosition != -1 && amPmString != null) {
5555
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-
)
56+
val startIndex = if (amPmPosition > 0 && full[amPmPosition - 1].isWhitespace()) {
57+
amPmPosition - 1
58+
} else {
59+
amPmPosition
60+
}
61+
val endIndex = amPmPosition + amPmString.length
62+
if (startIndex < endIndex) {
63+
spannable.setSpan(
64+
RelativeSizeSpan(AM_PM_SCALE),
65+
startIndex,
66+
endIndex,
67+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
68+
)
69+
}
70+
6271
reenter = true
6372

6473
try {
@@ -70,4 +79,4 @@ class MyTextClock @JvmOverloads constructor(
7079
super.setText(text, type)
7180
}
7281
}
73-
}
82+
}

0 commit comments

Comments
 (0)