Skip to content

Commit

Permalink
Add observer pattern and closure to update tableView after getting data.
Browse files Browse the repository at this point in the history
Cleaning some code
  • Loading branch information
popei69 committed Jan 13, 2018
1 parent 238b977 commit 4d209bd
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 11 deletions.
4 changes: 4 additions & 0 deletions TemplateProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
142350E32009668500930F1D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 142350E22009668500930F1D /* Assets.xcassets */; };
142350E62009668500930F1D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 142350E42009668500930F1D /* LaunchScreen.storyboard */; };
142350F12009668500930F1D /* TemplateProjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 142350F02009668500930F1D /* TemplateProjectTests.swift */; };
1456E4B5200A7F2200DCF2AF /* DynamicValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1456E4B4200A7F2200DCF2AF /* DynamicValue.swift */; };
14917E0C200A489A00234393 /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14917E0B200A489A00234393 /* Result.swift */; };
14917E0E200A48AB00234393 /* ErrorResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14917E0D200A48AB00234393 /* ErrorResult.swift */; };
14917E11200A491C00234393 /* RequestFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14917E10200A491C00234393 /* RequestFactory.swift */; };
Expand Down Expand Up @@ -48,6 +49,7 @@
142350EC2009668500930F1D /* TemplateProjectTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TemplateProjectTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
142350F02009668500930F1D /* TemplateProjectTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TemplateProjectTests.swift; sourceTree = "<group>"; };
142350F22009668500930F1D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1456E4B4200A7F2200DCF2AF /* DynamicValue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DynamicValue.swift; sourceTree = "<group>"; };
14917E0B200A489A00234393 /* Result.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
14917E0D200A48AB00234393 /* ErrorResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorResult.swift; sourceTree = "<group>"; };
14917E10200A491C00234393 /* RequestFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestFactory.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -176,6 +178,7 @@
children = (
14917E13200A4ECD00234393 /* Currency.swift */,
14917E1C200A528400234393 /* Converter.swift */,
1456E4B4200A7F2200DCF2AF /* DynamicValue.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -306,6 +309,7 @@
14917E0C200A489A00234393 /* Result.swift in Sources */,
14917E11200A491C00234393 /* RequestFactory.swift in Sources */,
14917E1D200A528400234393 /* Converter.swift in Sources */,
1456E4B5200A7F2200DCF2AF /* DynamicValue.swift in Sources */,
14917E27200A53CD00234393 /* RequestService.swift in Sources */,
14917E17200A4F4600234393 /* CurrencyViewModel.swift in Sources */,
14917E1B200A523E00234393 /* RequestHandler.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
8 changes: 1 addition & 7 deletions TemplateProject/Model/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ extension Converter : Parceable {
let date = dictionary["date"] as? String,
let rates = dictionary["rates"] as? [String: Double] {

var finalRates : [CurrencyRate] = []
for (currencyIso, value) in rates {
// if let currency = Currency(rawValue: currencyIso) {
finalRates.append(CurrencyRate(currency: currencyIso, rate: value))
// }
}

let finalRates : [CurrencyRate] = rates.flatMap({ CurrencyRate(currency: $0.key, rate: $0.value) })
let conversion = Converter(base: currency, date: date, rates: finalRates)

return Result.success(conversion)
Expand Down
43 changes: 43 additions & 0 deletions TemplateProject/Model/DynamicValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// DynamicValue.swift
// TemplateProject
//
// Created by Benoit PASQUIER on 13/01/2018.
// Copyright © 2018 Benoit PASQUIER. All rights reserved.
//

import Foundation

typealias CompletionHandler = (() -> Void)
class DynamicValue<T> {

var value : T {
didSet {
self.notify()
}
}

private var observers : [NSObject : CompletionHandler] = [NSObject : CompletionHandler]()

init(_ value: T) {
self.value = value
}

public func addObserver(_ observer: NSObject, completionHandler: @escaping CompletionHandler) {

observers[observer] = completionHandler
}

public func addAndNotify(observer: NSObject, completionHandler: @escaping CompletionHandler) {
self.addObserver(observer, completionHandler: completionHandler)
self.notify()
}

private func notify() {
observers.forEach({ $0.value() })
}

deinit {
observers.removeAll()
}
}
12 changes: 12 additions & 0 deletions TemplateProject/View/CurrencyCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ class CurrencyCell: UITableViewCell {
@IBOutlet weak var currencyLabel: UILabel!
@IBOutlet weak var rateLabel: UILabel!

var currencyRate : CurrencyRate? {
didSet {

guard let currencyRate = currencyRate else {
return
}

rateLabel.text = "\(currencyRate.rate)"
currencyLabel.text = currencyRate.currency
}
}

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
Expand Down
16 changes: 13 additions & 3 deletions TemplateProject/View/CurrencyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class CurrencyViewController: UIViewController {
// Do any additional setup after loading the view, typically from a nib.

self.tableView.dataSource = self.dataSource
self.dataSource.data.addAndNotify(observer: self) { [weak self] in
self?.tableView.reloadData()
}

self.viewModel.fetchCurrencies()
}

Expand All @@ -34,7 +38,7 @@ class CurrencyViewController: UIViewController {
}

class GenericDataSource<T> : NSObject {
var data: [T] = []
var data: DynamicValue<[T]> = DynamicValue([])
}

class CurrencyDataSource : GenericDataSource<CurrencyRate>, UITableViewDataSource {
Expand All @@ -44,10 +48,16 @@ class CurrencyDataSource : GenericDataSource<CurrencyRate>, UITableViewDataSourc
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
return data.value.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()

let cell = tableView.dequeueReusableCell(withIdentifier: "CurrencyCell", for: indexPath) as! CurrencyCell

let currencyRate = self.data.value[indexPath.row]
cell.currencyRate = currencyRate

return cell
}
}
2 changes: 1 addition & 1 deletion TemplateProject/ViewModel/CurrencyViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct CurrencyViewModel {
// reload data
print("Parser success \(converter)")
// self.converter = converter
self.dataSource?.data = converter.rates
self.dataSource?.data.value = converter.rates

break
case .failure(let error) :
Expand Down

0 comments on commit 4d209bd

Please sign in to comment.