Skip to content

Commit e1c5d2c

Browse files
authored
Merge pull request #75 from FossifyOrg/add_first_day_of_week
Add configuration and helper methods related to day of week
2 parents 5c9248f + c079e7a commit e1c5d2c

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

commons/src/main/kotlin/org/fossify/commons/extensions/Context.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import org.fossify.commons.helpers.*
4949
import org.fossify.commons.helpers.MyContentProvider.PERMISSION_WRITE_GLOBAL_SETTINGS
5050
import org.fossify.commons.models.AlarmSound
5151
import org.fossify.commons.models.BlockedNumber
52+
import org.joda.time.DateTimeConstants
5253
import java.io.File
5354
import java.text.SimpleDateFormat
5455
import java.util.Date
@@ -1294,3 +1295,18 @@ fun Context.openFullScreenIntentSettings(appId: String) {
12941295
startActivity(intent)
12951296
}
12961297
}
1298+
1299+
fun Context.getDayOfWeekString(dayOfWeek: Int): String {
1300+
val dayOfWeekResId = when (dayOfWeek) {
1301+
DateTimeConstants.MONDAY -> org.fossify.commons.R.string.monday
1302+
DateTimeConstants.TUESDAY -> org.fossify.commons.R.string.tuesday
1303+
DateTimeConstants.WEDNESDAY -> org.fossify.commons.R.string.wednesday
1304+
DateTimeConstants.THURSDAY -> org.fossify.commons.R.string.thursday
1305+
DateTimeConstants.FRIDAY -> org.fossify.commons.R.string.friday
1306+
DateTimeConstants.SATURDAY -> org.fossify.commons.R.string.saturday
1307+
DateTimeConstants.SUNDAY -> org.fossify.commons.R.string.sunday
1308+
else -> throw IllegalArgumentException("Invalid day: $dayOfWeek")
1309+
}
1310+
1311+
return getString(dayOfWeekResId)
1312+
}

commons/src/main/kotlin/org/fossify/commons/extensions/List.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.fossify.commons.extensions
22

3+
import java.util.Collections
4+
35
fun List<String>.getMimeType(): String {
46
val mimeGroups = HashSet<String>(size)
57
val subtypes = HashSet<String>(size)
@@ -19,3 +21,13 @@ fun List<String>.getMimeType(): String {
1921
else -> "*/*"
2022
}
2123
}
24+
25+
fun <T> List<T>.rotate(distance: Int): List<T> {
26+
return toMutableList().apply {
27+
Collections.rotate(this, distance)
28+
}
29+
}
30+
31+
fun <T> List<T>.rotateRight(distance: Int) = rotate(distance)
32+
33+
fun <T> List<T>.rotateLeft(distance: Int) = rotate(-distance)

commons/src/main/kotlin/org/fossify/commons/helpers/BaseConfig.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,14 @@ open class BaseConfig(val context: Context) {
605605
get() = prefs.getLong(PASSWORD_COUNTDOWN_START_MS, 0L)
606606
set(passwordCountdownStartMs) = prefs.edit().putLong(PASSWORD_COUNTDOWN_START_MS, passwordCountdownStartMs).apply()
607607

608+
// Returns the first day of week, indexing follows ISO 8601: Mon=1, ..., Sun=7
609+
var firstDayOfWeek: Int
610+
get() {
611+
val defaultFirstDayOfWeek = Calendar.getInstance(Locale.getDefault()).firstDayOfWeek
612+
return prefs.getInt(FIRST_DAY_OF_WEEK, getISODayOfWeekFromJava(defaultFirstDayOfWeek))
613+
}
614+
set(firstDayOfWeek) = prefs.edit().putInt(FIRST_DAY_OF_WEEK, firstDayOfWeek).apply()
615+
608616
// Accessibility
609617
var showCheckmarksOnSwitches: Boolean
610618
get() = prefs.getBoolean(SHOW_CHECKMARKS_ON_SWITCHES, false)

commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.fossify.commons.R
1212
import org.fossify.commons.extensions.normalizeString
1313
import org.fossify.commons.models.contacts.LocalContact
1414
import org.fossify.commons.overloads.times
15+
import org.joda.time.DateTimeConstants
1516

1617
const val EXTERNAL_STORAGE_PROVIDER_AUTHORITY = "com.android.externalstorage.documents"
1718
const val EXTRA_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED"
@@ -197,6 +198,7 @@ const val PASSWORD_COUNTDOWN_START_MS = "password_count_down_start_ms"
197198
const val LAST_UNLOCK_TIMESTAMP_MS = "last_unlock_timestamp_ms"
198199
const val UNLOCK_TIMEOUT_DURATION_MS = "unlock_timeout_duration_ms"
199200
const val SHOW_CHECKMARKS_ON_SWITCHES = "show_checkmarks_on_switches"
201+
const val FIRST_DAY_OF_WEEK = "first_day_of_week"
200202

201203
const val MAX_PASSWORD_RETRY_COUNT = 3
202204
const val DEFAULT_PASSWORD_COUNTDOWN = 5
@@ -721,3 +723,23 @@ fun getProperText(text: String, shouldNormalize: Boolean) =
721723
shouldNormalize -> text.normalizeString()
722724
else -> text
723725
}
726+
727+
fun getISODayOfWeekFromJava(javaDayOfWeek: Int): Int {
728+
if (javaDayOfWeek !in 1..7) {
729+
throw IllegalArgumentException("Invalid Java day of week: $javaDayOfWeek")
730+
}
731+
732+
// Java: Sun=1, ..., Sat=7
733+
// ISO: Mon=1, ..., Sun=7
734+
return (javaDayOfWeek + 5) % 7 + 1
735+
}
736+
737+
fun getJavaDayOfWeekFromISO(isoDayOfWeek: Int): Int {
738+
if (isoDayOfWeek !in 1..7) {
739+
throw IllegalArgumentException("Invalid ISO day of week: $isoDayOfWeek")
740+
}
741+
742+
// ISO: Mon=1, ..., Sun=7
743+
// Java: Sun=1, ..., Sat=7
744+
return (isoDayOfWeek % 7) + 1
745+
}

0 commit comments

Comments
 (0)