Skip to content

Commit 11ab68b

Browse files
authored
Merge pull request #59 from Aga-C/choose-first-day
Added choosing first day of the week (#19)
2 parents 04cbd0b + 7e2c243 commit 11ab68b

File tree

7 files changed

+73
-29
lines changed

7 files changed

+73
-29
lines changed

app/src/main/kotlin/org/fossify/clock/activities/MainActivity.kt

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MainActivity : SimpleActivity() {
4040
initFragments()
4141
setupTabs()
4242
updateWidgets()
43+
migrateFirstDayOfWeek()
4344

4445
getEnabledAlarms { enabledAlarms ->
4546
if (enabledAlarms.isNullOrEmpty()) {
@@ -298,6 +299,16 @@ class MainActivity : SimpleActivity() {
298299
startAboutActivity(R.string.app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true)
299300
}
300301

302+
@Deprecated("Remove this method in future releases")
303+
private fun migrateFirstDayOfWeek() {
304+
// check existing config.isSundayFirst to migrate setting value
305+
if (config.isSundayFirst) {
306+
config.firstDayOfWeek = 6
307+
// revert old setting to not run this code anymore
308+
config.isSundayFirst = false
309+
}
310+
}
311+
301312
private fun getTabIndex(tabId: Int): Int {
302313
return when (tabId) {
303314
TAB_CLOCK -> TAB_CLOCK_INDEX

app/src/main/kotlin/org/fossify/clock/activities/SettingsActivity.kt

+19-6
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SettingsActivity : SimpleActivity() {
103103
setupLanguage()
104104
setupDefaultTab()
105105
setupPreventPhoneFromSleeping()
106-
setupSundayFirst()
106+
setupStartWeekOn()
107107
setupAlarmMaxReminder()
108108
setupUseSameSnooze()
109109
setupSnoozeTime()
@@ -196,11 +196,24 @@ class SettingsActivity : SimpleActivity() {
196196
}
197197
}
198198

199-
private fun setupSundayFirst() {
200-
binding.settingsSundayFirst.isChecked = config.isSundayFirst
201-
binding.settingsSundayFirstHolder.setOnClickListener {
202-
binding.settingsSundayFirst.toggle()
203-
config.isSundayFirst = binding.settingsSundayFirst.isChecked
199+
private fun setupStartWeekOn() {
200+
val items = arrayListOf(
201+
RadioItem(6, getString(org.fossify.commons.R.string.sunday)),
202+
RadioItem(0, getString(org.fossify.commons.R.string.monday)),
203+
RadioItem(1, getString(org.fossify.commons.R.string.tuesday)),
204+
RadioItem(2, getString(org.fossify.commons.R.string.wednesday)),
205+
RadioItem(3, getString(org.fossify.commons.R.string.thursday)),
206+
RadioItem(4, getString(org.fossify.commons.R.string.friday)),
207+
RadioItem(5, getString(org.fossify.commons.R.string.saturday)),
208+
)
209+
210+
binding.settingsStartWeekOn.text = resources.getStringArray(org.fossify.commons.R.array.week_days)[config.firstDayOfWeek]
211+
binding.settingsStartWeekOnHolder.setOnClickListener {
212+
RadioGroupDialog(this@SettingsActivity, items, config.firstDayOfWeek) { day ->
213+
val firstDayOfWeek = day as Int
214+
config.firstDayOfWeek = firstDayOfWeek
215+
binding.settingsStartWeekOn.text = resources.getStringArray(org.fossify.commons.R.array.week_days)[config.firstDayOfWeek]
216+
}
204217
}
205218
}
206219

app/src/main/kotlin/org/fossify/clock/dialogs/EditAlarmDialog.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val onDism
9292
editAlarm.setText(alarm.label)
9393

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

10097
dayIndexes.forEach {
10198
val pow = Math.pow(2.0, it.toDouble()).toInt()

app/src/main/kotlin/org/fossify/clock/extensions/Context.kt

+12-8
Original file line numberDiff line numberDiff line change
@@ -548,20 +548,24 @@ fun Context.getAlarmSelectedDaysString(bitMask: Int): String {
548548
}
549549
}
550550

551+
fun Context.orderDaysList(days: List<Int>): List<Int> {
552+
if (config.firstDayOfWeek > 0) {
553+
val range = (config.firstDayOfWeek..6).toList() + (0..<config.firstDayOfWeek).toList()
554+
return days.slice(range)
555+
} else {
556+
return days
557+
}
558+
}
559+
551560
fun Context.firstDayOrder(bitMask: Int): Int {
552561
if (bitMask == TODAY_BIT) return -2
553562
if (bitMask == TOMORROW_BIT) return -1
554563

555-
val dayBits = arrayListOf(MONDAY_BIT, TUESDAY_BIT, WEDNESDAY_BIT, THURSDAY_BIT, FRIDAY_BIT, SATURDAY_BIT, SUNDAY_BIT)
556-
557-
val sundayFirst = baseConfig.isSundayFirst
558-
if (sundayFirst) {
559-
dayBits.moveLastItemToFront()
560-
}
564+
val dayBits = orderDaysList(arrayListOf(MONDAY_BIT, TUESDAY_BIT, WEDNESDAY_BIT, THURSDAY_BIT, FRIDAY_BIT, SATURDAY_BIT, SUNDAY_BIT))
561565

562-
dayBits.forEach { bit ->
566+
dayBits.forEachIndexed { i, bit ->
563567
if (bitMask and bit != 0) {
564-
return if (bit == SUNDAY_BIT && sundayFirst) 0 else bit
568+
return i
565569
}
566570
}
567571

app/src/main/kotlin/org/fossify/clock/helpers/Config.kt

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import org.fossify.commons.extensions.getDefaultAlarmSound
1111
import org.fossify.commons.extensions.getDefaultAlarmTitle
1212
import org.fossify.commons.helpers.BaseConfig
1313
import org.fossify.commons.helpers.SORT_DESCENDING
14+
import java.util.Calendar
15+
import java.util.Locale
1416

1517
class Config(context: Context) : BaseConfig(context) {
1618
companion object {
@@ -108,4 +110,11 @@ class Config(context: Context) : BaseConfig(context) {
108110
var lastDataExportPath: String
109111
get() = prefs.getString(LAST_DATA_EXPORT_PATH, "")!!
110112
set(lastDataExportPath) = prefs.edit().putString(LAST_DATA_EXPORT_PATH, lastDataExportPath).apply()
113+
114+
var firstDayOfWeek: Int
115+
get() {
116+
val defaultFirstDayOfWeek = Calendar.getInstance(Locale.getDefault()).firstDayOfWeek
117+
return prefs.getInt(FIRST_DAY_OF_WEEK, getDayNumber(defaultFirstDayOfWeek))
118+
}
119+
set(firstDayOfWeek) = prefs.edit().putInt(FIRST_DAY_OF_WEEK, firstDayOfWeek).apply()
111120
}

app/src/main/kotlin/org/fossify/clock/helpers/Constants.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const val STOPWATCH_LAPS_SORT_BY = "stopwatch_laps_sort_by"
2828
const val WAS_INITIAL_WIDGET_SET_UP = "was_initial_widget_set_up"
2929
const val DATA_EXPORT_EXTENSION = ".json"
3030
const val LAST_DATA_EXPORT_PATH = "last_alarms_export_path"
31+
const val FIRST_DAY_OF_WEEK = "first_day_of_week"
3132

3233
const val TABS_COUNT = 4
3334
const val EDITED_TIME_ZONE_SEPARATOR = ":"
@@ -126,16 +127,18 @@ fun formatTime(showSeconds: Boolean, use24HourFormat: Boolean, hours: Int, minut
126127
}
127128
}
128129

130+
fun getDayNumber(calendarDay: Int): Int = (calendarDay + 5) % 7
131+
129132
fun getTomorrowBit(): Int {
130133
val calendar = Calendar.getInstance()
131134
calendar.add(Calendar.DAY_OF_WEEK, 1)
132-
val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
135+
val dayOfWeek = getDayNumber(calendar.get(Calendar.DAY_OF_WEEK))
133136
return 2.0.pow(dayOfWeek).toInt()
134137
}
135138

136139
fun getTodayBit(): Int {
137140
val calendar = Calendar.getInstance()
138-
val dayOfWeek = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
141+
val dayOfWeek = getDayNumber(calendar.get(Calendar.DAY_OF_WEEK))
139142
return 2.0.pow(dayOfWeek).toInt()
140143
}
141144

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

+16-9
Original file line numberDiff line numberDiff line change
@@ -157,32 +157,39 @@
157157
</RelativeLayout>
158158

159159
<RelativeLayout
160-
android:id="@+id/settings_prevent_phone_from_sleeping_holder"
161-
style="@style/SettingsHolderCheckboxStyle"
160+
android:id="@+id/settings_start_week_on_holder"
161+
style="@style/SettingsHolderTextViewStyle"
162162
android:layout_width="match_parent"
163163
android:layout_height="wrap_content">
164164

165-
<org.fossify.commons.views.MyAppCompatCheckbox
166-
android:id="@+id/settings_prevent_phone_from_sleeping"
167-
style="@style/SettingsCheckboxStyle"
165+
<org.fossify.commons.views.MyTextView
166+
android:id="@+id/settings_start_week_on_label"
167+
style="@style/SettingsTextLabelStyle"
168+
android:layout_width="wrap_content"
169+
android:layout_height="wrap_content"
170+
android:text="@string/start_week_on" />
171+
<org.fossify.commons.views.MyTextView
172+
android:id="@+id/settings_start_week_on"
173+
style="@style/SettingsTextValueStyle"
168174
android:layout_width="match_parent"
169175
android:layout_height="wrap_content"
170-
android:text="@string/prevent_phone_from_sleeping" />
176+
android:layout_below="@+id/settings_start_week_on_label"
177+
tools:text="@string/monday" />
171178

172179
</RelativeLayout>
173180

174181
<RelativeLayout
175-
android:id="@+id/settings_sunday_first_holder"
182+
android:id="@+id/settings_prevent_phone_from_sleeping_holder"
176183
style="@style/SettingsHolderCheckboxStyle"
177184
android:layout_width="match_parent"
178185
android:layout_height="wrap_content">
179186

180187
<org.fossify.commons.views.MyAppCompatCheckbox
181-
android:id="@+id/settings_sunday_first"
188+
android:id="@+id/settings_prevent_phone_from_sleeping"
182189
style="@style/SettingsCheckboxStyle"
183190
android:layout_width="match_parent"
184191
android:layout_height="wrap_content"
185-
android:text="@string/sunday_first" />
192+
android:text="@string/prevent_phone_from_sleeping" />
186193

187194
</RelativeLayout>
188195

0 commit comments

Comments
 (0)