Skip to content

Commit

Permalink
Use bitshift operator instead of Math.pow
Browse files Browse the repository at this point in the history
  • Loading branch information
ikazuhiro committed Jan 18, 2024
1 parent 3f3a3f3 commit 1b1452e
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 9 deletions.
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

0 comments on commit 1b1452e

Please sign in to comment.