Initial commit: SimVision tvOS streaming app

Features:
- VOD library with movie grouping and version detection
- TV show library with season/episode organization
- TMDB integration for trending shows and recently aired episodes
- Recent releases section with TMDB release date sorting
- Watch history tracking with continue watching
- Playlist caching (12-hour TTL) for offline support
- M3U playlist parsing with XStream API support
- Authentication with credential storage

Technical:
- SwiftUI for tvOS
- Actor-based services for thread safety
- Persistent caching for playlists, TMDB data, and watch history
- KSPlayer integration for video playback

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-21 22:12:08 -06:00
commit 872354b834
283 changed files with 338296 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "255",
"green" : "255",
"red" : "255"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,75 @@
{
"images" : [
{
"filename" : "app-icon-1024.png",
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "16x16"
},
{
"filename" : "app-icon-32.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "16x16"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "32x32"
},
{
"filename" : "app-icon-64.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "32x32"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "128x128"
},
{
"filename" : "app-icon-256.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "128x128"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "256x256"
},
{
"filename" : "app-icon-512.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "256x256"
},
{
"idiom" : "mac",
"scale" : "1x",
"size" : "512x512"
},
{
"filename" : "app-icon-1024 1.png",
"idiom" : "mac",
"scale" : "2x",
"size" : "512x512"
},
{
"filename" : "app-icon-1024 2.png",
"idiom" : "universal",
"platform" : "watchos",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 644 B

View File

@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "app-icon-back-1280x768.png",
"idiom" : "tv"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,17 @@
{
"info" : {
"author" : "xcode",
"version" : 1
},
"layers" : [
{
"filename" : "Front.imagestacklayer"
},
{
"filename" : "Middle.imagestacklayer"
},
{
"filename" : "Back.imagestacklayer"
}
]
}

View File

@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "app-icon-front-1280x768.png",
"idiom" : "tv"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,11 @@
{
"images" : [
{
"idiom" : "tv"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,18 @@
{
"images" : [
{
"filename" : "app-icon-back-400.png",
"idiom" : "tv",
"scale" : "1x"
},
{
"filename" : "app-icon-back-400@2x.png",
"idiom" : "tv",
"scale" : "2x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,17 @@
{
"info" : {
"author" : "xcode",
"version" : 1
},
"layers" : [
{
"filename" : "Front.imagestacklayer"
},
{
"filename" : "Middle.imagestacklayer"
},
{
"filename" : "Back.imagestacklayer"
}
]
}

View File

@@ -0,0 +1,18 @@
{
"images" : [
{
"filename" : "app-icon-front-400.png",
"idiom" : "tv",
"scale" : "1x"
},
{
"filename" : "app-icon-front-400@2x.png",
"idiom" : "tv",
"scale" : "2x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,16 @@
{
"images" : [
{
"idiom" : "tv",
"scale" : "1x"
},
{
"idiom" : "tv",
"scale" : "2x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,32 @@
{
"assets" : [
{
"filename" : "App Icon - App Store.imagestack",
"idiom" : "tv",
"role" : "primary-app-icon",
"size" : "1280x768"
},
{
"filename" : "App Icon.imagestack",
"idiom" : "tv",
"role" : "primary-app-icon",
"size" : "400x240"
},
{
"filename" : "Top Shelf Image Wide.imageset",
"idiom" : "tv",
"role" : "top-shelf-image-wide",
"size" : "2320x720"
},
{
"filename" : "Top Shelf Image.imageset",
"idiom" : "tv",
"role" : "top-shelf-image",
"size" : "1920x720"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

View File

@@ -0,0 +1,18 @@
{
"images" : [
{
"filename" : "2320.png",
"idiom" : "tv",
"scale" : "1x"
},
{
"filename" : "4640.png",
"idiom" : "tv",
"scale" : "2x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,16 @@
{
"images" : [
{
"idiom" : "tv",
"scale" : "1x"
},
{
"idiom" : "tv",
"scale" : "2x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}

View File

@@ -0,0 +1,166 @@
import KSPlayer
import SwiftUI
struct ContentView: View {
#if !os(tvOS)
@Environment(\.openWindow) private var openWindow
#endif
@EnvironmentObject
private var appModel: APPModel
private var initialView: some View {
#if os(macOS)
NavigationSplitView {
List(selection: $appModel.tabSelected) {
link(to: .Home)
link(to: .Favorite)
link(to: .Files)
}
} detail: {
appModel.tabSelected.destination(appModel: appModel)
}
#else
TabView(selection: $appModel.tabSelected) {
tab(to: .Home)
tab(to: .Favorite)
tab(to: .Files)
tab(to: .Setting)
}
#endif
}
var body: some View {
initialView
.preferredColorScheme(.dark)
.background(Color.black)
.sheet(isPresented: $appModel.openURLImport) {
URLImportView()
}
.onChange(of: appModel.openURL) { url in
if let url {
#if !os(tvOS)
openWindow(value: url)
#endif
appModel.openURL = nil
}
}
.onChange(of: appModel.openPlayModel) { model in
if let model {
#if !os(tvOS)
openWindow(value: model)
#endif
appModel.openPlayModel = nil
}
}
#if !os(tvOS)
.onDrop(of: ["public.url", "public.file-url"], isTargeted: nil) { items -> Bool in
guard let item = items.first, let identifier = item.registeredTypeIdentifiers.first else {
return false
}
item.loadItem(forTypeIdentifier: identifier, options: nil) { urlData, _ in
if let urlData = urlData as? Data {
let url = NSURL(absoluteURLWithDataRepresentation: urlData, relativeTo: nil) as URL
DispatchQueue.main.async {
appModel.open(url: url)
}
}
}
return true
}
.fileImporter(isPresented: $appModel.openFileImport, allowedContentTypes: [.movie, .audio, .data]) { result in
guard let url = try? result.get() else {
return
}
if url.startAccessingSecurityScopedResource() {
appModel.open(url: url)
}
}
#endif
.onOpenURL { url in
KSLog("onOpenURL")
appModel.open(url: url)
}
}
func link(to item: TabBarItem) -> some View {
item.lable.tag(item)
}
func tab(to item: TabBarItem) -> some View {
Group {
if item == .Home {
NavigationStack(path: $appModel.path) {
item.destination(appModel: appModel)
}
} else {
NavigationStack {
item.destination(appModel: appModel)
}
}
}
.tabItem {
item.lable.tag(item)
}.tag(item)
}
}
enum TabBarItem: Int {
case Home
case Favorite
case Files
case Setting
var lable: Label<Text, Image> {
switch self {
case .Home:
return Label("Home", systemImage: "house.fill")
case .Favorite:
return Label("Favorite", systemImage: "star.fill")
case .Files:
return Label("Files", systemImage: "folder.fill.badge.gearshape")
case .Setting:
return Label("Setting", systemImage: "gear")
}
}
@MainActor
@ViewBuilder
func destination(appModel: APPModel) -> some View {
switch self {
case .Home:
HomeView(m3uURL: appModel.activeM3UModel?.m3uURL)
.navigationPlay()
case .Favorite:
FavoriteView()
.navigationPlay()
case .Files:
FilesView()
case .Setting:
SettingView()
}
}
}
public extension View {
@MainActor
@ViewBuilder
func navigationPlay() -> some View {
navigationDestination(for: URL.self) { url in
KSVideoPlayerView(url: url)
#if !os(macOS)
.toolbar(.hidden, for: .tabBar)
#endif
}
.navigationDestination(for: MovieModel.self) { model in
model.view
}
}
}
private extension MovieModel {
@MainActor
var view: some View {
KSVideoPlayerView(model: self)
#if !os(macOS)
.toolbar(.hidden, for: .tabBar)
#endif
}
}

View File

@@ -0,0 +1,225 @@
//
// Defaults.swift
// TracyPlayer
//
// Created by kintan on 2023/7/21.
//
import Foundation
import KSPlayer
import SwiftUI
public class Defaults: ObservableObject {
@AppStorage("showRecentPlayList") public var showRecentPlayList = false
@AppStorage("hardwareDecode")
public var hardwareDecode = KSOptions.hardwareDecode {
didSet {
KSOptions.hardwareDecode = hardwareDecode
}
}
@AppStorage("asynchronousDecompression")
public var asynchronousDecompression = KSOptions.asynchronousDecompression {
didSet {
KSOptions.asynchronousDecompression = asynchronousDecompression
}
}
@AppStorage("isUseDisplayLayer")
public var isUseDisplayLayer = MEOptions.isUseDisplayLayer {
didSet {
MEOptions.isUseDisplayLayer = isUseDisplayLayer
}
}
@AppStorage("preferredForwardBufferDuration")
public var preferredForwardBufferDuration = KSOptions.preferredForwardBufferDuration {
didSet {
KSOptions.preferredForwardBufferDuration = preferredForwardBufferDuration
}
}
@AppStorage("maxBufferDuration")
public var maxBufferDuration = KSOptions.maxBufferDuration {
didSet {
KSOptions.maxBufferDuration = maxBufferDuration
}
}
@AppStorage("isLoopPlay")
public var isLoopPlay = KSOptions.isLoopPlay {
didSet {
KSOptions.isLoopPlay = isLoopPlay
}
}
@AppStorage("canBackgroundPlay")
public var canBackgroundPlay = true {
didSet {
KSOptions.canBackgroundPlay = canBackgroundPlay
}
}
@AppStorage("isAutoPlay")
public var isAutoPlay = true {
didSet {
KSOptions.isAutoPlay = isAutoPlay
}
}
@AppStorage("isSecondOpen")
public var isSecondOpen = true {
didSet {
KSOptions.isSecondOpen = isSecondOpen
}
}
@AppStorage("isAccurateSeek")
public var isAccurateSeek = true {
didSet {
KSOptions.isAccurateSeek = isAccurateSeek
}
}
@AppStorage("isPipPopViewController")
public var isPipPopViewController = true {
didSet {
KSOptions.isPipPopViewController = isPipPopViewController
}
}
@AppStorage("textFontSize")
public var textFontSize = SubtitleModel.textFontSize {
didSet {
SubtitleModel.textFontSize = textFontSize
}
}
@AppStorage("textBold")
public var textBold = SubtitleModel.textBold {
didSet {
SubtitleModel.textBold = textBold
}
}
@AppStorage("textItalic")
public var textItalic = SubtitleModel.textItalic {
didSet {
SubtitleModel.textItalic = textItalic
}
}
@AppStorage("textColor")
public var textColor = SubtitleModel.textColor {
didSet {
SubtitleModel.textColor = textColor
}
}
@AppStorage("textBackgroundColor")
public var textBackgroundColor = SubtitleModel.textBackgroundColor {
didSet {
SubtitleModel.textBackgroundColor = textBackgroundColor
}
}
@AppStorage("horizontalAlign")
public var horizontalAlign = SubtitleModel.textPosition.horizontalAlign {
didSet {
SubtitleModel.textPosition.horizontalAlign = horizontalAlign
}
}
@AppStorage("verticalAlign")
public var verticalAlign = SubtitleModel.textPosition.verticalAlign {
didSet {
SubtitleModel.textPosition.verticalAlign = verticalAlign
}
}
@AppStorage("leftMargin")
public var leftMargin = SubtitleModel.textPosition.leftMargin {
didSet {
SubtitleModel.textPosition.leftMargin = leftMargin
}
}
@AppStorage("rightMargin")
public var rightMargin = SubtitleModel.textPosition.rightMargin {
didSet {
SubtitleModel.textPosition.rightMargin = rightMargin
}
}
@AppStorage("verticalMargin")
public var verticalMargin = SubtitleModel.textPosition.verticalMargin {
didSet {
SubtitleModel.textPosition.verticalMargin = verticalMargin
}
}
@AppStorage("yadifMode")
public var yadifMode = MEOptions.yadifMode {
didSet {
MEOptions.yadifMode = yadifMode
}
}
@AppStorage("audioPlayerType")
public var audioPlayerType = NSStringFromClass(KSOptions.audioPlayerType) {
didSet {
KSOptions.audioPlayerType = NSClassFromString(audioPlayerType) as! any AudioOutput.Type
}
}
public static let shared = Defaults()
private init() {
KSOptions.hardwareDecode = hardwareDecode
MEOptions.isUseDisplayLayer = isUseDisplayLayer
SubtitleModel.textFontSize = textFontSize
SubtitleModel.textBold = textBold
SubtitleModel.textItalic = textItalic
SubtitleModel.textColor = textColor
SubtitleModel.textBackgroundColor = textBackgroundColor
SubtitleModel.textPosition.horizontalAlign = horizontalAlign
SubtitleModel.textPosition.verticalAlign = verticalAlign
SubtitleModel.textPosition.leftMargin = leftMargin
SubtitleModel.textPosition.rightMargin = rightMargin
SubtitleModel.textPosition.verticalMargin = verticalMargin
KSOptions.preferredForwardBufferDuration = preferredForwardBufferDuration
KSOptions.maxBufferDuration = maxBufferDuration
KSOptions.isLoopPlay = isLoopPlay
KSOptions.canBackgroundPlay = canBackgroundPlay
KSOptions.isAutoPlay = isAutoPlay
KSOptions.isSecondOpen = isSecondOpen
KSOptions.isAccurateSeek = isAccurateSeek
KSOptions.isPipPopViewController = isPipPopViewController
MEOptions.yadifMode = yadifMode
KSOptions.audioPlayerType = NSClassFromString(audioPlayerType) as! any AudioOutput.Type
}
}
@propertyWrapper
public struct Default<T>: DynamicProperty {
@ObservedObject private var defaults: Defaults
private let keyPath: ReferenceWritableKeyPath<Defaults, T>
public init(_ keyPath: ReferenceWritableKeyPath<Defaults, T>, defaults: Defaults = .shared) {
self.keyPath = keyPath
self.defaults = defaults
}
public var wrappedValue: T {
get { defaults[keyPath: keyPath] }
nonmutating set { defaults[keyPath: keyPath] = newValue }
}
public var projectedValue: Binding<T> {
Binding(
get: { defaults[keyPath: keyPath] },
set: { value in
defaults[keyPath: keyPath] = value
}
)
}
}

View File

@@ -0,0 +1,38 @@
//
// FavoriteView.swift
// TracyPlayer
//
// Created by kintan on 2023/7/2.
//
import SwiftUI
struct FavoriteView: View {
@EnvironmentObject
private var appModel: APPModel
@State
private var nameFilter: String = ""
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \MovieModel.name, ascending: true)],
predicate: NSPredicate(format: "playmodel.isFavorite == YES")
)
private var favoritelist: FetchedResults<MovieModel>
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: MoiveView.width))]) {
let playlist = favoritelist.filter { model in
var isIncluded = true
if !nameFilter.isEmpty {
isIncluded = model.name!.contains(nameFilter)
}
return isIncluded
}
ForEach(playlist) { model in
appModel.content(model: model)
}
}
}
.padding()
.searchable(text: $nameFilter)
}
}

View File

@@ -0,0 +1,172 @@
//
// FilesView.swift
// TracyPlayer
//
// Created by kintan on 2023/7/3.
//
import KSPlayer
import SwiftUI
struct FilesView: View {
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \M3UModel.name, ascending: true)],
predicate: NSPredicate(format: "m3uURL != nil && name != nil")
)
private var m3uModels: FetchedResults<M3UModel>
@EnvironmentObject
private var appModel: APPModel
@State
private var addM3U = false
@State
private var openFileImport = false
@State
private var nameFilter: String = ""
var body: some View {
let models = m3uModels.filter { model in
var isIncluded = true
if !nameFilter.isEmpty {
isIncluded = model.name!.contains(nameFilter)
}
return isIncluded
}
#if os(tvOS)
HStack {
toolbarView
}
#endif
List(models, id: \.self, selection: $appModel.activeM3UModel) { model in
#if os(tvOS)
NavigationLink(value: model) {
M3UView(model: model)
}
#else
M3UView(model: model)
#endif
}
.searchable(text: $nameFilter)
.sheet(isPresented: $addM3U) {
AddM3UView()
}
#if !os(tvOS)
.toolbar {
toolbarView
}
.fileImporter(isPresented: $openFileImport, allowedContentTypes: [.data]) { result in
guard let url = try? result.get() else {
return
}
appModel.addM3U(url: url)
}
#endif
}
private var toolbarView: some View {
Group {
Button {
openFileImport = true
} label: {
Label("Add Local M3U", systemImage: "plus.rectangle.on.folder.fill")
}
#if !os(tvOS)
.keyboardShortcut("o")
#endif
Button {
addM3U = true
} label: {
Label("Add Remote M3U", systemImage: "plus.app.fill")
}
#if !os(tvOS)
.keyboardShortcut("o", modifiers: [.command, .shift])
#endif
}
.labelStyle(.titleAndIcon)
}
}
struct M3UView: View {
@ObservedObject
var model: M3UModel
var body: some View {
VStack(alignment: .leading) {
Text(model.name ?? "")
.font(.title2)
.foregroundColor(.primary)
Text("total \(model.count) channels")
.font(.callout)
.foregroundColor(.secondary)
Text(model.m3uURL?.description ?? "")
.font(.callout)
.foregroundColor(.secondary)
}
.contextMenu {
if PersistenceController.shared.container.canUpdateRecord(forManagedObjectWith: model.objectID) {
Button {
model.delete()
} label: {
Label("Delete", systemImage: "trash.fill")
}
}
Button {
Task {
try? await _ = model.parsePlaylist()
}
} label: {
Label("Refresh", systemImage: "arrow.clockwise.circle")
}
#if !os(tvOS)
Button {
#if os(macOS)
UIPasteboard.general.clearContents()
UIPasteboard.general.setString(model.m3uURL!.description, forType: .string)
#else
UIPasteboard.general.setValue(model.m3uURL!, forPasteboardType: "public.url")
#endif
} label: {
Label("Copy url", systemImage: "doc.on.doc.fill")
}
#endif
}
}
}
struct AddM3UView: View {
#if DEBUG && targetEnvironment(simulator)
@State
private var url = "https://raw.githubusercontent.com/kingslay/TestVideo/main/TestVideo.m3u"
#else
@State
private var url = ""
#endif
@State
private var name = ""
@EnvironmentObject private var appModel: APPModel
@Environment(\.dismiss) private var dismiss
var body: some View {
Form {
Section {
TextField("URL", text: $url)
TextField("Name", text: $name)
}
Section {
Text("Links to playlists you add will be public. All people can see it. But only you can modify and delete")
Button("Done") {
if let url = URL(string: url.trimmingCharacters(in: NSMutableCharacterSet.whitespacesAndNewlines)) {
let name = name.trimmingCharacters(in: NSMutableCharacterSet.whitespacesAndNewlines)
appModel.addM3U(url: url, name: name.isEmpty ? nil : name)
}
dismiss()
}
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
#if os(macOS) || targetEnvironment(macCatalyst)
Button("Cancel") {
dismiss()
}
.keyboardShortcut(.cancelAction)
#endif
}
}.padding()
}
}

View File

@@ -0,0 +1,185 @@
import KSPlayer
import SwiftUI
struct HomeView: View {
@EnvironmentObject
private var appModel: APPModel
@State
private var nameFilter: String = ""
@State
private var groupFilter: String?
@Default(\.showRecentPlayList)
private var showRecentPlayList
// @Environment(\.horizontalSizeClass) var horizontalSizeClass
@FetchRequest(fetchRequest: MovieModel.playTimeRequest)
private var historyModels: FetchedResults<MovieModel>
@FetchRequest
private var movieModels: FetchedResults<MovieModel>
init(m3uURL: URL?) {
let request = MovieModel.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \MovieModel.name, ascending: true)]
request.predicate = NSPredicate(format: "m3uURL == %@ && name != nil ", m3uURL?.description ?? "nil")
_movieModels = FetchRequest<MovieModel>(fetchRequest: request)
}
var body: some View {
ScrollView {
#if os(tvOS)
HStack {
toolbarView
}
#endif
if showRecentPlayList {
Section {
ScrollView(.horizontal) {
LazyHStack {
ForEach(historyModels) { model in
appModel.content(model: model)
}
}
}
} header: {
HStack {
Text("Recent Play").font(.title)
Spacer()
}
.padding(.horizontal)
}
.padding()
}
Section {
LazyVGrid(columns: [GridItem(.adaptive(minimum: MoiveView.width))]) {
let playlist = movieModels.filter { model in
var isIncluded = true
if !nameFilter.isEmpty {
isIncluded = model.name!.contains(nameFilter)
}
if let groupFilter {
isIncluded = isIncluded && model.group == groupFilter
}
return isIncluded
}
ForEach(playlist) { model in
appModel.content(model: model)
}
}
} header: {
HStack {
Text("Channels").font(.title)
Spacer()
}
.padding(.horizontal)
}
.padding()
}
#if !os(tvOS)
// tvossearchable
.searchable(text: $nameFilter)
.toolbar {
toolbarView
}
#endif
}
private var toolbarView: some View {
Group {
Button {
appModel.openFileImport = true
} label: {
Label("Open File", systemImage: "plus.rectangle.on.folder.fill")
}
#if !os(tvOS)
.keyboardShortcut("o")
#endif
Button {
appModel.openURLImport = true
} label: {
Label("Open URL", systemImage: "plus.app.fill")
}
#if !os(tvOS)
.keyboardShortcut("o", modifiers: [.command, .shift])
#endif
let groups = movieModels.reduce(Set<String>()) { partialResult, model in
if let group = model.group {
var set = partialResult
set.insert(group)
return set
} else {
return partialResult
}
}.sorted()
Picker("group filter", selection: $groupFilter) {
Text("All").tag(nil as String?)
ForEach(groups) { group in
Text(group).tag(group as String?)
}
}
#if os(tvOS)
.pickerStyle(.navigationLink)
#endif
}
.labelStyle(.titleAndIcon)
}
}
struct MoiveView: View {
static let width: CGFloat = {
#if canImport(UIKit)
if UIDevice.current.userInterfaceIdiom == .phone {
return min(KSOptions.sceneSize.width, KSOptions.sceneSize.height) / 2 - 20
} else if UIDevice.current.userInterfaceIdiom == .pad {
return min(KSOptions.sceneSize.width, KSOptions.sceneSize.height) / 3 - 20
} else if UIDevice.current.userInterfaceIdiom == .tv {
return KSOptions.sceneSize.width / 4 - 150
} else if UIDevice.current.userInterfaceIdiom == .mac {
return CGFloat(192)
} else {
return CGFloat(192)
}
#else
return CGFloat(192)
#endif
}()
@ObservedObject var model: MovieModel
var body: some View {
VStack {
image
Text(model.name ?? "").lineLimit(1)
}
.frame(width: MoiveView.width)
.contextMenu {
Button {
model.isFavorite.toggle()
try? model.managedObjectContext?.save()
} label: {
let isFavorite = model.isFavorite
Label(isFavorite ? "Cancel favorite" : "Favorite", systemImage: isFavorite ? "star" : "star.fill")
}
#if !os(tvOS)
Button {
#if os(macOS)
UIPasteboard.general.clearContents()
UIPasteboard.general.setString(model.url!.description, forType: .string)
#else
UIPasteboard.general.setValue(model.url!, forPasteboardType: "public.url")
#endif
} label: {
Label("Copy url", systemImage: "doc.on.doc.fill")
}
#endif
}
}
var image: some View {
AsyncImage(url: model.logo) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
} placeholder: {
Color.gray
}.frame(width: MoiveView.width, height: MoiveView.width / 16 * 9)
.clipShape(RoundedRectangle(cornerRadius: 5))
}
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22222" systemVersion="23A344" minimumToolsVersion="Automatic" sourceLanguage="Swift" usedWithSwiftData="YES" userDefinedModelVersionIdentifier="">
<entity name="M3UModel" representedClassName="M3UModel" syncable="YES" codeGenerationType="class">
<attribute name="count" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="m3uURL" optional="YES" attributeType="URI"/>
<attribute name="name" attributeType="String" defaultValueString=""/>
</entity>
<entity name="MovieModel" representedClassName="MovieModel" syncable="YES" codeGenerationType="category">
<attribute name="country" optional="YES" attributeType="String"/>
<attribute name="group" optional="YES" attributeType="String"/>
<attribute name="httpReferer" optional="YES" attributeType="String"/>
<attribute name="httpUserAgent" optional="YES" attributeType="String"/>
<attribute name="language" optional="YES" attributeType="String"/>
<attribute name="logo" optional="YES" attributeType="URI"/>
<attribute name="m3uURL" optional="YES" attributeType="URI"/>
<attribute name="name" attributeType="String" defaultValueString=""/>
<attribute name="tvgID" optional="YES" attributeType="String"/>
<attribute name="url" optional="YES" attributeType="URI"/>
<relationship name="playmodel" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PlayModel" inverseName="url" inverseEntity="PlayModel"/>
</entity>
<entity name="PlayModel" representedClassName="PlayModel" syncable="YES" codeGenerationType="class">
<attribute name="current" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="duration" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="isFavorite" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="playTime" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<relationship name="url" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="MovieModel" inverseName="playmodel" inverseEntity="MovieModel"/>
</entity>
<configuration name="local">
<memberEntity name="MovieModel"/>
</configuration>
<configuration name="private" usedWithCloudKit="YES">
<memberEntity name="MovieModel"/>
<memberEntity name="PlayModel"/>
</configuration>
<configuration name="public" usedWithCloudKit="YES">
<memberEntity name="M3UModel"/>
</configuration>
</model>

View File

@@ -0,0 +1,306 @@
//
// MovieModel.swift
// TracyPlayer
//
// Created by kintan on 2023/2/2.
//
import CoreData
import CoreMedia
import Foundation
import KSPlayer
#if canImport(UIKit)
import UIKit
#endif
class MEOptions: KSOptions {
#if os(iOS)
static var isUseDisplayLayer = true
#else
static var isUseDisplayLayer = false
#endif
override init() {
super.init()
}
override func process(assetTrack: some MediaPlayerTrack) {
super.process(assetTrack: assetTrack)
}
override func isUseDisplayLayer() -> Bool {
MEOptions.isUseDisplayLayer && display == .plane
}
}
@objc(MovieModel)
public class MovieModel: NSManagedObject, Codable {
enum CodingKeys: String, CodingKey {
case name, url, httpReferer, httpUserAgent
}
public required convenience init(from decoder: Decoder) throws {
self.init(context: PersistenceController.shared.viewContext)
let values = try decoder.container(keyedBy: CodingKeys.self)
url = try values.decode(URL.self, forKey: .url)
name = try values.decode(String.self, forKey: .name)
httpReferer = try values.decode(String.self, forKey: .httpReferer)
httpUserAgent = try values.decode(String.self, forKey: .httpUserAgent)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(url, forKey: .url)
try container.encode(name, forKey: .name)
try container.encode(httpReferer, forKey: .httpReferer)
try container.encode(httpUserAgent, forKey: .httpUserAgent)
}
}
extension MovieModel {
convenience init(context: NSManagedObjectContext = PersistenceController.shared.viewContext, url: URL) {
self.init(context: context, url: url, name: url.lastPathComponent)
}
convenience init(context: NSManagedObjectContext = PersistenceController.shared.viewContext, url: URL, name: String, extinf: [String: String]? = nil) {
self.init(context: context)
self.name = name
self.url = url
setExt(info: extinf)
}
func setExt(info: [String: String]? = nil) {
let logo = info?["tvg-logo"].flatMap { URL(string: $0) }
if logo != self.logo {
self.logo = logo
}
let language = info?["tvg-language"]
if language != self.language {
self.language = language
}
let country = info?["tvg-country"]
if country != self.country {
self.country = country
}
let group = info?["group-title"]
if group != self.group {
self.group = group
}
let tvgID = info?["tvg-id"]
if tvgID != self.tvgID {
self.tvgID = tvgID
}
let httpReferer = info?["http-referrer"] ?? info?["http-referer"]
if httpReferer != self.httpReferer {
self.httpReferer = httpReferer
}
let httpUserAgent = info?["http-user-agent"]
if httpUserAgent != self.httpUserAgent {
self.httpUserAgent = httpUserAgent
}
}
}
extension M3UModel {
convenience init(context: NSManagedObjectContext = PersistenceController.shared.viewContext, url: URL, name: String? = nil) {
self.init(context: context)
self.name = name ?? url.lastPathComponent
m3uURL = url
}
func delete() {
guard let context = managedObjectContext, let m3uURL else {
return
}
context.delete(self)
let request = M3UModel.fetchRequest()
request.predicate = NSPredicate(format: "m3uURL == %@", m3uURL.description)
do {
if let array = try? context.fetch(request), array.isEmpty {
let movieRequest = NSFetchRequest<MovieModel>(entityName: "MovieModel")
movieRequest.predicate = NSPredicate(format: "m3uURL == %@", m3uURL.description)
for model in try context.fetch(movieRequest) {
context.delete(model)
}
// let deleteRequest = NSBatchDeleteRequest(fetchRequest: movieRequest)
// _ = try? context.execute(deleteRequest)
}
try context.save()
} catch {
KSLog(level: .error, error.localizedDescription)
}
}
func getMovieModels() async -> [MovieModel] {
let viewContext = managedObjectContext ?? PersistenceController.shared.viewContext
let m3uURL = await viewContext.perform {
self.m3uURL
}
guard let m3uURL else {
return []
}
return await viewContext.perform {
let request = NSFetchRequest<MovieModel>(entityName: "MovieModel")
request.predicate = NSPredicate(format: "m3uURL == %@", m3uURL.description)
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
return (try? viewContext.fetch(request)) ?? []
}
}
func parsePlaylist() async throws -> [MovieModel] {
let array = await getMovieModels()
let viewContext = managedObjectContext ?? PersistenceController.shared.viewContext
let m3uURL = await viewContext.perform {
self.m3uURL
}
guard let m3uURL else {
return []
}
let result = try await m3uURL.parsePlaylist()
guard result.count > 0 else {
delete()
return []
}
return await viewContext.perform {
var dic = [URL?: MovieModel]()
for model in array {
if let oldModel = dic[model.url] {
if oldModel.playmodel == nil {
viewContext.delete(oldModel)
dic[model.url] = model
} else {
viewContext.delete(model)
}
} else {
dic[model.url] = model
}
}
let models = result.map { name, url, extinf -> MovieModel in
if let model = dic[url] {
dic.removeValue(forKey: url)
if name != model.name {
model.name = name
}
model.setExt(info: extinf)
return model
} else {
let model = MovieModel(context: viewContext, url: url, name: name, extinf: extinf)
model.m3uURL = self.m3uURL
return model
}
}
if self.count != Int32(models.count) {
self.count = Int32(models.count)
}
viewContext.perform {
if viewContext.hasChanges {
for model in dic.values {
viewContext.delete(model)
}
try? viewContext.save()
}
}
return models
}
}
}
extension MovieModel {
static var playTimeRequest: NSFetchRequest<MovieModel> {
let request = MovieModel.fetchRequest()
request.sortDescriptors = [
NSSortDescriptor(
keyPath: \MovieModel.playmodel?.playTime,
ascending: false
),
]
request.predicate = NSPredicate(format: "playmodel.playTime != nil")
request.fetchLimit = 20
return request
}
public var isFavorite: Bool {
get {
playmodel?.isFavorite ?? false
}
set {
let playmodel = getPlaymodel()
playmodel.isFavorite = newValue
}
}
func getPlaymodel() -> PlayModel {
if let playmodel {
return playmodel
}
let model = PlayModel()
playmodel = model
model.save()
return model
}
}
extension NSManagedObject {
func save() {
guard let context = managedObjectContext else {
return
}
context.perform {
do {
try context.save()
} catch {}
}
}
}
extension PlayModel {
convenience init() {
self.init(context: PersistenceController.shared.viewContext)
}
}
extension KSVideoPlayerView {
init(url: URL) {
let request = NSFetchRequest<MovieModel>(entityName: "MovieModel")
request.predicate = NSPredicate(format: "url == %@", url.description)
let model = (try? PersistenceController.shared.viewContext.fetch(request).first) ?? MovieModel(url: url)
self.init(model: model)
}
init(model: MovieModel) {
let url = model.url!
let options = MEOptions()
#if DEBUG
if url.lastPathComponent == "h264.mp4" {
// options.videoFilters = ["hflip", "vflip"]
// options.hardwareDecode = false
options.startPlayTime = 13
} else if url.lastPathComponent == "vr.mp4" {
options.display = .vr
} else if url.lastPathComponent == "mjpeg.flac" {
// options.videoDisable = true
options.syncDecodeAudio = true
} else if url.lastPathComponent == "subrip.mkv" {
options.asynchronousDecompression = false
options.videoFilters.append("yadif_videotoolbox=mode=\(MEOptions.yadifMode):parity=-1:deint=1")
} else if url.lastPathComponent == "big_buck_bunny.mp4" {
options.startPlayTime = 25
} else if url.lastPathComponent == "bipbopall.m3u8" {
#if os(macOS)
let moviesDirectory = try? FileManager.default.url(for: .moviesDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
options.outputURL = moviesDirectory?.appendingPathComponent("recording.mov")
#endif
}
#endif
options.referer = model.httpReferer
if let httpUserAgent = model.httpUserAgent {
options.userAgent = httpUserAgent
}
let playmodel = model.getPlaymodel()
playmodel.playTime = Date()
if playmodel.duration > 0, playmodel.current > 0, playmodel.duration > playmodel.current + 120 {
options.startPlayTime = TimeInterval(playmodel.current)
}
playmodel.save()
model.save()
self.init(url: url, options: options, title: model.name)
}
}

View File

@@ -0,0 +1,88 @@
//
// Persistence.swift
// DataTest
//
// Created by kintan on 2023/7/2.
//
import CloudKit
import CoreData
import KSPlayer
struct PersistenceController {
static let shared = PersistenceController()
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
var urls: [String] = [
"https://raw.githubusercontent.com/YanG-1989/m3u/main/Gather.m3u",
"https://iptv-org.github.io/iptv/index.m3u",
"https://iptv-org.github.io/iptv/countries/cn.m3u",
"https://iptv-org.github.io/iptv/countries/hk.m3u",
"https://iptv-org.github.io/iptv/countries/tw.m3u",
"https://iptv-org.github.io/iptv/regions/amer.m3u",
"https://iptv-org.github.io/iptv/regions/asia.m3u",
"https://iptv-org.github.io/iptv/regions/eur.m3u",
"https://iptv-org.github.io/iptv/categories/education.m3u",
"https://iptv-org.github.io/iptv/categories/movies.m3u",
"https://iptv-org.github.io/iptv/languages/zho.m3u",
"https://iptv-org.github.io/iptv/languages/eng.m3u",
"https://raw.githubusercontent.com/kingslay/TestVideo/main/test.m3u",
"https://raw.githubusercontent.com/kingslay/TestVideo/main/TestVideo.m3u",
"https://raw.githubusercontent.com/kingslay/bulaoge/main/bulaoge.m3u",
]
for str in urls {
if let url = URL(string: str) {
_ = M3UModel(context: viewContext, url: url)
}
}
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
let container: NSPersistentCloudKitContainer
let viewContext: NSManagedObjectContext
init(inMemory: Bool = false) {
let modelName = "Model"
// load Data Model
guard let url = Bundle.main.url(forResource: modelName, withExtension: "momd"),
let model = NSManagedObjectModel(contentsOf: url)
else {
fatalError("Can't get \(modelName).momd in Bundle")
}
container = NSPersistentCloudKitContainer(name: modelName, managedObjectModel: model)
viewContext = container.viewContext
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores { storeDescription, error in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
KSLog("Unresolved error \(error), \(error.userInfo), store url \(String(describing: storeDescription.url))")
// if let url = storeDescription.url {
// try? persistentStoreCoordinator.destroyPersistentStore(at: url, type: .sqlite)
// }
}
}
container.viewContext.automaticallyMergesChangesFromParent = true
}
}

View File

@@ -0,0 +1,205 @@
//
// SettingView.swift
// TracyPlayer
//
// Created by kintan on 2023/6/21.
//
import KSPlayer
import SwiftUI
struct SettingView: View {
var body: some View {
NavigationView {
List {
NavigationLink(destination: SettingGeneralView()) {
Label("General", systemImage: "switch.2")
}
NavigationLink(destination: SettingAudioView()) {
Label("Audio", systemImage: "waveform")
}
NavigationLink(destination: SettingVideoView()) {
Label("Video", systemImage: "play.rectangle.fill")
}
NavigationLink(destination: SettingSubtitleView()) {
Label("Subtitle", systemImage: "captions.bubble")
}
NavigationLink(destination: SettingAdvancedView()) {
Label("Advanced", systemImage: "gearshape.2.fill")
}
}
}
.preferredColorScheme(.dark)
.background(Color.black)
}
}
struct SettingGeneralView: View {
@Default(\.showRecentPlayList)
private var showRecentPlayList
var body: some View {
Form {
Toggle("Show Recent Play List", isOn: $showRecentPlayList)
}
}
}
struct SettingAudioView: View {
@Default(\.audioPlayerType)
private var audioPlayerType
init() {}
var body: some View {
Form {
Picker("audio Player Type", selection: $audioPlayerType) {
Text("AUGraph").tag(NSStringFromClass(AudioGraphPlayer.self))
Text("AudioUnit").tag(NSStringFromClass(AudioUnitPlayer.self))
Text("AVAudioEngine").tag(NSStringFromClass(AudioEnginePlayer.self))
}
}
}
}
struct SettingVideoView: View {
@Default(\.hardwareDecode)
private var hardwareDecode
@Default(\.asynchronousDecompression)
private var asynchronousDecompression
@Default(\.isUseDisplayLayer)
private var isUseDisplayLayer
@Default(\.yadifMode)
private var yadifMode
var body: some View {
Form {
Toggle("Hardware decoder", isOn: $hardwareDecode)
Toggle("Asynchronous Decompression", isOn: $asynchronousDecompression)
Toggle("Use DisplayLayer", isOn: $isUseDisplayLayer)
Picker("yadif Mode", selection: $yadifMode) {
Text("yadif").tag(0)
Text("yadif_2x").tag(1)
Text("yadif_spatial_skip").tag(2)
Text("yadif_2x_spatial_skip").tag(3)
}
}
}
}
struct SettingSubtitleView: View {
@Default(\.textFontSize)
private var textFontSize
@Default(\.textBold)
private var textBold
@Default(\.textItalic)
private var textItalic
@Default(\.textColor)
private var textColor
@Default(\.textBackgroundColor)
private var textBackgroundColor
@Default(\.verticalAlign)
private var verticalAlign
@Default(\.horizontalAlign)
private var horizontalAlign
@Default(\.leftMargin)
private var leftMargin
@Default(\.rightMargin)
private var rightMargin
@Default(\.verticalMargin)
private var verticalMargin
var body: some View {
Form {
Section("Position") {
HStack {
#if os(iOS)
Text("Fone Size:")
#endif
TextField("Fone Size:", value: $textFontSize, format: .number)
}
Toggle("Bold", isOn: $textBold)
Toggle("Italic", isOn: $textItalic)
#if !os(tvOS)
ColorPicker("Color:", selection: $textColor)
ColorPicker("Background:", selection: $textBackgroundColor)
#endif
}
Section("Position") {
Picker("Align X:", selection: $horizontalAlign) {
ForEach([HorizontalAlignment.leading, .center, .trailing]) { value in
Text(value.rawValue).tag(value)
}
}
Picker("Align Y:", selection: $verticalAlign) {
ForEach([VerticalAlignment.top, .center, .bottom]) { value in
Text(value.rawValue).tag(value)
}
}
HStack {
#if os(iOS)
Text("Margin Left:")
#endif
TextField("Margin Left:", value: $leftMargin, format: .number)
}
HStack {
#if os(iOS)
Text("Margin Right:")
#endif
TextField("Margin Right:", value: $rightMargin, format: .number)
}
HStack {
#if os(iOS)
Text("Margin Vertical:")
#endif
TextField("Margin Vertical:", value: $verticalMargin, format: .number)
}
}
}
.padding()
}
}
struct SettingAdvancedView: View {
@Default(\.preferredForwardBufferDuration)
private var preferredForwardBufferDuration
@Default(\.maxBufferDuration)
private var maxBufferDuration
@Default(\.isLoopPlay)
private var isLoopPlay
@Default(\.canBackgroundPlay)
private var canBackgroundPlay
@Default(\.isAutoPlay)
private var isAutoPlay
@Default(\.isSecondOpen)
private var isSecondOpen
@Default(\.isAccurateSeek)
private var isAccurateSeek
@Default(\.isPipPopViewController)
private var isPipPopViewController
// @Default(\.isLoopPlay)
// private var isLoopPlay
var body: some View {
Form {
HStack {
#if os(iOS)
Text("Preferred Forward Buffer Duration:")
#endif
TextField("Preferred Forward Buffer Duration:", value: $preferredForwardBufferDuration, format: .number)
}
HStack {
#if os(iOS)
Text("Max Buffer Second:")
#endif
TextField("Max Buffer Second:", value: $maxBufferDuration, format: .number)
}
Toggle("Loop Play", isOn: $isLoopPlay)
Toggle("Can Background Play", isOn: $canBackgroundPlay)
Toggle("Auto Play", isOn: $isAutoPlay)
Toggle("Fast Open Video", isOn: $isSecondOpen)
Toggle("Fast Seek Video", isOn: $isAccurateSeek)
Toggle("Picture In Picture Inline", isOn: $isPipPopViewController)
}
}
}
struct SettingView_Previews: PreviewProvider {
static var previews: some View {
SettingView()
}
}

View File

@@ -0,0 +1,253 @@
//
// TracyApp.swift
// Shared
//
// Created by kintan on 2021/5/3.
//
import AVFoundation
import AVKit
import KSPlayer
import SwiftUI
import UserNotifications
@main
struct TracyApp: App {
#if os(macOS)
@NSApplicationDelegateAdaptor
#else
@UIApplicationDelegateAdaptor
#endif
private var appDelegate: AppDelegate
private let appModel = APPModel()
init() {
let arguments = ProcessInfo.processInfo.arguments.dropFirst()
var dropNextArg = false
var playerArgs = [String]()
var filenames = [String]()
KSLog("launch arguments \(arguments)")
for argument in arguments {
if dropNextArg {
dropNextArg = false
continue
}
if argument.starts(with: "--") {
playerArgs.append(argument)
} else if argument.starts(with: "-") {
dropNextArg = true
} else {
filenames.append(argument)
}
}
if let urlString = filenames.first {
appModel.open(url: URL(fileURLWithPath: urlString))
}
}
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(appModel)
.environment(\.managedObjectContext, PersistenceController.shared.viewContext)
#if !os(tvOS)
.handlesExternalEvents(preferring: Set(arrayLiteral: "pause"), allowing: Set(arrayLiteral: "*"))
#endif
}
#if !os(tvOS)
// .handlesExternalEvents(matching: Set(arrayLiteral: "*"))
.commands {
CommandGroup(before: .newItem) {
Button("Open") {
appModel.openFileImport = true
}
.keyboardShortcut("o")
}
CommandGroup(before: .newItem) {
Button("Open URL") {
appModel.openURLImport = true
}
.keyboardShortcut("o", modifiers: [.command, .shift])
}
}
#endif
#if os(macOS)
.defaultSize(width: 1120, height: 630)
.defaultPosition(.center)
#endif
#if !os(tvOS)
WindowGroup("player", for: URL.self) { $url in
if let url {
KSVideoPlayerView(url: url)
}
}
#if os(macOS)
.defaultPosition(.center)
#endif
#endif
#if !os(tvOS)
WindowGroup("player", for: MovieModel.self) { $model in
if let model {
KSVideoPlayerView(model: model)
}
}
#if os(macOS)
.defaultPosition(.center)
#endif
#endif
#if os(macOS)
Settings {
TabBarItem.Setting.destination(appModel: appModel)
}
// MenuBarExtra {
// MenuBar()
// } label: {
// Image(systemName: "film.fill")
// }
// .menuBarExtraStyle(.menu)
#endif
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
#if os(macOS)
func applicationDidFinishLaunching(_: Notification) {
// requestNotification()
}
#else
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
// requestNotification()
true
}
#endif
func application(_: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.reduce("") { $0 + String(format: "%02x", $1) }
print("Device push notification token - \(tokenString)")
}
func application(_: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notification. Error \(error)")
}
private func requestNotification() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { allowed, error in
if allowed {
// register for remote push notification
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
print("Push notification allowed by user")
} else {
print("Error while requesting push notification permission. Error \(String(describing: error))")
}
}
}
}
class APPModel: ObservableObject {
@Published
var openURL: URL?
@Published
var openPlayModel: MovieModel?
@Published
var tabSelected: TabBarItem = .Home
@Published
var path = NavigationPath()
@Published
var openFileImport = false
@Published
var openURLImport = false
@Published
var hiddenTitleBar = false
@AppStorage("activeM3UURL")
private var activeM3UURL: URL?
@Published
var activeM3UModel: M3UModel? = nil {
didSet {
if let activeM3UModel, activeM3UModel != oldValue {
activeM3UURL = activeM3UModel.m3uURL
Task { @MainActor in
_ = try? await activeM3UModel.parsePlaylist()
// m3u
if activeM3UModel == self.activeM3UModel {
self.activeM3UModel = activeM3UModel
}
}
}
}
}
init() {
#if DEBUG
// KSOptions.logLevel = .debug
#else
var fileHandle = FileHandle.standardOutput
if let logURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("log.txt") {
if !FileManager.default.fileExists(atPath: logURL.path) {
FileManager.default.createFile(atPath: logURL.path, contents: nil)
}
if let handle = try? FileHandle(forWritingTo: logURL) {
fileHandle = handle
_ = try? fileHandle.seekToEnd()
}
}
KSOptions.logger = FileLog(fileHandle: fileHandle)
#endif
// KSOptions.firstPlayerType = KSMEPlayer.self
KSOptions.secondPlayerType = KSMEPlayer.self
_ = Defaults.shared
KSOptions.subtitleDataSouces = [DirectorySubtitleDataSouce(), ShooterSubtitleDataSouce(), AssrtSubtitleDataSouce(token: "5IzWrb2J099vmA96ECQXwdRSe9xdoBUv"), OpenSubtitleDataSouce(apiKey: "0D0gt8nV6SFHVVejdxAMpvOT0wByfKE5")]
if let activeM3UURL {
addM3U(url: activeM3UURL)
}
}
func addM3U(url: URL, name: String? = nil) {
let request = M3UModel.fetchRequest()
request.predicate = NSPredicate(format: "m3uURL == %@", url.description)
let context = PersistenceController.shared.viewContext
context.perform {
self.activeM3UModel = try? context.fetch(request).first ?? M3UModel(context: context, url: url, name: name)
}
}
func open(url: URL) {
if url.isPlaylist {
addM3U(url: url)
} else {
#if os(macOS)
openURL = url
#else
path.append(url)
#endif
}
}
func content(model: MovieModel) -> some View {
#if os(macOS)
Button {
self.openPlayModel = model
} label: {
MoiveView(model: model)
}
.buttonStyle(.borderless)
#else
NavigationLink(value: model) {
MoiveView(model: model)
}
#if targetEnvironment(macCatalyst)
.buttonStyle(.borderless)
#else
.buttonStyle(.automatic)
#endif
#endif
}
}
struct KSVideoPlayerView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(APPModel())
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}

View File

@@ -0,0 +1,81 @@
//
// URLImportView.swift
// Demo
//
// Created by kintan on 2020/3/22.
// Copyright © 2020 kintan. All rights reserved.
//
import KSPlayer
import SwiftUI
struct URLImportView: View {
@EnvironmentObject private var appModel: APPModel
@State private var username = ""
@State private var password = ""
@State private var playURL: String = ""
@State private var rememberURL = false
@AppStorage("historyURLs") private var historyURLs = [URL]()
@Environment(\.dismiss) private var dismiss
var body: some View {
Form {
Section {
TextField("URL:", text: $playURL)
Toggle("Remember URL", isOn: $rememberURL)
if !historyURLs.isEmpty {
Picker("History URL", selection: $playURL) {
Text("None").tag("")
ForEach(historyURLs) {
Text($0.description).tag($0.description)
}
}
#if os(tvOS)
.pickerStyle(.inline)
#endif
}
}
Section("HTTP Authentication") {
TextField("Username:", text: $username)
SecureField("Password:", text: $password)
}
Section {
Button("Done") {
dismiss()
let urlString = playURL.trimmingCharacters(in: NSMutableCharacterSet.whitespacesAndNewlines)
if !urlString.isEmpty, var components = URLComponents(string: urlString) {
if !username.isEmpty {
components.user = username
}
if !password.isEmpty {
components.password = password
}
if let url = components.url {
if rememberURL {
if let index = historyURLs.firstIndex(of: url) {
historyURLs.swapAt(index, historyURLs.startIndex)
} else {
historyURLs.insert(url, at: 0)
}
if historyURLs.count > 20 {
historyURLs.removeLast()
}
}
appModel.open(url: url)
}
}
}
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
#if os(macOS) || targetEnvironment(macCatalyst)
Button("Cancel") {
dismiss()
}
.keyboardShortcut(.cancelAction)
#endif
}
}
.padding()
}
}