5 // Created by Pawel Dabrowski on 30/05/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
12 class SyncManager: NSObject {
13 private var networkService = NetworkService()
19 func isLoggedIn() -> Bool {
20 return networkService.isLoggedIn()
24 networkService.logout()
25 DatabaseManager.shared.updateUser(usernameModel: nil)
28 func updateUserCredentials(oAuthTokenModel: OAuthTokenModel?){
29 networkService.updateUserCredentials(oAuthTokenModel: oAuthTokenModel)
32 func requestToken(completionHandler: ConnectionCompletionHandler?){
33 networkService.requestToken(completionHandler: completionHandler)
36 func accessToken(completionHandler: ConnectionCompletionHandler?){
37 networkService.requestAccessToken(completionHandler: completionHandler)
40 func getUsername(completionHandler: ConnectionCompletionHandler?){
41 networkService.performRequest(with: .username, responseModelType: UsernameModel.self, params: nil, completionHandler: completionHandler)
44 func filterBooks(params: FilterBooksParameters, completionHandler: ConnectionCompletionHandler?){
45 networkService.performRequest(with: .filterBooks, responseModelType: [BookModel].self, params: params.parameters(), completionHandler: completionHandler)
48 func getNews(after: String?, completionHandler: ConnectionCompletionHandler?){
50 var params = [String: Any]()
51 if let after = after {
52 params["after"] = after
56 networkService.performRequest(with: .blog, responseModelType: [NewsModel].self, params: params, completionHandler: completionHandler)
59 func getCategories(filterSection: FilterSection, bookOnly: Bool, completionHandler: ConnectionCompletionHandler?){
61 var method = RestAction.epochs
62 if filterSection == .genres{
65 else if filterSection == .kinds{
68 networkService.performRequest(with: method, responseModelType: [CategoryModel].self, params: ["book_only": bookOnly ? "true" : "false"], completionHandler: completionHandler)
71 func getDataForLibrary(libraryCollectionType: LibraryCollectionType, completionHandler: ConnectionCompletionHandler?){
72 var method: RestAction!
74 switch libraryCollectionType {
80 method = .getReadingBooks
83 networkService.performRequest(with: method, responseModelType: [BookModel].self, params: nil, completionHandler: completionHandler)
86 func getPreview(completionHandler: ConnectionCompletionHandler?){
88 networkService.performRequest(with: .preview, responseModelType: [BookModel].self, params: nil, completionHandler: completionHandler)
92 func getDataForListType(listViewControllerType: ListViewControllerType, params: FilterBooksParameters? = nil, completionHandler: ConnectionCompletionHandler?){
93 var method: RestAction!
95 switch listViewControllerType {
101 method = .getReadingBooks
107 method = .getFavourites
109 method = .getReadenBooks
113 networkService.performRequest(with: method, responseModelType: [NewsModel].self, params: params?.parameters(), completionHandler: completionHandler)
116 networkService.performRequest(with: method, responseModelType: [BookModel].self, params: params?.parameters(), completionHandler: completionHandler)
120 func getBookDetails(bookSlug: String, completionHandler: ConnectionCompletionHandler?){
122 networkService.performRequest(with: .books, responseModelType: BookDetailsModel.self, urlSuffix: bookSlug + "/", completionHandler: completionHandler)
125 func getFavouriteState(slug: String, completionHandler: ConnectionCompletionHandler?){
127 networkService.performRequest(with: .getFavouriteState, responseModelType: LikeModel.self, urlSuffix: slug + "/", params: nil, completionHandler: completionHandler)
130 func setFavouriteState(slug: String, favourite: Bool, completionHandler: ConnectionCompletionHandler?){
132 networkService.performRequest(with: .setFavouriteState, responseModelType: LikeModel.self, urlSuffix: slug + "/", params: ["action" : favourite ? "like" : "unlike"], completionHandler: completionHandler)
135 func setReadingState(slug: String, readingState: ReadingStateModel.ReadingState, completionHandler: ConnectionCompletionHandler?){
137 networkService.performRequest(with: .setReadingState, responseModelType: ReadingStateModel.self, urlSuffix: slug + "/" + readingState.rawValue + "/", params: nil, completionHandler: completionHandler)
140 func getReadingState(slug: String, completionHandler: ConnectionCompletionHandler?){
142 networkService.performRequest(with: .getReadingState, responseModelType: ReadingStateModel.self, urlSuffix: slug + "/", params: nil, completionHandler: completionHandler)