Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Creating AnyXXDataSource and AnyXXRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Martin committed Jun 27, 2018
1 parent 9c653db commit d1e5ff7
Show file tree
Hide file tree
Showing 12 changed files with 717 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ private extension ItemNetworkDataSource {
private func getAllItems() -> Future<[ItemEntity]> {
let url = "/items"
return sessionManager.request(url).toFuture().flatMap { json in
if let json = json {
guard let results = json["results"] as? [[String: AnyObject]] else {
return Future([]) // or pass error if desired
}
return results.decodeAs(keyDecodingStrategy: .map(ItemEntity.fromNetworkMap), forEach: { item in
item.lastUpdate = Date()
})
guard let json = json else {
return Future([]) // no json to parse
}
guard let results = json["results"] as? [[String: AnyObject]] else {
return Future([]) // or pass error if desired
}
return Future([])
return results.decodeAs(keyDecodingStrategy: .map(ItemEntity.fromNetworkMap), forEach: { item in
item.lastUpdate = Date()
})
}
}

Expand All @@ -104,15 +104,15 @@ private extension ItemNetworkDataSource {
return sessionManager.request(url,
parameters: ["name" : text])
.toFuture().flatMap { json in
if let json = json {
guard let results = json["results"] as? [[String: AnyObject]] else {
return Future([]) // or pass error if desired
}
return results.decodeAs(keyDecodingStrategy: .map(ItemEntity.fromNetworkMap), forEach: { item in
item.lastUpdate = Date()
})
guard let json = json else {
return Future([]) // no json to parse
}
guard let results = json["results"] as? [[String: AnyObject]] else {
return Future([]) // or pass error if desired
}
return Future([])
return results.decodeAs(keyDecodingStrategy: .map(ItemEntity.fromNetworkMap), forEach: { item in
item.lastUpdate = Date()
})
}
}
}
34 changes: 29 additions & 5 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions MJSwiftCore/Classes/Common/Repository/AnyDeleteDataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Copyright 2018 Mobile Jazz SL
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

public class AnyDeleteDataSource <T> : DeleteDataSource {
private let box: DeleteDataSourceBoxBase<T>

/// Default initializer.
///
/// - Parameters:
/// - dataSource: The dataSource to abstract
public init<D: DeleteDataSource>(_ dataSource: D) where D.T == T {
box = DeleteDataSourceBox(dataSource)
}

public func delete(_ query: Query) -> Future<Void> {
return box.delete(query)
}

public func deleteAll(_ query: Query) -> Future<Void> {
return box.deleteAll(query)
}
}

extension DeleteDataSource {
/// Returns an AnyDeleteDataSource abstraction of the current data source
///
/// - Returns: An AnyDeleteDataSource abstraction
public func asAnyDeleteDataSource() -> AnyDeleteDataSource<T> {
if let dataSource = self as? AnyDeleteDataSource<T> {
return dataSource
}
return AnyDeleteDataSource(self)
}
}

///
/// This is an abstract class. Do not use it.
/// DeleteDataSource base class defining a generic type T (which is unrelated to the associated type of the DeleteDataSource protocol)
///
fileprivate class DeleteDataSourceBoxBase <T>: DeleteDataSource {

func delete(_ query: Query) -> Future<Void> {
fatalError("This method is abstract.")
}

func deleteAll(_ query: Query) -> Future<Void> {
fatalError("This method is abstract.")
}
}

///
/// A data source box, which has as generic type a DeleteDataSource and links the DeleteDataSourceBoxBase type T as the Base.T type.
///
fileprivate class DeleteDataSourceBox <Base> : DeleteDataSourceBoxBase <Base.T> where Base : DeleteDataSource {

private let base: Base

init(_ base: Base) {
self.base = base
}

override func delete(_ query: Query) -> Future<Void> {
return base.delete(query)
}

override func deleteAll(_ query: Query) -> Future<Void> {
return base.deleteAll(query)
}
}
Loading

0 comments on commit d1e5ff7

Please sign in to comment.