Skip to content

Commit

Permalink
Refactor defaultCurrencyCode didSet logic into helper function
Browse files Browse the repository at this point in the history
- Moved the logic for updating currency rows into a separate helper function () to improve readability and maintainability.
- Cleaned up the didSet block to focus on calling the helper function.
  • Loading branch information
azisramdhan committed Dec 27, 2024
1 parent d8fe99f commit 8b559a1
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions litewallet/Views/DefaultCurrencyViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,11 @@ class DefaultCurrencyViewController: UITableViewController, Subscriber {
}
}

private var defaultCurrencyCode: String? {
didSet {
// Grab index paths of new and old rows when the currency changes
let paths: [IndexPath] = rates.enumerated().filter { $0.1.code == defaultCurrencyCode || $0.1.code == oldValue }.map { IndexPath(row: $0.0, section: 0) }

Task { @MainActor in
tableView.beginUpdates()
tableView.reloadRows(at: paths, with: .automatic)
tableView.endUpdates()

setExchangeRateLabel()
}
}
}
private var defaultCurrencyCode: String? {
didSet {
updateCurrencyRows(oldCurrencyCode: oldValue)
}
}

private let bitcoinLabel = UILabel(font: .customBold(size: 14.0), color: .grayTextTint)
private let bitcoinSwitch = UISegmentedControl(items: ["photons (\(S.Symbols.photons))", "lites (\(S.Symbols.lites))", "LTC (\(S.Symbols.ltc))"])
Expand Down Expand Up @@ -75,6 +66,19 @@ class DefaultCurrencyViewController: UITableViewController, Subscriber {
rateLabel.text = "\(bitsAmount.bits) = \(amount.string(forLocal: currentRate.locale))"
}
}

private func updateCurrencyRows(oldCurrencyCode: String?) {
// Grab index paths of new and old rows when the currency changes
let paths: [IndexPath] = rates.enumerated().filter { $0.1.code == defaultCurrencyCode || $0.1.code == oldCurrencyCode }.map { IndexPath(row: $0.0, section: 0) }

Task { @MainActor in
tableView.beginUpdates()
tableView.reloadRows(at: paths, with: .automatic)
tableView.endUpdates()

setExchangeRateLabel()
}
}

override func numberOfSections(in _: UITableView) -> Int {
return 1
Expand Down

0 comments on commit 8b559a1

Please sign in to comment.