Skip to content

Commit

Permalink
Move load-state into its own data-store.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-signal authored and greyson-signal committed Apr 12, 2023
1 parent e8570c3 commit 4359336
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.thoughtcrime.securesms.stories.viewer

data class StoryLoadState(
val isContentReady: Boolean = false,
val isCrossfaderReady: Boolean = false
) {
fun isReady(): Boolean = isContentReady && isCrossfaderReady
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.viewpager2.widget.ViewPager2
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import org.signal.core.util.getParcelableArrayListCompat
import org.signal.core.util.getParcelableCompat
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.recipients.Recipient
Expand Down Expand Up @@ -115,8 +116,11 @@ class StoryViewerFragment :
if (state.skipCrossfade) {
viewModel.setCrossfaderIsReady(true)
}
}

if (state.loadState.isReady()) {
lifecycleDisposable += viewModel.loadState.subscribe {
if (it.isReady()) {
Log.d(TAG, "Content is ready, clearing crossfader.")
storyCrossfader.alpha = 0f
}
}
Expand Down Expand Up @@ -203,6 +207,8 @@ class StoryViewerFragment :
}

companion object {
private val TAG = Log.tag(StoryViewerFragment::class.java)

private const val ARGS = "args"
private const val HIDDEN = "hidden"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ data class StoryViewerState(
val page: Int = -1,
val crossfadeSource: CrossfadeSource,
val crossfadeTarget: CrossfadeTarget? = null,
val loadState: LoadState = LoadState(),
val skipCrossfade: Boolean = false,
val noPosts: Boolean = false
) {
Expand All @@ -26,11 +25,4 @@ data class StoryViewerState(
object None : CrossfadeTarget()
data class Record(val messageRecord: MmsMessageRecord) : CrossfadeTarget()
}

data class LoadState(
val isContentReady: Boolean = false,
val isCrossfaderReady: Boolean = false
) {
fun isReady(): Boolean = isContentReady && isCrossfaderReady
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
Expand Down Expand Up @@ -34,10 +35,13 @@ class StoryViewerViewModel(
)
)

private val loadStore = RxStore(StoryLoadState())

private val disposables = CompositeDisposable()

val stateSnapshot: StoryViewerState get() = store.state
val state: Flowable<StoryViewerState> = store.stateFlowable
val loadState: Flowable<StoryLoadState> = loadStore.stateFlowable.distinctUntilChanged().observeOn(AndroidSchedulers.mainThread())

private val hidden = mutableSetOf<RecipientId>()

Expand All @@ -47,7 +51,7 @@ class StoryViewerViewModel(
private val childScrollStatePublisher: BehaviorSubject<Boolean> = BehaviorSubject.createDefault(false)
val allowParentScrolling: Observable<Boolean> = Observable.combineLatest(
childScrollStatePublisher.distinctUntilChanged(),
state.toObservable().map { it.loadState.isReady() }.distinctUntilChanged()
loadState.toObservable().map { it.isReady() }.distinctUntilChanged()
) { a, b -> !a && b }

var hasConsumedInitialState = false
Expand Down Expand Up @@ -80,14 +84,14 @@ class StoryViewerViewModel(
}

fun setContentIsReady() {
store.update {
it.copy(loadState = it.loadState.copy(isContentReady = true))
loadStore.update {
it.copy(isContentReady = true)
}
}

fun setCrossfaderIsReady(isReady: Boolean) {
store.update {
it.copy(loadState = it.loadState.copy(isCrossfaderReady = isReady))
loadStore.update {
it.copy(isCrossfaderReady = isReady)
}
}

Expand Down Expand Up @@ -151,6 +155,7 @@ class StoryViewerViewModel(
override fun onCleared() {
disposables.clear()
store.dispose()
loadStore.dispose()
}

fun setSelectedPage(page: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,12 @@ class StoryViewerPageFragment :
}
}

lifecycleDisposable += sharedViewModel.state.distinctUntilChanged().observeOn(AndroidSchedulers.mainThread()).subscribe { parentState ->
viewModel.setIsRunningSharedElementAnimation(!parentState.loadState.isCrossfaderReady)
storyContentContainer.visible = parentState.loadState.isCrossfaderReady
lifecycleDisposable += sharedViewModel.loadState.subscribe {
viewModel.setIsRunningSharedElementAnimation(!it.isCrossfaderReady)
storyContentContainer.visible = it.isCrossfaderReady
}

lifecycleDisposable += sharedViewModel.state.distinctUntilChanged().observeOn(AndroidSchedulers.mainThread()).subscribe { parentState ->
if (parentState.pages.size <= parentState.page) {
viewModel.setIsSelectedPage(false)
} else if (storyViewerPageArgs.recipientId == parentState.pages[parentState.page]) {
Expand Down

0 comments on commit 4359336

Please sign in to comment.