2 // DatabaseManager.swift
5 // Created by Pawel Dabrowski on 11/09/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
12 class DatabaseManager: NSObject {
13 static let shared = DatabaseManager()
20 var rlmApplication:RLMApplication?
22 func realmConfiguration()
24 Realm.Configuration.defaultConfiguration = Realm.Configuration(
26 migrationBlock: { migration, oldSchemaVersion in
28 if (oldSchemaVersion < 1) {
33 let realm = try! Realm()
34 self.rlmApplication = realm.objects(RLMApplication.self).last
36 print(realm.configuration.fileURL)
38 if self.rlmApplication == nil
40 self.rlmApplication = RLMApplication()
42 realm.add(self.rlmApplication!)
49 let realm = try! Realm()
50 self.rlmApplication = nil
55 self.rlmApplication = RLMApplication()
57 realm.add(self.rlmApplication!)
61 func isUserPremium()-> Bool{
62 if let premium = rlmApplication?.user?.premium, premium == true{
68 func updateUser(usernameModel: UsernameModel?){
69 self.rlmApplication?.updateUser(usernameModel: usernameModel)
72 func addBookToDownloaded(bookDetailsModel: BookDetailsModel) {
73 rlmApplication?.addBookToDownloaded(bookDetailsModel: bookDetailsModel)
76 func removeAllBooksFromDownloaded() {
77 guard let application = rlmApplication else { return }
78 for book in application.downloadedBooks {
79 DownloadManager.sharedInstance.deleteEbook(bookSlug: book.slug)
80 DownloadManager.sharedInstance.deleteAudiobook(bookSlug: book.slug)
83 application.removeAllBooksFromDownloaded()
86 func removeBookFromDownloaded(bookSlug: String) -> Bool{
87 guard let application = rlmApplication else {
91 DownloadManager.sharedInstance.deleteEbook(bookSlug: bookSlug)
92 DownloadManager.sharedInstance.deleteAudiobook(bookSlug: bookSlug)
93 return application.removeBookFromDownloaded(bookSlug: bookSlug)
96 func getBookFromDownloaded(bookSlug: String) -> BookDetailsModel? {
97 return rlmApplication?.getBookFromDownloaded(bookSlug: bookSlug)
100 func updateCurrentChapters(bookDetailsModel: BookDetailsModel, currentChapter: Int?, totalChapters: Int?, currentAudioChapter: Int?, totalAudioChapters: Int?)
102 let realm = try! Realm()
105 if let currentChapter = currentChapter{
106 bookDetailsModel.currentChapter = currentChapter
109 if let totalChapters = totalChapters{
110 bookDetailsModel.totalChapters = totalChapters
113 if let currentAudioChapter = currentAudioChapter{
114 bookDetailsModel.currentAudioChapter = currentAudioChapter
117 if let totalAudioChapters = totalAudioChapters{
118 bookDetailsModel.totalAudioChapters = totalAudioChapters
121 try! realm.commitWrite()
124 func removeBookFromDownloadedIfNoFiles(bookSlug: String) {
126 if FileManager.default.fileExists(atPath: FileType.ebook.destinationPathWithSlug(bookSlug: bookSlug)) {
130 let fileNames = try! FileManager.default.contentsOfDirectory(atPath: FileType.audiobook.destinationPathWithSlug(bookSlug: bookSlug))
132 if fileNames.count > 0 {
136 let _ = rlmApplication?.removeBookFromDownloaded(bookSlug: bookSlug)
140 rlmApplication?.realm?.refresh()
143 func setUserPremium() {
144 rlmApplication?.setUserPermium()