added iOS source code
[wl-app.git] / iOS / WolneLektury / Model / ReadingStateModel.swift
1 //
2 //  ReadingStateModel.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 29/08/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11 class ReadingStateModel: Decodable{
12     
13     public enum ReadingState : String {
14         case unknown        = "unknown"
15         case not_started    = "not_started"
16         case reading        = "reading"
17         case complete       = "complete"
18     }
19     
20     var state = ReadingState.unknown
21     
22     private enum ReadingStateModelCodingKeys: String, CodingKey {
23         case state
24     }
25
26     convenience required init(from decoder: Decoder) throws {
27         let container = try decoder.container(keyedBy: ReadingStateModelCodingKeys.self)
28         
29         let stateString = try container.decode(String.self, forKey: .state)
30         
31         var returnState: ReadingState = .unknown
32         
33         if let readingState  = ReadingState(rawValue: stateString) {
34             returnState = readingState
35         }
36         
37         self.init(state: returnState)
38     }
39     
40     convenience init(state: ReadingState) {
41         self.init()
42         self.state = state
43     }
44 }