Create ViewModel for AlbumActivity

This commit is contained in:
Fred 2023-07-18 18:22:30 +02:00
parent 88cd015325
commit d9da842df7
3 changed files with 99 additions and 7 deletions

View File

@ -2,30 +2,36 @@ package org.pixeldroid.app.posts
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.activity.viewModels
import org.pixeldroid.app.databinding.ActivityAlbumBinding import org.pixeldroid.app.databinding.ActivityAlbumBinding
import org.pixeldroid.app.utils.BaseActivity import org.pixeldroid.app.utils.BaseActivity
import org.pixeldroid.app.utils.api.objects.Attachment
class AlbumActivity : BaseActivity() { class AlbumActivity : BaseActivity() {
private lateinit var model: AlbumViewModel
private var isActionBarHidden = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val binding = ActivityAlbumBinding.inflate(layoutInflater) val binding = ActivityAlbumBinding.inflate(layoutInflater)
val _model: AlbumViewModel by viewModels { AlbumViewModelFactory(application, intent) }
model = _model
setContentView(binding.root) setContentView(binding.root)
val mediaAttachments = intent.getSerializableExtra("images") as ArrayList<Attachment>
val index = intent.getIntExtra("index", 0) binding.albumPager.adapter = AlbumViewPagerAdapter(model.uiState.value.mediaAttachments,
binding.albumPager.adapter = AlbumViewPagerAdapter(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
) )
binding.albumPager.currentItem = index binding.albumPager.currentItem = model.uiState.value.index
if(mediaAttachments.size == 1){ if (model.uiState.value.mediaAttachments.size == 1) {
binding.albumPager.isUserInputEnabled = false binding.albumPager.isUserInputEnabled = false
} }
else if((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 {
@ -35,5 +41,30 @@ class AlbumActivity : BaseActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowTitleEnabled(false) supportActionBar?.setDisplayShowTitleEnabled(false)
supportActionBar?.setBackgroundDrawable(null) supportActionBar?.setBackgroundDrawable(null)
// TODO: Move from StatusViewHolder (877-893) to here
// TODO: Issue is that albumPager does not listen to the clicks
/*binding.albumPager.setOnClickListener {
val windowInsetsController =
WindowCompat.getInsetsController(this.window, it)
// Configure the behavior of the hidden system bars
if (isActionBarHidden) {
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Hide both the status bar and the navigation bar
supportActionBar?.show()
windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
isActionBarHidden = false
} else {
// Configure the behavior of the hidden system bars
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// Hide both the status bar and the navigation bar
supportActionBar?.hide()
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
isActionBarHidden = true
}
}*/
} }
} }

View File

@ -0,0 +1,42 @@
package org.pixeldroid.app.posts
import android.app.Application
import android.content.Intent
import android.os.Build
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import org.pixeldroid.app.utils.api.objects.Attachment
data class AlbumUiState(
val mediaAttachments: ArrayList<Attachment> = arrayListOf(),
val index: Int = 0
)
class AlbumViewModel(application: Application, intent: Intent) : AndroidViewModel(application) {
private val _uiState: MutableStateFlow<AlbumUiState>
init {
_uiState = MutableStateFlow(AlbumUiState(
mediaAttachments = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra("images", arrayListOf<Attachment>()::class.java)!!
} else {
intent.getSerializableExtra("images") as ArrayList<Attachment>
},
index = intent.getIntExtra("index", 0)
))
}
val uiState: StateFlow<AlbumUiState> = _uiState.asStateFlow()
}
class AlbumViewModelFactory(val application: Application, val intent: Intent) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return modelClass.getConstructor(Application::class.java, Intent::class.java).newInstance(application, intent)
}
}

View File

@ -4516,6 +4516,14 @@
<sha256 value="faf82de0dc02e0c0ae327cd653f37255496b2e53fce280b3ab4cb34553a89086" origin="Generated by Gradle"/> <sha256 value="faf82de0dc02e0c0ae327cd653f37255496b2e53fce280b3ab4cb34553a89086" origin="Generated by Gradle"/>
</artifact> </artifact>
</component> </component>
<component group="com.jakewharton.timber" name="timber" version="4.7.1">
<artifact name="timber-4.7.1.aar">
<sha256 value="2843b37d53ad5016ab7f7a1d02f7fad5cdf11f23ee5285dd2968787c696208e3" origin="Generated by Gradle"/>
</artifact>
<artifact name="timber-4.7.1.pom">
<sha256 value="e3313f03ac62bf4ff331bf3aea9db078625b03790bba49ce0a1def0500f91336" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.jakewharton.timber" name="timber" version="5.0.1"> <component group="com.jakewharton.timber" name="timber" version="5.0.1">
<artifact name="timber-5.0.1.aar"> <artifact name="timber-5.0.1.aar">
<sha256 value="c6edddfcc8eff42a1604c8577fcfa4b4ffd9f252122c52ea36cfe7967f512f71" origin="Generated by Gradle"/> <sha256 value="c6edddfcc8eff42a1604c8577fcfa4b4ffd9f252122c52ea36cfe7967f512f71" origin="Generated by Gradle"/>
@ -4820,6 +4828,9 @@
<artifact name="okio-2.8.0.module"> <artifact name="okio-2.8.0.module">
<sha256 value="17baab7270389a5fa63ab12811864d0a00f381611bc4eb042fa1bd5918ed0965" origin="Generated by Gradle"/> <sha256 value="17baab7270389a5fa63ab12811864d0a00f381611bc4eb042fa1bd5918ed0965" origin="Generated by Gradle"/>
</artifact> </artifact>
<artifact name="okio-jvm-2.8.0.jar">
<sha256 value="4496b06e73982fcdd8a5393f46e5df2ce2fa4465df5895454cac68a32f09bbc8" origin="Generated by Gradle"/>
</artifact>
</component> </component>
<component group="com.squareup.okio" name="okio" version="3.2.0"> <component group="com.squareup.okio" name="okio" version="3.2.0">
<artifact name="okio-3.2.0.module"> <artifact name="okio-3.2.0.module">
@ -6028,6 +6039,14 @@
<sha256 value="965aeb2bedff369819bdde1bf7a0b3b89b8247dd69c88b86375d76163bb8c397" origin="Generated by Gradle"/> <sha256 value="965aeb2bedff369819bdde1bf7a0b3b89b8247dd69c88b86375d76163bb8c397" origin="Generated by Gradle"/>
</artifact> </artifact>
</component> </component>
<component group="org.jetbrains" name="annotations" version="16.0.1">
<artifact name="annotations-16.0.1.jar">
<sha256 value="b11d2523ad1e1f25a13cfccd7b4bc05b89c34647f768183ce4084345574dc9c5" origin="Generated by Gradle"/>
</artifact>
<artifact name="annotations-16.0.1.pom">
<sha256 value="7680ddc26d6ae358e61972d1ae29ca2f575ad3413254e2dbb1d72dbd57ff1f9b" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="org.jetbrains" name="annotations" version="20.1.0"> <component group="org.jetbrains" name="annotations" version="20.1.0">
<artifact name="annotations-20.1.0.jar"> <artifact name="annotations-20.1.0.jar">
<sha256 value="edc8e3ec9796a5f41c1ae44b2d318507ee6ac1212f121d93d33699b3d0aff638" origin="Generated by Gradle"/> <sha256 value="edc8e3ec9796a5f41c1ae44b2d318507ee6ac1212f121d93d33699b3d0aff638" origin="Generated by Gradle"/>