Skip to content

Commit

Permalink
AudioPlayer features #58: Show toolbar in two rows.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 7, 2019
1 parent 7b92eb9 commit a62aaa9
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 130 deletions.
32 changes: 20 additions & 12 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
F0562E0D2362025E0074ACF5 /* WikipediaView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0229BE02362021200F12F47 /* WikipediaView.swift */; };
F0562E0E2362026C0074ACF5 /* Wikipedia.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06D1C0D2361FFBC008A0C38 /* Wikipedia.swift */; };
F0562E0F2362026C0074ACF5 /* Wikipedia.swift in Sources */ = {isa = PBXBuildFile; fileRef = F06D1C0D2361FFBC008A0C38 /* Wikipedia.swift */; };
F0586482238FB03900E6DDFA /* PdfToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0586481238FB03900E6DDFA /* PdfToolbarView.swift */; };
F0586483238FB03900E6DDFA /* PdfToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0586481238FB03900E6DDFA /* PdfToolbarView.swift */; };
F0586482238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0586481238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift */; };
F0586483238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0586481238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift */; };
F058C7EE2396ACC0002C84F0 /* reverso-reverso-speaker.js in Resources */ = {isa = PBXBuildFile; fileRef = F058C7ED2396ACC0002C84F0 /* reverso-reverso-speaker.js */; };
F058C7EF2396ACC0002C84F0 /* reverso-reverso-speaker.js in Resources */ = {isa = PBXBuildFile; fileRef = F058C7ED2396ACC0002C84F0 /* reverso-reverso-speaker.js */; };
F058C7F22397A94D002C84F0 /* longman.json in Resources */ = {isa = PBXBuildFile; fileRef = F058C7F12397A180002C84F0 /* longman.json */; };
Expand Down Expand Up @@ -134,7 +134,7 @@
F0AA69DF232E97B0007CC07B /* GTranslator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69DB232E97AF007CC07B /* GTranslator.swift */; };
F0AA69E0232E97B0007CC07B /* WKRepresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69DC232E97B0007CC07B /* WKRepresenter.swift */; };
F0AA69E1232E97B0007CC07B /* PDFKitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69DD232E97B0007CC07B /* PDFKitView.swift */; };
F0AB12A3233F5798005B9F2A /* ReaderView_PDF.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A2233F5798005B9F2A /* ReaderView_PDF.swift */; };
F0AB12A3233F5798005B9F2A /* ReaderView_Pdf.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A2233F5798005B9F2A /* ReaderView_Pdf.swift */; };
F0AB12A5233F57B3005B9F2A /* ReaderView_Web.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A4233F57B3005B9F2A /* ReaderView_Web.swift */; };
F0AB12A8233F5802005B9F2A /* StatusBarView_Tabs.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A7233F5802005B9F2A /* StatusBarView_Tabs.swift */; };
F0AB12AA233F58A2005B9F2A /* StatusBarView_Zoom.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A9233F58A2005B9F2A /* StatusBarView_Zoom.swift */; };
Expand Down Expand Up @@ -169,7 +169,7 @@
F0BB437E2344845000ADBEF1 /* StatusBarView_Voice_Select.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12B1233F597B005B9F2A /* StatusBarView_Voice_Select.swift */; };
F0BB437F2344845000ADBEF1 /* StatusBarView_Voice_Favorite.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12B3233F599C005B9F2A /* StatusBarView_Voice_Favorite.swift */; };
F0BB43802344845000ADBEF1 /* StatusBarView_Voice_Volume.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12B5233F59B4005B9F2A /* StatusBarView_Voice_Volume.swift */; };
F0BB43812344846200ADBEF1 /* ReaderView_PDF.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A2233F5798005B9F2A /* ReaderView_PDF.swift */; };
F0BB43812344846200ADBEF1 /* ReaderView_Pdf.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A2233F5798005B9F2A /* ReaderView_Pdf.swift */; };
F0BB43822344846700ADBEF1 /* ReaderView_Web.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AB12A4233F57B3005B9F2A /* ReaderView_Web.swift */; };
F0BB43832344846C00ADBEF1 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0AA69A4232E9710007CC07B /* ContentView.swift */; };
F0C36A962359AF75001E396C /* StatusBarView_Bookmarks.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0C36A952359AF01001E396C /* StatusBarView_Bookmarks.swift */; };
Expand Down Expand Up @@ -280,7 +280,7 @@
F04C8312236187D6003A25B4 /* Macmillan.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Macmillan.swift; sourceTree = "<group>"; };
F0505C522360D37C004F2D50 /* Longman.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Longman.swift; sourceTree = "<group>"; };
F0505C532360D3DA004F2D50 /* LongmanView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongmanView.swift; sourceTree = "<group>"; };
F0586481238FB03900E6DDFA /* PdfToolbarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PdfToolbarView.swift; sourceTree = "<group>"; };
F0586481238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderView_Pdf_Toolbar.swift; sourceTree = "<group>"; };
F058C7ED2396ACC0002C84F0 /* reverso-reverso-speaker.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "reverso-reverso-speaker.js"; sourceTree = "<group>"; };
F058C7F12397A180002C84F0 /* longman.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = longman.json; sourceTree = "<group>"; };
F058C7FF23992F92002C84F0 /* BookmarksView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -338,7 +338,7 @@
F0AA69DC232E97B0007CC07B /* WKRepresenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WKRepresenter.swift; sourceTree = "<group>"; };
F0AA69DD232E97B0007CC07B /* PDFKitView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PDFKitView.swift; sourceTree = "<group>"; };
F0AA69E5232E989F007CC07B /* ReaderTranslator.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ReaderTranslator.entitlements; sourceTree = "<group>"; };
F0AB12A2233F5798005B9F2A /* ReaderView_PDF.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderView_PDF.swift; sourceTree = "<group>"; };
F0AB12A2233F5798005B9F2A /* ReaderView_Pdf.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderView_Pdf.swift; sourceTree = "<group>"; };
F0AB12A4233F57B3005B9F2A /* ReaderView_Web.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderView_Web.swift; sourceTree = "<group>"; };
F0AB12A7233F5802005B9F2A /* StatusBarView_Tabs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_Tabs.swift; sourceTree = "<group>"; };
F0AB12A9233F58A2005B9F2A /* StatusBarView_Zoom.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_Zoom.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -750,15 +750,23 @@
F01A50E823492398001DCC11 /* ReversoView.swift */,
F0D31C5023491C23003CF86B /* GTranslatorView.swift */,
F0305FD0238125E6002AC5F5 /* YTranslatorView.swift */,
F0AB12A2233F5798005B9F2A /* ReaderView_PDF.swift */,
F0C706D4239B98550061F790 /* ReaderView_Pdf */,
F023CEB0239A5648006DE5EA /* Bookmarks */,
F0AB12A4233F57B3005B9F2A /* ReaderView_Web.swift */,
F026B1E72344830200E032B9 /* SafariView.swift */,
F0586481238FB03900E6DDFA /* PdfToolbarView.swift */,
);
path = Modes;
sourceTree = "<group>";
};
F0C706D4239B98550061F790 /* ReaderView_Pdf */ = {
isa = PBXGroup;
children = (
F0AB12A2233F5798005B9F2A /* ReaderView_Pdf.swift */,
F0586481238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift */,
);
path = ReaderView_Pdf;
sourceTree = "<group>";
};
F0EE0A142347BEDB004A5EAD /* WebKit */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1089,7 +1097,7 @@
F0305FD42381271A002AC5F5 /* YTranslator.swift in Sources */,
F0EE0A172347BF09004A5EAD /* WKPageView.swift in Sources */,
F0C4EDA52349260000CCD97A /* ReversoView.swift in Sources */,
F0586483238FB03900E6DDFA /* PdfToolbarView.swift in Sources */,
F0586483238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift in Sources */,
F08EBB02239541FB009025D3 /* PeerConnection.swift in Sources */,
F06DB10B234504FD00C2DE90 /* EditorNSTextRepresentable.swift in Sources */,
F0AD8B1C236413F60017C22F /* AvailableView.swift in Sources */,
Expand Down Expand Up @@ -1141,7 +1149,7 @@
F0BB436C2344842200ADBEF1 /* ReaderView.swift in Sources */,
F0FCDD41236189760016F23F /* MacmillanView.swift in Sources */,
F0BB437A2344845000ADBEF1 /* StatusBarView_Zoom.swift in Sources */,
F0BB43812344846200ADBEF1 /* ReaderView_PDF.swift in Sources */,
F0BB43812344846200ADBEF1 /* ReaderView_Pdf.swift in Sources */,
F0EE0A1E2348A66E004A5EAD /* PDFKitViewRepresentable.swift in Sources */,
F06DB117234611BB00C2DE90 /* ExtensionManager.swift in Sources */,
F0BB43722344843D00ADBEF1 /* FavoriteVoiceName.swift in Sources */,
Expand Down Expand Up @@ -1183,14 +1191,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F0586482238FB03900E6DDFA /* PdfToolbarView.swift in Sources */,
F0586482238FB03900E6DDFA /* ReaderView_Pdf_Toolbar.swift in Sources */,
F0AB12A8233F5802005B9F2A /* StatusBarView_Tabs.swift in Sources */,
F0418B98239A32FD00B59C66 /* Bookmark.swift in Sources */,
F0AB12B4233F599C005B9F2A /* StatusBarView_Voice_Favorite.swift in Sources */,
F0AA69A1232E9710007CC07B /* AppDelegate.swift in Sources */,
F0C706D0239ACE610061F790 /* BookmarksView_Controls.swift in Sources */,
F0F256BD233D309F00C9D719 /* String.swift in Sources */,
F0AB12A3233F5798005B9F2A /* ReaderView_PDF.swift in Sources */,
F0AB12A3233F5798005B9F2A /* ReaderView_Pdf.swift in Sources */,
F0AD8B1E236414070017C22F /* TranslateAction.swift in Sources */,
F0C53174234B06D3003174B5 /* WKScriptsSetup.swift in Sources */,
F08EBB1023956D80009025D3 /* NetworkManager.swift in Sources */,
Expand Down
110 changes: 0 additions & 110 deletions ReaderTranslator/Views/ReaderView/Modes/PdfToolbarView.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

import SwiftUI

struct ReaderView_PDF: View {
struct ReaderView_Pdf: View {
@ObservedObject var store = Store.shared

var body: some View {
VStack {
if store.viewMode == .pdf {
PDFKitView()
PdfToolbarView().frame(height: 30)
PDFKitView().padding(.bottom)
ReaderView_Pdf_Toolbar().frame(height: 30)
}
}
}
}

struct ReaderView_PDF_Previews: PreviewProvider {
struct ReaderView_Pdf_Previews: PreviewProvider {
static var previews: some View {
ReaderView_PDF()
ReaderView_Pdf()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//
// AudioPlayerView.swift
// ReaderTranslator
//
// Created by Viktor Kushnerov on 28/11/19.
// Copyright © 2019 Viktor Kushnerov. All rights reserved.
//

import SwiftUI
import AVFoundation

private var player: AVAudioPlayer?

struct ReaderView_Pdf_Toolbar: View {
@ObservedObject var store = Store.shared

@State var currentStatus = ""
@State var isPlaying = false
@State var audioRate: Float = 1 {
didSet {
player?.rate = audioRate
}
}

private var openPdfButton: some View {
Button(
action: {
OpenPanel.showChooseFileDialog(title: "Open PDF file", allowedFileTypes: ["pdf"]) { url in
guard let url = url?.absoluteString else { return }

self.store.lastPdfPage = "1"
self.store.lastPdf = url
}
},
label: { Text("Open PDF") })
}

private var openAudioButton: some View {
Button(action: {
OpenPanel.showChooseFileDialog(title: "Open audio file", allowedFileTypes: ["mp3", "mov"]) { url in
guard let url = url else { return }
self.store.pdfAudio = url
self.openAudio(url: url)
self.audioRate = 1
}
}, label: { Text("Open audio") })
}

private var rewindButtonsView: some View {
HStack(spacing: 2) {
Button(action: { player?.currentTime = 0 }, label: { Text("|<") })
rewindButton(label: "-50", step: -50)
rewindButton(label: "-5", step: -5)
rewindButton(label: "-1", step: -1)

Button(action: {
guard let player = player else { return }
if self.isPlaying {
player.pause()
} else {
//hack: currentTime jump forward for some time after an audio is continue to play
let currentTime = player.currentTime
player.play()
player.currentTime = currentTime
}
}, label: { Text(isPlaying ? "Pause" : "Play").frame(width: 40) })

rewindButton(label: "+1", step: 1)
rewindButton(label: "+5", step: 5)
rewindButton(label: "+50", step: 50)
}
}

private var audioRateButtonsView: some View {
HStack(spacing: 2) {
Divider()
Button(action: { self.audioRate = 0.2 }, label: { Text(".2") })
Button(action: { self.audioRate = 0.5 }, label: { Text(".5") })
Button(action: { self.audioRate -= 0.1 }, label: { Text("-") })
Text(String(format: "%.1f", arguments: [audioRate]))
Button(action: { self.audioRate += 0.1 }, label: { Text("+") })
Button(action: { self.audioRate = 1 }, label: { Text("1") })
}
}

var body: some View {
VStack {
HStack {
openPdfButton
openAudioButton

Text("\(currentStatus)").frame(width: 100)
}
HStack {
rewindButtonsView
audioRateButtonsView
}
}
.onAppear {
_ = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { _ in
guard let player = player else { return }
self.currentStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
self.isPlaying = player.isPlaying
}
if let url = self.store.pdfAudio { self.openAudio(url: url) }
}
}

private func rewindButton(label: String, step: Double) -> some View {
Button(action: { player?.currentTime += step }, label: { Text(label) })
}

private func openAudio(url: URL) {
do {
player = try AVAudioPlayer(contentsOf: url)
} catch {
print(error)
}
player?.enableRate = true
}
}

struct ReaderView_Pdf_Toolbar_Previews: PreviewProvider {
static var previews: some View {
ReaderView_Pdf_Toolbar()
}
}
2 changes: 1 addition & 1 deletion ReaderTranslator/Views/ReaderView/ReaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct ReaderView: View {
if store.enabledViews.contains(.gTranslator) { GTranslatorView() }
if store.enabledViews.contains(.yTranslator) { YTranslatorView() }
}
ReaderView_PDF()
ReaderView_Pdf()
ReaderView_Web()
}
}
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>486</string>
<string>492</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
Loading

0 comments on commit a62aaa9

Please sign in to comment.