Hide dot indicator in fullscreen album

This commit is contained in:
Matthieu 2024-03-07 07:56:30 +01:00
parent 908d1a54c9
commit 54711d4e81
2 changed files with 31 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package org.pixeldroid.app.posts
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import android.view.MotionEvent
import android.view.View import android.view.View
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -12,6 +11,7 @@ import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle import androidx.lifecycle.repeatOnLifecycle
import androidx.viewpager2.widget.ViewPager2
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.pixeldroid.app.databinding.ActivityAlbumBinding import org.pixeldroid.app.databinding.ActivityAlbumBinding
@ -25,29 +25,43 @@ class AlbumActivity : AppCompatActivity() {
val binding = ActivityAlbumBinding.inflate(layoutInflater) val binding = ActivityAlbumBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
binding.albumPager.adapter = AlbumViewPagerAdapter(model.uiState.value.mediaAttachments, binding.albumPager.adapter = AlbumViewPagerAdapter(
model.uiState.value.mediaAttachments,
sensitive = false, sensitive = false,
opened = true, opened = true,
//In the activity, we assume we want to show everything //In the activity, we assume we want to show everything
alwaysShowNsfw = true, alwaysShowNsfw = true,
clickCallback = ::clickCallback clickCallback = ::clickCallback
) )
binding.albumPager.currentItem = model.uiState.value.index
binding.albumPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) { model.positionSelected(position) }
})
if (model.uiState.value.mediaAttachments.size == 1) { if (model.uiState.value.mediaAttachments.size == 1) {
binding.albumPager.isUserInputEnabled = false binding.albumPager.isUserInputEnabled = false
} } else if ((model.uiState.value.mediaAttachments.size) > 1) {
else if ((model.uiState.value.mediaAttachments.size) > 1) {
binding.postIndicator.setViewPager(binding.albumPager) binding.postIndicator.setViewPager(binding.albumPager)
binding.postIndicator.visibility = View.VISIBLE binding.postIndicator.visibility = View.VISIBLE
} else { } else {
binding.postIndicator.visibility = View.GONE binding.postIndicator.visibility = View.GONE
} }
// Not really necessary because the ViewPager saves its state in onSaveInstanceState, but
// it's good to stay consistent in case something gets out of sync
binding.albumPager.setCurrentItem(model.uiState.value.index, false)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowTitleEnabled(false) supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.setBackgroundDrawable(null) supportActionBar?.setBackgroundDrawable(null)
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
model.uiState.collect { uiState ->
binding.albumPager.currentItem = uiState.index
}
}
}
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
model.isActionBarHidden.collect { isActionBarHidden -> model.isActionBarHidden.collect { isActionBarHidden ->
@ -60,6 +74,7 @@ class AlbumActivity : AppCompatActivity() {
// Hide both the status bar and the navigation bar // Hide both the status bar and the navigation bar
supportActionBar?.hide() supportActionBar?.hide()
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
binding.postIndicator.visibility = View.GONE
} else { } else {
// Configure the behavior of the hidden system bars // Configure the behavior of the hidden system bars
windowInsetsController.systemBarsBehavior = windowInsetsController.systemBarsBehavior =
@ -67,6 +82,9 @@ class AlbumActivity : AppCompatActivity() {
// Show both the status bar and the navigation bar // Show both the status bar and the navigation bar
supportActionBar?.show() supportActionBar?.show()
windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
if ((model.uiState.value.mediaAttachments.size) > 1) {
binding.postIndicator.visibility = View.VISIBLE
}
} }
} }
} }

View File

@ -17,10 +17,6 @@ data class AlbumUiState(
@HiltViewModel @HiltViewModel
class AlbumViewModel @Inject constructor(state: SavedStateHandle) : ViewModel() { class AlbumViewModel @Inject constructor(state: SavedStateHandle) : ViewModel() {
fun barHide() {
_isActionBarHidden.update { !it }
}
companion object { companion object {
const val ALBUM_IMAGES = "AlbumViewImages" const val ALBUM_IMAGES = "AlbumViewImages"
const val ALBUM_INDEX = "AlbumViewIndex" const val ALBUM_INDEX = "AlbumViewIndex"
@ -39,4 +35,12 @@ class AlbumViewModel @Inject constructor(state: SavedStateHandle) : ViewModel()
val uiState: StateFlow<AlbumUiState> = _uiState.asStateFlow() val uiState: StateFlow<AlbumUiState> = _uiState.asStateFlow()
val isActionBarHidden: StateFlow<Boolean> = _isActionBarHidden val isActionBarHidden: StateFlow<Boolean> = _isActionBarHidden
fun barHide() {
_isActionBarHidden.update { !it }
}
fun positionSelected(position: Int) {
_uiState.update { it.copy(index = position) }
}
} }