Added Android code
[wl-app.git] / iOS / WolneLektury / Connection / SyncManager.swift
1 //
2 //  SyncManager.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 UIKit
10
11
12 class SyncManager: NSObject {
13     private var networkService = NetworkService()
14
15     override init() {
16         
17     }
18     
19     func isLoggedIn() -> Bool {
20         return networkService.isLoggedIn()
21     }
22     
23     func logout() {
24         networkService.logout()
25         DatabaseManager.shared.updateUser(usernameModel: nil)
26     }
27     
28     func updateUserCredentials(oAuthTokenModel: OAuthTokenModel?){
29         networkService.updateUserCredentials(oAuthTokenModel: oAuthTokenModel)
30     }
31     
32     func requestToken(completionHandler: ConnectionCompletionHandler?){
33         networkService.requestToken(completionHandler: completionHandler)
34     }
35     
36     func accessToken(completionHandler: ConnectionCompletionHandler?){
37         networkService.requestAccessToken(completionHandler: completionHandler)
38     }
39
40     func getUsername(completionHandler: ConnectionCompletionHandler?){
41         networkService.performRequest(with: .username, responseModelType: UsernameModel.self, params: nil, completionHandler: completionHandler)
42     }
43     
44     func filterBooks(params: FilterBooksParameters, completionHandler: ConnectionCompletionHandler?){
45         networkService.performRequest(with: .filterBooks, responseModelType: [BookModel].self, params: params.parameters(), completionHandler: completionHandler)
46     }
47
48     func getNews(after: String?, completionHandler: ConnectionCompletionHandler?){
49         
50         var params = [String: Any]()
51         if let after = after {
52             params["after"] = after
53         }
54         params["count"] = 20
55         
56         networkService.performRequest(with: .blog, responseModelType: [NewsModel].self, params: params, completionHandler: completionHandler)
57     }
58
59     func getCategories(filterSection: FilterSection, bookOnly: Bool, completionHandler: ConnectionCompletionHandler?){
60         
61         var method = RestAction.epochs
62         if filterSection == .genres{
63             method = .genres
64         }
65         else if filterSection == .kinds{
66             method = .kinds
67         }
68         networkService.performRequest(with: method, responseModelType: [CategoryModel].self, params: ["book_only": bookOnly ? "true" : "false"], completionHandler: completionHandler)
69     }
70     
71     func getDataForLibrary(libraryCollectionType: LibraryCollectionType, completionHandler: ConnectionCompletionHandler?){
72         var method: RestAction!
73         
74         switch libraryCollectionType {
75         case .newest:
76             method = .newest
77         case .recommended:
78             method = .recommended
79         case .reading_now:
80             method = .getReadingBooks
81         }
82         
83         networkService.performRequest(with: method, responseModelType: [BookModel].self, params: nil, completionHandler: completionHandler)
84     }
85     
86     func getPreview(completionHandler: ConnectionCompletionHandler?){
87         
88         networkService.performRequest(with: .preview, responseModelType: [BookModel].self, params: nil, completionHandler: completionHandler)
89     }
90
91
92     func getDataForListType(listViewControllerType: ListViewControllerType, params: FilterBooksParameters? = nil, completionHandler: ConnectionCompletionHandler?){
93         var method: RestAction!
94         
95         switch listViewControllerType {
96         case .newest:
97             method = .newest
98         case .recommended:
99             method = .recommended
100         case .reading_now:
101             method = .getReadingBooks
102         case .audiobooks:
103             method = .audiobooks
104         case .news:
105             method = .blog
106         case .favourites:
107             method = .getFavourites
108         case .completed:
109             method = .getReadenBooks
110         }
111         
112         if method == .blog {
113             networkService.performRequest(with: method, responseModelType: [NewsModel].self, params: params?.parameters(), completionHandler: completionHandler)
114         }
115         else {
116             networkService.performRequest(with: method, responseModelType: [BookModel].self, params: params?.parameters(), completionHandler: completionHandler)
117         }
118     }
119
120     func getBookDetails(bookSlug: String, completionHandler: ConnectionCompletionHandler?){
121         
122         networkService.performRequest(with: .books, responseModelType: BookDetailsModel.self, urlSuffix: bookSlug + "/", completionHandler: completionHandler)
123     }
124
125     func getFavouriteState(slug: String, completionHandler: ConnectionCompletionHandler?){
126         
127         networkService.performRequest(with: .getFavouriteState, responseModelType: LikeModel.self, urlSuffix: slug + "/", params: nil, completionHandler: completionHandler)
128     }
129     
130     func setFavouriteState(slug: String, favourite: Bool, completionHandler: ConnectionCompletionHandler?){
131         
132         networkService.performRequest(with: .setFavouriteState, responseModelType: LikeModel.self, urlSuffix: slug + "/", params: ["action" : favourite ? "like" : "unlike"], completionHandler: completionHandler)
133     }
134     
135     func setReadingState(slug: String, readingState: ReadingStateModel.ReadingState, completionHandler: ConnectionCompletionHandler?){
136         
137         networkService.performRequest(with: .setReadingState, responseModelType: ReadingStateModel.self, urlSuffix: slug + "/" + readingState.rawValue + "/", params: nil, completionHandler: completionHandler)
138     }
139     
140     func getReadingState(slug: String, completionHandler: ConnectionCompletionHandler?){
141         
142         networkService.performRequest(with: .getReadingState, responseModelType: ReadingStateModel.self, urlSuffix: slug + "/", params: nil, completionHandler: completionHandler)
143     }
144 }