added iOS source code
[wl-app.git] / iOS / WolneLektury / Model / Realm / RLMApplication.swift
1 //
2 //  RLMApplication.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 24/08/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10 import RealmSwift
11
12 class RLMApplication: Object {
13     
14     @objc dynamic var user: RLMUser?
15     var downloadedBooks = List<BookDetailsModel>()
16     @objc dynamic var premium: Bool = false
17
18     
19     func updateUser(usernameModel: UsernameModel?){
20         let realm = try! Realm()
21         
22         if let usernameModel = usernameModel{
23             try! realm.write {
24                 let usr = RLMUser()
25                 usr.username = usernameModel.username
26                 usr.premium = usernameModel.premium
27                 self.user = usr
28             }
29         }
30         else {
31             if user != nil {
32                 try! realm.write {
33                     realm.delete( user!)
34                     self.user = nil
35                 }
36             }
37         }
38     }
39     
40     func addBookToDownloaded(bookDetailsModel: BookDetailsModel){
41         let realm = try! Realm()
42         realm.beginWrite()
43
44         var bookExists = false
45         if let b = getBookFromDownloaded(bookSlug: bookDetailsModel.slug) {
46             bookDetailsModel.currentChapter = b.currentChapter
47             bookDetailsModel.totalChapters = b.totalChapters
48             bookDetailsModel.currentAudioChapter = b.currentAudioChapter
49             bookDetailsModel.totalAudioChapters = b.totalAudioChapters
50             
51             bookDetailsModel.authors = b.authors
52             bookDetailsModel.epochs = b.epochs
53             bookDetailsModel.genres = b.genres
54             bookDetailsModel.kinds = b.kinds
55             bookDetailsModel.media = b.media
56
57             bookExists = true
58         }
59         
60         realm.add(bookDetailsModel, update: true)
61         if !bookExists{
62             self.downloadedBooks.append(bookDetailsModel)
63         }
64         try! realm.commitWrite()
65     }
66     
67     func removeAllBooksFromDownloaded() {
68         
69         guard downloadedBooks.count > 0 else { return }
70         
71         let realm = try! Realm()
72         realm.beginWrite()
73         realm.delete(downloadedBooks)
74         try! realm.commitWrite()
75     }
76     
77     func removeBookFromDownloaded(bookSlug: String) -> Bool {
78         
79         let books = downloadedBooks.filter({$0.slug == bookSlug})
80         
81         if books.count > 0 {
82             let realm = try! Realm()
83             realm.beginWrite()
84             realm.delete(books)
85             try! realm.commitWrite()
86             return true
87         }
88         return false
89     }
90     
91     func getBookFromDownloaded(bookSlug: String) -> BookDetailsModel? {
92         return downloadedBooks.first(where: {$0.slug == bookSlug})
93     }
94     
95     func setUserPermium() {
96         guard let user = user else { return }
97         
98         let realm = try! Realm()
99         realm.beginWrite()
100         user.premium = true
101         try! realm.commitWrite()
102     }
103 }