Fixes crash cluster 2e777e9c

parent 1208f8bb
......@@ -39,7 +39,7 @@ android {
lintOptions {
isWarningsAsErrors = true
setLintConfig(file("../lint.xml"))
lintConfig = file("../lint.xml")
}
}
......@@ -65,4 +65,6 @@ dependencies {
implementation(project(":lib")) {
exclude(group = "org.threeten", module = "threetenbp")
}
testImplementation("junit", "junit", "4.12")
}
......@@ -150,14 +150,12 @@ open class ChatViewModel : QuasselViewModel() {
)
}
private fun recentMessagesChange(value: Int) {
val current = recentlySentMessageIndex.safeValue
val size = recentlySentMessages.safeValue.size
val nextValue = current + value
recentlySentMessageIndex.onNext(
if (nextValue < 0) -1
else (size + current + value) % size
)
private fun recentMessagesChange(change: Int) {
recentlySentMessageIndex.onNext(recentMessagesChangeInternal(
recentlySentMessageIndex.safeValue,
recentlySentMessages.safeValue.size,
change
))
}
fun recentMessagesValue() =
......@@ -197,5 +195,9 @@ open class ChatViewModel : QuasselViewModel() {
const val KEY_SELECTED_BUFFER_ID = "model_chat_selectedBufferId"
const val MAX_RECENT_MESSAGES = 20
fun recentMessagesChangeInternal(current: Int, size: Int, change: Int) =
if (current + change < 0 || size == 0) -1
else (size + current + change) % size
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2019 Janne Mareike Koschinski
* Copyright (c) 2019 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.viewmodel
import de.kuschku.quasseldroid.viewmodel.ChatViewModel.Companion.recentMessagesChangeInternal
import org.junit.Assert.assertEquals
import org.junit.Test
class ChatViewModelTest {
@Test
fun testRecentMessagesChange() {
assertEquals(recentMessagesChangeInternal(0, 0, -1), -1)
assertEquals(recentMessagesChangeInternal(0, 0, +1), -1)
assertEquals(recentMessagesChangeInternal(-1, 0, -1), -1)
assertEquals(recentMessagesChangeInternal(-1, 0, +1), -1)
assertEquals(recentMessagesChangeInternal(1, 0, -1), -1)
assertEquals(recentMessagesChangeInternal(1, 0, +1), -1)
assertEquals(recentMessagesChangeInternal(0, 5, -1), -1)
assertEquals(recentMessagesChangeInternal(0, 5, +1), 1)
assertEquals(recentMessagesChangeInternal(1, 5, -1), 0)
assertEquals(recentMessagesChangeInternal(1, 5, +1), 2)
assertEquals(recentMessagesChangeInternal(4, 5, -1), 3)
assertEquals(recentMessagesChangeInternal(4, 5, +1), 0)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment