Skip to content

Commit

Permalink
Merge pull request capcom6#56 from capcom6/feature/interval-delay
Browse files Browse the repository at this point in the history
Add delay interval option
  • Loading branch information
capcom6 authored Apr 16, 2024
2 parents 3f8b78e + 95893cb commit 96dffb3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ class MessagesService(
continue
}

if (settings.secondsBetweenMessages > 0) {
delay((0..settings.secondsBetweenMessages).random() * 1000L)
settings.sendIntervalRange?.let {
delay(it.random() * 1000L)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ class MessagesSettings(
PerDay(86400000L),
}

val secondsBetweenMessages: Int
get() = storage.get<Int>(SECONDS_BETWEEN_MESSAGES) ?: 0
private var version: Int
get() = storage.get<Int>(VERSION) ?: 0
set(value) = storage.set(VERSION, value)

val sendIntervalRange: IntRange?
get() {
val min = sendIntervalMin
val max = sendIntervalMax
return when {
max == null -> null
min > max -> null
else -> min..max
}
}
private val sendIntervalMin: Int
get() = storage.get<Int>(SEND_INTERVAL_MIN) ?: 0
private val sendIntervalMax: Int?
get() = storage.get<Int>(SEND_INTERVAL_MAX)

val limitEnabled: Boolean
get() = limitValue > 0 && limitPeriod != Period.Disabled
Expand All @@ -26,8 +42,30 @@ class MessagesSettings(
val logLifetimeDays: Int?
get() = storage.get<Int?>(LOG_LIFETIME_DAYS)?.takeIf { it > 0 }

init {
migrate()
}

private fun migrate() {
if (version == VERSION_CODE) {
return
}

if (version < 1) {
val SECONDS_BETWEEN_MESSAGES = "SECONDS_BETWEEN_MESSAGES"

storage.set(SEND_INTERVAL_MAX, storage.get<Int>(SECONDS_BETWEEN_MESSAGES)?.toString())
}

version = VERSION_CODE
}

companion object {
private const val SECONDS_BETWEEN_MESSAGES = "SECONDS_BETWEEN_MESSAGES"
private const val VERSION_CODE = 1
private const val VERSION = "version"

private const val SEND_INTERVAL_MIN = "send_interval_min"
private const val SEND_INTERVAL_MAX = "send_interval_max"

private const val LIMIT_PERIOD = "limit_period"
private const val LIMIT_VALUE = "limit_value"
Expand Down
14 changes: 5 additions & 9 deletions app/src/main/java/me/capcom/smsgateway/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,11 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

if (preference.key == "messages.limit_value") {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
it.setSelectAllOnFocus(true)
it.selectAll()
}
}

if (preference.key == "messages.log_lifetime_days") {
if (preference.key == "messages.limit_value"
|| preference.key == "messages.log_lifetime_days"
|| preference.key == "messages.send_interval_min"
|| preference.key == "messages.send_interval_max"
) {
(preference as EditTextPreference).setOnBindEditTextListener {
it.inputType = InputType.TYPE_CLASS_NUMBER
it.setSelectAllOnFocus(true)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@
<string name="limits">Limits</string>
<string name="period">Period</string>
<string name="messages_count">Messages count</string>
<string name="delays_seconds">Delays, seconds</string>
<string name="minimum">Minimum</string>
<string name="maximum">Maximum</string>
<string name="set_maximum_value_to_activate">Set maximum value to activate</string>
</resources>
24 changes: 15 additions & 9 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,24 @@
app:useSimpleSummaryProvider="true"
app:title="@string/log_lifetime_days" />

<SeekBarPreference
android:max="600"
app:defaultValue="0"
app:enabled="true"
app:icon="@drawable/ic_timer"
app:key="messages.SECONDS_BETWEEN_MESSAGES"
app:showSeekBarValue="true"
app:summary="@string/random_value_from_0_to_selected_value_will_be_used"
app:title="@string/delay_between_messages_seconds" />

</PreferenceCategory>

<PreferenceCategory
app:summary="@string/set_maximum_value_to_activate"
app:title="@string/delays_seconds">
<EditTextPreference
app:key="messages.send_interval_min"
app:title="@string/minimum"
app:icon="@drawable/ic_timer"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
app:icon="@drawable/ic_timer"
app:key="messages.send_interval_max"
app:title="@string/maximum"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/limits">
<DropDownPreference
android:defaultValue="Disabled"
Expand Down

0 comments on commit 96dffb3

Please sign in to comment.