2 // RLMApplication.swift
5 // Created by Pawel Dabrowski on 24/08/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
12 class RLMApplication: Object {
14 @objc dynamic var user: RLMUser?
15 var downloadedBooks = List<BookDetailsModel>()
16 @objc dynamic var premium: Bool = false
19 func updateUser(usernameModel: UsernameModel?){
20 let realm = try! Realm()
22 if let usernameModel = usernameModel{
25 usr.username = usernameModel.username
26 usr.premium = usernameModel.premium
40 func addBookToDownloaded(bookDetailsModel: BookDetailsModel){
41 let realm = try! Realm()
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
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
60 realm.add(bookDetailsModel, update: true)
62 self.downloadedBooks.append(bookDetailsModel)
64 try! realm.commitWrite()
67 func removeAllBooksFromDownloaded() {
69 guard downloadedBooks.count > 0 else { return }
71 let realm = try! Realm()
73 realm.delete(downloadedBooks)
74 try! realm.commitWrite()
77 func removeBookFromDownloaded(bookSlug: String) -> Bool {
79 let books = downloadedBooks.filter({$0.slug == bookSlug})
82 let realm = try! Realm()
85 try! realm.commitWrite()
91 func getBookFromDownloaded(bookSlug: String) -> BookDetailsModel? {
92 return downloadedBooks.first(where: {$0.slug == bookSlug})
95 func setUserPermium() {
96 guard let user = user else { return }
98 let realm = try! Realm()
101 try! realm.commitWrite()