Skip to content

Commit

Permalink
AudioPlayer features #58: Divide AudioStore to AudioStore and FileStore
Browse files Browse the repository at this point in the history
  • Loading branch information
filimo committed Dec 31, 2019
1 parent 9fda7e8 commit 2f77723
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 114 deletions.
4 changes: 4 additions & 0 deletions ReaderTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
F0305FD223812711002AC5F5 /* YTranslatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0305FD0238125E6002AC5F5 /* YTranslatorView.swift */; };
F0305FD32381271A002AC5F5 /* YTranslatorRepresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F062029423812264002EEAEE /* YTranslatorRepresenter.swift */; };
F0305FD42381271A002AC5F5 /* YTranslatorRepresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F062029423812264002EEAEE /* YTranslatorRepresenter.swift */; };
F03176B323BB2C51004388A7 /* FileStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = F03176B223BB2C51004388A7 /* FileStore.swift */; };
F033F41223A6D3A800605325 /* DirectoryObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = F033F41123A6D3A800605325 /* DirectoryObserver.swift */; };
F033F41523A75B5600605325 /* SentencesView_Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = F033F41423A75B5600605325 /* SentencesView_Row.swift */; };
F0369E3223A2D92C00C33139 /* StatusBarView_Sync.swift in Sources */ = {isa = PBXBuildFile; fileRef = F0369E3123A2D92C00C33139 /* StatusBarView_Sync.swift */; };
Expand Down Expand Up @@ -417,6 +418,7 @@
F02B04BE23A2898300F93B84 /* PeerConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerConnection.swift; sourceTree = "<group>"; };
F02B04C223A2930D00F93B84 /* ConnectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionView.swift; sourceTree = "<group>"; };
F0305FD0238125E6002AC5F5 /* YTranslatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YTranslatorView.swift; sourceTree = "<group>"; };
F03176B223BB2C51004388A7 /* FileStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileStore.swift; sourceTree = "<group>"; };
F033F41123A6D3A800605325 /* DirectoryObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectoryObserver.swift; sourceTree = "<group>"; };
F033F41423A75B5600605325 /* SentencesView_Row.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentencesView_Row.swift; sourceTree = "<group>"; };
F0369E3123A2D92C00C33139 /* StatusBarView_Sync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarView_Sync.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -647,6 +649,7 @@
children = (
F012038C239286A2008D0B47 /* Store.swift */,
F065095B23ADF971003D2410 /* AudioStore.swift */,
F03176B223BB2C51004388A7 /* FileStore.swift */,
);
path = Store;
sourceTree = "<group>";
Expand Down Expand Up @@ -1395,6 +1398,7 @@
F06EE7C923AFF32700CF989E /* ConnectionView_Servers.swift in Sources */,
F01203902392D586008D0B47 /* RoundButtonStyle.swift in Sources */,
F033F41523A75B5600605325 /* SentencesView_Row.swift in Sources */,
F03176B323BB2C51004388A7 /* FileStore.swift in Sources */,
F0C02D3723B26A3800B393A5 /* OSLog.swift in Sources */,
F099424A23AD549C003CF1EB /* BookmarksStore.swift in Sources */,
F0B4843F23A8C6860031A811 /* URL.swift in Sources */,
Expand Down
7 changes: 4 additions & 3 deletions ReaderTranslator/Extentions/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import os.log
import Foundation

final class Logger {
static func debug(
log: OSLog,
static func log(
log: OSLog = .default,
type: OSLogType = .error,
className: AnyClass? = nil,
callback: String = "",
delegate: String = "",
Expand All @@ -28,7 +29,7 @@ final class Logger {
let className = className == nil ? "" : String(describing: className!)
let value = value == nil ? "" : ": \(String(describing: value!))"

os_log("%s:%i %s%s%s.%s%s", log: log, type: .debug,
os_log("%s:%i %s%s%s.%s%s", log: log, type: type,
file, line, className, delegate, callback, function, value)
}
}
6 changes: 4 additions & 2 deletions ReaderTranslator/Model/AvailableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ enum AvailableView: String, Codable, CaseIterable {

var order: Binding<String> {
Binding<String>(
get: { "\(ViewsStore.shared.viewOrder[self] ?? 0)" },
get: {
"\(ViewsStore.shared.viewOrder[self] ?? 0)"
},
set: {
ViewsStore.shared.viewOrder[self] = $0.intValue
}
)
}

var orderInt: Int {
order.wrappedValue.intValue
ViewsStore.shared.viewOrder[self] ?? 0
}

var view: some View {
Expand Down
5 changes: 4 additions & 1 deletion ReaderTranslator/Networking/ConnectionServerStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Viktor Kushnerov on 13/12/19.
// Copyright © 2019 Viktor Kushnerov. All rights reserved.
//

import Foundation
import Network

enum ErrorServerConnection: Error {
Expand Down Expand Up @@ -56,3 +56,6 @@ enum ConnectionServerStatus: Equatable {
}
}
}



10 changes: 5 additions & 5 deletions ReaderTranslator/Networking/NetBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NetBrowser: ObservableObject {
}

func connectToServer(server: NWBrowser.Result, passcode: String) {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
sharedConnection = PeerConnection(
endpoint: server.endpoint,
interface: server.interfaces.first,
Expand All @@ -35,25 +35,25 @@ class NetBrowser: ObservableObject {

extension NetBrowser: PeerBrowserDelegate {
func refreshResults(results: Set<NWBrowser.Result>) {
Logger.debug(log: .p2p, delegate: "PeerBrowserDelegate")
Logger.log(log: .p2p, delegate: "PeerBrowserDelegate")
status = .none
servers = results.map { $0 }
}
}

extension NetBrowser: PeerConnectionDelegate {
func connectionReady() {
Logger.debug(log: .p2p, delegate: "PeerBrowserDelegate")
Logger.log(log: .p2p, delegate: "PeerBrowserDelegate")
status = .connected
}

func connectionFailed() {
Logger.debug(log: .p2p, delegate: "PeerBrowserDelegate")
Logger.log(log: .p2p, delegate: "PeerBrowserDelegate")
status = .failed(error: "failed")
}

func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
Logger.debug(log: .p2p, delegate: "PeerBrowserDelegate")
Logger.log(log: .p2p, delegate: "PeerBrowserDelegate")
// switch message.readerTranslatorMessageType {
// case .invalid: print("Received invalid message")
// case .send: store.bookmarks.merge(data: content)
Expand Down
8 changes: 4 additions & 4 deletions ReaderTranslator/Networking/PeerBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PeerBrowser {

// Start browsing for services.
func startBrowsing() {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
// Create parameters, and allow browsing over peer-to-peer link.
let parameters = NWParameters()
parameters.includePeerToPeer = true
Expand All @@ -37,11 +37,11 @@ class PeerBrowser {
let browser = NWBrowser(for: .bonjour(type: "_reader_translator._tcp", domain: nil), using: parameters)
self.browser = browser
browser.stateUpdateHandler = { newState in
Logger.debug(log: .p2p, callback: "stateUpdateHandler", value: newState)
Logger.log(log: .p2p, callback: "stateUpdateHandler", value: newState)
switch newState {
case let .failed(error):
// Restart the browser if it fails.
Logger.debug(
Logger.log(
log: .p2p,
className: Self.self,
value: "Browser failed with \(error), restarting")
Expand All @@ -54,7 +54,7 @@ class PeerBrowser {

// When the list of discovered endpoints changes, refresh the delegate.
browser.browseResultsChangedHandler = { results, _ in
Logger.debug(log: .p2p, callback: "browseResultsChangedHandler")
Logger.log(log: .p2p, callback: "browseResultsChangedHandler")
self.delegate?.refreshResults(results: results)
}

Expand Down
10 changes: 5 additions & 5 deletions ReaderTranslator/Networking/PeerConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class PeerConnection {

// Handle starting the peer-to-peer connection for both inbound and outbound connections.
func startConnection() {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
guard let connection = connection else {
return
}

connection.stateUpdateHandler = { newState in
Logger.debug(log: .p2p, callback: "stateUpdateHandler", value: newState)
Logger.log(log: .p2p, callback: "stateUpdateHandler", value: newState)
switch newState {
case .ready:
print("\(connection) established")
Expand Down Expand Up @@ -91,7 +91,7 @@ class PeerConnection {

// Handle sending a "send" message.
func sendBookmarks(_ bookmarks: String) {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
guard let connection = connection else {
return
}
Expand All @@ -112,13 +112,13 @@ class PeerConnection {

// Receive a message, deliver it to your delegate, and continue receiving more messages.
func receiveNextMessage() {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
guard let connection = connection else {
return
}

connection.receiveMessage { content, context, _, error in
Logger.debug(log: .p2p, callback: "receiveMessage")
Logger.log(log: .p2p, callback: "receiveMessage")
// Extract your message type from the received context.
let message = context?.protocolMetadata(definition: ReaderTranslatorProtocol.definition)
if let message = message as? NWProtocolFramer.Message {
Expand Down
8 changes: 4 additions & 4 deletions ReaderTranslator/Networking/PeerListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PeerListener {

// Start listening and advertising.
func startListening() {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
do {
// Create the listener object.
let listener = try NWListener(using: NWParameters(passcode: passcode))
Expand All @@ -36,7 +36,7 @@ class PeerListener {
listener.service = NWListener.Service(name: name, type: "_reader_translator._tcp")

listener.newConnectionHandler = { newConnection in
Logger.debug(log: .p2p, callback: "newConnectionHandler", value: newConnection)
Logger.log(log: .p2p, callback: "newConnectionHandler", value: newConnection)
if let delegate = self.delegate {
if sharedConnection == nil {
// Accept a new connection.
Expand All @@ -57,14 +57,14 @@ class PeerListener {
}

func stopListening() {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
listener?.cancel()
sharedConnection = nil
}

// If the user changes their name, update the advertised name.
func resetName(_ name: String) {
Logger.debug(log: .p2p, value: name)
Logger.log(log: .p2p, value: name)
self.name = name
if let listener = listener {
// Reset the service to advertise.
Expand Down
4 changes: 2 additions & 2 deletions ReaderTranslator/Networking/ReaderTranslatorProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ReaderTranslatorProtocol: NWProtocolFramerImplementation {
messageLength: Int,
isComplete _: Bool
) {
Logger.debug(log: .p2p, value: message)
Logger.log(log: .p2p, value: message)
// Extract the type of message.
let type = message.readerTranslatorMessageType

Expand Down Expand Up @@ -84,7 +84,7 @@ class ReaderTranslatorProtocol: NWProtocolFramerImplementation {
messageType = parsedMessageType
}
let message = NWProtocolFramer.Message(readerTranslatorMessageType: messageType)
Logger.debug(log: .p2p, value: message)
Logger.log(log: .p2p, value: message)

// Deliver the body of the message, along with the message object.
if !framer.deliverInputNoCopy(length: Int(header.length), message: message, isComplete: true) {
Expand Down
10 changes: 5 additions & 5 deletions ReaderTranslator/Networking/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Server: ObservableObject {

extension Server {
func stateUpdateHandler(newState: NWListener.State) {
Logger.debug(log: .p2p, value: newState)
Logger.log(log: .p2p, value: newState)
switch newState {
case .ready:
status = .ready
Expand All @@ -38,7 +38,7 @@ extension Server {
}

func start() {
Logger.debug(log: .p2p)
Logger.log(log: .p2p)
passcode = "1111" // self.generatePasscode
connectionCount += 1
sharedListener?.stopListening()
Expand All @@ -52,20 +52,20 @@ extension Server {
extension Server: PeerConnectionDelegate {
// When a connection becomes ready, move into game mode.
func connectionReady() {
Logger.debug(log: .p2p, delegate: "PeerConnectionDelegate")
Logger.log(log: .p2p, delegate: "PeerConnectionDelegate")
// navigationController?.performSegue(withIdentifier: "showGameSegue", sender: nil)
status = .connected
}

// Ignore connection failures and messages prior to starting a game.
func connectionFailed() {
Logger.debug(log: .p2p, delegate: "PeerConnectionDelegate")
Logger.log(log: .p2p, delegate: "PeerConnectionDelegate")
status = .failed(error: .connection(text: "connection failed"))
start()
}

func receivedMessage(content: Data?, message: NWProtocolFramer.Message) {
Logger.debug(log: .p2p, delegate: "PeerConnectionDelegate", value: message)
Logger.log(log: .p2p, delegate: "PeerConnectionDelegate", value: message)
guard let content = content else { return }
if let text = String(data: content, encoding: .unicode) {
print(text)
Expand Down
1 change: 1 addition & 0 deletions ReaderTranslator/Views/StatusBarView/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ struct SettingsView_Previews: PreviewProvider {
SettingsView()
}
}

Loading

0 comments on commit 2f77723

Please sign in to comment.