Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added choosing first day of the week (#19) #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/kotlin/org/fossify/clock/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MainActivity : SimpleActivity() {
initFragments()
setupTabs()
updateWidgets()
setFirstDayOfTheWeek()

getEnabledAlarms { enabledAlarms ->
if (enabledAlarms.isNullOrEmpty()) {
Expand Down Expand Up @@ -298,4 +299,13 @@ class MainActivity : SimpleActivity() {

startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true)
}

private fun setFirstDayOfTheWeek() {
// check existing config.isSundayFirst to migrate setting value
if (config.isSundayFirst) {
config.firstDayOfWeek = 6
// revert old setting to not run this code anymore
config.isSundayFirst = false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import org.fossify.clock.databinding.ActivitySettingsBinding
import org.fossify.clock.extensions.config
import org.fossify.clock.helpers.DEFAULT_MAX_ALARM_REMINDER_SECS
import org.fossify.clock.helpers.DEFAULT_MAX_TIMER_REMINDER_SECS
import org.fossify.commons.dialogs.RadioGroupDialog
import org.fossify.commons.extensions.*
import org.fossify.commons.helpers.IS_CUSTOMIZING_COLORS
import org.fossify.commons.helpers.MINUTE_SECONDS
import org.fossify.commons.helpers.NavigationIcon
import org.fossify.commons.helpers.isTiramisuPlus
import org.fossify.commons.models.RadioItem
import java.util.Locale
import kotlin.system.exitProcess

Expand All @@ -35,7 +37,7 @@ class SettingsActivity : SimpleActivity() {
setupUseEnglish()
setupLanguage()
setupPreventPhoneFromSleeping()
setupSundayFirst()
setupStartWeekOn()
setupAlarmMaxReminder()
setupUseSameSnooze()
setupSnoozeTime()
Expand Down Expand Up @@ -94,11 +96,24 @@ class SettingsActivity : SimpleActivity() {
}
}

private fun setupSundayFirst() {
binding.settingsSundayFirst.isChecked = config.isSundayFirst
binding.settingsSundayFirstHolder.setOnClickListener {
binding.settingsSundayFirst.toggle()
config.isSundayFirst = binding.settingsSundayFirst.isChecked
private fun setupStartWeekOn() {
val items = arrayListOf(
RadioItem(6, getString(org.fossify.commons.R.string.sunday)),
RadioItem(0, getString(org.fossify.commons.R.string.monday)),
RadioItem(1, getString(org.fossify.commons.R.string.tuesday)),
RadioItem(2, getString(org.fossify.commons.R.string.wednesday)),
RadioItem(3, getString(org.fossify.commons.R.string.thursday)),
RadioItem(4, getString(org.fossify.commons.R.string.friday)),
RadioItem(5, getString(org.fossify.commons.R.string.saturday)),
)

binding.settingsStartWeekOn.text = resources.getStringArray(org.fossify.commons.R.array.week_days)[config.firstDayOfWeek]
binding.settingsStartWeekOnHolder.setOnClickListener {
RadioGroupDialog(this@SettingsActivity, items, config.firstDayOfWeek) { day ->
val firstDayOfWeek = day as Int
config.firstDayOfWeek = firstDayOfWeek
binding.settingsStartWeekOn.text = resources.getStringArray(org.fossify.commons.R.array.week_days)[config.firstDayOfWeek]
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val onDism
editAlarm.setText(alarm.label)

val dayLetters = activity.resources.getStringArray(org.fossify.commons.R.array.week_day_letters).toList() as ArrayList<String>
val dayIndexes = arrayListOf(0, 1, 2, 3, 4, 5, 6)
if (activity.config.isSundayFirst) {
dayIndexes.moveLastItemToFront()
}
val dayIndexes = activity.orderDaysList(arrayListOf(0, 1, 2, 3, 4, 5, 6))

dayIndexes.forEach {
val pow = Math.pow(2.0, it.toDouble()).toInt()
Expand Down
20 changes: 12 additions & 8 deletions app/src/main/kotlin/org/fossify/clock/extensions/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,24 @@ fun Context.getAlarmSelectedDaysString(bitMask: Int): String {
}
}

fun Context.orderDaysList(days: List<Int>): List<Int> {
if (config.firstDayOfWeek > 0) {
val range = (config.firstDayOfWeek..6).toList() + (0..<config.firstDayOfWeek).toList()
return days.slice(range)
} else {
return days
}
}

fun Context.firstDayOrder(bitMask: Int): Int {
if (bitMask == TODAY_BIT) return -2
if (bitMask == TOMORROW_BIT) return -1

val dayBits = arrayListOf(MONDAY_BIT, TUESDAY_BIT, WEDNESDAY_BIT, THURSDAY_BIT, FRIDAY_BIT, SATURDAY_BIT, SUNDAY_BIT)

val sundayFirst = baseConfig.isSundayFirst
if (sundayFirst) {
dayBits.moveLastItemToFront()
}
val dayBits = orderDaysList(arrayListOf(MONDAY_BIT, TUESDAY_BIT, WEDNESDAY_BIT, THURSDAY_BIT, FRIDAY_BIT, SATURDAY_BIT, SUNDAY_BIT))

dayBits.forEach { bit ->
dayBits.forEachIndexed { i, bit ->
if (bitMask and bit != 0) {
return if (bit == SUNDAY_BIT && sundayFirst) 0 else bit
return i
}
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/kotlin/org/fossify/clock/helpers/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import org.fossify.commons.extensions.getDefaultAlarmSound
import org.fossify.commons.extensions.getDefaultAlarmTitle
import org.fossify.commons.helpers.BaseConfig
import org.fossify.commons.helpers.SORT_DESCENDING
import java.util.Calendar
import java.util.Locale

class Config(context: Context) : BaseConfig(context) {
companion object {
Expand Down Expand Up @@ -104,4 +106,11 @@ class Config(context: Context) : BaseConfig(context) {
var wasInitialWidgetSetUp: Boolean
get() = prefs.getBoolean(WAS_INITIAL_WIDGET_SET_UP, false)
set(wasInitialWidgetSetUp) = prefs.edit().putBoolean(WAS_INITIAL_WIDGET_SET_UP, wasInitialWidgetSetUp).apply()

var firstDayOfWeek: Int
get() {
val defaultFirstDayOfWeek = Calendar.getInstance(Locale.getDefault()).firstDayOfWeek
return prefs.getInt(FIRST_DAY_OF_WEEK, getDayNumber(defaultFirstDayOfWeek))
}
set(firstDayOfWeek) = prefs.edit().putInt(FIRST_DAY_OF_WEEK, firstDayOfWeek).apply()
}
7 changes: 5 additions & 2 deletions app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const val INCREASE_VOLUME_GRADUALLY = "increase_volume_gradually"
const val ALARMS_SORT_BY = "alarms_sort_by"
const val STOPWATCH_LAPS_SORT_BY = "stopwatch_laps_sort_by"
const val WAS_INITIAL_WIDGET_SET_UP = "was_initial_widget_set_up"
const val FIRST_DAY_OF_WEEK = "first_day_of_week"

const val TABS_COUNT = 4
const val EDITED_TIME_ZONE_SEPARATOR = ":"
Expand Down Expand Up @@ -108,16 +109,18 @@ fun formatTime(showSeconds: Boolean, use24HourFormat: Boolean, hours: Int, minut
}
}

fun getDayNumber(calendarDay: Int): Int = (calendarDay + 5) % 7

fun getTomorrowBit(): Int {
val calendar = Calendar.getInstance()
calendar.add(Calendar.DAY_OF_WEEK, 1)
val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
val dayOfWeek = getDayNumber(calendar.get(Calendar.DAY_OF_WEEK))
return 2.0.pow(dayOfWeek).toInt()
}

fun getTodayBit(): Int {
val calendar = Calendar.getInstance()
val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
val dayOfWeek = getDayNumber(calendar.get(Calendar.DAY_OF_WEEK))
return 2.0.pow(dayOfWeek).toInt()
}

Expand Down
26 changes: 17 additions & 9 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,32 +134,40 @@
</RelativeLayout>

<RelativeLayout
android:id="@+id/settings_prevent_phone_from_sleeping_holder"
style="@style/SettingsHolderCheckboxStyle"
android:id="@+id/settings_start_week_on_holder"
style="@style/SettingsHolderTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_prevent_phone_from_sleeping"
style="@style/SettingsCheckboxStyle"
<org.fossify.commons.views.MyTextView
android:id="@+id/settings_start_week_on_label"
style="@style/SettingsTextLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_week_on" />

<org.fossify.commons.views.MyTextView
android:id="@+id/settings_start_week_on"
style="@style/SettingsTextValueStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/prevent_phone_from_sleeping" />
android:layout_below="@+id/settings_start_week_on_label"
tools:text="@string/monday" />

</RelativeLayout>

<RelativeLayout
android:id="@+id/settings_sunday_first_holder"
android:id="@+id/settings_prevent_phone_from_sleeping_holder"
style="@style/SettingsHolderCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<org.fossify.commons.views.MyAppCompatCheckbox
android:id="@+id/settings_sunday_first"
android:id="@+id/settings_prevent_phone_from_sleeping"
style="@style/SettingsCheckboxStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sunday_first" />
android:text="@string/prevent_phone_from_sleeping" />

</RelativeLayout>

Expand Down
Loading