Skip to content

Commit

Permalink
refactor using proxy, refactor logging, more
Browse files Browse the repository at this point in the history
  • Loading branch information
LePips committed Oct 6, 2022
1 parent 8a2ebe2 commit 5e1a021
Show file tree
Hide file tree
Showing 17 changed files with 275 additions and 278 deletions.
Binary file modified Example/VLCUIExample/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Example/VLCUIExample/Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct ContentView: View {
var body: some View {
ZStack(alignment: .bottom) {
VLCVideoPlayer(configuration: viewModel.configuration)
.eventSubject(viewModel.eventSubject)
.proxy(viewModel.proxy)
.onTicksUpdated { ticks, playbackInformation in
viewModel.ticks = ticks
viewModel.totalTicks = playbackInformation.length
Expand Down
6 changes: 3 additions & 3 deletions Example/VLCUIExample/Shared/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ContentViewModel: ObservableObject {
@Published
var totalTicks: Int32 = 0

var eventSubject: CurrentValueSubject<VLCVideoPlayer.Event?, Never> = .init(nil)
let proxy: VLCVideoPlayer.Proxy = .init()

var configuration: VLCVideoPlayer.Configuration {
let configuration = VLCVideoPlayer
Expand All @@ -30,7 +30,7 @@ class ContentViewModel: ObservableObject {
((totalTicks.roundDownNearestThousand - ticks.roundDownNearestThousand) / 1000).timeLabel
}

func setCustomPosition(_ position: Float) {
self.position = position
func setCustomPosition(_ newPosition: Float) {
position = newPosition
}
}
1 change: 1 addition & 0 deletions Example/VLCUIExample/Shared/IntExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation

extension Int32 {

var timeLabel: String {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .abbreviated
Expand Down
16 changes: 8 additions & 8 deletions Example/VLCUIExample/iOS/OverlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ struct OverlayView: View {
HStack(spacing: 20) {

Button {
viewModel.eventSubject.send(.jumpBackward(15))
viewModel.proxy.jumpBackward(15)
} label: {
Image(systemName: "gobackward.15")
.font(.system(size: 28, weight: .regular, design: .default))
}

Button {
if viewModel.playerState == .playing {
viewModel.eventSubject.send(.pause)
viewModel.proxy.pause()
} else {
viewModel.eventSubject.send(.play)
viewModel.proxy.play()
}
} label: {
Group {
Expand All @@ -41,14 +41,14 @@ struct OverlayView: View {
}

Button {
viewModel.eventSubject.send(.jumpForward(15))
viewModel.proxy.jumpForward(15)
} label: {
Image(systemName: "goforward.15")
.font(.system(size: 28, weight: .regular, design: .default))
}

HStack(spacing: 5) {
Text((viewModel.ticks.roundDownNearestThousand / 1000).timeLabel)
Text(viewModel.positiveTimeLabel)
.frame(width: 50)

Slider(
Expand All @@ -58,17 +58,17 @@ struct OverlayView: View {
isScrubbing = isEditing
}

Text(((viewModel.totalTicks.roundDownNearestThousand - viewModel.ticks.roundDownNearestThousand) / 1000).timeLabel)
Text(viewModel.negativeTimeLabel)
.frame(width: 50)
}
}
.onChange(of: isScrubbing) { isScrubbing in
guard !isScrubbing else { return }
self.viewModel.eventSubject.send(.setTime(.ticks(viewModel.totalTicks * Int32(currentPosition * 100) / 100)))
viewModel.proxy.setTime(.ticks(viewModel.totalTicks * Int32(currentPosition * 100) / 100))
}
.onChange(of: viewModel.position) { newValue in
guard !isScrubbing else { return }
self.currentPosition = newValue
currentPosition = newValue
}
}
}
12 changes: 6 additions & 6 deletions Example/VLCUIExample/macOS/OverlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct OverlayView: View {
HStack(spacing: 20) {

Button {
viewModel.eventSubject.send(.jumpBackward(15))
viewModel.proxy.jumpBackward(15)
} label: {
Image(systemName: "gobackward.15")
.font(.system(size: 28, weight: .regular, design: .default))
Expand All @@ -23,9 +23,9 @@ struct OverlayView: View {

Button {
if viewModel.playerState == .playing {
viewModel.eventSubject.send(.pause)
viewModel.proxy.pause()
} else {
viewModel.eventSubject.send(.play)
viewModel.proxy.play()
}
} label: {
Group {
Expand All @@ -43,7 +43,7 @@ struct OverlayView: View {
.buttonStyle(.plain)

Button {
viewModel.eventSubject.send(.jumpForward(15))
viewModel.proxy.jumpForward(15)
} label: {
Image(systemName: "goforward.15")
.font(.system(size: 28, weight: .regular, design: .default))
Expand All @@ -67,11 +67,11 @@ struct OverlayView: View {
}
.onChange(of: isScrubbing) { isScrubbing in
guard !isScrubbing else { return }
self.viewModel.eventSubject.send(.setTime(.ticks(viewModel.totalTicks * Int32(currentPosition * 100) / 100)))
viewModel.proxy.setTime(.ticks(viewModel.totalTicks * Int32(currentPosition * 100) / 100))
}
.onChange(of: viewModel.position) { newValue in
guard !isScrubbing else { return }
self.currentPosition = newValue
currentPosition = newValue
}
}
}
8 changes: 4 additions & 4 deletions Example/VLCUIExample/tvOS/OverlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct OverlayView: View {

HStack {
Button {
viewModel.eventSubject.send(.jumpBackward(15))
viewModel.proxy.jumpBackward(15)
} label: {
Image(systemName: "gobackward.15")
.font(.system(size: 28, weight: .regular, design: .default))
Expand All @@ -28,9 +28,9 @@ struct OverlayView: View {

Button {
if viewModel.playerState == .playing {
viewModel.eventSubject.send(.pause)
viewModel.proxy.pause()
} else {
viewModel.eventSubject.send(.play)
viewModel.proxy.play()
}
} label: {
Group {
Expand All @@ -45,7 +45,7 @@ struct OverlayView: View {
.buttonStyle(.plain)

Button {
viewModel.eventSubject.send(.jumpForward(15))
viewModel.proxy.jumpForward(15)
} label: {
Image(systemName: "goforward.15")
.font(.system(size: 28, weight: .regular, design: .default))
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# VLCUI

A [VLCKit](https://code.videolan.org/videolan/VLCKit) wrapper for SwiftUI.

## Requirements
Expand Down
42 changes: 8 additions & 34 deletions Sources/VLCUI/Extensions/VLCMediaPlayerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,57 +73,31 @@ extension VLCMediaPlayer {
func subtitleTrackIndex(from track: VLCVideoPlayer.ValueSelector<Int32>) -> Int32 {
guard let indexes = videoSubTitlesIndexes as? [Int32] else { return -1 }

let trackIndex: Int32

switch track {
case .auto:
if let firstValidTrackIndex = indexes.first(where: { $0 != -1 }) {
trackIndex = firstValidTrackIndex
} else {
trackIndex = -1
}
return indexes.first(where: { $0 != -1 }) ?? -1
case let .absolute(index):
if indexes.contains(index) {
trackIndex = index
} else {
trackIndex = -1
}
return indexes.contains(index) ? index : -1
}

return trackIndex
}

func audioTrackIndex(from track: VLCVideoPlayer.ValueSelector<Int32>) -> Int32 {
guard let indexes = audioTrackIndexes as? [Int32] else { return -1 }

let trackIndex: Int32

switch track {
case .auto:
if let firstValidTrackIndex = indexes.first(where: { $0 != -1 }) {
trackIndex = firstValidTrackIndex
} else {
trackIndex = -1
}
return indexes.first(where: { $0 != -1 }) ?? -1
case let .absolute(index):
if indexes.contains(index) {
trackIndex = index
} else {
trackIndex = -1
}
return indexes.contains(index) ? index : -1
}

return trackIndex
}

func fastForwardSpeed(from speed: VLCVideoPlayer.ValueSelector<Float>) -> Float {
let newSpeed: Float
switch speed {
func rate(from rate: VLCVideoPlayer.ValueSelector<Float>) -> Float {
switch rate {
case .auto:
newSpeed = 1
return 1
case let .absolute(speed):
newSpeed = speed
return speed
}
return newSpeed
}
}
10 changes: 10 additions & 0 deletions Sources/VLCUI/Extensions/ViewExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import SwiftUI

#if os(macOS)
import AppKit
Expand All @@ -22,3 +23,12 @@ extension _PlatformView {
#endif
}
}

extension View {

func copy<Value>(modifying keyPath: WritableKeyPath<Self, Value>, with newValue: Value) -> Self {
var copy = self
copy[keyPath: keyPath] = newValue
return copy
}
}
Loading

0 comments on commit 5e1a021

Please sign in to comment.