added iOS source code
[wl-app.git] / iOS / WolneLektury / Model / ReadingStateModel.swift
diff --git a/iOS/WolneLektury/Model/ReadingStateModel.swift b/iOS/WolneLektury/Model/ReadingStateModel.swift
new file mode 100644 (file)
index 0000000..0ff9f05
--- /dev/null
@@ -0,0 +1,44 @@
+//
+//  ReadingStateModel.swift
+//  WolneLektury
+//
+//  Created by Pawel Dabrowski on 29/08/2018.
+//  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
+//
+
+import UIKit
+
+class ReadingStateModel: Decodable{
+    
+    public enum ReadingState : String {
+        case unknown        = "unknown"
+        case not_started    = "not_started"
+        case reading        = "reading"
+        case complete       = "complete"
+    }
+    
+    var state = ReadingState.unknown
+    
+    private enum ReadingStateModelCodingKeys: String, CodingKey {
+        case state
+    }
+
+    convenience required init(from decoder: Decoder) throws {
+        let container = try decoder.container(keyedBy: ReadingStateModelCodingKeys.self)
+        
+        let stateString = try container.decode(String.self, forKey: .state)
+        
+        var returnState: ReadingState = .unknown
+        
+        if let readingState  = ReadingState(rawValue: stateString) {
+            returnState = readingState
+        }
+        
+        self.init(state: returnState)
+    }
+    
+    convenience init(state: ReadingState) {
+        self.init()
+        self.state = state
+    }
+}