From 1b1452e999dab761db32e1549f4d79ed79f3d347 Mon Sep 17 00:00:00 2001 From: Kazuhiro Ito Date: Thu, 25 Nov 2021 00:32:43 +0900 Subject: [PATCH] Use bitshift operator instead of Math.pow --- .../kotlin/org/fossify/calendar/activities/EventActivity.kt | 4 ++-- .../kotlin/org/fossify/calendar/activities/TaskActivity.kt | 5 ++--- .../org/fossify/calendar/dialogs/RepeatRuleWeeklyDialog.kt | 2 +- app/src/main/kotlin/org/fossify/calendar/extensions/Long.kt | 2 +- app/src/main/kotlin/org/fossify/calendar/helpers/Parser.kt | 4 ++-- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt index fcece8ff7..61c9e5c18 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/EventActivity.kt @@ -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) } @@ -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()) { diff --git a/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt b/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt index 7828a4d38..55916e9db 100644 --- a/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt +++ b/app/src/main/kotlin/org/fossify/calendar/activities/TaskActivity.kt @@ -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 @@ -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()) { @@ -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) } diff --git a/app/src/main/kotlin/org/fossify/calendar/dialogs/RepeatRuleWeeklyDialog.kt b/app/src/main/kotlin/org/fossify/calendar/dialogs/RepeatRuleWeeklyDialog.kt index 33f8b1660..a39f8900d 100644 --- a/app/src/main/kotlin/org/fossify/calendar/dialogs/RepeatRuleWeeklyDialog.kt +++ b/app/src/main/kotlin/org/fossify/calendar/dialogs/RepeatRuleWeeklyDialog.kt @@ -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(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] diff --git a/app/src/main/kotlin/org/fossify/calendar/extensions/Long.kt b/app/src/main/kotlin/org/fossify/calendar/extensions/Long.kt index f717f4271..b33c81d3c 100644 --- a/app/src/main/kotlin/org/fossify/calendar/extensions/Long.kt +++ b/app/src/main/kotlin/org/fossify/calendar/extensions/Long.kt @@ -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 } diff --git a/app/src/main/kotlin/org/fossify/calendar/helpers/Parser.kt b/app/src/main/kotlin/org/fossify/calendar/helpers/Parser.kt index 3f33d444d..497b21665 100644 --- a/app/src/main/kotlin/org/fossify/calendar/helpers/Parser.kt +++ b/app/src/main/kotlin/org/fossify/calendar/helpers/Parser.kt @@ -32,7 +32,7 @@ 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)) { @@ -40,7 +40,7 @@ class Parser { // 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