perf(media card): use recyleboxlayout for more efficiency and better performance

This commit is contained in:
Benex254
2024-08-05 09:46:56 +03:00
parent db7b38ffac
commit 2e2572db3f
10 changed files with 197 additions and 66 deletions

View File

@@ -1,19 +1,23 @@
<MediaCardsContainer>
size_hint:1,None
height:max(self.minimum_height,dp(350),container.minimum_height)
height: dp(250)
container:container
orientation: 'vertical'
padding:"10dp"
spacing:"5dp"
MDLabel:
bold:True
adaptive_height:True
text:root.list_name
MDScrollView:
size_hint:1,None
height:container.minimum_height
MDBoxLayout:
MDRecycleView:
id:container
spacing:"10dp"
padding:"0dp","10dp","100dp","10dp"
size_hint:None,None
height:self.minimum_height
width:self.minimum_width
key_viewclass:"viewclass"
key_size:"width"
RecycleBoxLayout:
size_hint:None,1
width:self.minimum_width
default_size_hint:None, None
default_size:dp(150),dp(100)
spacing:"10dp"
padding:"0dp","10dp","100dp","10dp"

View File

@@ -1,10 +1,12 @@
from kivy.uix.videoplayer import VideoPlayer
# TODO: make fullscreen exp better
class MediaPopupVideoPlayer(VideoPlayer):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# FIXME: find way to make fullscreen stable
#
self.allow_fullscreen = False
def on_fullscreen(self, instance, value):
super().on_fullscreen(instance, value)

View File

@@ -27,9 +27,10 @@
height: dp(280)
line_color:root.caller.has_trailer_color
line_width:1
# TODO: Remove the test source
MediaPopupVideoPlayer:
id:player
source: root.caller.trailer_url
source: root.caller.trailer_url if root.caller.trailer_url else 'https://www088.vipanicdn.net/streamhls/abae70787c7bd2fcd4fab986c2a5aeba/ep.7.1703900604.m3u8'
thumbnail:app.default_anime_image
state:"play" if root.caller.trailer_url else "stop"
# fit_mode:"fill"

View File

@@ -59,7 +59,7 @@ class MediaPopup(
self.caller = caller
def on_caller(self, *args):
Clock.schedule_once(lambda _: self.apply_class_lang_rules(), -1)
self.apply_class_lang_rules()
def open(self, *_args, **kwargs):
"""Display the modal in the Window.

View File

@@ -1,11 +1,8 @@
<MediaCard>
adaptive_height:True
spacing:"5dp"
image:"https://s4.anilist.co/file/anilistcdn/media/anime/cover/small/bx163270-oxwgbe43Cpog.jpg"
size_hint_x: None
width:dp(100)
on_release:
self.open()
FitImage:
source:root.cover_image_url
fit_mode:"fill"
@@ -14,6 +11,8 @@
height: dp(150)
MDDivider:
color:root.theme_cls.primaryColor if root._trailer_url else [0.5, 0.5, 0.5, 0.5]
size_hint: None, 1
width: dp(100)
SingleLineLabel:
font_style:"Label"
role:"medium"
@@ -21,4 +20,7 @@
max_lines:2
halign:"center"
color:self.theme_cls.secondaryColor
size_hint_x: None
shorten:True
width: dp(100)

View File

@@ -1,4 +1,5 @@
from kivy.clock import Clock
from kivy.factory import Factory
from kivy.properties import (
BooleanProperty,
ListProperty,
@@ -6,7 +7,6 @@ from kivy.properties import (
ObjectProperty,
StringProperty,
)
from kivy.uix.behaviors import ButtonBehavior
from kivymd.app import MDApp
from kivymd.uix.behaviors import HoverBehavior
from kivymd.uix.boxlayout import MDBoxLayout
@@ -14,7 +14,7 @@ from kivymd.uix.boxlayout import MDBoxLayout
from .components.media_popup import MediaPopup
class MediaCard(ButtonBehavior, HoverBehavior, MDBoxLayout):
class MediaCard(HoverBehavior, MDBoxLayout):
screen = ObjectProperty()
anime_id = NumericProperty()
title = StringProperty()
@@ -38,6 +38,7 @@ class MediaCard(ButtonBehavior, HoverBehavior, MDBoxLayout):
cover_image_url = StringProperty()
preview_image = StringProperty()
has_trailer_color = ListProperty([0.5, 0.5, 0.5, 0.5])
_popup_opened = False
def __init__(self, trailer_url=None, **kwargs):
super().__init__(**kwargs)
@@ -49,6 +50,38 @@ class MediaCard(ButtonBehavior, HoverBehavior, MDBoxLayout):
self.trailer_url = trailer_url
self.adaptive_size = True
def on_touch_down(self, touch):
if touch.is_mouse_scrolling:
return False
if not self.collide_point(touch.x, touch.y):
return False
if self in touch.ud:
return False
if not self.app:
return False
disabled = True
# FIXME: double tap not working
#
if not disabled and touch.is_double_tap:
self.app.show_anime_screen(self.anime_id, self.screen.name)
touch.grab(self)
touch.ud[self] = True
self.last_touch = touch
elif self.collide_point(*touch.pos):
if not self._popup_opened:
Clock.schedule_once(self.open)
self._popup_opened = True
touch.grab(self)
touch.ud[self] = True
self.last_touch = touch
return True
else:
super().on_touch_down(touch)
# FIXME: Figure a good way implement this
# for now its debugy so scraping it till fix
def on_enter(self):
def _open_popup(dt):
if self.hovering:
@@ -59,15 +92,16 @@ class MediaCard(ButtonBehavior, HoverBehavior, MDBoxLayout):
return
self.open()
Clock.schedule_once(_open_popup, 5)
# Clock.schedule_once(_open_popup, 5)
def on_popup_open(self, popup: MediaPopup):
popup.center = self.center
def on_dismiss(self, popup: MediaPopup):
popup.player.state = "stop"
if popup.player._video:
popup.player._video.unload()
self._popup_opened = False
# if popup.player._video:
# popup.player._video.unload()
def set_preview_image(self, image):
self.preview_image = image
@@ -84,20 +118,19 @@ class MediaCard(ButtonBehavior, HoverBehavior, MDBoxLayout):
popup.bind(on_dismiss=self.on_dismiss, on_open=self.on_popup_open)
popup.open(self)
# trailer stuff
from ....Utility.media_card_loader import media_card_loader
def _get_trailer(dt):
if trailer := self._trailer_url:
# trailer stuff
from ....Utility.media_card_loader import media_card_loader
if trailer := self._trailer_url:
# from ....Utility import show_notification
if trailer_url := media_card_loader.get_trailer_from_pytube(
trailer, self.title
):
self.trailer_url = trailer_url
else:
self._trailer_url = ""
# TODO: show an indefinate progress while traile is still not available
# show_notification("Pytube", "Please wait for trailer to load")
if trailer_url := media_card_loader.get_trailer_from_pytube(
trailer, self.title
):
self.trailer_url = trailer_url
else:
self._trailer_url = ""
Clock.schedule_once(_get_trailer, 1)
# ---------------respond to user actions and call appropriate model-------------------------
def on_is_in_my_list(self, instance, in_user_anime_list):
@@ -111,6 +144,9 @@ class MediaCard(ButtonBehavior, HoverBehavior, MDBoxLayout):
pass
Factory.register("MediaCard", MediaCard)
class MediaCardsContainer(MDBoxLayout):
container = ObjectProperty()
list_name = StringProperty()