Fix lifecycle state for media preview.

After a fragment is destroyed, the media remains loaded in the view model, and it is up to the re-created fragment to take that loaded data and make it ready to be viewed.
This commit is contained in:
Nicholas
2022-10-31 10:08:14 -04:00
committed by GitHub
parent bae070e60e
commit 9f2c7a65ac
2 changed files with 15 additions and 5 deletions

View File

@@ -292,10 +292,10 @@ class MediaPreviewV2Fragment : Fragment(R.layout.fragment_media_preview_v2), Med
ViewCompat.setOnApplyWindowInsetsListener(viewToAnchor) { view: View, windowInsetsCompat: WindowInsetsCompat ->
val layoutParams = view.layoutParams as MarginLayoutParams
layoutParams.setMargins(
windowInsetsCompat.getSystemWindowInsetLeft(),
windowInsetsCompat.systemWindowInsetLeft,
layoutParams.topMargin,
windowInsetsCompat.getSystemWindowInsetRight(),
windowInsetsCompat.getSystemWindowInsetBottom()
windowInsetsCompat.systemWindowInsetRight,
windowInsetsCompat.systemWindowInsetBottom
)
view.layoutParams = layoutParams
windowInsetsCompat
@@ -440,6 +440,11 @@ class MediaPreviewV2Fragment : Fragment(R.layout.fragment_media_preview_v2), Med
getMediaPreviewFragmentFromChildFragmentManager(binding.mediaPager.currentItem)?.pause()
}
override fun onDestroyView() {
super.onDestroyView()
viewModel.onDestroyView()
}
companion object {
const val ARGS_KEY: String = "args"

View File

@@ -26,8 +26,7 @@ class MediaPreviewV2ViewModel : ViewModel() {
fun fetchAttachments(startingAttachmentId: AttachmentId, threadId: Long, sorting: MediaDatabase.Sorting, forceRefresh: Boolean = false) {
if (store.state.loadState == MediaPreviewV2State.LoadState.INIT || forceRefresh) {
disposables += store.update(repository.getAttachments(startingAttachmentId, threadId, sorting)) {
result: MediaPreviewRepository.Result, oldState: MediaPreviewV2State ->
disposables += store.update(repository.getAttachments(startingAttachmentId, threadId, sorting)) { result: MediaPreviewRepository.Result, oldState: MediaPreviewV2State ->
if (oldState.leftIsRecent) {
oldState.copy(
position = result.initialPosition,
@@ -76,4 +75,10 @@ class MediaPreviewV2ViewModel : ViewModel() {
disposables.dispose()
store.dispose()
}
fun onDestroyView() {
store.update { oldState ->
oldState.copy(loadState = MediaPreviewV2State.LoadState.DATA_LOADED)
}
}
}