Skip to content

Commit

Permalink
Merge pull request #143 from tanhakabir/issue-138
Browse files Browse the repository at this point in the history
Experimental set download location
  • Loading branch information
tanhakabir authored Aug 17, 2021
2 parents abb0a29 + 006b94e commit 43e887b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Source/Model/AudioDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ protocol AudioDataManagable {
var numberOfActive: Int { get }

var allowCellular: Bool { get set }
var downloadDirectory: FileManager.SearchPathDirectory { get }

func setHTTPHeaderFields(_ fields: [String: String]?)
func setBackgroundCompletionHandler(_ completionHandler: @escaping () -> ())
func setAllowCellularDownloadPreference(_ preference: Bool)
func setDownloadDirectory(_ dir: FileManager.SearchPathDirectory)

func clear()

Expand All @@ -54,6 +56,7 @@ protocol AudioDataManagable {

class AudioDataManager: AudioDataManagable {
var allowCellular: Bool = true
var downloadDirectory: FileManager.SearchPathDirectory = .documentDirectory

static let shared: AudioDataManagable = AudioDataManager()

Expand Down Expand Up @@ -110,6 +113,10 @@ class AudioDataManager: AudioDataManagable {
allowCellular = preference
}

func setDownloadDirectory(_ dir: FileManager.SearchPathDirectory) {
downloadDirectory = dir
}

func attach(callback: @escaping (_ id: ID, _ progress: Double)->()) {
globalDownloadProgressCallback = callback
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Model/Downloading/FileStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct FileStorage {
// MARK:- Audio
extension FileStorage {
struct Audio {
private static let directory: FileManager.SearchPathDirectory = .documentDirectory
private static let directory: FileManager.SearchPathDirectory = AudioDataManager.shared.downloadDirectory
private init() {}

static func isStored(_ id: ID) -> Bool {
Expand Down Expand Up @@ -103,7 +103,7 @@ extension FileStorage {
}

static func locate(_ id: ID) -> URL? {
let folderUrls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let folderUrls = FileManager.default.urls(for: directory, in: .userDomainMask)
guard folderUrls.count != 0 else { return nil }

if let urls = try? FileManager.default.contentsOfDirectory(at: folderUrls[0], includingPropertiesForKeys: nil) {
Expand Down
9 changes: 9 additions & 0 deletions Source/SAPlayerDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,14 @@ extension SAPlayer {
AudioDataManager.shared.setAllowCellularDownloadPreference(allowUsingCellularData)
}
}

/**
EXPERIMENTAL!
*/
public static var downloadDirectory: FileManager.SearchPathDirectory = .documentDirectory {
didSet {
AudioDataManager.shared.setDownloadDirectory(downloadDirectory)
}
}
}
}

0 comments on commit 43e887b

Please sign in to comment.