Skip to content

Commit

Permalink
Add helper functions for handling first day of week
Browse files Browse the repository at this point in the history
  • Loading branch information
naveensingh committed Feb 7, 2025
1 parent 91ffc6a commit c079e7a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import org.fossify.commons.helpers.*
import org.fossify.commons.helpers.MyContentProvider.PERMISSION_WRITE_GLOBAL_SETTINGS
import org.fossify.commons.models.AlarmSound
import org.fossify.commons.models.BlockedNumber
import org.joda.time.DateTimeConstants
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
Expand Down Expand Up @@ -1259,3 +1260,18 @@ fun Context.openFullScreenIntentSettings(appId: String) {
startActivity(intent)
}
}

fun Context.getDayOfWeekString(dayOfWeek: Int): String {
val dayOfWeekResId = when (dayOfWeek) {
DateTimeConstants.MONDAY -> org.fossify.commons.R.string.monday
DateTimeConstants.TUESDAY -> org.fossify.commons.R.string.tuesday
DateTimeConstants.WEDNESDAY -> org.fossify.commons.R.string.wednesday
DateTimeConstants.THURSDAY -> org.fossify.commons.R.string.thursday
DateTimeConstants.FRIDAY -> org.fossify.commons.R.string.friday
DateTimeConstants.SATURDAY -> org.fossify.commons.R.string.saturday
DateTimeConstants.SUNDAY -> org.fossify.commons.R.string.sunday
else -> throw IllegalArgumentException("Invalid day: $dayOfWeek")
}

return getString(dayOfWeekResId)
}
12 changes: 12 additions & 0 deletions commons/src/main/kotlin/org/fossify/commons/extensions/List.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.fossify.commons.extensions

import java.util.Collections

fun List<String>.getMimeType(): String {
val mimeGroups = HashSet<String>(size)
val subtypes = HashSet<String>(size)
Expand All @@ -19,3 +21,13 @@ fun List<String>.getMimeType(): String {
else -> "*/*"
}
}

fun <T> List<T>.rotate(distance: Int): List<T> {
return toMutableList().apply {
Collections.rotate(this, distance)
}
}

fun <T> List<T>.rotateRight(distance: Int) = rotate(distance)

fun <T> List<T>.rotateLeft(distance: Int) = rotate(-distance)
21 changes: 21 additions & 0 deletions commons/src/main/kotlin/org/fossify/commons/helpers/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.fossify.commons.R
import org.fossify.commons.extensions.normalizeString
import org.fossify.commons.models.contacts.LocalContact
import org.fossify.commons.overloads.times
import org.joda.time.DateTimeConstants

const val EXTERNAL_STORAGE_PROVIDER_AUTHORITY = "com.android.externalstorage.documents"
const val EXTRA_SHOW_ADVANCED = "android.content.extra.SHOW_ADVANCED"
Expand Down Expand Up @@ -721,3 +722,23 @@ fun getProperText(text: String, shouldNormalize: Boolean) =
shouldNormalize -> text.normalizeString()
else -> text
}

fun getISODayOfWeekFromJava(javaDayOfWeek: Int): Int {
if (javaDayOfWeek !in 1..7) {
throw IllegalArgumentException("Invalid Java day of week: $javaDayOfWeek")
}

// Java: Sun=1, ..., Sat=7
// ISO: Mon=1, ..., Sun=7
return (javaDayOfWeek + 5) % 7 + 1
}

fun getJavaDayOfWeekFromISO(isoDayOfWeek: Int): Int {
if (isoDayOfWeek !in 1..7) {
throw IllegalArgumentException("Invalid ISO day of week: $isoDayOfWeek")
}

// ISO: Mon=1, ..., Sun=7
// Java: Sun=1, ..., Sat=7
return (isoDayOfWeek % 7) + 1
}

0 comments on commit c079e7a

Please sign in to comment.