Skip to content

Commit

Permalink
feat: サムネイルが読み込まれるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Oct 9, 2023
1 parent 8f48854 commit 87cdc54
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,37 @@ import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.wada811.databinding.dataBinding
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import net.pantasystem.milktea.common.glide.GlideApp
import net.pantasystem.milktea.media.databinding.FragmentImageBinding

class ImageFragment : Fragment(R.layout.fragment_image){
class ImageFragment : Fragment(R.layout.fragment_image) {

companion object{
companion object {
private const val EXTRA_INDEX = "jp.panta.misskeyandroidclient.ui.media.EXTRA_INDEX"
fun newInstance(index: Int, url: String): ImageFragment {
val bundle = Bundle().apply{
fun newInstance(index: Int, url: String, thumbnailUrl: String? = null): ImageFragment {
val bundle = Bundle().apply {
putString(ImageViewModel.EXTRA_IMAGE_URL, url)
putInt(EXTRA_INDEX, index)
putString(ImageViewModel.EXTRA_IMAGE_THUMBNAIL_URL, thumbnailUrl)
}
return ImageFragment().apply{
return ImageFragment().apply {
arguments = bundle
}
}

fun newInstance(index: Int, uri: Uri): ImageFragment {
return ImageFragment().apply{
arguments = Bundle().apply{
return ImageFragment().apply {
arguments = Bundle().apply {
putString(ImageViewModel.EXTRA_IMAGE_URI, uri.toString())
putInt(EXTRA_INDEX, index)
}
}
}

fun newInstance(index: Int, file: File): ImageFragment {
return newInstance(index, file.path!!)
return newInstance(index, file.path!!, file.thumbnailUrl)
}
}

Expand All @@ -57,18 +56,16 @@ class ImageFragment : Fragment(R.layout.fragment_image){
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

index = arguments?.getInt(EXTRA_INDEX)?: 0
index = arguments?.getInt(EXTRA_INDEX) ?: 0

binding.swipeFinishLayout.setOnFinishEventListener {
requireActivity().finish()
}


viewModel.uiState.map {
it.uri ?: it.url
}.distinctUntilChanged().onEach { url ->
viewModel.uiState.onEach { uiState ->
GlideApp.with(view)
.load(url)
.load(uiState.uri)
.addListener(object : RequestListener<Drawable?> {
override fun onLoadFailed(
e: GlideException?,
Expand All @@ -91,12 +88,18 @@ class ImageFragment : Fragment(R.layout.fragment_image){
return false
}
})
.let {
if (uiState.thumbnailUrl != null && uiState.uri != uiState.thumbnailUrl)
it.thumbnail(
GlideApp.with(view).load(uiState.thumbnailUrl)
)
else
it
}
.into(binding.imageView)
}.flowWithLifecycle(lifecycle).launchIn(lifecycleScope)

viewModel.uiState.map {
it.isLoading
}.distinctUntilChanged().onEach {
viewModel.isImageLoading.onEach {
binding.progressBar.isVisible = it
}.flowWithLifecycle(lifecycle).launchIn(lifecycleScope)
}
Expand All @@ -105,7 +108,7 @@ class ImageFragment : Fragment(R.layout.fragment_image){
super.onResume()

val activity = requireActivity()
if(activity is MediaActivity){
if (activity is MediaActivity) {
activity.setCurrentFileIndex(index)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package net.pantasystem.milktea.media

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
Expand All @@ -15,6 +19,7 @@ class ImageViewModel @Inject constructor(
companion object {
const val EXTRA_IMAGE_URL = "jp.panta.misskeyandroidclient.ui.media.EXTRA_IMAGE_URL"
const val EXTRA_IMAGE_URI = "jp.panta.misskeyandroidclient.ui.media.EXTRA_IMAGE_URI"
const val EXTRA_IMAGE_THUMBNAIL_URL = "jp.panta.misskeyandroidclient.ui.media.EXTRA_IMAGE_THUMBNAIL_URL"
}

private val url = savedStateHandle.getStateFlow<String?>(
Expand All @@ -27,27 +32,37 @@ class ImageViewModel @Inject constructor(
null,
)

private val isImageLoading = MutableStateFlow(true)
private val thumbnailUrl = savedStateHandle.getStateFlow<String?>(
EXTRA_IMAGE_THUMBNAIL_URL,
null,
)

private val _isImageLoading = MutableStateFlow(true)
val isImageLoading = _isImageLoading.asStateFlow()

val uiState = combine(url, uri, isImageLoading) { url, uri, loading ->
val uiState = combine(url, uri, thumbnailUrl) { url, uri, thumbnail ->
ImageUiState(
url = url,
uri = uri,
isLoading = loading,
uri = url ?: uri,
thumbnailUrl = thumbnail,
)
}

fun onResourceReady() {
isImageLoading.value = false
viewModelScope.launch {
delay(100)
_isImageLoading.value = false
}
}

fun onLoadFailed() {
isImageLoading.value = false
viewModelScope.launch {
delay(100)
_isImageLoading.value = false
}
}
}

data class ImageUiState(
val uri: String?,
val url: String?,
val isLoading: Boolean = false,
val thumbnailUrl: String?,
)

0 comments on commit 87cdc54

Please sign in to comment.