diff --git a/VideoPlayer/Features/RemoteControl/Views/RemoteControlView.swift b/VideoPlayer/Features/RemoteControl/Views/RemoteControlView.swift index f8afe9d..c77ee16 100644 --- a/VideoPlayer/Features/RemoteControl/Views/RemoteControlView.swift +++ b/VideoPlayer/Features/RemoteControl/Views/RemoteControlView.swift @@ -1,4 +1,112 @@ import SwiftUI +import UIKit + +private struct RemoteChapterThumbnailView: View { + let url: URL? + let isCurrent: Bool + let timeText: String + let action: () -> Void + + @State private var image: UIImage? + @State private var didFail = false + + private let maximumLoadAttempts = 4 + private let retryDelayNanoseconds: UInt64 = 700_000_000 + private let width: CGFloat = 156 + private let height: CGFloat = 88 + + var body: some View { + Button(action: action) { + ZStack(alignment: .bottomTrailing) { + thumbnailContent + .frame(width: width, height: height) + .clipped() + + Text(timeText) + .font(.caption2.monospacedDigit().weight(.semibold)) + .foregroundStyle(.white) + .padding(.horizontal, 6) + .padding(.vertical, 3) + .background(.black.opacity(0.72), in: Capsule()) + .padding(6) + } + .clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous)) + .overlay { + RoundedRectangle(cornerRadius: 14, style: .continuous) + .stroke( + isCurrent ? Color.appGold : Color.white.opacity(0.16), + lineWidth: isCurrent ? 3 : 1 + ) + } + .shadow(color: isCurrent ? Color.appGold.opacity(0.28) : .clear, radius: 10) + } + .buttonStyle(.plain) + .accessibilityLabel("\(timeText)へ移動") + .task(id: url) { + await loadImage() + } + } + + @ViewBuilder + private var thumbnailContent: some View { + if let image { + Image(uiImage: image) + .resizable() + .scaledToFill() + } else { + ZStack { + Color.white.opacity(0.08) + if didFail { + Image(systemName: "photo.badge.exclamationmark") + .foregroundStyle(.white.opacity(0.5)) + } else { + ProgressView() + .tint(Color.appGold) + } + } + } + } + + private func loadImage() async { + image = nil + didFail = false + guard let url else { + didFail = true + return + } + + for attempt in 0.. 0 else { return [] } + return (0.. URL? { guard let serverAddress, let videoID = viewModel.playbackState.videoID else { return nil } return ServerAuth.mediaURL( address: serverAddress, - path: "/thumbnail/\(videoID)" + path: "/thumbnail/\(videoID)", + query: [ + URLQueryItem(name: "time", value: String(format: "%.3f", time)), + URLQueryItem(name: "original", value: "true"), + URLQueryItem(name: "max", value: "480") + ] ) } + private func isCurrentChapter(_ chapterTime: Double) -> Bool { + let duration = viewModel.playbackState.duration + guard duration.isFinite, duration > 0 else { return false } + let interval = duration / Double(chapterCount) + let currentTime = viewModel.playbackState.currentTime + return currentTime >= chapterTime && currentTime < chapterTime + interval + } + private func timeText(_ seconds: Double) -> String { guard seconds.isFinite else { return "0:00" } let totalSeconds = max(Int(seconds), 0)