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

Use bitshift operator instead of Math.pow #83

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ class EventActivity : SimpleActivity() {
checkRepeatTexts(interval)

when {
mRepeatInterval.isXWeeklyRepetition() -> setRepeatRule(Math.pow(2.0, (mEventStartDateTime.dayOfWeek - 1).toDouble()).toInt())
mRepeatInterval.isXWeeklyRepetition() -> setRepeatRule(1 shl (mEventStartDateTime.dayOfWeek - 1))
mRepeatInterval.isXMonthlyRepetition() -> setRepeatRule(REPEAT_SAME_DAY)
mRepeatInterval.isXYearlyRepetition() -> setRepeatRule(REPEAT_SAME_DAY)
}
Expand Down Expand Up @@ -1603,7 +1603,7 @@ class EventActivity : SimpleActivity() {
if (mRepeatInterval.isXWeeklyRepetition()) {
val day = mRepeatRule
if (day == MONDAY_BIT || day == TUESDAY_BIT || day == WEDNESDAY_BIT || day == THURSDAY_BIT || day == FRIDAY_BIT || day == SATURDAY_BIT || day == SUNDAY_BIT) {
setRepeatRule(Math.pow(2.0, (mEventStartDateTime.dayOfWeek - 1).toDouble()).toInt())
setRepeatRule(1 shl (mEventStartDateTime.dayOfWeek - 1))
}
} else if (mRepeatInterval.isXMonthlyRepetition() || mRepeatInterval.isXYearlyRepetition()) {
if (mRepeatRule == REPEAT_LAST_DAY && !isLastDayOfTheMonth()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import org.fossify.commons.extensions.*
import org.fossify.commons.helpers.*
import org.fossify.commons.models.RadioItem
import org.joda.time.DateTime
import kotlin.math.pow

class TaskActivity : SimpleActivity() {
private var mEventTypeId = REGULAR_EVENT_TYPE_ID
Expand Down Expand Up @@ -610,7 +609,7 @@ class TaskActivity : SimpleActivity() {
if (mRepeatInterval.isXWeeklyRepetition()) {
val day = mRepeatRule
if (day == MONDAY_BIT || day == TUESDAY_BIT || day == WEDNESDAY_BIT || day == THURSDAY_BIT || day == FRIDAY_BIT || day == SATURDAY_BIT || day == SUNDAY_BIT) {
setRepeatRule(2.0.pow((mTaskDateTime.dayOfWeek - 1).toDouble()).toInt())
setRepeatRule(1 shl (mTaskDateTime.dayOfWeek - 1))
}
} else if (mRepeatInterval.isXMonthlyRepetition() || mRepeatInterval.isXYearlyRepetition()) {
if (mRepeatRule == REPEAT_LAST_DAY && !isLastDayOfTheMonth()) {
Expand Down Expand Up @@ -825,7 +824,7 @@ class TaskActivity : SimpleActivity() {
checkRepeatTexts(interval)

when {
mRepeatInterval.isXWeeklyRepetition() -> setRepeatRule(2.0.pow((mTaskDateTime.dayOfWeek - 1).toDouble()).toInt())
mRepeatInterval.isXWeeklyRepetition() -> setRepeatRule(1 shl (mTaskDateTime.dayOfWeek - 1))
mRepeatInterval.isXMonthlyRepetition() -> setRepeatRule(REPEAT_SAME_DAY)
mRepeatInterval.isXYearlyRepetition() -> setRepeatRule(REPEAT_SAME_DAY)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RepeatRuleWeeklyDialog(val activity: Activity, val curRepeatRule: Int, val
val days = activity.resources.getStringArray(org.fossify.commons.R.array.week_days)
var checkboxes = ArrayList<MyAppCompatCheckbox>(7)
for (i in 0..6) {
val pow = Math.pow(2.0, i.toDouble()).toInt()
val pow = 1 shl i
MyCheckboxBinding.inflate(activity.layoutInflater).root.apply {
isChecked = curRepeatRule and pow != 0
text = days[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import org.fossify.calendar.models.Event

fun Long.isTsOnProperDay(event: Event): Boolean {
val dateTime = Formatter.getDateTimeFromTS(this)
val power = Math.pow(2.0, (dateTime.dayOfWeek - 1).toDouble()).toInt()
val power = 1 shl (dateTime.dayOfWeek - 1)
return event.repeatRule and power != 0
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/org/fossify/calendar/helpers/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class Parser {
repeatInterval = getFrequencySeconds(value)
if (value == WEEKLY) {
val start = Formatter.getDateTimeFromTS(startTS)
repeatRule = Math.pow(2.0, (start.dayOfWeek - 1).toDouble()).toInt()
repeatRule = 1 shl (start.dayOfWeek - 1)
} else if (value == MONTHLY || value == YEARLY) {
repeatRule = REPEAT_SAME_DAY
} else if (value == DAILY && fullString.contains(INTERVAL)) {
val interval = fullString.substringAfter("$INTERVAL=").substringBefore(";")
// properly handle events repeating by 14 days or so, just add a repeat rule to specify a day of the week
if (interval.areDigitsOnly() && interval.toInt() % 7 == 0) {
val dateTime = Formatter.getDateTimeFromTS(startTS)
repeatRule = Math.pow(2.0, (dateTime.dayOfWeek - 1).toDouble()).toInt()
repeatRule = 1 shl (dateTime.dayOfWeek - 1)
} else if (fullString.contains("BYDAY")) {
// some services use weekly repetition for repeating on specific week days, some use daily
// make these produce the same result
Expand Down