Skip to content

Commit

Permalink
default implementations for Diffable protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
mbuchetics committed Feb 19, 2019
1 parent cf54f5a commit 34911f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
15 changes: 9 additions & 6 deletions DataSource/Diffable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ public protocol Diffable {
func isEqualToDiffable(_ other: Diffable?) -> Bool
}

extension String: Diffable {
public var diffIdentifier: String {
return self
public extension Diffable where Self: Hashable {

var diffIdentifier: String {
return String(hashValue)
}
}

public extension Diffable where Self: Equatable {

public func isEqualToDiffable(_ other: Diffable?) -> Bool {
guard let other = other as? String else { return false }
func isEqualToDiffable(_ other: Diffable?) -> Bool {
guard let other = other as? Self else { return false }
return self == other
}
}
Expand Down
14 changes: 5 additions & 9 deletions Example/ViewControllers/Examples/DiffViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,19 @@ class DiffViewController: UITableViewController {

// MARK: - Diff Item

struct DiffItem {
struct DiffItem: Hashable, Equatable {

let value: Int
let text: String
let diffIdentifier: String

init(_ value: Int, text: String) {
self.value = value
self.text = text
self.diffIdentifier = String(value)
}
}

extension DiffItem: Diffable {

public func isEqualToDiffable(_ other: Diffable?) -> Bool {
guard let other = other as? DiffItem else { return false }
return self.text == other.text
var hashValue: Int {
return value
}
}

extension DiffItem: Diffable { }
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ extension Person: Diffable {
public var diffIdentifier: String {
return fullName
}

func isEqualToDiffable(_ other: Diffable?) -> Bool {
guard let other = other as? Person else { return false }
return self == other
}
}

extension Person: CustomStringConvertible {
Expand Down

0 comments on commit 34911f5

Please sign in to comment.