Added Android code
[wl-app.git] / iOS / WolneLektury / Utils / DatabaseManager.swift
1 //
2 //  DatabaseManager.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 11/09/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10 import RealmSwift
11
12 class DatabaseManager: NSObject {
13     static let shared = DatabaseManager()
14
15     override init() {
16         super.init()
17         realmConfiguration()
18     }
19     
20     var rlmApplication:RLMApplication?
21
22     func realmConfiguration()
23     {
24         Realm.Configuration.defaultConfiguration = Realm.Configuration(
25             schemaVersion: 1,
26             migrationBlock: { migration, oldSchemaVersion in
27                 /*
28                  if (oldSchemaVersion < 1) {
29                  
30                  } */
31         })
32         
33         let realm = try! Realm()
34         self.rlmApplication = realm.objects(RLMApplication.self).last
35         
36         print(realm.configuration.fileURL)
37         
38         if  self.rlmApplication  == nil
39         {
40             self.rlmApplication  = RLMApplication()
41             try! realm.write {
42                 realm.add(self.rlmApplication!)
43             }
44         }
45     }
46
47     /// removing database
48     func purgeData(){
49         let realm = try! Realm()
50         self.rlmApplication = nil
51         try! realm.write {
52             realm.deleteAll()
53         }
54         
55         self.rlmApplication  = RLMApplication()
56         try! realm.write {
57             realm.add(self.rlmApplication!)
58         }
59     }
60
61     func isUserPremium()-> Bool{
62         if let premium = rlmApplication?.user?.premium, premium == true{
63             return true
64         }
65         return false
66     }
67
68     func updateUser(usernameModel: UsernameModel?){
69         self.rlmApplication?.updateUser(usernameModel: usernameModel)
70     }
71
72     func addBookToDownloaded(bookDetailsModel: BookDetailsModel) {
73         rlmApplication?.addBookToDownloaded(bookDetailsModel: bookDetailsModel)
74     }
75     
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)
81         }
82         
83         application.removeAllBooksFromDownloaded()
84     }
85     
86     func removeBookFromDownloaded(bookSlug: String) -> Bool{
87         guard let application = rlmApplication else {
88             return false
89         }
90
91         DownloadManager.sharedInstance.deleteEbook(bookSlug: bookSlug)
92         DownloadManager.sharedInstance.deleteAudiobook(bookSlug: bookSlug)
93         return application.removeBookFromDownloaded(bookSlug: bookSlug)
94     }
95     
96     func getBookFromDownloaded(bookSlug: String) -> BookDetailsModel? {
97         return rlmApplication?.getBookFromDownloaded(bookSlug: bookSlug)
98     }
99
100     func updateCurrentChapters(bookDetailsModel: BookDetailsModel, currentChapter: Int?, totalChapters: Int?, currentAudioChapter: Int?, totalAudioChapters: Int?)
101     {
102         let realm = try! Realm()
103         realm.beginWrite()
104      
105         if let currentChapter = currentChapter{
106             bookDetailsModel.currentChapter = currentChapter
107         }
108         
109         if let totalChapters = totalChapters{
110             bookDetailsModel.totalChapters = totalChapters
111         }
112
113         if let currentAudioChapter = currentAudioChapter{
114             bookDetailsModel.currentAudioChapter = currentAudioChapter
115         }
116
117         if let totalAudioChapters = totalAudioChapters{
118             bookDetailsModel.totalAudioChapters = totalAudioChapters
119         }
120
121         try! realm.commitWrite()
122     }
123     
124     func removeBookFromDownloadedIfNoFiles(bookSlug: String) {
125         
126         if FileManager.default.fileExists(atPath: FileType.ebook.destinationPathWithSlug(bookSlug: bookSlug)) {
127             return
128         }
129         
130         let fileNames = try! FileManager.default.contentsOfDirectory(atPath: FileType.audiobook.destinationPathWithSlug(bookSlug: bookSlug))
131
132         if fileNames.count > 0 {
133             return
134         }
135         
136         let _ = rlmApplication?.removeBookFromDownloaded(bookSlug: bookSlug)
137     }
138     
139     func refresh(){
140         rlmApplication?.realm?.refresh()
141     }
142     
143     func setUserPremium() {
144         rlmApplication?.setUserPermium()
145     }
146 }