Skip to content

Commit

Permalink
Bug/435 (trustwallet#482)
Browse files Browse the repository at this point in the history
* Code cleaning.

* Add sorting of the elements.

* Temp patch to avoid UI issue.
  • Loading branch information
OlegGordiichuk authored and vikmeup committed Mar 15, 2018
1 parent 34203a8 commit 125b652
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
1 change: 1 addition & 0 deletions Trust/Transactions/Storage/TransactionCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import RealmSwift

class TransactionCategory: Object {
@objc dynamic var title: String = ""
@objc dynamic var date = Date()
var transactions = List<Transaction>()

override static func primaryKey() -> String? {
Expand Down
23 changes: 8 additions & 15 deletions Trust/Transactions/Storage/TransactionsStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ class TransactionsStorage {

let realm: Realm

var transactions: Results<Transaction> {
return realm.objects(Transaction.self).sorted(byKeyPath: "date", ascending: false)
}

var transactionsCategory: Results<TransactionCategory> {
return realm.objects(TransactionCategory.self)
return realm.objects(TransactionCategory.self).sorted(byKeyPath: "date", ascending: false)
}

init(
Expand All @@ -22,10 +18,6 @@ class TransactionsStorage {
self.realm = realm
}

var count: Int {
return objects.count
}

var objects: [Transaction] {
return realm.objects(Transaction.self)
.sorted(byKeyPath: "date", ascending: false)
Expand All @@ -46,9 +38,9 @@ class TransactionsStorage {

func add(_ items: [Transaction]) {
let trnasactions = transactionCategory(for: items)
realm.beginWrite()
realm.add(trnasactions, update: true)
try! realm.commitWrite()
try! realm.write {
realm.add(trnasactions, update: true)
}
}

private func tokens(from transactions: [Transaction]) -> [Token] {
Expand All @@ -73,12 +65,13 @@ class TransactionsStorage {
var category = [TransactionCategory]()
let headerDates = NSOrderedSet(array: transactions.map { TransactionsViewModel.realmBaseFormmater.string(from: $0.date ) })
headerDates.forEach {
guard let date = $0 as? String else {
guard let stringDate = $0 as? String, let date = TransactionsViewModel.convert(stringDate) else {
return
}
let filteredTransactionByDate = transactions.filter { TransactionsViewModel.realmBaseFormmater.string(from: $0.date ) == date }
let filteredTransactionByDate = transactions.filter { TransactionsViewModel.realmBaseFormmater.string(from: $0.date ) == stringDate }
let item = TransactionCategory()
item.title = date
item.title = stringDate
item.date = date
item.transactions.append(objectsIn: filteredTransactionByDate)
category.append(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,8 @@ class TransactionsViewController: UIViewController {
case .initial:
tableView.reloadData()
self?.endLoading()
case .update(_, let deletions, let insertions, let modifications):
tableView.beginUpdates()
var insertIndexSet = IndexSet()
insertions.forEach { insertIndexSet.insert($0) }
tableView.insertSections(insertIndexSet, with: .none)
var deleteIndexSet = IndexSet()
deletions.forEach { deleteIndexSet.insert($0) }
tableView.deleteSections(deleteIndexSet, with: .none)
var updateIndexSet = IndexSet()
modifications.forEach { updateIndexSet.insert($0) }
tableView.reloadSections(updateIndexSet, with: .none)
tableView.endUpdates()
case .update:
tableView.reloadData()
self?.endLoading()
case .error(let error):
self?.endLoading(animated: true, error: error, completion: nil)
Expand Down
33 changes: 18 additions & 15 deletions Trust/Transactions/ViewModels/TransactionsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ struct TransactionsViewModel {

static let realmDateFormat = "MMddyyyy"

static let dateFormatter = DateFormatter()

static let realmBaseFormmater: DateFormatter = {
dateFormatter.dateFormat = realmDateFormat
return dateFormatter
let formatter = DateFormatter()
formatter.dateFormat = realmDateFormat
return formatter
}()

var backgroundColor: UIColor {
Expand Down Expand Up @@ -54,7 +53,7 @@ struct TransactionsViewModel {

var transactions: Results<TransactionCategory>

var tokensObserver: NotificationToken?
var transactionsObserver: NotificationToken?

let config: Config

Expand All @@ -78,7 +77,7 @@ struct TransactionsViewModel {
}

mutating func setTransactionsObservation(with block: @escaping (RealmCollectionChange<Results<TransactionCategory>>) -> Void) {
tokensObserver = transactions.observe(block)
transactionsObserver = transactions.observe(block)
}

func numberOfItems(for section: Int) -> Int {
Expand Down Expand Up @@ -156,15 +155,17 @@ struct TransactionsViewModel {
}

func fetchPending() {
self.storage.transactions.forEach {
self.network.update(for: $0, completion: { result in
switch result.1 {
case .deleted:
self.storage.delete([result.0])
default:
self.storage.update(state: result.1, for: result.0)
}
})
self.storage.transactionsCategory.forEach { transactions in
for transaction in transactions.transactions {
self.network.update(for: transaction, completion: { result in
switch result.1 {
case .deleted:
self.storage.delete([result.0])
default:
self.storage.update(state: result.1, for: result.0)
}
})
}
}
}

Expand Down Expand Up @@ -193,12 +194,14 @@ struct TransactionsViewModel {
}

static func convert(_ date: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = realmDateFormat
let date = dateFormatter.date(from: date)
return date
}

static func title(from date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d yyyy"
return dateFormatter.string(from: date)
}
Expand Down

0 comments on commit 125b652

Please sign in to comment.