Skip to content

Commit

Permalink
AudioPlayer features #58: Fix: Restored the directory observer.
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Jan 13, 2020
1 parent b8797a4 commit 479ee8f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 30 deletions.
4 changes: 2 additions & 2 deletions ReaderTranslatorMac/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<string>1.8.10</string>
<key>CFBundleVersion</key>
<string>1768</string>
<string>1775</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.education</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion ReaderTranslatorPlayer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>35</string>
<string>64</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
Expand Down
37 changes: 25 additions & 12 deletions ReaderTranslatorPlayer/Store/AudioStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ final class AudioStore: NSObject, ObservableObject {
private(set) var player: AVAudioPlayer?
private var someObservationContext = ""

private static var directoryObserver: DirectoryObserver?

@Published private(set) var allAudioPlayers = [AVAudioPlayer]()

@Published var timelineStatus = "0.0/0.0"
Expand Down Expand Up @@ -52,6 +54,25 @@ final class AudioStore: NSObject, ObservableObject {
private override init() {
super.init()

initDirectoryObserver()
initAudioSesssion()
saveAllAudioPlayer()
initMPRemoteCommandCenter()

if let lastAudio = lastAudio { openAudio(url: lastAudio) }
}
}

extension AudioStore {
private func initDirectoryObserver() {
if let url = FileStore.shared.folderUrl {
Self.directoryObserver = DirectoryObserver(URL: url) {
RunLoop.main.perform { self.saveAllAudioPlayer() }
}
}
}

private func initAudioSesssion() {
do {
let sharedInstance = AVAudioSession.sharedInstance()

Expand All @@ -60,25 +81,18 @@ final class AudioStore: NSObject, ObservableObject {
} catch {
Logger.log(type: .error, value: error)
}

saveAllAudioPlayer()
if let lastAudio = lastAudio { openAudio(url: lastAudio) }

initMPRemoteCommandCenter()
}
}

extension AudioStore {

func updateTimeline(timer _: Timer? = nil) {
guard let player = self.player else { return }
self.timelineStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
timelineStatus = String(format: "%.1f/%.1f", player.currentTime, player.duration)
}
}

extension AudioStore {
func openAudio(url: URL?) {
guard let url = url else { return }

if player != nil { player?.pause() }
player = allAudioPlayers.first { $0.url == url }
guard let player = player else { return }
Expand Down Expand Up @@ -123,9 +137,8 @@ extension AudioStore {
}
}


extension AudioStore: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
func audioPlayerDidFinishPlaying(_: AVAudioPlayer, successfully flag: Bool) {
if flag {
nextPlay()
}
Expand Down
4 changes: 1 addition & 3 deletions ReaderTranslatorPlayer/Store/FileStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ final class FileStore: ObservableObject {
private init() {}
static let shared = FileStore()

static private var directoryObserver: DirectoryObserver?

var files: [URL] {
guard let url = folderUrl else { return [] }

Expand All @@ -25,7 +23,7 @@ final class FileStore: ObservableObject {
}
}

private let folderUrl: URL? = {
let folderUrl: URL? = {
guard let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions ReaderTranslatorPlayer/Views/PlayerContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct PlayerContentView: View {
PlayerControlsView()
BookmarksView()
}
.navigationBarTitle("", displayMode: .inline)
.navigationBarHidden(store.hideNavBar)
.navigationBarTitle("ReaderTranslator", displayMode: .inline)
// .navigationBarHidden(store.hideNavBar)
.onAppear { self.store.hideNavBar = true }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ struct PlayerControlsView: View {

private var playPauseButton: some View {
Button(
action: {
self.audioStore.saveAllAudioPlayer()
self.audioStore.isPlaying.toggle()
},
action: { self.audioStore.isPlaying.toggle() },
label: { Text(audioStore.isPlaying ? "Pause" : "Play") }
).buttonStyle(RoundButtonStyle())
}
Expand Down
6 changes: 3 additions & 3 deletions ReaderTranslatorPlayer/Views/ViewModes/BookmarksView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct BookmarksView: View {
destination: SentencesView(bookmark: bookmark.text),
label: {
Text(bookmark.text)
.font(.largeTitle)
.font(.title)
.lineLimit(1)
}
).layoutPriority(2)
Expand All @@ -40,7 +40,7 @@ struct BookmarksView: View {
Spacer()

CircleButton {
Text("\(bookmark.counter)")
Text("\(bookmark.counter)").font(.footnote)
}
.modifier(ButtonModifier())
.onTapGesture {
Expand All @@ -67,7 +67,7 @@ struct BookmarksView: View {

struct BookmarksView_Previews: PreviewProvider {
static var previews: some View {
Store.shared.bookmarks = [.init(text: "test bookmark test test")]
Store.shared.bookmarks = [.init(counter: 11, text: "test bookmark test test")]
return BookmarksView()
}
}
7 changes: 4 additions & 3 deletions ReaderTranslatorPlayer/Views/ViewModes/FileListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import Dispatch
import SwiftUI

struct FileListView: View {
@ObservedObject var fileStore = FileStore.shared
@ObservedObject var audioStore = AudioStore.shared
@State var files = FileStore.shared.files

@State var isEditMode: EditMode = .inactive

var body: some View {
List {
Expand All @@ -26,12 +26,13 @@ struct FileListView: View {

do {
try FileManager.default.removeItem(at: url)
self.audioStore.saveAllAudioPlayer()
} catch {
Logger.log(type: .error, value: error)
}
}
}
.navigationBarItems(trailing: EditButton())
.environment(\.editMode, $isEditMode)
}

private func buttonView(_ player: AVAudioPlayer) -> some View {
Expand Down

0 comments on commit 479ee8f

Please sign in to comment.