Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Refactor JSON decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul committed Oct 10, 2017
1 parent 871bd35 commit a1a4fa0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
14 changes: 14 additions & 0 deletions Drrrible/Sources/Models/ModelType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ import Then

protocol ModelType: Codable, Then {
associatedtype Event

static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy { get }
}

extension ModelType {
static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy {
return .iso8601
}

static var decoder: JSONDecoder {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = self.dateDecodingStrategy
return decoder
}
}
21 changes: 9 additions & 12 deletions Drrrible/Sources/Rx/Moya+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import Moya
import RxSwift

extension PrimitiveSequence where TraitType == SingleTrait, Element == Moya.Response {
func map<T: Decodable>(
_ listType: List<T>.Type,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .base64,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate
) -> PrimitiveSequence<TraitType, List<T>> {
func map<T: ModelType>(_ type: T.Type) -> PrimitiveSequence<TraitType, T> {
return self.map(T.self, using: T.decoder)
}

func map<T: ModelType>(_ listType: List<T>.Type) -> PrimitiveSequence<TraitType, List<T>> {
return self
.map { response in
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = dataDecodingStrategy
decoder.dateDecodingStrategy = dateDecodingStrategy
let items = try decoder.decode([T].self, from: response.data)
.map { response -> List<T> in
let items = try response.map([T].self, using: T.decoder)
let nextURL = response.response?
.findLink(relation: "next")
.flatMap { URL(string: $0.uri) }
return List<T>(items: items, nextURL: nextURL)
}
.do(onError: { error in
if error is DecodingError {
log.error(error)
if case let MoyaError.objectMapping(decodingError, _) = error {
log.error(decodingError)
}
})
}
Expand Down
9 changes: 3 additions & 6 deletions Drrrible/Sources/Services/ShotService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ final class ShotService: ShotServiceType {
case .refresh: api = .shots
case .next(let url): api = .url(url)
}
return self.networking.request(api)
.map(List<Shot>.self, dateDecodingStrategy: .iso8601)
return self.networking.request(api).map(List<Shot>.self)
}

func shot(id: Int) -> Single<Shot> {
return self.networking.request(.shot(id: id))
.map(Shot.self, dateDecodingStrategy: .iso8601)
return self.networking.request(.shot(id: id)).map(Shot.self)
}

func isLiked(shotID: Int) -> Single<Bool> {
Expand Down Expand Up @@ -69,7 +67,6 @@ final class ShotService: ShotServiceType {
}

func comments(shotID: Int) -> Single<List<Comment>> {
return self.networking.request(.shotComments(shotID: shotID))
.map(List<Comment>.self, dateDecodingStrategy: .iso8601)
return self.networking.request(.shotComments(shotID: shotID)).map(List<Comment>.self)
}
}
2 changes: 1 addition & 1 deletion Drrrible/Sources/Services/UserService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class UserService: UserServiceType {
fileprivate let userSubject = ReplaySubject<User?>.create(bufferSize: 1)
lazy var currentUser: Observable<User?> = self.userSubject.asObservable()
.startWith(nil)
.shareReplay(1)
.share(replay: 1)

func fetchMe() -> Single<Void> {
return self.networking.request(.me)
Expand Down

0 comments on commit a1a4fa0

Please sign in to comment.