mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-26 19:36:08 +03:00
Move logic to dedicated ViewModel
This commit is contained in:
parent
1d3cc52991
commit
364457d102
3 changed files with 75 additions and 29 deletions
|
@ -21,13 +21,8 @@ import androidx.lifecycle.LifecycleOwner
|
||||||
import arrow.core.Option
|
import arrow.core.Option
|
||||||
import im.vector.app.core.di.ActiveSessionHolder
|
import im.vector.app.core.di.ActiveSessionHolder
|
||||||
import im.vector.app.core.utils.BehaviorDataSource
|
import im.vector.app.core.utils.BehaviorDataSource
|
||||||
import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider
|
|
||||||
import im.vector.app.features.session.coroutineScope
|
import im.vector.app.features.session.coroutineScope
|
||||||
import im.vector.app.features.ui.UiStateRepository
|
import im.vector.app.features.ui.UiStateRepository
|
||||||
import io.reactivex.Observable
|
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
|
||||||
import io.reactivex.rxkotlin.addTo
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
@ -38,11 +33,8 @@ import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.matrix.android.sdk.api.extensions.tryOrNull
|
import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||||
import org.matrix.android.sdk.api.session.Session
|
import org.matrix.android.sdk.api.session.Session
|
||||||
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
|
|
||||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
|
||||||
import org.matrix.android.sdk.api.session.group.model.GroupSummary
|
import org.matrix.android.sdk.api.session.group.model.GroupSummary
|
||||||
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||||
import org.matrix.android.sdk.rx.rx
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@ -62,7 +54,6 @@ fun RoomGroupingMethod.group() = (this as? RoomGroupingMethod.ByLegacyGroup)?.gr
|
||||||
@Singleton
|
@Singleton
|
||||||
class AppStateHandler @Inject constructor(
|
class AppStateHandler @Inject constructor(
|
||||||
private val sessionDataSource: ActiveSessionDataSource,
|
private val sessionDataSource: ActiveSessionDataSource,
|
||||||
private val matrixItemColorProvider: MatrixItemColorProvider,
|
|
||||||
private val uiStateRepository: UiStateRepository,
|
private val uiStateRepository: UiStateRepository,
|
||||||
private val activeSessionHolder: ActiveSessionHolder
|
private val activeSessionHolder: ActiveSessionHolder
|
||||||
) : DefaultLifecycleObserver {
|
) : DefaultLifecycleObserver {
|
||||||
|
@ -141,11 +132,6 @@ class AppStateHandler @Inject constructor(
|
||||||
return (selectedSpaceDataSource.currentValue?.orNull() as? RoomGroupingMethod.ByLegacyGroup)?.groupSummary?.groupId
|
return (selectedSpaceDataSource.currentValue?.orNull() as? RoomGroupingMethod.ByLegacyGroup)?.groupSummary?.groupId
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
|
|
||||||
fun entersForeground() {
|
|
||||||
observeUserAccountData()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onResume(owner: LifecycleOwner) {
|
override fun onResume(owner: LifecycleOwner) {
|
||||||
observeActiveSession()
|
observeActiveSession()
|
||||||
}
|
}
|
||||||
|
@ -164,19 +150,4 @@ class AppStateHandler @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun observeUserAccountData() {
|
|
||||||
sessionDataSource.observe()
|
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
|
||||||
.switchMap {
|
|
||||||
it.orNull()?.rx()?.liveUserAccountData(setOf(UserAccountDataTypes.TYPE_OVERRIDE_COLORS))
|
|
||||||
?: Observable.just(emptyList())
|
|
||||||
}
|
|
||||||
.distinctUntilChanged()
|
|
||||||
.subscribe {
|
|
||||||
val overrideColorSpecs = it?.firstOrNull()?.content?.toModel<Map<String, String>>()
|
|
||||||
matrixItemColorProvider.setOverrideColors(overrideColorSpecs)
|
|
||||||
}
|
|
||||||
.addTo(compositeDisposable)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,6 +106,8 @@ class HomeActivity :
|
||||||
private val homeActivityViewModel: HomeActivityViewModel by viewModel()
|
private val homeActivityViewModel: HomeActivityViewModel by viewModel()
|
||||||
@Suppress("UNUSED")
|
@Suppress("UNUSED")
|
||||||
private val analyticsAccountDataViewModel: AnalyticsAccountDataViewModel by viewModel()
|
private val analyticsAccountDataViewModel: AnalyticsAccountDataViewModel by viewModel()
|
||||||
|
@Suppress("UNUSED")
|
||||||
|
private val userColorAccountDataViewModel: UserColorAccountDataViewModel by viewModel()
|
||||||
|
|
||||||
private val serverBackupStatusViewModel: ServerBackupStatusViewModel by viewModel()
|
private val serverBackupStatusViewModel: ServerBackupStatusViewModel by viewModel()
|
||||||
private val promoteRestrictedViewModel: PromoteRestrictedViewModel by viewModel()
|
private val promoteRestrictedViewModel: PromoteRestrictedViewModel by viewModel()
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2021 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.features.home
|
||||||
|
|
||||||
|
import com.airbnb.mvrx.MavericksState
|
||||||
|
import com.airbnb.mvrx.MavericksViewModelFactory
|
||||||
|
import dagger.assisted.Assisted
|
||||||
|
import dagger.assisted.AssistedFactory
|
||||||
|
import dagger.assisted.AssistedInject
|
||||||
|
import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
||||||
|
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||||
|
import im.vector.app.core.platform.EmptyAction
|
||||||
|
import im.vector.app.core.platform.EmptyViewEvents
|
||||||
|
import im.vector.app.core.platform.VectorViewModel
|
||||||
|
import im.vector.app.features.home.room.detail.timeline.helper.MatrixItemColorProvider
|
||||||
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
import org.matrix.android.sdk.api.session.Session
|
||||||
|
import org.matrix.android.sdk.api.session.accountdata.UserAccountDataTypes
|
||||||
|
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||||
|
import org.matrix.android.sdk.flow.flow
|
||||||
|
|
||||||
|
data class DummyState(
|
||||||
|
val dummy: Boolean = false
|
||||||
|
) : MavericksState
|
||||||
|
|
||||||
|
class UserColorAccountDataViewModel @AssistedInject constructor(
|
||||||
|
@Assisted initialState: DummyState,
|
||||||
|
private val session: Session,
|
||||||
|
private val matrixItemColorProvider: MatrixItemColorProvider
|
||||||
|
) : VectorViewModel<DummyState, EmptyAction, EmptyViewEvents>(initialState) {
|
||||||
|
|
||||||
|
@AssistedFactory
|
||||||
|
interface Factory : MavericksAssistedViewModelFactory<UserColorAccountDataViewModel, DummyState> {
|
||||||
|
override fun create(initialState: DummyState): UserColorAccountDataViewModel
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object : MavericksViewModelFactory<UserColorAccountDataViewModel, DummyState> by hiltMavericksViewModelFactory()
|
||||||
|
|
||||||
|
init {
|
||||||
|
observeAccountData()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun observeAccountData() {
|
||||||
|
session.flow()
|
||||||
|
.liveUserAccountData(setOf(UserAccountDataTypes.TYPE_OVERRIDE_COLORS))
|
||||||
|
.mapNotNull { it.firstOrNull() }
|
||||||
|
.mapNotNull { it.content.toModel<Map<String, String>>() }
|
||||||
|
.onEach { userColorAccountDataContent ->
|
||||||
|
matrixItemColorProvider.setOverrideColors(userColorAccountDataContent)
|
||||||
|
}
|
||||||
|
.launchIn(viewModelScope)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun handle(action: EmptyAction) {
|
||||||
|
// No op
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue