From 52d0890032aa9693fabcb04eb4400327336e9e96 Mon Sep 17 00:00:00 2001 From: ronaldheft Date: Sat, 16 Jul 2022 21:46:49 -0400 Subject: [PATCH] Refactor to use extensions for objects --- ios/App/App.xcodeproj/project.pbxproj | 4 ++ ios/App/App/plugins/AbsDownloader.swift | 2 +- ios/App/Shared/models/LocalLibrary.swift | 39 ++------------ .../models/LocalLibraryExtensions.swift | 54 +++++++++++++++++++ 4 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 ios/App/Shared/models/LocalLibraryExtensions.swift diff --git a/ios/App/App.xcodeproj/project.pbxproj b/ios/App/App.xcodeproj/project.pbxproj index 015d9deb..fcc0948b 100644 --- a/ios/App/App.xcodeproj/project.pbxproj +++ b/ios/App/App.xcodeproj/project.pbxproj @@ -37,6 +37,7 @@ A084ECDBA7D38E1E42DFC39D /* Pods_App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AF277DCFFFF123FFC6DF26C7 /* Pods_App.framework */; }; C4B265F5285A5A6600E1B5C3 /* LocalLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4B265F4285A5A6600E1B5C3 /* LocalLibrary.swift */; }; C4D0677528106D0C00B8F875 /* DataClasses.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4D0677428106D0C00B8F875 /* DataClasses.swift */; }; + E99C8C932883A00F00E3279A /* LocalLibraryExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E99C8C922883A00F00E3279A /* LocalLibraryExtensions.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -74,6 +75,7 @@ AF51FD2D460BCFE21FA515B2 /* Pods-App.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App.release.xcconfig"; path = "Pods/Target Support Files/Pods-App/Pods-App.release.xcconfig"; sourceTree = ""; }; C4B265F4285A5A6600E1B5C3 /* LocalLibrary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalLibrary.swift; sourceTree = ""; }; C4D0677428106D0C00B8F875 /* DataClasses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataClasses.swift; sourceTree = ""; }; + E99C8C922883A00F00E3279A /* LocalLibraryExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalLibraryExtensions.swift; sourceTree = ""; }; FC68EB0AF532CFC21C3344DD /* Pods-App.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App.debug.xcconfig"; path = "Pods/Target Support Files/Pods-App/Pods-App.debug.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -138,6 +140,7 @@ 3ABF580828059BAE005DFBE5 /* PlaybackSession.swift */, C4D0677428106D0C00B8F875 /* DataClasses.swift */, C4B265F4285A5A6600E1B5C3 /* LocalLibrary.swift */, + E99C8C922883A00F00E3279A /* LocalLibraryExtensions.swift */, 3A90295E280968E700E1D427 /* PlaybackReport.swift */, 4DF74911287105C600AC7814 /* DeviceSettings.swift */, ); @@ -338,6 +341,7 @@ C4D0677528106D0C00B8F875 /* DataClasses.swift in Sources */, 4D66B954282EE87C008272D4 /* AbsDownloader.swift in Sources */, 3AB34055280832720039308B /* PlayerEvents.swift in Sources */, + E99C8C932883A00F00E3279A /* LocalLibraryExtensions.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ios/App/App/plugins/AbsDownloader.swift b/ios/App/App/plugins/AbsDownloader.swift index 6db10669..8b8191b0 100644 --- a/ios/App/App/plugins/AbsDownloader.swift +++ b/ios/App/App/plugins/AbsDownloader.swift @@ -37,7 +37,7 @@ public class AbsDownloader: CAPPlugin { position, track -> LocalFile in startLibraryItemTrackDownload(item: item, position: position, track: track) } - let localLibraryItem = LocalLibraryItem(item: item, localUrl: documentsDirectory, server: Store.serverConfig!, files: files) + let localLibraryItem = LocalLibraryItem(item, localUrl: documentsDirectory, server: Store.serverConfig!, files: files) Database.shared.saveLocalLibraryItem(localLibraryItem: localLibraryItem) } else { NSLog("No audio tracks for the supplied library item") diff --git a/ios/App/Shared/models/LocalLibrary.swift b/ios/App/Shared/models/LocalLibrary.swift index 7a66e093..ffd98000 100644 --- a/ios/App/Shared/models/LocalLibrary.swift +++ b/ios/App/Shared/models/LocalLibrary.swift @@ -25,34 +25,6 @@ class LocalLibraryItem: Object, Encodable { @Persisted var serverAddress: String? = nil @Persisted var serverUserId: String? = nil @Persisted var libraryItemId: String? = nil - - override init() { - super.init() - } - - init(item: LibraryItem, localUrl: URL, server: ServerConnectionConfig, files: [LocalFile]) { - super.init() - self.id = item.id - self.contentUrl = localUrl.absoluteString - self.mediaType = item.mediaType - self.media = LocalMediaType(mediaType: item.media) - self.localFiles.append(objectsIn: files) - // TODO: self.coverContentURL - // TODO: self.converAbsolutePath - self.libraryItemId = item.id - self.serverConnectionConfigId = server.id - self.serverAddress = server.address - self.serverUserId = server.userId - } - - enum CodingKeys: CodingKey { - case id - } - - func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(id, forKey: .id) - } } class LocalMediaType: Object { @@ -113,13 +85,10 @@ class LocalMetadata: Object { @Persisted var narratorName: String? = "" @Persisted var seriesName: String? = "" @Persisted var feedUrl: String? = "" - - override init() { - super.init() - } - - init(metadata: Metadata) { - super.init() +} + +extension LocalMetadata { + convenience init(metadata: Metadata) { self.title = metadata.title self.subtitle = metadata.subtitle self.narrators.append(objectsIn: metadata.narrators ?? []) diff --git a/ios/App/Shared/models/LocalLibraryExtensions.swift b/ios/App/Shared/models/LocalLibraryExtensions.swift new file mode 100644 index 00000000..7474e9cc --- /dev/null +++ b/ios/App/Shared/models/LocalLibraryExtensions.swift @@ -0,0 +1,54 @@ +// +// LocalLibraryExtensions.swift +// App +// +// Created by Ron Heft on 7/16/22. +// + +import Foundation + +extension LocalLibraryItem { + enum CodingKeys: CodingKey { + case id + case basePath + case absolutePath + case contentUrl + case isInvalid + case mediaType + case media + case localFiles + case coverContentUrl + case coverAbsolutePath + case isLocal + case serverConnectionConfigId + case serverAddress + case serverUserId + case libraryItemId + } + + func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .id) + try container.encode(basePath, forKey: .basePath) + try container.encode(absolutePath, forKey: .absolutePath) + try container.encode(contentUrl, forKey: .contentUrl) + try container.encode(isInvalid, forKey: .isInvalid) + try container.encode(mediaType, forKey: .mediaType) + //try container.encode(media, forKey: .media) + //try container.encode(localFiles, forKey: .localFiles) + } + + convenience init(_ item: LibraryItem, localUrl: URL, server: ServerConnectionConfig, files: [LocalFile]) { + self.init() + self.contentUrl = localUrl.absoluteString + self.mediaType = item.mediaType + self.media = LocalMediaType(mediaType: item.media) + self.localFiles.append(objectsIn: files) + // TODO: self.coverContentURL + // TODO: self.converAbsolutePath + self.libraryItemId = item.id + self.serverConnectionConfigId = server.id + self.serverAddress = server.address + self.serverUserId = server.userId + } +}