added iOS source code
[wl-app.git] / iOS / WolneLektury / Extensions / NSObject+Ext.swift
1 //
2 //  NSObject+Ext.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 30/05/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import Foundation
10 import UIKit
11
12 extension NSObject
13 {
14     var appDelegate:AppDelegate {
15         return UIApplication.shared.delegate as! AppDelegate
16     }
17     
18     func ebookExists(bookSlug: String) -> Bool {
19         let path = FileType.ebook.pathForFileName(filename: bookSlug, bookSlug: bookSlug)
20         print(path)
21         return FileManager.default.fileExists(atPath: path)
22     }
23     
24     static func audiobookExists(audioBookUrlString: String, bookSlug: String) -> Bool {
25         let fileName = (audioBookUrlString as NSString).lastPathComponent
26         return FileManager.default.fileExists(atPath: FileType.audiobook.pathForFileName(filename: fileName, bookSlug: bookSlug))
27     }
28     
29     static func audiobookPathIfExists(audioBookUrlString: String, bookSlug: String) -> URL? {
30         let fileName = (audioBookUrlString as NSString).lastPathComponent
31         let path = FileType.audiobook.pathForFileName(filename: fileName, bookSlug: bookSlug)
32         
33         if FileManager.default.fileExists(atPath: path) {
34             return URL(fileURLWithPath: path)
35         }
36         return nil
37     }
38
39 }