damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit df20b67fc12d10bec362e79f2ef7f4205f532871
parent 0b5d68c0b8fc78b1be69db87588926c75534d612
Author: gladiusKatana <garthsnyder@protonmail.com>
Date:   Thu,  8 Jun 2023 19:44:40 -0400

video: stop video in post when it disappears from view

Changelog-Fixes: Stop video when it disappears from view

Diffstat:
Mdamus/Views/Video/DamusVideoPlayer.swift | 41+++++++++++++++++++++++++----------------
1 file changed, 25 insertions(+), 16 deletions(-)

diff --git a/damus/Views/Video/DamusVideoPlayer.swift b/damus/Views/Video/DamusVideoPlayer.swift @@ -43,25 +43,34 @@ struct DamusVideoPlayer: View { } var body: some View { - ZStack(alignment: .bottomTrailing) { - VideoPlayer(url: url, model: model) - .onAppear { - model.start() + GeometryReader { geo in + let localFrame = geo.frame(in: .local) + let localCenter = CGPoint(x: localFrame.midX, y: localFrame.midY) + let globalCenter = geo.frame(in: .global).origin.applying(.init(translationX: localCenter.x, y: localCenter.y)) + let centerY = globalCenter.y + + ZStack(alignment: .bottomTrailing) { + VideoPlayer(url: url, model: model) + if model.has_audio == true { + MuteIcon + .zIndex(11.0) + .onTapGesture { + self.model.muted = !self.model.muted + } } - - if model.has_audio == true { - MuteIcon - .zIndex(11.0) - .onTapGesture { - self.model.muted = !self.model.muted - } } - } - .onChange(of: model.size) { size in - guard let size else { - return + .onChange(of: model.size) { size in + guard let size else { + return + } + video_size = size + } + .onChange(of: centerY) { _ in + let screenHeight = UIScreen.main.bounds.height + let screenMidY = screenHeight / 2 + let tol = 0.20 * screenHeight /// tolerance - can vary to taste ie., % of screen height of a centered box in which video plays + model.play = centerY > screenMidY - tol && centerY < screenMidY + tol /// video plays when inside tolerance box } - video_size = size } } }