Commit 61382dc4 by Dmitriy Stepanets

Fixed shorts list parsing

parent 54dc1958
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
CD2D4C6126BD65F80016A180 /* LossyCodableList.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2D4C6026BD65F80016A180 /* LossyCodableList.swift */; };
CD2D4C6326BD67790016A180 /* GlancesList.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2D4C6226BD67790016A180 /* GlancesList.swift */; };
CD427D1F266F657900B4350A /* OneWeatherCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD427D1E266F657900B4350A /* OneWeatherCore.framework */; }; CD427D1F266F657900B4350A /* OneWeatherCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD427D1E266F657900B4350A /* OneWeatherCore.framework */; };
CD427D23266F715900B4350A /* OneWeatherAnalytics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD427D22266F715900B4350A /* OneWeatherAnalytics.framework */; }; CD427D23266F715900B4350A /* OneWeatherAnalytics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD427D22266F715900B4350A /* OneWeatherAnalytics.framework */; };
CDAEC2092679F70600818A2A /* GlanceOverlayImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDAEC2082679F70600818A2A /* GlanceOverlayImage.swift */; }; CDAEC2092679F70600818A2A /* GlanceOverlayImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDAEC2082679F70600818A2A /* GlanceOverlayImage.swift */; };
...@@ -33,6 +35,8 @@ ...@@ -33,6 +35,8 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
CD2C227E2670AC6D001ADA9A /* InMobiShortsPG.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = InMobiShortsPG.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; CD2C227E2670AC6D001ADA9A /* InMobiShortsPG.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = InMobiShortsPG.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
CD2D4C6026BD65F80016A180 /* LossyCodableList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LossyCodableList.swift; sourceTree = "<group>"; };
CD2D4C6226BD67790016A180 /* GlancesList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlancesList.swift; sourceTree = "<group>"; };
CD427D1E266F657900B4350A /* OneWeatherCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = OneWeatherCore.framework; sourceTree = BUILT_PRODUCTS_DIR; }; CD427D1E266F657900B4350A /* OneWeatherCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = OneWeatherCore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CD427D22266F715900B4350A /* OneWeatherAnalytics.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = OneWeatherAnalytics.framework; sourceTree = BUILT_PRODUCTS_DIR; }; CD427D22266F715900B4350A /* OneWeatherAnalytics.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = OneWeatherAnalytics.framework; sourceTree = BUILT_PRODUCTS_DIR; };
CDAEC2082679F70600818A2A /* GlanceOverlayImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlanceOverlayImage.swift; sourceTree = "<group>"; }; CDAEC2082679F70600818A2A /* GlanceOverlayImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlanceOverlayImage.swift; sourceTree = "<group>"; };
...@@ -129,6 +133,8 @@ ...@@ -129,6 +133,8 @@
CDAEC2082679F70600818A2A /* GlanceOverlayImage.swift */, CDAEC2082679F70600818A2A /* GlanceOverlayImage.swift */,
CDFE3F2B266E519E00E72910 /* PeekData.swift */, CDFE3F2B266E519E00E72910 /* PeekData.swift */,
CDFE3F2D266E51B400E72910 /* Peek.swift */, CDFE3F2D266E51B400E72910 /* Peek.swift */,
CD2D4C6026BD65F80016A180 /* LossyCodableList.swift */,
CD2D4C6226BD67790016A180 /* GlancesList.swift */,
); );
path = Models; path = Models;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -243,6 +249,8 @@ ...@@ -243,6 +249,8 @@
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
CDFE3F2E266E51B400E72910 /* Peek.swift in Sources */, CDFE3F2E266E51B400E72910 /* Peek.swift in Sources */,
CD2D4C6126BD65F80016A180 /* LossyCodableList.swift in Sources */,
CD2D4C6326BD67790016A180 /* GlancesList.swift in Sources */,
CDFE3F23266E453500E72910 /* InMobiShortSource.swift in Sources */, CDFE3F23266E453500E72910 /* InMobiShortSource.swift in Sources */,
CDFE3F29266E489E00E72910 /* GlanceImage.swift in Sources */, CDFE3F29266E489E00E72910 /* GlanceImage.swift in Sources */,
CDFE3F25266E45B800E72910 /* Glance.swift in Sources */, CDFE3F25266E45B800E72910 /* Glance.swift in Sources */,
......
...@@ -74,15 +74,10 @@ public class InMobiShortSource: ShortSource { ...@@ -74,15 +74,10 @@ public class InMobiShortSource: ShortSource {
} }
let decoder = JSONDecoder() let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase decoder.keyDecodingStrategy = .useDefaultKeys
do { do {
let glancesDict = try decoder.decode([String:[GlanceDetails]].self, from: data) let glancesList = try decoder.decode(GlancesList.self, from: data)
if let glanceDetailsArray = glancesDict["glances"] { completion(glancesList.glances.map{self.toAppModel(glanceDetails: $0)}, nil)
completion(glanceDetailsArray.map{self.toAppModel(glanceDetails: $0)}, nil)
}
else {
completion(nil, InMobiShortSourceError.dataEncodingError("Failed to decode glance details array"))
}
} }
catch { catch {
completion(nil, InMobiShortSourceError.dataEncodingError(error.localizedDescription)) completion(nil, InMobiShortSourceError.dataEncodingError(error.localizedDescription))
......
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
import Foundation import Foundation
struct GlanceOverlayImage: Codable { struct GlanceOverlayImage: Codable {
let id:String let id: String
let supportedImages:[GlanceImageFormat] var supportedImages: [GlanceImageFormat]
} }
//
// GlancesList.swift
// InMobiShortsSource
//
// Created by Dmitry Stepanets on 06.08.2021.
//
import Foundation
struct GlancesList: Codable {
@LossyCodableList
var glances: [GlanceDetails]
}
//
// LossyCodableList.swift
// InMobiShortsSource
//
// Created by Dmitry Stepanets on 06.08.2021.
//
import Foundation
@propertyWrapper
struct LossyCodableList<Element> {
var elements: [Element]
var wrappedValue: [Element] {
get { elements }
set { elements = newValue }
}
}
extension LossyCodableList: Decodable where Element: Decodable {
private struct ElementWrapper: Decodable {
var element: Element?
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
element = try? container.decode(Element.self)
}
}
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let wrappers = try container.decode([ElementWrapper].self)
elements = wrappers.compactMap(\.element)
}
}
extension LossyCodableList: Encodable where Element: Encodable {
func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in elements {
try? container.encode(element)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment