-
-
Notifications
You must be signed in to change notification settings - Fork 512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new experimental picture viewer #1829
Merged
nielsvanvelzen
merged 3 commits into
jellyfin:master
from
nielsvanvelzen:picture-viewer
Aug 11, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/org/jellyfin/androidtv/ui/picture/PictureViewerActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.jellyfin.androidtv.ui.picture | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.view.KeyEvent | ||
import android.view.View | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.fragment.app.add | ||
import androidx.fragment.app.commit | ||
import androidx.lifecycle.lifecycleScope | ||
import kotlinx.coroutines.launch | ||
import org.jellyfin.androidtv.R | ||
import org.jellyfin.sdk.model.api.BaseItemDto | ||
import org.jellyfin.sdk.model.api.BaseItemKind | ||
import org.jellyfin.sdk.model.serializer.toUUIDOrNull | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
|
||
class PictureViewerActivity : FragmentActivity(R.layout.fragment_content_view) { | ||
companion object { | ||
const val EXTRA_ITEM_ID = "item_id" | ||
const val EXTRA_AUTO_PLAY = "auto_play" | ||
|
||
fun createIntent(context: Context, item: BaseItemDto, autoPlay: Boolean): Intent { | ||
require(item.type == BaseItemKind.PHOTO) { "Expected item of type PHOTO but got ${item.type}" } | ||
|
||
return Intent(context, PictureViewerActivity::class.java).apply { | ||
putExtra(EXTRA_ITEM_ID, item.id.toString()) | ||
putExtra(EXTRA_AUTO_PLAY, autoPlay) | ||
} | ||
} | ||
} | ||
|
||
private val pictureViewerViewModel by viewModel<PictureViewerViewModel>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
// Load requested item in viewmodel | ||
lifecycleScope.launch { | ||
val itemId = requireNotNull(intent.extras?.getString(EXTRA_ITEM_ID)?.toUUIDOrNull()) | ||
pictureViewerViewModel.loadItem(itemId) | ||
|
||
val autoPlay = intent.extras?.getBoolean(EXTRA_AUTO_PLAY) == true | ||
if (autoPlay) pictureViewerViewModel.startPresentation() | ||
} | ||
|
||
// Show fragment | ||
supportFragmentManager.commit { | ||
add<PictureViewerFragment>(R.id.content_view) | ||
} | ||
} | ||
|
||
// Forward key events to fragments | ||
private fun onKeyEvent(keyCode: Int, event: KeyEvent?): Boolean = supportFragmentManager.fragments | ||
.filter { it.isVisible } | ||
.filterIsInstance<View.OnKeyListener>() | ||
.any { it.onKey(currentFocus, keyCode, event) } | ||
|
||
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean = | ||
onKeyEvent(keyCode, event) || super.onKeyDown(keyCode, event) | ||
|
||
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean = | ||
onKeyEvent(keyCode, event) || super.onKeyUp(keyCode, event) | ||
} |
159 changes: 159 additions & 0 deletions
159
app/src/main/java/org/jellyfin/androidtv/ui/picture/PictureViewerFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package org.jellyfin.androidtv.ui.picture | ||
|
||
import android.os.Bundle | ||
import android.view.KeyEvent | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.animation.Animation | ||
import android.view.animation.AnimationUtils | ||
import androidx.core.view.isGone | ||
import androidx.core.view.isVisible | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.lifecycleScope | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.launch | ||
import org.jellyfin.androidtv.R | ||
import org.jellyfin.androidtv.databinding.FragmentPictureViewerBinding | ||
import org.jellyfin.androidtv.ui.AsyncImageView | ||
import org.jellyfin.androidtv.util.createKeyHandler | ||
import org.jellyfin.sdk.api.client.ApiClient | ||
import org.jellyfin.sdk.api.client.extensions.imageApi | ||
import org.jellyfin.sdk.api.client.extensions.libraryApi | ||
import org.jellyfin.sdk.model.api.BaseItemDto | ||
import org.jellyfin.sdk.model.api.ImageType | ||
import org.koin.android.ext.android.inject | ||
import org.koin.androidx.viewmodel.ext.android.sharedViewModel | ||
|
||
class PictureViewerFragment : Fragment(), View.OnKeyListener { | ||
companion object { | ||
/** | ||
* The download API is used by jellyfin-web when loading images. Unfortunately with our | ||
* current code it doesn't work for the app. Larger files (gifs, large photos etc.) can | ||
* cause the app to go out of memory. This is mostly caught by Glide but it ends up | ||
* never displaying the picture in those cases. | ||
* | ||
* This toggle is left in the code in case we migrate to a different image processor that | ||
* potentially fixes the issue. | ||
*/ | ||
const val USE_DOWNLOAD_API = false | ||
} | ||
|
||
private val pictureViewerViewModel by sharedViewModel<PictureViewerViewModel>() | ||
private val api by inject<ApiClient>() | ||
private lateinit var binding: FragmentPictureViewerBinding | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||
binding = FragmentPictureViewerBinding.inflate(inflater, container, false) | ||
binding.actionPrevious.setOnClickListener { pictureViewerViewModel.showPrevious() } | ||
binding.actionNext.setOnClickListener { pictureViewerViewModel.showNext() } | ||
binding.actionPlayPause.setOnClickListener { pictureViewerViewModel.togglePresentation() } | ||
binding.root.setOnClickListener { toggleActions() } | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
lifecycleScope.launch { | ||
pictureViewerViewModel.currentItem.filterNotNull().collect { item -> | ||
binding.itemSwitcher.getNextView<AsyncImageView>().load(item) | ||
binding.itemSwitcher.showNextView() | ||
} | ||
} | ||
|
||
lifecycleScope.launch { | ||
pictureViewerViewModel.presentationActive.collect { active -> | ||
binding.actionPlayPause.isActivated = active | ||
} | ||
} | ||
} | ||
|
||
private val keyHandler = createKeyHandler { | ||
keyDown(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PAUSE) | ||
.body { pictureViewerViewModel.togglePresentation() } | ||
|
||
keyDown(KeyEvent.KEYCODE_DPAD_LEFT) | ||
.condition { !binding.actions.isVisible } | ||
.body { pictureViewerViewModel.showPrevious() } | ||
|
||
keyDown(KeyEvent.KEYCODE_MEDIA_SKIP_BACKWARD, KeyEvent.KEYCODE_MEDIA_PREVIOUS) | ||
.body { pictureViewerViewModel.showPrevious() } | ||
|
||
keyDown(KeyEvent.KEYCODE_DPAD_RIGHT) | ||
.condition { !binding.actions.isVisible } | ||
.body { pictureViewerViewModel.showNext() } | ||
|
||
keyDown(KeyEvent.KEYCODE_MEDIA_SKIP_FORWARD, KeyEvent.KEYCODE_MEDIA_NEXT) | ||
Check warning Code scanning / Android Lint Using inlined constants on older versions
Field requires API level 23 (current min is 21): android.view.KeyEvent#KEYCODE_MEDIA_SKIP_FORWARD
|
||
.body { pictureViewerViewModel.showNext() } | ||
|
||
keyDown( | ||
KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, | ||
KeyEvent.KEYCODE_ENTER, KeyEvent.KEYCODE_DPAD_CENTER, | ||
) | ||
.condition { !binding.actions.isVisible } | ||
.body { showActions() } | ||
|
||
keyDown(KeyEvent.KEYCODE_DPAD_DOWN, KeyEvent.KEYCODE_BACK) | ||
.condition { binding.actions.isVisible } | ||
.body { hideActions() } | ||
} | ||
|
||
override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean = keyHandler.onKey(event) | ||
|
||
private var focusedActionView: View? = null | ||
fun showActions(): Boolean { | ||
if (binding.actions.isVisible) return false | ||
|
||
binding.actions.isVisible = true | ||
if (focusedActionView?.requestFocus() != true) binding.actionPlayPause.requestFocus() | ||
binding.actions.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_in)) | ||
return true | ||
} | ||
|
||
fun hideActions(): Boolean { | ||
if (!binding.actions.isVisible) return false | ||
binding.actions.startAnimation(AnimationUtils.loadAnimation(context, R.anim.fade_out).apply { | ||
setAnimationListener(object : Animation.AnimationListener { | ||
override fun onAnimationStart(animation: Animation?) = Unit | ||
|
||
override fun onAnimationEnd(animation: Animation?) { | ||
focusedActionView = binding.actions.findFocus() | ||
binding.actions.isGone = true | ||
} | ||
|
||
override fun onAnimationRepeat(animation: Animation?) = Unit | ||
}) | ||
}) | ||
return true | ||
} | ||
|
||
fun toggleActions(): Boolean { | ||
return if (binding.actions.isVisible) hideActions() | ||
else showActions() | ||
} | ||
|
||
private fun AsyncImageView.load(item: BaseItemDto) { | ||
val url = when (USE_DOWNLOAD_API) { | ||
true -> api.libraryApi.getDownloadUrl( | ||
itemId = item.id, | ||
includeCredentials = true, // TODO send authentication via header | ||
) | ||
false -> api.imageApi.getItemImageUrl( | ||
itemId = item.id, | ||
imageType = ImageType.PRIMARY, | ||
tag = item.imageTags?.get(ImageType.PRIMARY), | ||
// Ask the server to downscale the image to avoid the app going out of memory | ||
// unfortunately this can be a bit slow for larger files | ||
maxWidth = context.resources.displayMetrics.widthPixels, | ||
maxHeight = context.resources.displayMetrics.heightPixels, | ||
) | ||
} | ||
|
||
load( | ||
url = url, | ||
blurHash = item.imageBlurHashes?.get(ImageType.PRIMARY)?.get(item.imageTags?.get(ImageType.PRIMARY)), | ||
aspectRatio = item.primaryImageAspectRatio ?: 1.0, | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / Android Lint
Using inlined constants on older versions