Commit 7c37d285 authored by Janne Koschinski's avatar Janne Koschinski

Fixed issue where unhiding buffers would sort them wrongly

parent fafd18f3
Pipeline #513 canceled with stages
in 12 minutes and 5 seconds
......@@ -497,7 +497,7 @@ class QuasselService : DaggerLifecycleService(),
session.bufferViewManager.requestCreateBufferView(
Defaults.bufferViewConfigInitial(translatedLocale).apply {
for (info in session.bufferSyncer.bufferInfos()) {
handleBuffer(info, session.bufferSyncer)
handleBuffer(info, session.bufferSyncer, session.networks)
}
}.toVariantMap()
)
......
......@@ -191,7 +191,7 @@ object BufferContextPresenter {
true
}
R.id.action_unhide -> {
bufferViewConfig?.insertBufferSorted(info, bufferSyncer)
bufferViewConfig?.insertBufferSorted(info, bufferSyncer, session.networks)
actionMode.finish()
true
}
......
......@@ -245,7 +245,7 @@ class BufferSyncer constructor(
live_bufferInfos.onNext(Unit)
if (oldInfo == null) {
session.bufferViewManager.handleBuffer(info, this)
session.bufferViewManager.handleBuffer(info, this, session.networks)
}
}
}
......@@ -282,7 +282,7 @@ class BufferSyncer constructor(
activity hasFlag Message_Type.Notice ||
activity hasFlag Message_Type.Action) {
bufferInfo(buffer)?.let {
session.bufferViewManager.handleBuffer(it, this, true)
session.bufferViewManager.handleBuffer(it, this, session.networks, true)
}
}
_bufferActivities[buffer] = activity
......
......@@ -26,7 +26,6 @@ import de.kuschku.libquassel.quassel.syncables.interfaces.IBufferViewConfig
import de.kuschku.libquassel.session.SignalProxy
import de.kuschku.libquassel.util.flag.hasFlag
import de.kuschku.libquassel.util.helper.clampOf
import de.kuschku.libquassel.util.irc.IrcCaseMappers
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
......@@ -326,28 +325,33 @@ class BufferViewConfig constructor(
(a?.bufferViewName() ?: "").compareTo((b?.bufferViewName() ?: ""), true)
}
fun insertBufferSorted(info: BufferInfo, bufferSyncer: BufferSyncer) {
fun insertBufferSorted(info: BufferInfo, bufferSyncer: BufferSyncer,
networks: Map<NetworkId, Network>) {
if (!_buffers.contains(info.bufferId)) {
val element = IrcCaseMappers.unicode.toLowerCaseNullable(info.bufferName)
val position =
if (_sortAlphabetically) -_buffers.mapNotNull {
IrcCaseMappers.unicode.toLowerCaseNullable(bufferSyncer.bufferInfo(it)?.bufferName)
}.binarySearch(element) - 1
else _buffers.size
requestAddBuffer(info.bufferId, position)
requestAddBuffer(
info.bufferId,
sortBuffers(
_buffers.mapNotNull { bufferSyncer.bufferInfo(it) } + info,
sortAlphabetically(),
{ it.bufferName ?: "" },
{ networks[it.networkId]?.networkName() ?: "" },
{ it.type }
).indexOf(info)
)
}
}
fun handleBuffer(info: BufferInfo, bufferSyncer: BufferSyncer, unhide: Boolean = false) {
fun handleBuffer(info: BufferInfo, bufferSyncer: BufferSyncer, networks: Map<NetworkId, Network>,
unhide: Boolean = false) {
if (_addNewBuffersAutomatically &&
!_buffers.contains(info.bufferId) &&
!_temporarilyRemovedBuffers.contains(info.bufferId) &&
!_removedBuffers.contains(info.bufferId) &&
!info.type.hasFlag(Buffer_Type.StatusBuffer)) {
insertBufferSorted(info, bufferSyncer)
insertBufferSorted(info, bufferSyncer, networks)
} else if (unhide && !_buffers.contains(info.bufferId) &&
_temporarilyRemovedBuffers.contains(info.bufferId)) {
insertBufferSorted(info, bufferSyncer)
insertBufferSorted(info, bufferSyncer, networks)
}
}
......@@ -365,4 +369,18 @@ class BufferViewConfig constructor(
override fun toString(): String {
return "BufferViewConfig(_bufferViewId=$_bufferViewId, _bufferViewName='$_bufferViewName', _networkId=$_networkId, _addNewBuffersAutomatically=$_addNewBuffersAutomatically, _sortAlphabetically=$_sortAlphabetically, _hideInactiveBuffers=$_hideInactiveBuffers, _hideInactiveNetworks=$_hideInactiveNetworks, _disableDecoration=$_disableDecoration, _allowedBufferTypes=$_allowedBufferTypes, _minimumActivity=$_minimumActivity, _showSearch=$_showSearch)"
}
companion object {
inline fun <T> sortBuffers(list: List<T>, sortAlphabetically: Boolean,
crossinline bufferName: (T) -> String,
crossinline networkName: (T) -> String,
crossinline type: (T) -> Buffer_Types) =
list.let {
if (sortAlphabetically) list.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER,
bufferName))
else list
}.sortedBy {
!type(it).hasFlag(Buffer_Type.StatusBuffer)
}.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, networkName))
}
}
......@@ -90,12 +90,13 @@ class BufferViewManager constructor(
private val _bufferViewConfigs: MutableMap<Int, BufferViewConfig> = mutableMapOf()
private val live_bufferViewConfigs: BehaviorSubject<Set<Int>> = BehaviorSubject.createDefault<Set<Int>>(
private val live_bufferViewConfigs: BehaviorSubject<Set<Int>> = BehaviorSubject.createDefault(
emptySet())
fun handleBuffer(info: BufferInfo, bufferSyncer: BufferSyncer, unhide: Boolean = false) {
fun handleBuffer(info: BufferInfo, bufferSyncer: BufferSyncer, networks: Map<NetworkId, Network>,
unhide: Boolean = false) {
for (bufferViewConfig in bufferViewConfigs()) {
bufferViewConfig.handleBuffer(info, bufferSyncer, unhide)
bufferViewConfig.handleBuffer(info, bufferSyncer, networks, unhide)
}
}
......
......@@ -280,7 +280,7 @@ class Session(
initCallback?.invoke(this)
for (config in bufferViewManager.bufferViewConfigs()) {
for (info in bufferSyncer.bufferInfos()) {
config.handleBuffer(info, bufferSyncer)
config.handleBuffer(info, bufferSyncer, networks)
}
}
notificationManager?.init(this)
......
......@@ -43,7 +43,7 @@ class BufferViewConfigTest {
val bufferViewConfig = bufferViewManager.bufferViewConfig(0)!!
ensure {
bufferViewConfig.insertBufferSorted(bufferSyncer.bufferInfo(BufferId(4))!!, bufferSyncer)
bufferViewConfig.insertBufferSorted(bufferSyncer.bufferInfo(BufferId(4))!!, bufferSyncer, networks)
}.does {
callSync(bufferViewConfig, "requestAddBuffer", listOf(
QVariant_.of(BufferId(4), QType.BufferId),
......
......@@ -60,5 +60,5 @@ open class ArchiveViewModelHelper @Inject constructor(
showHandle
)
val selectedBuffer = processSelectedBuffer(archive.selectedBufferId)
val selectedBuffer = processSelectedBuffer(bufferViewConfig, archive.selectedBufferId)
}
......@@ -170,7 +170,7 @@ open class ChatViewModelHelper @Inject constructor(
val nickDataThrottled =
nickData.distinctUntilChanged().throttleLast(100, TimeUnit.MILLISECONDS)
val selectedBuffer = processSelectedBuffer(chat.selectedBufferId)
val selectedBuffer = processSelectedBuffer(bufferViewConfig, chat.selectedBufferId)
fun processChatBufferList(
filtered: Observable<Pair<Map<BufferId, Int>, Int>>
......
......@@ -456,50 +456,76 @@ open class QuasselViewModelHelper @Inject constructor(
}
fun processSelectedBuffer(
bufferViewConfigObservable: Observable<Optional<BufferViewConfig>>,
selectedBufferId: Observable<BufferId>
) = combineLatest(connectedSession, selectedBufferId)
.safeSwitchMap { (sessionOptional, buffer) ->
) = combineLatest(connectedSession, selectedBufferId, bufferViewConfigObservable)
.safeSwitchMap { (sessionOptional, buffer, bufferViewConfigOptional) ->
val session = sessionOptional.orNull()
val bufferViewConfig = bufferViewConfigOptional.orNull()
val bufferSyncer = session?.bufferSyncer
if (bufferSyncer != null) {
session.liveNetworks().safeSwitchMap { networks ->
val info = if (!buffer.isValidId()) networks[NetworkId(-buffer.id)]?.let {
BufferInfo(
bufferId = buffer,
networkId = it.networkId(),
groupId = 0,
bufferName = it.networkName(),
type = Buffer_Type.of(Buffer_Type.StatusBuffer)
)
} else bufferSyncer.bufferInfo(buffer)
if (info != null) {
val network = networks[info.networkId]
when (info.type.enabledValues().firstOrNull()) {
Buffer_Type.StatusBuffer -> {
network?.liveConnectionState()?.map {
SelectedBufferItem(
if (bufferSyncer != null && bufferViewConfig != null) {
bufferHiddenState(bufferViewConfig, buffer).safeSwitchMap { bufferHiddenState ->
session.liveNetworks().safeSwitchMap { networks ->
val info = if (!buffer.isValidId()) networks[NetworkId(-buffer.id)]?.let {
BufferInfo(
bufferId = buffer,
networkId = it.networkId(),
groupId = 0,
bufferName = it.networkName(),
type = Buffer_Type.of(Buffer_Type.StatusBuffer)
)
} else bufferSyncer.bufferInfo(buffer)
if (info != null) {
val network = networks[info.networkId]
when (info.type.enabledValues().firstOrNull()) {
Buffer_Type.StatusBuffer -> {
network?.liveConnectionState()?.map {
SelectedBufferItem(
info,
connectionState = it,
hiddenState = bufferHiddenState
)
} ?: Observable.just(SelectedBufferItem(info))
}
Buffer_Type.ChannelBuffer -> {
network?.liveIrcChannel(info.bufferName)?.mapNullable(IrcChannel.NULL) {
SelectedBufferItem(
info,
joined = it != null,
hiddenState = bufferHiddenState
)
} ?: Observable.just(SelectedBufferItem(
info,
connectionState = it
)
} ?: Observable.just(SelectedBufferItem(info))
}
Buffer_Type.ChannelBuffer -> {
network?.liveIrcChannel(info.bufferName)?.mapNullable(IrcChannel.NULL) {
SelectedBufferItem(
hiddenState = bufferHiddenState
))
}
else ->
Observable.just(SelectedBufferItem(
info,
joined = it != null
)
} ?: Observable.just(SelectedBufferItem(info))
hiddenState = bufferHiddenState
))
}
else ->
Observable.just(SelectedBufferItem(info))
} else {
Observable.just(SelectedBufferItem())
}
} else {
Observable.just(SelectedBufferItem())
}
}
} else {
Observable.just(SelectedBufferItem())
}
}
fun bufferHiddenState(bufferViewConfig: BufferViewConfig,
bufferId: BufferId): Observable<BufferHiddenState> =
combineLatest(bufferViewConfig.liveBuffers(),
bufferViewConfig.liveTemporarilyRemovedBuffers(),
bufferViewConfig.liveRemovedBuffers())
.map { (visible, temp, perm) ->
when (bufferId) {
in visible -> BufferHiddenState.VISIBLE
in temp -> BufferHiddenState.HIDDEN_TEMPORARY
in perm -> BufferHiddenState.HIDDEN_PERMANENT
else -> BufferHiddenState.HIDDEN_PERMANENT
}
}
}
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