Skip to content

Commit

Permalink
chapter editor view
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Jan 29, 2024
1 parent c0e575c commit 3649bf8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 69 deletions.
18 changes: 11 additions & 7 deletions easy-chapters.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
8D34510E2B680AAD0041EB03 /* AlertButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8D34510D2B680AAD0041EB03 /* AlertButton.swift */; };
8DC223AB2B6836C0002B189F /* ChapterEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DC223AA2B6836C0002B189F /* ChapterEditor.swift */; };
8DF2CB382B670E880037CB9B /* easy_chaptersApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DF2CB372B670E880037CB9B /* easy_chaptersApp.swift */; };
8DF2CB3A2B670E880037CB9B /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8DF2CB392B670E880037CB9B /* ContentView.swift */; };
8DF2CB3C2B670E890037CB9B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8DF2CB3B2B670E890037CB9B /* Assets.xcassets */; };
Expand Down Expand Up @@ -37,6 +38,7 @@

/* Begin PBXFileReference section */
8D34510D2B680AAD0041EB03 /* AlertButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertButton.swift; sourceTree = "<group>"; };
8DC223AA2B6836C0002B189F /* ChapterEditor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChapterEditor.swift; sourceTree = "<group>"; };
8DF2CB342B670E880037CB9B /* EasyChapters.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = EasyChapters.app; sourceTree = BUILT_PRODUCTS_DIR; };
8DF2CB372B670E880037CB9B /* easy_chaptersApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = easy_chaptersApp.swift; sourceTree = "<group>"; };
8DF2CB392B670E880037CB9B /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -112,17 +114,18 @@
8DF2CB362B670E880037CB9B /* easy-chapters */ = {
isa = PBXGroup;
children = (
8DF2CB372B670E880037CB9B /* easy_chaptersApp.swift */,
8DF2CB392B670E880037CB9B /* ContentView.swift */,
8D34510D2B680AAD0041EB03 /* AlertButton.swift */,
8DF2CB3B2B670E890037CB9B /* Assets.xcassets */,
8DF2CB6A2B6729F70037CB9B /* Chapter.swift */,
8DC223AA2B6836C0002B189F /* ChapterEditor.swift */,
8DF2CB392B670E880037CB9B /* ContentView.swift */,
8DF2CB402B670E890037CB9B /* easy_chapters.entitlements */,
8DF2CB372B670E880037CB9B /* easy_chaptersApp.swift */,
8DF2CB3D2B670E890037CB9B /* Preview Content */,
8DF2CB492B6725650037CB9B /* VLCPlayerView.swift */,
8DF2CB6A2B6729F70037CB9B /* Chapter.swift */,
8DF2CB6C2B6750C40037CB9B /* VideoInfo.swift */,
8DF2CB6E2B67549F0037CB9B /* Utils.swift */,
8DF2CB892B67D5890037CB9B /* SwiftUIDoubleClickModifier.swift */,
8D34510D2B680AAD0041EB03 /* AlertButton.swift */,
8DF2CB6E2B67549F0037CB9B /* Utils.swift */,
8DF2CB6C2B6750C40037CB9B /* VideoInfo.swift */,
8DF2CB492B6725650037CB9B /* VLCPlayerView.swift */,
);
path = "easy-chapters";
sourceTree = "<group>";
Expand Down Expand Up @@ -234,6 +237,7 @@
buildActionMask = 2147483647;
files = (
8D34510E2B680AAD0041EB03 /* AlertButton.swift in Sources */,
8DC223AB2B6836C0002B189F /* ChapterEditor.swift in Sources */,
8DF2CB8A2B67D5890037CB9B /* SwiftUIDoubleClickModifier.swift in Sources */,
8DF2CB3A2B670E880037CB9B /* ContentView.swift in Sources */,
8DF2CB4A2B6725650037CB9B /* VLCPlayerView.swift in Sources */,
Expand Down
66 changes: 66 additions & 0 deletions easy-chapters/ChapterEditor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// ChapterEditor.swift
// easy-chapters
//
// Created by Nicolas Bonamy on 1/29/24.
//

import SwiftUI

struct ChapterEditor: View {

@Binding var videoInfo: VideoInfo
@Binding var isEditing: Bool
@Binding var selection: Set<UUID>
@Binding var editedValue: String

var body: some View {
VStack {

// icon
Spacer(minLength: 10)
Image(nsImage: NSImage(named: "AppIcon")!)
.resizable()
.frame(width: 52, height: 52)
Spacer(minLength: 26)

// title
Text("Chapter Title").bold()
Spacer(minLength: 18)

// field
TextField("Title", text: $editedValue).frame(minWidth: 225)
Spacer(minLength: 16)

// ok
AlertButton("OK", prominent: true) {
videoInfo.updateChapterName(selection.first!, name: editedValue)
isEditing.toggle()
}
Spacer(minLength: 6)

// next
AlertButton("Next") {
let id = selection.first!
videoInfo.updateChapterName(id, name: editedValue)
let next = videoInfo.nextChapter(id)
if next != nil {
selection.removeAll()
selection.insert(next!.id)
editedValue = next!.name
} else {
isEditing.toggle()
}
}
Spacer(minLength: 16)

// cancel
AlertButton("Cancel") {
isEditing.toggle()
}

}.padding(16).background(Color(hex: 0xDAD9D8))
}


}
57 changes: 7 additions & 50 deletions easy-chapters/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
struct ContentView: View {

@State private var player: VLCPlayer!
@ObservedObject var videoInfo = VideoInfo()
@State var videoInfo = VideoInfo()
@State private var selection = Set<UUID>()
@State private var progress: Double = 0
@State private var isScrobbing = false
Expand Down Expand Up @@ -112,56 +112,13 @@ struct ContentView: View {
progress = player.progress()
}
}
.onAppear {
openFilePicker()
}

.sheet(isPresented: $isEditing) {
VStack {

// icon
Spacer(minLength: 10)
Image(nsImage: NSImage(named: "AppIcon")!)
.resizable()
.frame(width: 52, height: 52)
Spacer(minLength: 26)

// title
Text("Chapter Title").bold()
Spacer(minLength: 18)

// field
TextField("Title", text: $editedValue).frame(minWidth: 225)
Spacer(minLength: 16)

// ok
AlertButton("OK", prominent: true) {
videoInfo.updateChapterName(selection.first!, name: editedValue)
isEditing.toggle()
}
Spacer(minLength: 6)

// next
AlertButton("Next") {
let id = selection.first!
videoInfo.updateChapterName(id, name: editedValue)
let next = videoInfo.nextChapter(id)
if next != nil {
selection.removeAll()
selection.insert(next!.id)
editedValue = next!.name
} else {
isEditing.toggle()
}
}
Spacer(minLength: 16)

// cancel
AlertButton("Cancel") {
isEditing.toggle()
}

}.padding(16).background(Color(hex: 0xDAD9D8))
ChapterEditor(
videoInfo: $videoInfo,
isEditing: $isEditing,
selection: $selection,
editedValue: $editedValue
)
}
.alert(isPresented: self.$showAlert) {
Alert(
Expand Down
16 changes: 4 additions & 12 deletions easy-chapters/VideoInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@

import Foundation

class VideoInfo : ObservableObject, VLCPlayerDelegate {
@Observable class VideoInfo : VLCPlayerDelegate {

var chapters = [Chapter]()
var duration = 0
var ready = false

var chapters = [Chapter]()
@ObservationIgnored var duration = 0

func playerReset(_ player: VLCPlayer) -> Void {
ready = false
chapters.removeAll()
self.objectWillChange.send()
}

func playerReady(_ player: VLCPlayer) -> Void {
ready = true
self.objectWillChange.send()
}

func mediaParsed(_ player: VLCPlayer, chapters: [Chapter]) -> Void {
self.chapters = chapters
self.duration = player.duration()
self.objectWillChange.send()
}

func getChapter(_ id : UUID) -> Chapter? {
Expand All @@ -49,23 +46,20 @@ class VideoInfo : ObservableObject, VLCPlayerDelegate {
if (chapters[i].offset > offset) {
let chapter = Chapter(name: String(format: "Chapter %d", i+1), offset: offset, duration: 0);
chapters.insert(chapter, at: i)
self.objectWillChange.send()
return chapter;
}
}

// append
let chapter = Chapter(name: String(format: "Chapter %d", chapters.count+1), offset: offset, duration: 0);
chapters.append(chapter)
self.objectWillChange.send()
return chapter;
}

func updateChapterName(_ id: UUID, name: String) -> Void {
let chapter = getChapter(id)
if (chapter != nil) {
chapter!.name = name
self.objectWillChange.send()
}
}

Expand All @@ -74,13 +68,11 @@ class VideoInfo : ObservableObject, VLCPlayerDelegate {
if (chapter != nil) {
chapter!.offset = offset
chapters.sort { $0.offset < $1.offset }
self.objectWillChange.send()
}
}

func deleteChapter(_ id: UUID) -> Void {
chapters.removeAll{ $0.id == id }
self.objectWillChange.send()
}

func ffmpegChapters() -> String {
Expand Down

0 comments on commit 3649bf8

Please sign in to comment.